github.com/joselitofilho/goreleaser@v0.155.1-0.20210123221854-e4891856c593/internal/pipe/brew/brew_test.go (about) 1 package brew 2 3 import ( 4 "flag" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "testing" 10 11 "github.com/goreleaser/goreleaser/internal/artifact" 12 "github.com/goreleaser/goreleaser/internal/client" 13 "github.com/goreleaser/goreleaser/internal/testlib" 14 "github.com/goreleaser/goreleaser/pkg/config" 15 "github.com/goreleaser/goreleaser/pkg/context" 16 "github.com/stretchr/testify/require" 17 ) 18 19 var update = flag.Bool("update", false, "update .golden files") 20 21 func TestDescription(t *testing.T) { 22 require.NotEmpty(t, Pipe{}.String()) 23 } 24 25 func TestNameWithDash(t *testing.T) { 26 require.Equal(t, formulaNameFor("some-binary"), "SomeBinary") 27 } 28 29 func TestNameWithUnderline(t *testing.T) { 30 require.Equal(t, formulaNameFor("some_binary"), "SomeBinary") 31 } 32 33 func TestNameWithAT(t *testing.T) { 34 require.Equal(t, formulaNameFor("some_binary@1"), "SomeBinaryAT1") 35 } 36 37 func TestSimpleName(t *testing.T) { 38 require.Equal(t, formulaNameFor("binary"), "Binary") 39 } 40 41 var defaultTemplateData = templateData{ 42 Desc: "Some desc", 43 Homepage: "https://google.com", 44 MacOS: downloadable{ 45 DownloadURL: "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Darwin_x86_64.tar.gz", 46 SHA256: "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68", 47 }, 48 LinuxAmd64: downloadable{ 49 DownloadURL: "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Linux_x86_64.tar.gz", 50 SHA256: "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c67", 51 }, 52 LinuxArm: downloadable{ 53 DownloadURL: "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Arm6.tar.gz", 54 SHA256: "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c67", 55 }, 56 LinuxArm64: downloadable{ 57 DownloadURL: "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Arm64.tar.gz", 58 SHA256: "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c67", 59 }, 60 Name: "Test", 61 Version: "0.1.3", 62 Caveats: []string{}, 63 } 64 65 func assertDefaultTemplateData(t *testing.T, formulae string) { 66 t.Helper() 67 require.Contains(t, formulae, "class Test < Formula") 68 require.Contains(t, formulae, `homepage "https://google.com"`) 69 require.Contains(t, formulae, `url "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Darwin_x86_64.tar.gz"`) 70 require.Contains(t, formulae, `sha256 "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68"`) 71 require.Contains(t, formulae, `version "0.1.3"`) 72 } 73 74 func TestFullFormulae(t *testing.T) { 75 data := defaultTemplateData 76 data.License = "MIT" 77 data.Caveats = []string{"Here are some caveats"} 78 data.Dependencies = []config.HomebrewDependency{{Name: "gtk+"}} 79 data.Conflicts = []string{"svn"} 80 data.Plist = "it works" 81 data.PostInstall = `system "touch", "/tmp/foo"` 82 data.CustomBlock = []string{"devel do", ` url "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Darwin_x86_64.tar.gz"`, ` sha256 "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68"`, "end"} 83 data.Install = []string{"custom install script", "another install script"} 84 data.Tests = []string{`system "#{bin}/{{.ProjectName}} -version"`} 85 formulae, err := doBuildFormula(context.New(config.Project{ 86 ProjectName: "foo", 87 }), data) 88 require.NoError(t, err) 89 90 var golden = "testdata/test.rb.golden" 91 if *update { 92 err := ioutil.WriteFile(golden, []byte(formulae), 0655) 93 require.NoError(t, err) 94 } 95 bts, err := ioutil.ReadFile(golden) 96 require.NoError(t, err) 97 require.Equal(t, string(bts), formulae) 98 } 99 100 func TestFullFormulaeLinuxOnly(t *testing.T) { 101 data := defaultTemplateData 102 data.MacOS = downloadable{} 103 data.Install = []string{`bin.install "test"`} 104 formulae, err := doBuildFormula(context.New(config.Project{ 105 ProjectName: "foo", 106 }), data) 107 require.NoError(t, err) 108 109 var golden = "testdata/test_linux_only.rb.golden" 110 if *update { 111 err := ioutil.WriteFile(golden, []byte(formulae), 0655) 112 require.NoError(t, err) 113 } 114 bts, err := ioutil.ReadFile(golden) 115 require.NoError(t, err) 116 require.Equal(t, string(bts), formulae) 117 } 118 119 func TestFormulaeSimple(t *testing.T) { 120 formulae, err := doBuildFormula(context.New(config.Project{}), defaultTemplateData) 121 require.NoError(t, err) 122 assertDefaultTemplateData(t, formulae) 123 require.NotContains(t, formulae, "def caveats") 124 require.NotContains(t, formulae, "depends_on") 125 require.NotContains(t, formulae, "def plist;") 126 } 127 128 func TestSplit(t *testing.T) { 129 var parts = split("system \"true\"\nsystem \"#{bin}/foo -h\"") 130 require.Equal(t, []string{"system \"true\"", "system \"#{bin}/foo -h\""}, parts) 131 parts = split("") 132 require.Equal(t, []string{}, parts) 133 parts = split("\n ") 134 require.Equal(t, []string{}, parts) 135 } 136 137 func TestRunPipe(t *testing.T) { 138 for name, fn := range map[string]func(ctx *context.Context){ 139 "default": func(ctx *context.Context) { 140 ctx.TokenType = context.TokenTypeGitHub 141 ctx.Config.Brews[0].Tap.Owner = "test" 142 ctx.Config.Brews[0].Tap.Name = "test" 143 ctx.Config.Brews[0].Homepage = "https://github.com/goreleaser" 144 }, 145 "custom_download_strategy": func(ctx *context.Context) { 146 ctx.TokenType = context.TokenTypeGitHub 147 ctx.Config.Brews[0].Tap.Owner = "test" 148 ctx.Config.Brews[0].Tap.Name = "test" 149 ctx.Config.Brews[0].Homepage = "https://github.com/goreleaser" 150 151 ctx.Config.Brews[0].DownloadStrategy = "GitHubPrivateRepositoryReleaseDownloadStrategy" 152 }, 153 "custom_require": func(ctx *context.Context) { 154 ctx.TokenType = context.TokenTypeGitHub 155 ctx.Config.Brews[0].Tap.Owner = "test" 156 ctx.Config.Brews[0].Tap.Name = "test" 157 ctx.Config.Brews[0].Homepage = "https://github.com/goreleaser" 158 159 ctx.Config.Brews[0].DownloadStrategy = "CustomDownloadStrategy" 160 ctx.Config.Brews[0].CustomRequire = "custom_download_strategy" 161 }, 162 "custom_block": func(ctx *context.Context) { 163 ctx.TokenType = context.TokenTypeGitHub 164 ctx.Config.Brews[0].Tap.Owner = "test" 165 ctx.Config.Brews[0].Tap.Name = "test" 166 ctx.Config.Brews[0].Homepage = "https://github.com/goreleaser" 167 168 ctx.Config.Brews[0].CustomBlock = `head "https://github.com/caarlos0/test.git"` 169 }, 170 "default_gitlab": func(ctx *context.Context) { 171 ctx.TokenType = context.TokenTypeGitLab 172 ctx.Config.Brews[0].Tap.Owner = "test" 173 ctx.Config.Brews[0].Tap.Name = "test" 174 ctx.Config.Brews[0].Homepage = "https://gitlab.com/goreleaser" 175 }, 176 } { 177 t.Run(name, func(t *testing.T) { 178 var folder = t.TempDir() 179 var ctx = &context.Context{ 180 Git: context.GitInfo{ 181 CurrentTag: "v1.0.1", 182 }, 183 Version: "1.0.1", 184 Artifacts: artifact.New(), 185 Env: map[string]string{ 186 "FOO": "foo_is_bar", 187 }, 188 Config: config.Project{ 189 Dist: folder, 190 ProjectName: name, 191 Brews: []config.Homebrew{ 192 { 193 Name: name, 194 IDs: []string{ 195 "foo", 196 }, 197 Description: "A run pipe test formula and FOO={{ .Env.FOO }}", 198 Caveats: "don't do this {{ .ProjectName }}", 199 Test: "system \"true\"\nsystem \"#{bin}/foo -h\"", 200 Plist: `<xml>whatever</xml>`, 201 Dependencies: []config.HomebrewDependency{{Name: "zsh", Type: "optional"}, {Name: "bash"}}, 202 Conflicts: []string{"gtk+", "qt"}, 203 Install: `bin.install "{{ .ProjectName }}"`, 204 }, 205 }, 206 }, 207 } 208 fn(ctx) 209 ctx.Artifacts.Add(&artifact.Artifact{ 210 Name: "bar_bin.tar.gz", 211 Path: "doesnt matter", 212 Goos: "darwin", 213 Goarch: "amd64", 214 Type: artifact.UploadableArchive, 215 Extra: map[string]interface{}{ 216 "ID": "bar", 217 "Format": "tar.gz", 218 "ArtifactUploadHash": "820ead5d9d2266c728dce6d4d55b6460", 219 }, 220 }) 221 var path = filepath.Join(folder, "bin.tar.gz") 222 ctx.Artifacts.Add(&artifact.Artifact{ 223 Name: "bin.tar.gz", 224 Path: path, 225 Goos: "darwin", 226 Goarch: "amd64", 227 Type: artifact.UploadableArchive, 228 Extra: map[string]interface{}{ 229 "ID": "foo", 230 "Format": "tar.gz", 231 "ArtifactUploadHash": "820ead5d9d2266c728dce6d4d55b6460", 232 }, 233 }) 234 235 _, err := os.Create(path) 236 require.NoError(t, err) 237 client := &DummyClient{} 238 var distFile = filepath.Join(folder, name+".rb") 239 240 require.NoError(t, doRun(ctx, ctx.Config.Brews[0], client)) 241 require.True(t, client.CreatedFile) 242 var golden = fmt.Sprintf("testdata/%s.rb.golden", name) 243 if *update { 244 require.NoError(t, ioutil.WriteFile(golden, []byte(client.Content), 0655)) 245 } 246 bts, err := ioutil.ReadFile(golden) 247 require.NoError(t, err) 248 require.Equal(t, string(bts), client.Content) 249 250 distBts, err := ioutil.ReadFile(distFile) 251 require.NoError(t, err) 252 require.Equal(t, string(bts), string(distBts)) 253 }) 254 } 255 } 256 257 func TestRunPipeNameTemplate(t *testing.T) { 258 var folder = t.TempDir() 259 var ctx = &context.Context{ 260 Git: context.GitInfo{ 261 CurrentTag: "v1.0.1", 262 }, 263 Version: "1.0.1", 264 Artifacts: artifact.New(), 265 Env: map[string]string{ 266 "FOO_BAR": "is_bar", 267 }, 268 Config: config.Project{ 269 Dist: folder, 270 ProjectName: "foo", 271 Brews: []config.Homebrew{ 272 { 273 Name: "foo_{{ .Env.FOO_BAR }}", 274 Tap: config.RepoRef{ 275 Owner: "foo", 276 Name: "bar", 277 }, 278 IDs: []string{ 279 "foo", 280 }, 281 }, 282 }, 283 }, 284 } 285 var path = filepath.Join(folder, "bin.tar.gz") 286 ctx.Artifacts.Add(&artifact.Artifact{ 287 Name: "bin.tar.gz", 288 Path: path, 289 Goos: "darwin", 290 Goarch: "amd64", 291 Type: artifact.UploadableArchive, 292 Extra: map[string]interface{}{ 293 "ID": "foo", 294 "Format": "tar.gz", 295 "ArtifactUploadHash": "820ead5d9d2266c728dce6d4d55b6460", 296 }, 297 }) 298 299 _, err := os.Create(path) 300 require.NoError(t, err) 301 client := &DummyClient{} 302 var distFile = filepath.Join(folder, "foo_is_bar.rb") 303 304 require.NoError(t, doRun(ctx, ctx.Config.Brews[0], client)) 305 require.True(t, client.CreatedFile) 306 var golden = "testdata/foo_is_bar.rb.golden" 307 if *update { 308 require.NoError(t, ioutil.WriteFile(golden, []byte(client.Content), 0655)) 309 } 310 bts, err := ioutil.ReadFile(golden) 311 require.NoError(t, err) 312 require.Equal(t, string(bts), client.Content) 313 314 distBts, err := ioutil.ReadFile(distFile) 315 require.NoError(t, err) 316 require.Equal(t, string(bts), string(distBts)) 317 } 318 319 func TestRunPipeMultipleBrewsWithSkip(t *testing.T) { 320 var folder = t.TempDir() 321 var ctx = &context.Context{ 322 Git: context.GitInfo{ 323 CurrentTag: "v1.0.1", 324 }, 325 Version: "1.0.1", 326 Artifacts: artifact.New(), 327 Env: map[string]string{ 328 "FOO_BAR": "is_bar", 329 }, 330 Config: config.Project{ 331 Dist: folder, 332 ProjectName: "foo", 333 Brews: []config.Homebrew{ 334 { 335 Name: "foo", 336 Tap: config.RepoRef{ 337 Owner: "foo", 338 Name: "bar", 339 }, 340 IDs: []string{ 341 "foo", 342 }, 343 SkipUpload: "true", 344 }, 345 { 346 Name: "bar", 347 Tap: config.RepoRef{ 348 Owner: "foo", 349 Name: "bar", 350 }, 351 IDs: []string{ 352 "foo", 353 }, 354 }, 355 { 356 Name: "foobar", 357 Tap: config.RepoRef{ 358 Owner: "foo", 359 Name: "bar", 360 }, 361 IDs: []string{ 362 "foo", 363 }, 364 SkipUpload: "true", 365 }, 366 }, 367 }, 368 } 369 var path = filepath.Join(folder, "bin.tar.gz") 370 ctx.Artifacts.Add(&artifact.Artifact{ 371 Name: "bin.tar.gz", 372 Path: path, 373 Goos: "darwin", 374 Goarch: "amd64", 375 Type: artifact.UploadableArchive, 376 Extra: map[string]interface{}{ 377 "ID": "foo", 378 "Format": "tar.gz", 379 "ArtifactUploadHash": "820ead5d9d2266c728dce6d4d55b6460", 380 }, 381 }) 382 383 _, err := os.Create(path) 384 require.NoError(t, err) 385 386 var cli = &DummyClient{} 387 require.EqualError(t, publishAll(ctx, cli), `brew.skip_upload is set`) 388 require.True(t, cli.CreatedFile) 389 390 for _, brew := range ctx.Config.Brews { 391 var distFile = filepath.Join(folder, brew.Name+".rb") 392 _, err := os.Stat(distFile) 393 require.NoError(t, err, "file should exist: "+distFile) 394 } 395 396 } 397 398 func TestRunPipeForMultipleArmVersions(t *testing.T) { 399 for name, fn := range map[string]func(ctx *context.Context){ 400 "multiple_armv5": func(ctx *context.Context) { 401 ctx.Config.Brews[0].Goarm = "5" 402 }, 403 "multiple_armv6": func(ctx *context.Context) { 404 ctx.Config.Brews[0].Goarm = "6" 405 }, 406 "multiple_armv7": func(ctx *context.Context) { 407 ctx.Config.Brews[0].Goarm = "7" 408 }, 409 } { 410 var folder = t.TempDir() 411 var ctx = &context.Context{ 412 TokenType: context.TokenTypeGitHub, 413 Git: context.GitInfo{ 414 CurrentTag: "v1.0.1", 415 }, 416 Version: "1.0.1", 417 Artifacts: artifact.New(), 418 Env: map[string]string{ 419 "FOO": "foo_is_bar", 420 }, 421 Config: config.Project{ 422 Dist: folder, 423 ProjectName: name, 424 Brews: []config.Homebrew{ 425 { 426 Name: name, 427 Description: "A run pipe test formula and FOO={{ .Env.FOO }}", 428 Caveats: "don't do this {{ .ProjectName }}", 429 Test: "system \"true\"\nsystem \"#{bin}/foo -h\"", 430 Plist: `<xml>whatever</xml>`, 431 Dependencies: []config.HomebrewDependency{{Name: "zsh"}, {Name: "bash", Type: "recommended"}}, 432 Conflicts: []string{"gtk+", "qt"}, 433 Install: `bin.install "{{ .ProjectName }}"`, 434 Tap: config.RepoRef{ 435 Owner: "test", 436 Name: "test", 437 }, 438 Homepage: "https://github.com/goreleaser", 439 }, 440 }, 441 GitHubURLs: config.GitHubURLs{ 442 Download: "https://github.com", 443 }, 444 Release: config.Release{ 445 GitHub: config.Repo{ 446 Owner: "test", 447 Name: "test", 448 }, 449 }, 450 }, 451 } 452 fn(ctx) 453 for _, a := range []struct { 454 name string 455 goos string 456 goarch string 457 goarm string 458 }{ 459 { 460 name: "bin", 461 goos: "darwin", 462 goarch: "amd64", 463 }, 464 { 465 name: "arm64", 466 goos: "linux", 467 goarch: "arm64", 468 }, 469 { 470 name: "armv5", 471 goos: "linux", 472 goarch: "arm", 473 goarm: "5", 474 }, 475 { 476 name: "armv6", 477 goos: "linux", 478 goarch: "arm", 479 goarm: "6", 480 }, 481 { 482 name: "armv7", 483 goos: "linux", 484 goarch: "arm", 485 goarm: "7", 486 }, 487 } { 488 var path = filepath.Join(folder, fmt.Sprintf("%s.tar.gz", a.name)) 489 ctx.Artifacts.Add(&artifact.Artifact{ 490 Name: fmt.Sprintf("%s.tar.gz", a.name), 491 Path: path, 492 Goos: a.goos, 493 Goarch: a.goarch, 494 Goarm: a.goarm, 495 Type: artifact.UploadableArchive, 496 Extra: map[string]interface{}{ 497 "ID": a.name, 498 "Format": "tar.gz", 499 }, 500 }) 501 _, err := os.Create(path) 502 require.NoError(t, err) 503 } 504 505 client := &DummyClient{} 506 var distFile = filepath.Join(folder, name+".rb") 507 508 require.NoError(t, doRun(ctx, ctx.Config.Brews[0], client)) 509 require.True(t, client.CreatedFile) 510 var golden = fmt.Sprintf("testdata/%s.rb.golden", name) 511 if *update { 512 require.NoError(t, ioutil.WriteFile(golden, []byte(client.Content), 0655)) 513 } 514 bts, err := ioutil.ReadFile(golden) 515 require.NoError(t, err) 516 require.Equal(t, string(bts), client.Content) 517 518 distBts, err := ioutil.ReadFile(distFile) 519 require.NoError(t, err) 520 require.Equal(t, string(bts), string(distBts)) 521 } 522 } 523 524 func TestRunPipeNoBuilds(t *testing.T) { 525 var ctx = &context.Context{ 526 TokenType: context.TokenTypeGitHub, 527 Config: config.Project{ 528 Brews: []config.Homebrew{ 529 { 530 Tap: config.RepoRef{ 531 Owner: "test", 532 Name: "test", 533 }, 534 }, 535 }, 536 }, 537 } 538 client := &DummyClient{} 539 require.Equal(t, ErrNoArchivesFound, doRun(ctx, ctx.Config.Brews[0], client)) 540 require.False(t, client.CreatedFile) 541 } 542 543 func TestRunPipeMultipleArchivesSameOsBuild(t *testing.T) { 544 var ctx = context.New( 545 config.Project{ 546 Brews: []config.Homebrew{ 547 { 548 Tap: config.RepoRef{ 549 Owner: "test", 550 Name: "test", 551 }, 552 }, 553 }, 554 }, 555 ) 556 557 ctx.TokenType = context.TokenTypeGitHub 558 f, err := ioutil.TempFile(t.TempDir(), "") 559 require.NoError(t, err) 560 t.Cleanup(func() { f.Close() }) 561 562 tests := []struct { 563 expectedError error 564 osarchs []struct { 565 goos string 566 goarch string 567 goarm string 568 } 569 }{ 570 { 571 expectedError: ErrMultipleArchivesSameOS, 572 osarchs: []struct { 573 goos string 574 goarch string 575 goarm string 576 }{ 577 { 578 goos: "darwin", 579 goarch: "amd64", 580 }, 581 { 582 goos: "darwin", 583 goarch: "amd64", 584 }, 585 }, 586 }, 587 { 588 expectedError: ErrMultipleArchivesSameOS, 589 osarchs: []struct { 590 goos string 591 goarch string 592 goarm string 593 }{ 594 { 595 goos: "linux", 596 goarch: "amd64", 597 }, 598 { 599 goos: "linux", 600 goarch: "amd64", 601 }, 602 }, 603 }, 604 { 605 expectedError: ErrMultipleArchivesSameOS, 606 osarchs: []struct { 607 goos string 608 goarch string 609 goarm string 610 }{ 611 { 612 goos: "linux", 613 goarch: "arm64", 614 }, 615 { 616 goos: "linux", 617 goarch: "arm64", 618 }, 619 }, 620 }, 621 { 622 expectedError: ErrMultipleArchivesSameOS, 623 osarchs: []struct { 624 goos string 625 goarch string 626 goarm string 627 }{ 628 { 629 goos: "linux", 630 goarch: "arm", 631 goarm: "6", 632 }, 633 { 634 goos: "linux", 635 goarch: "arm", 636 goarm: "6", 637 }, 638 }, 639 }, 640 { 641 expectedError: ErrMultipleArchivesSameOS, 642 osarchs: []struct { 643 goos string 644 goarch string 645 goarm string 646 }{ 647 { 648 goos: "linux", 649 goarch: "arm", 650 goarm: "5", 651 }, 652 { 653 goos: "linux", 654 goarch: "arm", 655 goarm: "6", 656 }, 657 { 658 goos: "linux", 659 goarch: "arm", 660 goarm: "7", 661 }, 662 }, 663 }, 664 } 665 666 for _, test := range tests { 667 for idx, ttt := range test.osarchs { 668 ctx.Artifacts.Add(&artifact.Artifact{ 669 Name: fmt.Sprintf("bin%d", idx), 670 Path: f.Name(), 671 Goos: ttt.goos, 672 Goarch: ttt.goarch, 673 Type: artifact.UploadableArchive, 674 Extra: map[string]interface{}{ 675 "ID": fmt.Sprintf("foo%d", idx), 676 "Format": "tar.gz", 677 }, 678 }) 679 } 680 client := &DummyClient{} 681 require.Equal(t, test.expectedError, doRun(ctx, ctx.Config.Brews[0], client)) 682 require.False(t, client.CreatedFile) 683 // clean the artifacts for the next run 684 ctx.Artifacts = artifact.New() 685 } 686 } 687 688 func TestRunPipeBrewNotSetup(t *testing.T) { 689 var ctx = &context.Context{ 690 Config: config.Project{}, 691 } 692 client := &DummyClient{} 693 testlib.AssertSkipped(t, doRun(ctx, config.Homebrew{}, client)) 694 require.False(t, client.CreatedFile) 695 } 696 697 func TestRunPipeBinaryRelease(t *testing.T) { 698 var ctx = context.New( 699 config.Project{ 700 Brews: []config.Homebrew{ 701 { 702 Tap: config.RepoRef{ 703 Owner: "test", 704 Name: "test", 705 }, 706 }, 707 }, 708 }, 709 ) 710 ctx.Artifacts.Add(&artifact.Artifact{ 711 Name: "bin", 712 Path: "doesnt mather", 713 Goos: "darwin", 714 Goarch: "amd64", 715 Type: artifact.Binary, 716 }) 717 client := &DummyClient{} 718 require.Equal(t, ErrNoArchivesFound, doRun(ctx, ctx.Config.Brews[0], client)) 719 require.False(t, client.CreatedFile) 720 } 721 722 func TestRunPipeNoUpload(t *testing.T) { 723 var folder = t.TempDir() 724 var ctx = context.New(config.Project{ 725 Dist: folder, 726 ProjectName: "foo", 727 Release: config.Release{}, 728 Brews: []config.Homebrew{ 729 { 730 Tap: config.RepoRef{ 731 Owner: "test", 732 Name: "test", 733 }, 734 }, 735 }, 736 }) 737 ctx.TokenType = context.TokenTypeGitHub 738 ctx.Git = context.GitInfo{CurrentTag: "v1.0.1"} 739 var path = filepath.Join(folder, "whatever.tar.gz") 740 _, err := os.Create(path) 741 require.NoError(t, err) 742 ctx.Artifacts.Add(&artifact.Artifact{ 743 Name: "bin", 744 Path: path, 745 Goos: "darwin", 746 Goarch: "amd64", 747 Type: artifact.UploadableArchive, 748 Extra: map[string]interface{}{ 749 "ID": "foo", 750 "Format": "tar.gz", 751 }, 752 }) 753 client := &DummyClient{} 754 755 var assertNoPublish = func(t *testing.T) { 756 t.Helper() 757 testlib.AssertSkipped(t, doRun(ctx, ctx.Config.Brews[0], client)) 758 require.False(t, client.CreatedFile) 759 } 760 t.Run("skip upload", func(t *testing.T) { 761 ctx.Config.Release.Draft = false 762 ctx.Config.Brews[0].SkipUpload = "true" 763 ctx.SkipPublish = false 764 assertNoPublish(t) 765 }) 766 t.Run("skip publish", func(t *testing.T) { 767 ctx.Config.Release.Draft = false 768 ctx.Config.Brews[0].SkipUpload = "false" 769 ctx.SkipPublish = true 770 assertNoPublish(t) 771 }) 772 } 773 774 func TestRunEmptyTokenType(t *testing.T) { 775 var folder = t.TempDir() 776 var ctx = context.New(config.Project{ 777 Dist: folder, 778 ProjectName: "foo", 779 Release: config.Release{}, 780 Brews: []config.Homebrew{ 781 { 782 Tap: config.RepoRef{ 783 Owner: "test", 784 Name: "test", 785 }, 786 }, 787 }, 788 }) 789 ctx.Git = context.GitInfo{CurrentTag: "v1.0.1"} 790 var path = filepath.Join(folder, "whatever.tar.gz") 791 _, err := os.Create(path) 792 require.NoError(t, err) 793 ctx.Artifacts.Add(&artifact.Artifact{ 794 Name: "bin", 795 Path: path, 796 Goos: "darwin", 797 Goarch: "amd64", 798 Type: artifact.UploadableArchive, 799 Extra: map[string]interface{}{ 800 "ID": "foo", 801 "Format": "tar.gz", 802 }, 803 }) 804 client := &DummyClient{} 805 require.NoError(t, doRun(ctx, ctx.Config.Brews[0], client)) 806 } 807 808 func TestRunTokenTypeNotImplementedForBrew(t *testing.T) { 809 var folder = t.TempDir() 810 var ctx = context.New(config.Project{ 811 Dist: folder, 812 ProjectName: "foo", 813 Release: config.Release{}, 814 Brews: []config.Homebrew{ 815 { 816 Tap: config.RepoRef{ 817 Owner: "test", 818 Name: "test", 819 }, 820 }, 821 }, 822 }) 823 ctx.TokenType = context.TokenTypeGitea 824 ctx.Git = context.GitInfo{CurrentTag: "v1.0.1"} 825 var path = filepath.Join(folder, "whatever.tar.gz") 826 _, err := os.Create(path) 827 require.NoError(t, err) 828 ctx.Artifacts.Add(&artifact.Artifact{ 829 Name: "bin", 830 Path: path, 831 Goos: "darwin", 832 Goarch: "amd64", 833 Type: artifact.UploadableArchive, 834 Extra: map[string]interface{}{ 835 "ID": "foo", 836 "Format": "tar.gz", 837 }, 838 }) 839 client := &DummyClient{NotImplemented: true} 840 require.Equal(t, ErrTokenTypeNotImplementedForBrew{TokenType: "gitea"}, doRun(ctx, ctx.Config.Brews[0], client)) 841 } 842 843 func TestDefault(t *testing.T) { 844 testlib.Mktmp(t) 845 846 var ctx = &context.Context{ 847 TokenType: context.TokenTypeGitHub, 848 Config: config.Project{ 849 ProjectName: "myproject", 850 Brews: []config.Homebrew{ 851 {}, 852 }, 853 Builds: []config.Build{ 854 { 855 Binary: "foo", 856 Goos: []string{"linux", "darwin"}, 857 Goarch: []string{"386", "amd64"}, 858 }, 859 { 860 Binary: "bar", 861 Goos: []string{"linux", "darwin"}, 862 Goarch: []string{"386", "amd64"}, 863 Ignore: []config.IgnoredBuild{ 864 {Goos: "darwin", Goarch: "amd64"}, 865 }, 866 }, 867 { 868 Binary: "foobar", 869 Goos: []string{"linux"}, 870 Goarch: []string{"amd64"}, 871 }, 872 }, 873 }, 874 } 875 require.NoError(t, Pipe{}.Default(ctx)) 876 require.Equal(t, ctx.Config.ProjectName, ctx.Config.Brews[0].Name) 877 require.NotEmpty(t, ctx.Config.Brews[0].CommitAuthor.Name) 878 require.NotEmpty(t, ctx.Config.Brews[0].CommitAuthor.Email) 879 require.Equal(t, `bin.install "myproject"`, ctx.Config.Brews[0].Install) 880 } 881 882 func TestGHFolder(t *testing.T) { 883 require.Equal(t, "bar.rb", buildFormulaPath("", "bar.rb")) 884 require.Equal(t, "fooo/bar.rb", buildFormulaPath("fooo", "bar.rb")) 885 } 886 887 type DummyClient struct { 888 CreatedFile bool 889 Content string 890 NotImplemented bool 891 } 892 893 func (dc *DummyClient) CloseMilestone(ctx *context.Context, repo client.Repo, title string) error { 894 return nil 895 } 896 897 func (dc *DummyClient) CreateRelease(ctx *context.Context, body string) (releaseID string, err error) { 898 return 899 } 900 901 func (dc *DummyClient) ReleaseURLTemplate(ctx *context.Context) (string, error) { 902 if dc.NotImplemented { 903 return "", client.NotImplementedError{} 904 } 905 906 return "https://dummyhost/download/{{ .Tag }}/{{ .ArtifactName }}", nil 907 } 908 909 func (dc *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo client.Repo, content []byte, path, msg string) (err error) { 910 dc.CreatedFile = true 911 dc.Content = string(content) 912 return 913 } 914 915 func (dc *DummyClient) Upload(ctx *context.Context, releaseID string, artifact *artifact.Artifact, file *os.File) (err error) { 916 return 917 }