github.com/windmeup/goreleaser@v1.21.95/internal/pipe/scoop/scoop_test.go (about) 1 package scoop 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 "github.com/windmeup/goreleaser/internal/artifact" 10 "github.com/windmeup/goreleaser/internal/client" 11 "github.com/windmeup/goreleaser/internal/golden" 12 "github.com/windmeup/goreleaser/internal/skips" 13 "github.com/windmeup/goreleaser/internal/testctx" 14 "github.com/windmeup/goreleaser/internal/testlib" 15 "github.com/windmeup/goreleaser/pkg/config" 16 "github.com/windmeup/goreleaser/pkg/context" 17 ) 18 19 func TestContinueOnError(t *testing.T) { 20 require.True(t, Pipe{}.ContinueOnError()) 21 } 22 23 func TestDescription(t *testing.T) { 24 require.NotEmpty(t, Pipe{}.String()) 25 } 26 27 func TestDefault(t *testing.T) { 28 testlib.Mktmp(t) 29 30 ctx := testctx.NewWithCfg( 31 config.Project{ 32 ProjectName: "barr", 33 Scoops: []config.Scoop{ 34 { 35 Repository: config.RepoRef{ 36 Name: "foo", 37 }, 38 }, 39 }, 40 }, 41 testctx.GitHubTokenType, 42 ) 43 require.NoError(t, Pipe{}.Default(ctx)) 44 require.Len(t, ctx.Config.Scoops, 1) 45 require.Equal(t, ctx.Config.ProjectName, ctx.Config.Scoops[0].Name) 46 require.NotEmpty(t, ctx.Config.Scoops[0].CommitAuthor.Name) 47 require.NotEmpty(t, ctx.Config.Scoops[0].CommitAuthor.Email) 48 require.NotEmpty(t, ctx.Config.Scoops[0].CommitMessageTemplate) 49 } 50 51 func TestDefaultDeprecated(t *testing.T) { 52 testlib.Mktmp(t) 53 54 ctx := testctx.NewWithCfg( 55 config.Project{ 56 ProjectName: "barr", 57 Scoop: config.Scoop{ 58 Bucket: config.RepoRef{ 59 Name: "foo", 60 }, 61 }, 62 }, 63 testctx.GitHubTokenType, 64 ) 65 require.NoError(t, Pipe{}.Default(ctx)) 66 require.Len(t, ctx.Config.Scoops, 1) 67 require.Equal(t, ctx.Config.ProjectName, ctx.Config.Scoops[0].Name) 68 require.NotEmpty(t, ctx.Config.Scoops[0].CommitAuthor.Name) 69 require.NotEmpty(t, ctx.Config.Scoops[0].CommitAuthor.Email) 70 require.NotEmpty(t, ctx.Config.Scoops[0].CommitMessageTemplate) 71 require.Equal(t, "foo", ctx.Config.Scoops[0].Repository.Name) 72 require.True(t, ctx.Deprecated) 73 } 74 75 func Test_doRun(t *testing.T) { 76 folder := t.TempDir() 77 file := filepath.Join(folder, "archive") 78 require.NoError(t, os.WriteFile(file, []byte("lorem ipsum"), 0o644)) 79 80 type args struct { 81 ctx *context.Context 82 client *client.Mock 83 } 84 85 type asserter func(testing.TB, args) 86 type errChecker func(testing.TB, error) 87 shouldErr := func(msg string) errChecker { 88 return func(tb testing.TB, err error) { 89 tb.Helper() 90 require.Error(tb, err) 91 require.EqualError(tb, err, msg) 92 } 93 } 94 noAssertions := func(tb testing.TB, _ args) { 95 tb.Helper() 96 } 97 shouldNotErr := func(tb testing.TB, err error) { 98 tb.Helper() 99 require.NoError(tb, err) 100 } 101 102 tests := []struct { 103 name string 104 args args 105 artifacts []artifact.Artifact 106 assertRunError errChecker 107 assertPublishError errChecker 108 assert asserter 109 }{ 110 { 111 "multiple_artifacts", 112 args{ 113 testctx.NewWithCfg( 114 config.Project{ 115 Dist: t.TempDir(), 116 ProjectName: "multi-arts", 117 Scoops: []config.Scoop{{ 118 Repository: config.RepoRef{ 119 Owner: "test", 120 Name: "test", 121 }, 122 Folder: "scoops", 123 Description: "A run pipe test formula", 124 Homepage: "https://github.com/goreleaser", 125 }}, 126 }, 127 testctx.GitHubTokenType, 128 testctx.WithCurrentTag("v1.0.1"), 129 testctx.WithVersion("1.0.1"), 130 ), 131 client.NewMock(), 132 }, 133 []artifact.Artifact{ 134 {Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Goamd64: "v1", Path: file}, 135 {Name: "foo_1.0.1_windows_arm64.tar.gz", Goos: "windows", Goarch: "arm64", Path: file}, 136 {Name: "foos_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Goamd64: "v1", Path: file}, 137 }, 138 func(tb testing.TB, err error) { 139 tb.Helper() 140 require.EqualError(tb, err, ErrIncorrectArchiveCount{ 141 goamd64: "v1", 142 archives: []*artifact.Artifact{ 143 {Name: "foo_1.0.1_windows_amd64.tar.gz"}, 144 {Name: "foo_1.0.1_windows_arm64.tar.gz"}, 145 {Name: "foos_1.0.1_windows_amd64.tar.gz"}, 146 }, 147 }.Error()) 148 }, 149 nil, 150 noAssertions, 151 }, 152 { 153 "multiple_binaries", 154 args{ 155 testctx.NewWithCfg( 156 config.Project{ 157 Dist: t.TempDir(), 158 ProjectName: "multi-bins", 159 Scoops: []config.Scoop{{ 160 Repository: config.RepoRef{ 161 Owner: "test", 162 Name: "test", 163 }, 164 IDs: []string{"id2"}, 165 Folder: "scoops", 166 Description: "A run pipe test formula", 167 Homepage: "https://github.com/goreleaser", 168 }}, 169 }, 170 testctx.GitHubTokenType, 171 testctx.WithCurrentTag("v1.0.1"), 172 testctx.WithVersion("1.0.1"), 173 ), 174 client.NewMock(), 175 }, 176 []artifact.Artifact{ 177 {Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Goamd64: "v1", Path: file, Extra: map[string]any{ 178 artifact.ExtraID: "id1", 179 artifact.ExtraBinaries: []string{"bin1", "bin2"}, 180 }}, 181 {Name: "foos_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Goamd64: "v1", Path: file, Extra: map[string]any{ 182 artifact.ExtraID: "id2", 183 artifact.ExtraBinaries: []string{"bin4", "bin3"}, 184 }}, 185 }, 186 shouldNotErr, 187 shouldNotErr, 188 func(tb testing.TB, a args) { 189 tb.Helper() 190 require.Equal(tb, "scoops/multi-bins.json", a.client.Path) 191 golden.RequireEqualJSON(tb, []byte(a.client.Content)) 192 }, 193 }, 194 { 195 "valid public github", 196 args{ 197 testctx.NewWithCfg( 198 config.Project{ 199 Dist: t.TempDir(), 200 ProjectName: "run-pipe", 201 Scoop: config.Scoop{ 202 Repository: config.RepoRef{ 203 Owner: "test", 204 Name: "test", 205 }, 206 Folder: "scoops", 207 Description: "A run pipe test formula", 208 Homepage: "https://github.com/goreleaser", 209 }, 210 }, 211 testctx.GitHubTokenType, 212 testctx.WithCurrentTag("v1.0.1"), 213 testctx.WithVersion("1.0.1"), 214 ), 215 client.NewMock(), 216 }, 217 []artifact.Artifact{ 218 {Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Goamd64: "v1", Path: file}, 219 {Name: "foo_1.0.1_windows_386.tar.gz", Goos: "windows", Goarch: "386", Path: file}, 220 }, 221 shouldNotErr, 222 shouldNotErr, 223 func(tb testing.TB, a args) { 224 tb.Helper() 225 require.Equal(tb, "scoops/run-pipe.json", a.client.Path) 226 golden.RequireEqualJSON(tb, []byte(a.client.Content)) 227 }, 228 }, 229 { 230 "git_remote", 231 args{ 232 testctx.NewWithCfg( 233 config.Project{ 234 ProjectName: "git-run-pipe", 235 Dist: t.TempDir(), 236 Scoop: config.Scoop{ 237 Repository: config.RepoRef{ 238 Name: "test", 239 Branch: "main", 240 Git: config.GitRepoRef{ 241 URL: testlib.GitMakeBareRepository(t), 242 PrivateKey: testlib.MakeNewSSHKey(t, ""), 243 }, 244 }, 245 Folder: "scoops", 246 Description: "A run pipe test formula", 247 Homepage: "https://github.com/goreleaser", 248 }, 249 }, 250 testctx.GitHubTokenType, 251 testctx.WithCurrentTag("v1.0.1"), 252 testctx.WithVersion("1.0.1"), 253 ), 254 client.NewMock(), 255 }, 256 []artifact.Artifact{ 257 {Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Goamd64: "v1", Path: file}, 258 {Name: "foo_1.0.1_windows_386.tar.gz", Goos: "windows", Goarch: "386", Path: file}, 259 }, 260 shouldNotErr, 261 shouldNotErr, 262 func(tb testing.TB, a args) { 263 tb.Helper() 264 content := testlib.CatFileFromBareRepositoryOnBranch( 265 tb, 266 a.ctx.Config.Scoop.Repository.Git.URL, 267 a.ctx.Config.Scoop.Repository.Branch, 268 "scoops/git-run-pipe.json", 269 ) 270 golden.RequireEqualJSON(tb, content) 271 }, 272 }, 273 { 274 "wrap in directory", 275 args{ 276 testctx.NewWithCfg( 277 config.Project{ 278 ProjectName: "run-pipe", 279 Scoop: config.Scoop{ 280 Repository: config.RepoRef{ 281 Owner: "test", 282 Name: "test", 283 }, 284 Description: "A run pipe test formula", 285 Homepage: "https://github.com/goreleaser", 286 }, 287 }, 288 testctx.GitHubTokenType, 289 testctx.WithCurrentTag("v1.0.1"), 290 testctx.WithVersion("1.0.1"), 291 ), 292 client.NewMock(), 293 }, 294 []artifact.Artifact{ 295 { 296 Name: "foo_1.0.1_windows_amd64.tar.gz", 297 Goos: "windows", 298 Goarch: "amd64", 299 Goamd64: "v1", 300 Path: file, 301 Extra: map[string]interface{}{ 302 "Wrap": "foo_1.0.1_windows_amd64", 303 }, 304 }, 305 { 306 Name: "foo_1.0.1_windows_386.tar.gz", 307 Goos: "windows", 308 Goarch: "386", 309 Path: file, 310 Extra: map[string]interface{}{ 311 "Wrap": "foo_1.0.1_windows_386", 312 }, 313 }, 314 }, 315 shouldNotErr, 316 shouldNotErr, 317 noAssertions, 318 }, 319 { 320 "valid enterprise github", 321 args{ 322 testctx.NewWithCfg( 323 config.Project{ 324 GitHubURLs: config.GitHubURLs{Download: "https://api.custom.github.enterprise.com"}, 325 ProjectName: "run-pipe", 326 Scoop: config.Scoop{ 327 Repository: config.RepoRef{ 328 Owner: "test", 329 Name: "test", 330 }, 331 Description: "A run pipe test formula", 332 Homepage: "https://github.com/goreleaser", 333 }, 334 }, 335 testctx.GitHubTokenType, 336 testctx.WithCurrentTag("v1.0.1"), 337 testctx.WithVersion("1.0.1"), 338 ), 339 client.NewMock(), 340 }, 341 []artifact.Artifact{ 342 {Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Goamd64: "v1", Path: file}, 343 {Name: "foo_1.0.1_windows_386.tar.gz", Goos: "windows", Goarch: "386", Path: file}, 344 }, 345 shouldNotErr, 346 shouldNotErr, 347 func(tb testing.TB, a args) { 348 tb.Helper() 349 require.Equal(tb, "run-pipe.json", a.client.Path) 350 }, 351 }, 352 { 353 "valid public gitlab", 354 args{ 355 testctx.NewWithCfg( 356 config.Project{ 357 ProjectName: "run-pipe", 358 Scoop: config.Scoop{ 359 Repository: config.RepoRef{ 360 Owner: "test", 361 Name: "test", 362 }, 363 Description: "A run pipe test formula", 364 Homepage: "https://gitlab.com/goreleaser", 365 }, 366 }, 367 testctx.GitHubTokenType, 368 testctx.WithCurrentTag("v1.0.1"), 369 testctx.WithVersion("1.0.1"), 370 ), 371 client.NewMock(), 372 }, 373 []artifact.Artifact{ 374 { 375 Name: "foo_1.0.1_windows_amd64.tar.gz", 376 Goos: "windows", 377 Goarch: "amd64", 378 Goamd64: "v1", 379 Path: file, 380 }, 381 { 382 Name: "foo_1.0.1_windows_386.tar.gz", 383 Goos: "windows", 384 Goarch: "386", 385 Path: file, 386 }, 387 }, 388 shouldNotErr, 389 shouldNotErr, 390 noAssertions, 391 }, 392 { 393 "valid enterprise gitlab", 394 args{ 395 testctx.NewWithCfg( 396 config.Project{ 397 GitHubURLs: config.GitHubURLs{Download: "https://api.custom.gitlab.enterprise.com"}, 398 ProjectName: "run-pipe", 399 Scoop: config.Scoop{ 400 Repository: config.RepoRef{ 401 Owner: "test", 402 Name: "test", 403 }, 404 Description: "A run pipe test formula", 405 Homepage: "https://gitlab.com/goreleaser", 406 }, 407 }, 408 testctx.GitHubTokenType, 409 testctx.WithCurrentTag("v1.0.1"), 410 testctx.WithVersion("1.0.1"), 411 ), 412 client.NewMock(), 413 }, 414 []artifact.Artifact{ 415 { 416 Name: "foo_1.0.1_windows_amd64.tar.gz", 417 Goos: "windows", 418 Goarch: "amd64", 419 Goamd64: "v1", 420 Path: file, 421 }, 422 { 423 Name: "foo_1.0.1_windows_386.tar.gz", 424 Goos: "windows", 425 Goarch: "386", 426 Path: file, 427 }, 428 }, 429 shouldNotErr, 430 shouldNotErr, 431 noAssertions, 432 }, 433 { 434 "no windows build", 435 args{ 436 testctx.NewWithCfg( 437 config.Project{ 438 ProjectName: "run-pipe", 439 Scoop: config.Scoop{ 440 Repository: config.RepoRef{ 441 Owner: "test", 442 Name: "test", 443 }, 444 Description: "A run pipe test formula", 445 Homepage: "https://github.com/goreleaser", 446 }, 447 }, 448 testctx.GitHubTokenType, 449 testctx.WithCurrentTag("v1.0.1"), 450 testctx.WithVersion("1.0.1"), 451 ), 452 client.NewMock(), 453 }, 454 []artifact.Artifact{}, 455 shouldErr(ErrIncorrectArchiveCount{"v1", nil, nil}.Error()), 456 shouldNotErr, 457 noAssertions, 458 }, 459 { 460 "is prerelease and skip upload set to auto", 461 args{ 462 testctx.NewWithCfg( 463 config.Project{ 464 ProjectName: "run-pipe", 465 Scoop: config.Scoop{ 466 SkipUpload: "auto", 467 Repository: config.RepoRef{ 468 Owner: "test", 469 Name: "test", 470 }, 471 Description: "A run pipe test formula", 472 Homepage: "https://github.com/goreleaser", 473 }, 474 }, 475 testctx.GitHubTokenType, 476 testctx.WithCurrentTag("v1.0.1-pre.1"), 477 testctx.WithVersion("1.0.1-pre.1"), 478 testctx.WithSemver(1, 0, 0, "pre.1"), 479 ), 480 client.NewMock(), 481 }, 482 []artifact.Artifact{ 483 {Name: "foo_1.0.1-pre.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Goamd64: "v1", Path: file}, 484 {Name: "foo_1.0.1-pre.1_windows_386.tar.gz", Goos: "windows", Goarch: "386", Path: file}, 485 }, 486 shouldNotErr, 487 shouldErr("release is prerelease"), 488 noAssertions, 489 }, 490 { 491 "skip upload set to true", 492 args{ 493 testctx.NewWithCfg( 494 config.Project{ 495 ProjectName: "run-pipe", 496 Scoop: config.Scoop{ 497 SkipUpload: "true", 498 Repository: config.RepoRef{ 499 Owner: "test", 500 Name: "test", 501 }, 502 Description: "A run pipe test formula", 503 Homepage: "https://github.com/goreleaser", 504 }, 505 }, 506 testctx.GitHubTokenType, 507 testctx.WithCurrentTag("v1.0.1"), 508 testctx.WithVersion("1.0.1"), 509 ), 510 client.NewMock(), 511 }, 512 []artifact.Artifact{ 513 {Name: "foo_1.0.1-pre.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Goamd64: "v1", Path: file}, 514 {Name: "foo_1.0.1-pre.1_windows_386.tar.gz", Goos: "windows", Goarch: "386", Path: file}, 515 }, 516 shouldNotErr, 517 shouldErr("scoop.skip_upload is true"), 518 noAssertions, 519 }, 520 { 521 "no archive", 522 args{ 523 testctx.NewWithCfg( 524 config.Project{ 525 ProjectName: "run-pipe", 526 Scoop: config.Scoop{ 527 Repository: config.RepoRef{ 528 Owner: "test", 529 Name: "test", 530 }, 531 Description: "A run pipe test formula", 532 Homepage: "https://github.com/goreleaser", 533 }, 534 }, 535 testctx.GitHubTokenType, 536 testctx.WithCurrentTag("v1.0.1"), 537 testctx.WithVersion("1.0.1"), 538 ), 539 client.NewMock(), 540 }, 541 []artifact.Artifact{}, 542 shouldErr(ErrIncorrectArchiveCount{"v1", nil, nil}.Error()), 543 shouldNotErr, 544 noAssertions, 545 }, 546 { 547 "invalid name tmpl", 548 args{ 549 testctx.NewWithCfg( 550 config.Project{ 551 ProjectName: "run-pipe", 552 Scoop: config.Scoop{ 553 Repository: config.RepoRef{ 554 Owner: "test", 555 Name: "test", 556 }, 557 Name: "{{.Nope}}", 558 }, 559 }, 560 testctx.GitHubTokenType, 561 testctx.WithCurrentTag("v1.0.1"), 562 testctx.WithVersion("1.0.1"), 563 ), 564 client.NewMock(), 565 }, 566 []artifact.Artifact{ 567 {Name: "foo_1.0.1-pre.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Goamd64: "v1", Path: file}, 568 }, 569 testlib.RequireTemplateError, 570 shouldNotErr, 571 noAssertions, 572 }, 573 { 574 "invalid description tmpl", 575 args{ 576 testctx.NewWithCfg( 577 config.Project{ 578 ProjectName: "run-pipe", 579 Scoop: config.Scoop{ 580 Repository: config.RepoRef{ 581 Owner: "test", 582 Name: "test", 583 }, 584 Description: "{{.Nope}}", 585 }, 586 }, 587 testctx.GitHubTokenType, 588 testctx.WithCurrentTag("v1.0.1"), 589 testctx.WithVersion("1.0.1"), 590 ), 591 client.NewMock(), 592 }, 593 []artifact.Artifact{ 594 {Name: "foo_1.0.1-pre.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Goamd64: "v1", Path: file}, 595 }, 596 testlib.RequireTemplateError, 597 shouldNotErr, 598 noAssertions, 599 }, 600 { 601 "invalid homepage tmpl", 602 args{ 603 testctx.NewWithCfg( 604 config.Project{ 605 ProjectName: "run-pipe", 606 Scoop: config.Scoop{ 607 Repository: config.RepoRef{ 608 Owner: "test", 609 Name: "test", 610 }, 611 Homepage: "{{.Nope}}", 612 }, 613 }, 614 testctx.GitHubTokenType, 615 testctx.WithCurrentTag("v1.0.1"), 616 testctx.WithVersion("1.0.1"), 617 ), 618 client.NewMock(), 619 }, 620 []artifact.Artifact{ 621 {Name: "foo_1.0.1-pre.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Goamd64: "v1", Path: file}, 622 }, 623 testlib.RequireTemplateError, 624 shouldNotErr, 625 noAssertions, 626 }, 627 { 628 "invalid skip upload tmpl", 629 args{ 630 testctx.NewWithCfg( 631 config.Project{ 632 ProjectName: "run-pipe", 633 Scoop: config.Scoop{ 634 Repository: config.RepoRef{ 635 Owner: "test", 636 Name: "test", 637 }, 638 SkipUpload: "{{.Nope}}", 639 }, 640 }, 641 testctx.GitHubTokenType, 642 testctx.WithCurrentTag("v1.0.1"), 643 testctx.WithVersion("1.0.1"), 644 ), 645 client.NewMock(), 646 }, 647 []artifact.Artifact{ 648 {Name: "foo_1.0.1-pre.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Goamd64: "v1", Path: file}, 649 }, 650 testlib.RequireTemplateError, 651 shouldNotErr, 652 noAssertions, 653 }, 654 { 655 "invalid ref tmpl", 656 args{ 657 testctx.NewWithCfg( 658 config.Project{ 659 ProjectName: "run-pipe", 660 Scoop: config.Scoop{ 661 Repository: config.RepoRef{ 662 Owner: "{{ .Env.aaaaaa }}", 663 Name: "test", 664 }, 665 Folder: "scoops", 666 Description: "A run pipe test formula", 667 Homepage: "https://github.com/goreleaser", 668 }, 669 }, 670 testctx.GitHubTokenType, 671 testctx.WithCurrentTag("v1.0.1"), 672 testctx.WithVersion("1.0.1"), 673 ), 674 client.NewMock(), 675 }, 676 []artifact.Artifact{ 677 {Name: "foo_1.0.1-pre.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Goamd64: "v1", Path: file}, 678 }, 679 testlib.RequireTemplateError, 680 shouldNotErr, 681 noAssertions, 682 }, 683 { 684 "ref templ", 685 args{ 686 testctx.NewWithCfg( 687 config.Project{ 688 Env: []string{"FOO=test", "BRANCH=main"}, 689 ProjectName: "run-pipe", 690 Scoops: []config.Scoop{{ 691 Repository: config.RepoRef{ 692 Owner: "{{ .Env.FOO }}", 693 Name: "{{ .Env.FOO }}", 694 Branch: "{{ .Env.BRANCH }}", 695 }, 696 Folder: "scoops", 697 Description: "A run pipe test formula", 698 Homepage: "https://github.com/goreleaser", 699 }}, 700 }, 701 testctx.GitHubTokenType, 702 testctx.WithCurrentTag("v1.0.1"), 703 testctx.WithVersion("1.0.1"), 704 ), 705 client.NewMock(), 706 }, 707 []artifact.Artifact{ 708 {Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Goamd64: "v1", Path: file}, 709 }, 710 shouldNotErr, 711 shouldNotErr, 712 func(tb testing.TB, a args) { 713 tb.Helper() 714 require.Equal(tb, "scoops/run-pipe.json", a.client.Path) 715 }, 716 }, 717 } 718 for _, tt := range tests { 719 t.Run(tt.name, func(t *testing.T) { 720 ctx := tt.args.ctx 721 ctx.Config.Dist = t.TempDir() 722 for _, a := range tt.artifacts { 723 a := a 724 a.Type = artifact.UploadableArchive 725 ctx.Artifacts.Add(&a) 726 } 727 require.NoError(t, Pipe{}.Default(ctx)) 728 tt.assertRunError(t, runAll(ctx, tt.args.client)) 729 if tt.assertPublishError != nil { 730 tt.assertPublishError(t, publishAll(ctx, tt.args.client)) 731 } 732 tt.assert(t, tt.args) 733 }) 734 } 735 } 736 737 func TestRunPipePullRequest(t *testing.T) { 738 folder := t.TempDir() 739 ctx := testctx.NewWithCfg( 740 config.Project{ 741 Dist: folder, 742 ProjectName: "foo", 743 Scoops: []config.Scoop{{ 744 Name: "{{.Env.FOO}}", 745 Homepage: "https://{{.Env.FOO}}.com", 746 Description: "Fake desc for {{.ProjectName}}", 747 Repository: config.RepoRef{ 748 Owner: "foo", 749 Name: "bar", 750 Branch: "update-{{.Version}}", 751 PullRequest: config.PullRequest{ 752 Enabled: true, 753 }, 754 }, 755 }}, 756 }, 757 testctx.WithVersion("1.2.1"), 758 testctx.WithCurrentTag("v1.2.1"), 759 testctx.WithEnv(map[string]string{"FOO": "foobar"}), 760 ) 761 path := filepath.Join(folder, "dist/foo_windows_amd64/foo.exe") 762 ctx.Artifacts.Add(&artifact.Artifact{ 763 Name: "foo_windows_amd64.tar.gz", 764 Path: path, 765 Goos: "windows", 766 Goarch: "amd64", 767 Type: artifact.UploadableArchive, 768 Extra: map[string]interface{}{ 769 artifact.ExtraID: "foo", 770 artifact.ExtraFormat: "tar.gz", 771 artifact.ExtraBinary: "foo", 772 }, 773 }) 774 775 require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) 776 f, err := os.Create(path) 777 require.NoError(t, err) 778 require.NoError(t, f.Close()) 779 780 client := client.NewMock() 781 require.NoError(t, runAll(ctx, client)) 782 require.NoError(t, publishAll(ctx, client)) 783 require.True(t, client.CreatedFile) 784 require.True(t, client.OpenedPullRequest) 785 golden.RequireEqualJSON(t, []byte(client.Content)) 786 } 787 788 func Test_buildManifest(t *testing.T) { 789 folder := t.TempDir() 790 file := filepath.Join(folder, "archive") 791 require.NoError(t, os.WriteFile(file, []byte("lorem ipsum"), 0o644)) 792 793 tests := []struct { 794 desc string 795 ctx *context.Context 796 }{ 797 { 798 "common", 799 testctx.NewWithCfg( 800 config.Project{ 801 GitHubURLs: config.GitHubURLs{ 802 Download: "https://github.com", 803 }, 804 ProjectName: "run-pipe", 805 Release: config.Release{ 806 GitHub: config.Repo{ 807 Owner: "test", 808 Name: "test", 809 }, 810 }, 811 Scoop: config.Scoop{ 812 Repository: config.RepoRef{ 813 Owner: "test", 814 Name: "test", 815 }, 816 Description: "A run pipe test formula", 817 Homepage: "https://github.com/goreleaser", 818 Persist: []string{"data", "config", "test.ini"}, 819 }, 820 }, 821 testctx.GitHubTokenType, 822 testctx.WithCurrentTag("v1.0.1"), 823 testctx.WithVersion("1.0.1"), 824 ), 825 }, 826 { 827 "pre-post-install", 828 testctx.NewWithCfg( 829 config.Project{ 830 GitHubURLs: config.GitHubURLs{ 831 Download: "https://github.com", 832 }, 833 ProjectName: "run-pipe", 834 Release: config.Release{ 835 GitHub: config.Repo{ 836 Owner: "test", 837 Name: "test", 838 }, 839 }, 840 Scoop: config.Scoop{ 841 Repository: config.RepoRef{ 842 Owner: "test", 843 Name: "test", 844 }, 845 Description: "A run pipe test formula", 846 Homepage: "https://github.com/goreleaser", 847 Persist: []string{"data", "config", "test.ini"}, 848 PreInstall: []string{"Write-Host 'Running preinstall command'"}, 849 PostInstall: []string{"Write-Host 'Running postinstall command'"}, 850 }, 851 }, 852 testctx.GitHubTokenType, 853 testctx.WithCurrentTag("v1.0.1"), 854 testctx.WithVersion("1.0.1"), 855 ), 856 }, 857 { 858 "url template", 859 testctx.NewWithCfg( 860 config.Project{ 861 GitHubURLs: config.GitHubURLs{ 862 Download: "https://github.com", 863 }, 864 ProjectName: "run-pipe", 865 Scoop: config.Scoop{ 866 Repository: config.RepoRef{ 867 Owner: "test", 868 Name: "test", 869 }, 870 Description: "A run pipe test formula", 871 Homepage: "https://github.com/goreleaser", 872 URLTemplate: "http://github.mycompany.com/foo/bar/{{ .Tag }}/{{ .ArtifactName }}", 873 CommitMessageTemplate: "chore(scoop): update {{ .ProjectName }} version {{ .Tag }}", 874 Persist: []string{"data.cfg", "etc"}, 875 }, 876 }, 877 testctx.WithCurrentTag("v1.0.1"), 878 testctx.GitHubTokenType, 879 testctx.WithVersion("1.0.1"), 880 ), 881 }, 882 { 883 "gitlab url template", 884 testctx.NewWithCfg( 885 config.Project{ 886 GitLabURLs: config.GitLabURLs{ 887 Download: "https://gitlab.com", 888 }, 889 ProjectName: "run-pipe", 890 Scoop: config.Scoop{ 891 Repository: config.RepoRef{ 892 Owner: "test", 893 Name: "test", 894 }, 895 Description: "A run pipe test formula", 896 Homepage: "https://gitlab.com/goreleaser", 897 URLTemplate: "http://gitlab.mycompany.com/foo/bar/-/releases/{{ .Tag }}/downloads/{{ .ArtifactName }}", 898 CommitMessageTemplate: "chore(scoop): update {{ .ProjectName }} version {{ .Tag }}", 899 Persist: []string{"data.cfg", "etc"}, 900 }, 901 }, 902 testctx.GitHubTokenType, 903 testctx.WithCurrentTag("v1.0.1"), 904 testctx.WithVersion("1.0.1"), 905 ), 906 }, 907 } 908 909 for _, tt := range tests { 910 t.Run(tt.desc, func(t *testing.T) { 911 ctx := tt.ctx 912 err := Pipe{}.Default(ctx) 913 require.NoError(t, err) 914 915 cl, err := client.New(ctx) 916 require.NoError(t, err) 917 require.NoError(t, Pipe{}.Default(ctx)) 918 919 mf, err := dataFor(ctx, ctx.Config.Scoops[0], cl, []*artifact.Artifact{ 920 { 921 Name: "foo_1.0.1_windows_amd64.tar.gz", 922 Goos: "windows", 923 Goarch: "amd64", 924 Goamd64: "v1", 925 Path: file, 926 Extra: map[string]interface{}{ 927 artifact.ExtraBinaries: []string{ 928 "foo.exe", 929 "bar.exe", 930 }, 931 }, 932 }, 933 { 934 Name: "foo_1.0.1_windows_arm.tar.gz", 935 Goos: "windows", 936 Goarch: "arm", 937 Path: file, 938 Extra: map[string]interface{}{ 939 artifact.ExtraBinaries: []string{ 940 "foo.exe", 941 "bar.exe", 942 }, 943 }, 944 }, 945 { 946 Name: "foo_1.0.1_windows_arm64.tar.gz", 947 Goos: "windows", 948 Goarch: "arm64", 949 Path: file, 950 Extra: map[string]interface{}{ 951 artifact.ExtraBinaries: []string{ 952 "foo.exe", 953 "bar.exe", 954 }, 955 }, 956 }, 957 { 958 Name: "foo_1.0.1_windows_386.tar.gz", 959 Goos: "windows", 960 Goarch: "386", 961 Path: file, 962 Extra: map[string]interface{}{ 963 artifact.ExtraBinaries: []string{ 964 "foo.exe", 965 "bar.exe", 966 }, 967 }, 968 }, 969 }) 970 require.NoError(t, err) 971 972 out, err := doBuildManifest(mf) 973 require.NoError(t, err) 974 975 golden.RequireEqualJSON(t, out.Bytes()) 976 }) 977 } 978 } 979 980 func getScoopPipeSkipCtx(folder string) (*context.Context, string) { 981 ctx := testctx.NewWithCfg( 982 config.Project{ 983 Dist: folder, 984 ProjectName: "run-pipe", 985 Scoop: config.Scoop{ 986 Repository: config.RepoRef{ 987 Owner: "test", 988 Name: "test", 989 }, 990 Description: "A run pipe test formula", 991 Homepage: "https://github.com/goreleaser", 992 Name: "run-pipe", 993 }, 994 }, 995 testctx.WithCurrentTag("v1.0.1"), 996 testctx.WithVersion("1.0.1"), 997 ) 998 999 path := filepath.Join(folder, "bin.tar.gz") 1000 1001 ctx.Artifacts.Add(&artifact.Artifact{ 1002 Name: "bin.tar.gz", 1003 Path: path, 1004 Goos: "windows", 1005 Goarch: "amd64", 1006 Goamd64: "v1", 1007 Type: artifact.UploadableArchive, 1008 Extra: map[string]interface{}{ 1009 artifact.ExtraID: "foo", 1010 artifact.ExtraFormat: "tar.gz", 1011 }, 1012 }) 1013 ctx.Artifacts.Add(&artifact.Artifact{ 1014 Name: "ignored.tar.gz", 1015 Path: path, 1016 Goos: "windows", 1017 Goarch: "amd64", 1018 Goamd64: "v3", 1019 Type: artifact.UploadableArchive, 1020 Extra: map[string]interface{}{ 1021 artifact.ExtraID: "foo", 1022 artifact.ExtraFormat: "tar.gz", 1023 }, 1024 }) 1025 1026 return ctx, path 1027 } 1028 1029 func TestRunPipeScoopWithSkipUpload(t *testing.T) { 1030 folder := t.TempDir() 1031 ctx, path := getScoopPipeSkipCtx(folder) 1032 ctx.Config.Scoop.SkipUpload = "true" 1033 1034 f, err := os.Create(path) 1035 require.NoError(t, err) 1036 require.NoError(t, f.Close()) 1037 1038 cli := client.NewMock() 1039 require.NoError(t, Pipe{}.Default(ctx)) 1040 require.NoError(t, runAll(ctx, cli)) 1041 require.EqualError(t, publishAll(ctx, cli), `scoop.skip_upload is true`) 1042 1043 distFile := filepath.Join(folder, "scoop", ctx.Config.Scoop.Name+".json") 1044 _, err = os.Stat(distFile) 1045 require.NoError(t, err, "file should exist: "+distFile) 1046 } 1047 1048 func TestWrapInDirectory(t *testing.T) { 1049 folder := t.TempDir() 1050 file := filepath.Join(folder, "archive") 1051 require.NoError(t, os.WriteFile(file, []byte("lorem ipsum"), 0o644)) 1052 1053 ctx := testctx.NewWithCfg( 1054 config.Project{ 1055 GitLabURLs: config.GitLabURLs{ 1056 Download: "https://gitlab.com", 1057 }, 1058 ProjectName: "run-pipe", 1059 Scoops: []config.Scoop{{ 1060 Repository: config.RepoRef{ 1061 Owner: "test", 1062 Name: "test", 1063 }, 1064 Description: "A run pipe test formula", 1065 Homepage: "https://gitlab.com/goreleaser", 1066 URLTemplate: "http://gitlab.mycompany.com/foo/bar/-/releases/{{ .Tag }}/downloads/{{ .ArtifactName }}", 1067 CommitMessageTemplate: "chore(scoop): update {{ .ProjectName }} version {{ .Tag }}", 1068 Persist: []string{"data.cfg", "etc"}, 1069 }}, 1070 }, 1071 testctx.GitHubTokenType, 1072 testctx.WithCurrentTag("v1.0.1"), 1073 testctx.WithVersion("1.0.1"), 1074 ) 1075 1076 require.NoError(t, Pipe{}.Default(ctx)) 1077 cl, err := client.New(ctx) 1078 require.NoError(t, err) 1079 mf, err := dataFor(ctx, ctx.Config.Scoops[0], cl, []*artifact.Artifact{ 1080 { 1081 Name: "foo_1.0.1_windows_amd64.tar.gz", 1082 Goos: "windows", 1083 Goarch: "amd64", 1084 Goamd64: "v1", 1085 Path: file, 1086 Extra: map[string]interface{}{ 1087 artifact.ExtraWrappedIn: "foo_1.0.1_windows_amd64", 1088 artifact.ExtraBinaries: []string{ 1089 "foo.exe", 1090 "bar.exe", 1091 }, 1092 }, 1093 }, 1094 }) 1095 require.NoError(t, err) 1096 1097 out, err := doBuildManifest(mf) 1098 require.NoError(t, err) 1099 golden.RequireEqualJSON(t, out.Bytes()) 1100 } 1101 1102 func TestSkip(t *testing.T) { 1103 t.Run("skip", func(t *testing.T) { 1104 require.True(t, Pipe{}.Skip(testctx.New())) 1105 }) 1106 t.Run("skip flag", func(t *testing.T) { 1107 ctx := testctx.NewWithCfg(config.Project{ 1108 Scoop: config.Scoop{ 1109 Repository: config.RepoRef{ 1110 Name: "a", 1111 }, 1112 }, 1113 }, testctx.Skip(skips.Scoop)) 1114 require.True(t, Pipe{}.Skip(ctx)) 1115 }) 1116 t.Run("dont skip", func(t *testing.T) { 1117 ctx := testctx.NewWithCfg(config.Project{ 1118 Scoop: config.Scoop{ 1119 Repository: config.RepoRef{ 1120 Name: "a", 1121 }, 1122 }, 1123 }) 1124 require.False(t, Pipe{}.Skip(ctx)) 1125 }) 1126 }