github.com/goreleaser/goreleaser@v1.25.1/internal/pipe/brew/brew_test.go (about)

     1  package brew
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/goreleaser/goreleaser/internal/artifact"
    10  	"github.com/goreleaser/goreleaser/internal/client"
    11  	"github.com/goreleaser/goreleaser/internal/golden"
    12  	"github.com/goreleaser/goreleaser/internal/skips"
    13  	"github.com/goreleaser/goreleaser/internal/testctx"
    14  	"github.com/goreleaser/goreleaser/internal/testlib"
    15  	"github.com/goreleaser/goreleaser/internal/tmpl"
    16  	"github.com/goreleaser/goreleaser/pkg/config"
    17  	"github.com/goreleaser/goreleaser/pkg/context"
    18  	"github.com/stretchr/testify/require"
    19  )
    20  
    21  func TestContinueOnError(t *testing.T) {
    22  	require.True(t, Pipe{}.ContinueOnError())
    23  }
    24  
    25  func TestDescription(t *testing.T) {
    26  	require.NotEmpty(t, Pipe{}.String())
    27  }
    28  
    29  func TestNameWithDash(t *testing.T) {
    30  	require.Equal(t, "SomeBinary", formulaNameFor("some-binary"))
    31  }
    32  
    33  func TestNameWithUnderline(t *testing.T) {
    34  	require.Equal(t, "SomeBinary", formulaNameFor("some_binary"))
    35  }
    36  
    37  func TestNameWithDots(t *testing.T) {
    38  	require.Equal(t, "Binaryv000", formulaNameFor("binaryv0.0.0"))
    39  }
    40  
    41  func TestNameWithAT(t *testing.T) {
    42  	require.Equal(t, "SomeBinaryAT1", formulaNameFor("some_binary@1"))
    43  }
    44  
    45  func TestSimpleName(t *testing.T) {
    46  	require.Equal(t, "Binary", formulaNameFor("binary"))
    47  }
    48  
    49  var defaultTemplateData = templateData{
    50  	Desc:     "Some desc",
    51  	Homepage: "https://google.com",
    52  	LinuxPackages: []releasePackage{
    53  		{
    54  			DownloadURL: "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Linux_x86_64.tar.gz",
    55  			SHA256:      "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c67",
    56  			OS:          "linux",
    57  			Arch:        "amd64",
    58  			Install:     []string{`bin.install "test"`},
    59  		},
    60  		{
    61  			DownloadURL: "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Arm6.tar.gz",
    62  			SHA256:      "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c67",
    63  			OS:          "linux",
    64  			Arch:        "arm",
    65  			Install:     []string{`bin.install "test"`},
    66  		},
    67  		{
    68  			DownloadURL: "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Arm64.tar.gz",
    69  			SHA256:      "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c67",
    70  			OS:          "linux",
    71  			Arch:        "arm64",
    72  			Install:     []string{`bin.install "test"`},
    73  		},
    74  	},
    75  	MacOSPackages: []releasePackage{
    76  		{
    77  			DownloadURL: "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Darwin_x86_64.tar.gz",
    78  			SHA256:      "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68",
    79  			OS:          "darwin",
    80  			Arch:        "amd64",
    81  			Install:     []string{`bin.install "test"`},
    82  		},
    83  		{
    84  			DownloadURL: "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Darwin_arm64.tar.gz",
    85  			SHA256:      "1df5fdc2bad4ed4c28fbdc77b6c542988c0dc0e2ae34e0dc912bbb1c66646c58",
    86  			OS:          "darwin",
    87  			Arch:        "arm64",
    88  			Install:     []string{`bin.install "test"`},
    89  		},
    90  	},
    91  	Name:                 "Test",
    92  	Version:              "0.1.3",
    93  	Caveats:              []string{},
    94  	HasOnlyAmd64MacOsPkg: false,
    95  }
    96  
    97  func assertDefaultTemplateData(t *testing.T, formulae string) {
    98  	t.Helper()
    99  	require.Contains(t, formulae, "class Test < Formula")
   100  	require.Contains(t, formulae, `homepage "https://google.com"`)
   101  	require.Contains(t, formulae, `url "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Darwin_x86_64.tar.gz"`)
   102  	require.Contains(t, formulae, `sha256 "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68"`)
   103  	require.Contains(t, formulae, `version "0.1.3"`)
   104  }
   105  
   106  func TestFullFormulae(t *testing.T) {
   107  	data := defaultTemplateData
   108  	data.License = "MIT"
   109  	data.Caveats = []string{"Here are some caveats"}
   110  	data.Dependencies = []config.HomebrewDependency{{Name: "gtk+"}}
   111  	data.Conflicts = []string{"svn"}
   112  	data.Plist = "it works"
   113  	data.PostInstall = []string{`touch "/tmp/foo"`, `system "echo", "done"`}
   114  	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"}
   115  	data.Tests = []string{`system "#{bin}/{{.ProjectName}}", "-version"`}
   116  	formulae, err := doBuildFormula(testctx.NewWithCfg(config.Project{
   117  		ProjectName: "foo",
   118  	}), data)
   119  	require.NoError(t, err)
   120  
   121  	golden.RequireEqualRb(t, []byte(formulae))
   122  }
   123  
   124  func TestFullFormulaeLinuxOnly(t *testing.T) {
   125  	data := defaultTemplateData
   126  	data.MacOSPackages = []releasePackage{}
   127  	formulae, err := doBuildFormula(testctx.NewWithCfg(config.Project{
   128  		ProjectName: "foo",
   129  	}), data)
   130  	require.NoError(t, err)
   131  
   132  	golden.RequireEqualRb(t, []byte(formulae))
   133  }
   134  
   135  func TestFullFormulaeMacOSOnly(t *testing.T) {
   136  	data := defaultTemplateData
   137  	data.LinuxPackages = []releasePackage{}
   138  	formulae, err := doBuildFormula(testctx.NewWithCfg(config.Project{
   139  		ProjectName: "foo",
   140  	}), data)
   141  	require.NoError(t, err)
   142  
   143  	golden.RequireEqualRb(t, []byte(formulae))
   144  }
   145  
   146  func TestFormulaeSimple(t *testing.T) {
   147  	formulae, err := doBuildFormula(testctx.NewWithCfg(config.Project{}), defaultTemplateData)
   148  	require.NoError(t, err)
   149  	assertDefaultTemplateData(t, formulae)
   150  	require.NotContains(t, formulae, "def caveats")
   151  	require.NotContains(t, formulae, "def plist;")
   152  }
   153  
   154  func TestSplit(t *testing.T) {
   155  	parts := split("system \"true\"\nsystem \"#{bin}/foo\", \"-h\"")
   156  	require.Equal(t, []string{"system \"true\"", "system \"#{bin}/foo\", \"-h\""}, parts)
   157  	parts = split("")
   158  	require.Equal(t, []string{}, parts)
   159  	parts = split("\n  ")
   160  	require.Equal(t, []string{}, parts)
   161  }
   162  
   163  func TestFullPipe(t *testing.T) {
   164  	type testcase struct {
   165  		prepare                func(ctx *context.Context)
   166  		expectedRunError       string
   167  		expectedRunErrorAs     any
   168  		expectedPublishError   string
   169  		expectedPublishErrorAs any
   170  	}
   171  	for name, tt := range map[string]testcase{
   172  		"default": {
   173  			prepare: func(ctx *context.Context) {
   174  				ctx.TokenType = context.TokenTypeGitHub
   175  				ctx.Config.Brews[0].Repository.Owner = "test"
   176  				ctx.Config.Brews[0].Repository.Name = "test"
   177  				ctx.Config.Brews[0].Homepage = "https://github.com/goreleaser"
   178  			},
   179  		},
   180  		"with_header": {
   181  			prepare: func(ctx *context.Context) {
   182  				ctx.TokenType = context.TokenTypeGitHub
   183  				ctx.Config.Brews[0].Repository.Owner = "test"
   184  				ctx.Config.Brews[0].Repository.Name = "test"
   185  				ctx.Config.Brews[0].Homepage = "https://github.com/goreleaser"
   186  				ctx.Config.Brews[0].URLHeaders = []string{
   187  					`Authorization: bearer #{ENV["HOMEBREW_GITHUB_API_TOKEN"]}`,
   188  				}
   189  			},
   190  		},
   191  		"with_many_headers": {
   192  			prepare: func(ctx *context.Context) {
   193  				ctx.TokenType = context.TokenTypeGitHub
   194  				ctx.Config.Brews[0].Repository.Owner = "test"
   195  				ctx.Config.Brews[0].Repository.Name = "test"
   196  				ctx.Config.Brews[0].Homepage = "https://github.com/goreleaser"
   197  				ctx.Config.Brews[0].URLHeaders = []string{
   198  					"Accept: application/octet-stream",
   199  					`Authorization: bearer #{ENV["HOMEBREW_GITHUB_API_TOKEN"]}`,
   200  				}
   201  			},
   202  		},
   203  		"git_remote": {
   204  			prepare: func(ctx *context.Context) {
   205  				ctx.TokenType = context.TokenTypeGitHub
   206  				ctx.Config.Brews[0].Homepage = "https://github.com/goreleaser"
   207  				ctx.Config.Brews[0].Repository = config.RepoRef{
   208  					Name:   "test",
   209  					Branch: "main",
   210  					Git: config.GitRepoRef{
   211  						URL:        testlib.GitMakeBareRepository(t),
   212  						PrivateKey: testlib.MakeNewSSHKey(t, ""),
   213  					},
   214  				}
   215  			},
   216  		},
   217  		"open_pr": {
   218  			prepare: func(ctx *context.Context) {
   219  				ctx.TokenType = context.TokenTypeGitHub
   220  				ctx.Config.Brews[0].Homepage = "https://github.com/goreleaser"
   221  				ctx.Config.Brews[0].Repository = config.RepoRef{
   222  					Owner:  "test",
   223  					Name:   "test",
   224  					Branch: "update-{{.Version}}",
   225  					PullRequest: config.PullRequest{
   226  						Enabled: true,
   227  					},
   228  				}
   229  			},
   230  		},
   231  		"custom_download_strategy": {
   232  			prepare: func(ctx *context.Context) {
   233  				ctx.TokenType = context.TokenTypeGitHub
   234  				ctx.Config.Brews[0].Repository.Owner = "test"
   235  				ctx.Config.Brews[0].Repository.Name = "test"
   236  				ctx.Config.Brews[0].Homepage = "https://github.com/goreleaser"
   237  
   238  				ctx.Config.Brews[0].DownloadStrategy = "GitHubPrivateRepositoryReleaseDownloadStrategy"
   239  			},
   240  		},
   241  		"custom_require": {
   242  			prepare: func(ctx *context.Context) {
   243  				ctx.TokenType = context.TokenTypeGitHub
   244  				ctx.Config.Brews[0].Repository.Owner = "test"
   245  				ctx.Config.Brews[0].Repository.Name = "test"
   246  				ctx.Config.Brews[0].Homepage = "https://github.com/goreleaser"
   247  
   248  				ctx.Config.Brews[0].DownloadStrategy = "CustomDownloadStrategy"
   249  				ctx.Config.Brews[0].CustomRequire = "custom_download_strategy"
   250  			},
   251  		},
   252  		"custom_block": {
   253  			prepare: func(ctx *context.Context) {
   254  				ctx.TokenType = context.TokenTypeGitHub
   255  				ctx.Config.Brews[0].Repository.Owner = "test"
   256  				ctx.Config.Brews[0].Repository.Name = "test"
   257  				ctx.Config.Brews[0].Homepage = "https://github.com/goreleaser"
   258  
   259  				ctx.Config.Brews[0].CustomBlock = `head "https://github.com/caarlos0/test.git"`
   260  			},
   261  		},
   262  		"default_gitlab": {
   263  			prepare: func(ctx *context.Context) {
   264  				ctx.TokenType = context.TokenTypeGitLab
   265  				ctx.Config.Brews[0].Repository.Owner = "test"
   266  				ctx.Config.Brews[0].Repository.Name = "test"
   267  				ctx.Config.Brews[0].Homepage = "https://gitlab.com/goreleaser"
   268  			},
   269  		},
   270  		"invalid_commit_template": {
   271  			prepare: func(ctx *context.Context) {
   272  				ctx.Config.Brews[0].Repository.Owner = "test"
   273  				ctx.Config.Brews[0].Repository.Name = "test"
   274  				ctx.Config.Brews[0].CommitMessageTemplate = "{{ .Asdsa }"
   275  			},
   276  			expectedPublishErrorAs: &tmpl.Error{},
   277  		},
   278  		"valid_repository_templates": {
   279  			prepare: func(ctx *context.Context) {
   280  				ctx.TokenType = context.TokenTypeGitHub
   281  				ctx.Env = map[string]string{
   282  					"FOO": "templated",
   283  				}
   284  				ctx.Config.Brews[0].Repository.Owner = "{{.Env.FOO}}"
   285  				ctx.Config.Brews[0].Repository.Name = "{{.Env.FOO}}"
   286  			},
   287  		},
   288  		"invalid_repository_name_template": {
   289  			prepare: func(ctx *context.Context) {
   290  				ctx.Config.Brews[0].Repository.Owner = "test"
   291  				ctx.Config.Brews[0].Repository.Name = "{{ .Asdsa }"
   292  			},
   293  			expectedRunErrorAs: &tmpl.Error{},
   294  		},
   295  		"invalid_repository_owner_template": {
   296  			prepare: func(ctx *context.Context) {
   297  				ctx.Config.Brews[0].Repository.Owner = "{{ .Asdsa }"
   298  				ctx.Config.Brews[0].Repository.Name = "test"
   299  			},
   300  			expectedRunErrorAs: &tmpl.Error{},
   301  		},
   302  		"invalid_repository_skip_upload_template": {
   303  			prepare: func(ctx *context.Context) {
   304  				ctx.Config.Brews[0].SkipUpload = "{{ .Asdsa }"
   305  				ctx.Config.Brews[0].Repository.Owner = "test"
   306  				ctx.Config.Brews[0].Repository.Name = "test"
   307  			},
   308  			expectedRunErrorAs: &tmpl.Error{},
   309  		},
   310  		"invalid_install_template": {
   311  			prepare: func(ctx *context.Context) {
   312  				ctx.Config.Brews[0].Repository.Owner = "test"
   313  				ctx.Config.Brews[0].Repository.Name = "test"
   314  				ctx.Config.Brews[0].Install = "{{ .aaaa }"
   315  			},
   316  			expectedRunErrorAs: &tmpl.Error{},
   317  		},
   318  	} {
   319  		t.Run(name, func(t *testing.T) {
   320  			folder := t.TempDir()
   321  			ctx := testctx.NewWithCfg(
   322  				config.Project{
   323  					Dist:        folder,
   324  					ProjectName: name,
   325  					Brews: []config.Homebrew{
   326  						{
   327  							Name: name,
   328  							IDs: []string{
   329  								"foo",
   330  							},
   331  							Description: "Run pipe test formula and FOO={{ .Env.FOO }}",
   332  							Caveats:     "don't do this {{ .ProjectName }}",
   333  							Test:        "system \"true\"\nsystem \"#{bin}/foo\", \"-h\"",
   334  							Plist:       `<xml>whatever</xml>`,
   335  							Dependencies: []config.HomebrewDependency{
   336  								{Name: "zsh", Type: "optional"},
   337  								{Name: "bash", Version: "3.2.57"},
   338  								{Name: "fish", Type: "optional", Version: "v1.2.3"},
   339  								{Name: "powershell", Type: "optional", OS: "mac"},
   340  								{Name: "ash", Version: "1.0.0", OS: "linux"},
   341  							},
   342  							Conflicts:   []string{"gtk+", "qt"},
   343  							Service:     "run foo/bar\nkeep_alive true",
   344  							PostInstall: "system \"echo\"\ntouch \"/tmp/hi\"",
   345  							Install:     `bin.install "{{ .ProjectName }}_{{.Os}}_{{.Arch}} => {{.ProjectName}}"`,
   346  							Goamd64:     "v1",
   347  						},
   348  					},
   349  					Env: []string{"FOO=foo_is_bar"},
   350  				},
   351  				testctx.WithVersion("1.0.1"),
   352  				testctx.WithCurrentTag("v1.0.1"),
   353  			)
   354  			tt.prepare(ctx)
   355  			ctx.Artifacts.Add(&artifact.Artifact{
   356  				Name:    "bar_bin.tar.gz",
   357  				Path:    "doesnt matter",
   358  				Goos:    "darwin",
   359  				Goarch:  "amd64",
   360  				Goamd64: "v1",
   361  				Type:    artifact.UploadableArchive,
   362  				Extra: map[string]interface{}{
   363  					artifact.ExtraID:     "bar",
   364  					artifact.ExtraFormat: "tar.gz",
   365  				},
   366  			})
   367  			path := filepath.Join(folder, "bin.tar.gz")
   368  			ctx.Artifacts.Add(&artifact.Artifact{
   369  				Name:    "bin.tar.gz",
   370  				Path:    path,
   371  				Goos:    "darwin",
   372  				Goarch:  "amd64",
   373  				Goamd64: "v1",
   374  				Type:    artifact.UploadableArchive,
   375  				Extra: map[string]interface{}{
   376  					artifact.ExtraID:     "foo",
   377  					artifact.ExtraFormat: "tar.gz",
   378  				},
   379  			})
   380  			ctx.Artifacts.Add(&artifact.Artifact{
   381  				Name:   "bin.tar.gz",
   382  				Path:   path,
   383  				Goos:   "darwin",
   384  				Goarch: "arm64",
   385  				Type:   artifact.UploadableArchive,
   386  				Extra: map[string]interface{}{
   387  					artifact.ExtraID:     "foo",
   388  					artifact.ExtraFormat: "tar.gz",
   389  				},
   390  			})
   391  			ctx.Artifacts.Add(&artifact.Artifact{
   392  				Name:    "bin.tar.gz",
   393  				Path:    path,
   394  				Goos:    "linux",
   395  				Goarch:  "amd64",
   396  				Goamd64: "v1",
   397  				Type:    artifact.UploadableArchive,
   398  				Extra: map[string]interface{}{
   399  					artifact.ExtraID:     "foo",
   400  					artifact.ExtraFormat: "tar.gz",
   401  				},
   402  			})
   403  
   404  			f, err := os.Create(path)
   405  			require.NoError(t, err)
   406  			require.NoError(t, f.Close())
   407  			client := client.NewMock()
   408  			distFile := filepath.Join(folder, "homebrew", name+".rb")
   409  
   410  			require.NoError(t, Pipe{}.Default(ctx))
   411  
   412  			err = runAll(ctx, client)
   413  			if tt.expectedRunError != "" {
   414  				require.EqualError(t, err, tt.expectedRunError)
   415  				return
   416  			}
   417  			if tt.expectedRunErrorAs != nil {
   418  				require.ErrorAs(t, err, tt.expectedRunErrorAs)
   419  				return
   420  			}
   421  			require.NoError(t, err)
   422  
   423  			err = publishAll(ctx, client)
   424  			if tt.expectedPublishError != "" {
   425  				require.EqualError(t, err, tt.expectedPublishError)
   426  				return
   427  			}
   428  			if tt.expectedPublishErrorAs != nil {
   429  				require.ErrorAs(t, err, tt.expectedPublishErrorAs)
   430  				return
   431  			}
   432  			require.NoError(t, err)
   433  
   434  			content := []byte(client.Content)
   435  			if url := ctx.Config.Brews[0].Repository.Git.URL; url == "" {
   436  				require.True(t, client.CreatedFile, "should have created a file")
   437  			} else {
   438  				content = testlib.CatFileFromBareRepositoryOnBranch(
   439  					t, url,
   440  					ctx.Config.Brews[0].Repository.Branch,
   441  					name+".rb",
   442  				)
   443  			}
   444  
   445  			golden.RequireEqualRb(t, content)
   446  
   447  			distBts, err := os.ReadFile(distFile)
   448  			require.NoError(t, err)
   449  			require.Equal(t, string(content), string(distBts))
   450  		})
   451  	}
   452  }
   453  
   454  func TestRunPipeNameTemplate(t *testing.T) {
   455  	folder := t.TempDir()
   456  	ctx := testctx.NewWithCfg(
   457  		config.Project{
   458  			Dist:        folder,
   459  			ProjectName: "foo",
   460  			Brews: []config.Homebrew{
   461  				{
   462  					Name:        "foo_{{ .Env.FOO_BAR }}",
   463  					Description: "Foo bar",
   464  					Homepage:    "https://goreleaser.com",
   465  					Goamd64:     "v1",
   466  					Install:     `bin.install "foo"`,
   467  					Repository: config.RepoRef{
   468  						Owner: "foo",
   469  						Name:  "bar",
   470  					},
   471  					IDs: []string{
   472  						"foo",
   473  					},
   474  				},
   475  			},
   476  			Env: []string{"FOO_BAR=is_bar"},
   477  		},
   478  		testctx.WithVersion("1.0.1"),
   479  		testctx.WithCurrentTag("v1.0.1"),
   480  	)
   481  	path := filepath.Join(folder, "bin.tar.gz")
   482  	ctx.Artifacts.Add(&artifact.Artifact{
   483  		Name:    "bin.tar.gz",
   484  		Path:    path,
   485  		Goos:    "darwin",
   486  		Goarch:  "amd64",
   487  		Goamd64: "v1",
   488  		Type:    artifact.UploadableArchive,
   489  		Extra: map[string]interface{}{
   490  			artifact.ExtraID:     "foo",
   491  			artifact.ExtraFormat: "tar.gz",
   492  		},
   493  	})
   494  
   495  	f, err := os.Create(path)
   496  	require.NoError(t, err)
   497  	require.NoError(t, f.Close())
   498  	client := client.NewMock()
   499  	distFile := filepath.Join(folder, "homebrew", "foo_is_bar.rb")
   500  
   501  	require.NoError(t, runAll(ctx, client))
   502  	require.NoError(t, publishAll(ctx, client))
   503  	require.True(t, client.CreatedFile)
   504  	golden.RequireEqualRb(t, []byte(client.Content))
   505  	distBts, err := os.ReadFile(distFile)
   506  	require.NoError(t, err)
   507  	require.Equal(t, client.Content, string(distBts))
   508  }
   509  
   510  func TestRunPipeMultipleBrewsWithSkip(t *testing.T) {
   511  	folder := t.TempDir()
   512  	ctx := testctx.NewWithCfg(
   513  		config.Project{
   514  			Dist:        folder,
   515  			ProjectName: "foo",
   516  			Brews: []config.Homebrew{
   517  				{
   518  					Name:    "foo",
   519  					Goamd64: "v1",
   520  					Repository: config.RepoRef{
   521  						Owner: "foo",
   522  						Name:  "bar",
   523  					},
   524  					IDs: []string{
   525  						"foo",
   526  					},
   527  					SkipUpload: "true",
   528  				},
   529  				{
   530  					Name:    "bar",
   531  					Goamd64: "v1",
   532  					Repository: config.RepoRef{
   533  						Owner: "foo",
   534  						Name:  "bar",
   535  					},
   536  					IDs: []string{
   537  						"foo",
   538  					},
   539  				},
   540  				{
   541  					Name:    "foobar",
   542  					Goamd64: "v1",
   543  					Repository: config.RepoRef{
   544  						Owner: "foo",
   545  						Name:  "bar",
   546  					},
   547  					IDs: []string{
   548  						"foo",
   549  					},
   550  					SkipUpload: "true",
   551  				},
   552  				{
   553  					Name:    "baz",
   554  					Goamd64: "v1",
   555  					Repository: config.RepoRef{
   556  						Owner: "foo",
   557  						Name:  "bar",
   558  					},
   559  					IDs: []string{
   560  						"foo",
   561  					},
   562  					SkipUpload: "{{ .Env.SKIP_UPLOAD }}",
   563  				},
   564  			},
   565  			Env: []string{
   566  				"FOO_BAR=is_bar",
   567  				"SKIP_UPLOAD=true",
   568  			},
   569  		},
   570  		testctx.WithVersion("1.0.1"),
   571  		testctx.WithCurrentTag("v1.0.1"),
   572  	)
   573  	path := filepath.Join(folder, "bin.tar.gz")
   574  	ctx.Artifacts.Add(&artifact.Artifact{
   575  		Name:    "bin.tar.gz",
   576  		Path:    path,
   577  		Goos:    "darwin",
   578  		Goarch:  "amd64",
   579  		Goamd64: "v1",
   580  		Type:    artifact.UploadableArchive,
   581  		Extra: map[string]interface{}{
   582  			artifact.ExtraID:     "foo",
   583  			artifact.ExtraFormat: "tar.gz",
   584  		},
   585  	})
   586  
   587  	f, err := os.Create(path)
   588  	require.NoError(t, err)
   589  	require.NoError(t, f.Close())
   590  
   591  	cli := client.NewMock()
   592  	require.NoError(t, runAll(ctx, cli))
   593  	require.EqualError(t, publishAll(ctx, cli), `brew.skip_upload is set`)
   594  	require.True(t, cli.CreatedFile)
   595  
   596  	for _, brew := range ctx.Config.Brews {
   597  		distFile := filepath.Join(folder, "homebrew", brew.Name+".rb")
   598  		_, err := os.Stat(distFile)
   599  		require.NoError(t, err, "file should exist: "+distFile)
   600  	}
   601  }
   602  
   603  func TestRunPipeForMultipleAmd64Versions(t *testing.T) {
   604  	for name, fn := range map[string]func(ctx *context.Context){
   605  		"v1": func(ctx *context.Context) {
   606  			ctx.Config.Brews[0].Goamd64 = "v1"
   607  		},
   608  		"v2": func(ctx *context.Context) {
   609  			ctx.Config.Brews[0].Goamd64 = "v2"
   610  		},
   611  		"v3": func(ctx *context.Context) {
   612  			ctx.Config.Brews[0].Goamd64 = "v3"
   613  		},
   614  		"v4": func(ctx *context.Context) {
   615  			ctx.Config.Brews[0].Goamd64 = "v4"
   616  		},
   617  	} {
   618  		t.Run(name, func(t *testing.T) {
   619  			folder := t.TempDir()
   620  			ctx := testctx.NewWithCfg(
   621  				config.Project{
   622  					Dist:        folder,
   623  					ProjectName: name,
   624  					Brews: []config.Homebrew{
   625  						{
   626  							Name:        name,
   627  							Description: "Run pipe test formula",
   628  							Repository: config.RepoRef{
   629  								Owner: "test",
   630  								Name:  "test",
   631  							},
   632  							Homepage:     "https://github.com/goreleaser",
   633  							Install:      `bin.install "foo"`,
   634  							ExtraInstall: `man1.install "./man/foo.1.gz"`,
   635  						},
   636  					},
   637  					GitHubURLs: config.GitHubURLs{
   638  						Download: "https://github.com",
   639  					},
   640  					Release: config.Release{
   641  						GitHub: config.Repo{
   642  							Owner: "test",
   643  							Name:  "test",
   644  						},
   645  					},
   646  					Env: []string{"FOO=foo_is_bar"},
   647  				},
   648  				testctx.GitHubTokenType,
   649  				testctx.WithVersion("1.0.1"),
   650  				testctx.WithCurrentTag("v1.0.1"),
   651  			)
   652  			fn(ctx)
   653  			for _, a := range []struct {
   654  				name    string
   655  				goos    string
   656  				goarch  string
   657  				goamd64 string
   658  			}{
   659  				{
   660  					name:   "bin",
   661  					goos:   "darwin",
   662  					goarch: "arm64",
   663  				},
   664  				{
   665  					name:   "arm64",
   666  					goos:   "linux",
   667  					goarch: "arm64",
   668  				},
   669  				{
   670  					name:    "amd64v2",
   671  					goos:    "linux",
   672  					goarch:  "amd64",
   673  					goamd64: "v1",
   674  				},
   675  				{
   676  					name:    "amd64v2",
   677  					goos:    "linux",
   678  					goarch:  "amd64",
   679  					goamd64: "v2",
   680  				},
   681  				{
   682  					name:    "amd64v3",
   683  					goos:    "linux",
   684  					goarch:  "amd64",
   685  					goamd64: "v3",
   686  				},
   687  				{
   688  					name:    "amd64v3",
   689  					goos:    "linux",
   690  					goarch:  "amd64",
   691  					goamd64: "v4",
   692  				},
   693  			} {
   694  				path := filepath.Join(folder, fmt.Sprintf("%s.tar.gz", a.name))
   695  				ctx.Artifacts.Add(&artifact.Artifact{
   696  					Name:    fmt.Sprintf("%s.tar.gz", a.name),
   697  					Path:    path,
   698  					Goos:    a.goos,
   699  					Goarch:  a.goarch,
   700  					Goamd64: a.goamd64,
   701  					Type:    artifact.UploadableArchive,
   702  					Extra: map[string]interface{}{
   703  						artifact.ExtraID:     a.name,
   704  						artifact.ExtraFormat: "tar.gz",
   705  					},
   706  				})
   707  				f, err := os.Create(path)
   708  				require.NoError(t, err)
   709  				require.NoError(t, f.Close())
   710  			}
   711  
   712  			client := client.NewMock()
   713  			distFile := filepath.Join(folder, "homebrew", name+".rb")
   714  
   715  			require.NoError(t, runAll(ctx, client))
   716  			require.NoError(t, publishAll(ctx, client))
   717  			require.True(t, client.CreatedFile)
   718  			golden.RequireEqualRb(t, []byte(client.Content))
   719  
   720  			distBts, err := os.ReadFile(distFile)
   721  			require.NoError(t, err)
   722  			require.Equal(t, client.Content, string(distBts))
   723  		})
   724  	}
   725  }
   726  
   727  func TestRunPipeForMultipleArmVersions(t *testing.T) {
   728  	for name, fn := range map[string]func(ctx *context.Context){
   729  		"multiple_armv5": func(ctx *context.Context) {
   730  			ctx.Config.Brews[0].Goarm = "5"
   731  		},
   732  		"multiple_armv6": func(ctx *context.Context) {
   733  			ctx.Config.Brews[0].Goarm = "6"
   734  		},
   735  		"multiple_armv7": func(ctx *context.Context) {
   736  			ctx.Config.Brews[0].Goarm = "7"
   737  		},
   738  	} {
   739  		t.Run(name, func(t *testing.T) {
   740  			folder := t.TempDir()
   741  			ctx := testctx.NewWithCfg(
   742  				config.Project{
   743  					Dist:        folder,
   744  					ProjectName: name,
   745  					Brews: []config.Homebrew{
   746  						{
   747  							Name:         name,
   748  							Description:  "Run pipe test formula and FOO={{ .Env.FOO }}",
   749  							Caveats:      "don't do this {{ .ProjectName }}",
   750  							Test:         "system \"true\"\nsystem \"#{bin}/foo\", \"-h\"",
   751  							Plist:        `<xml>whatever</xml>`,
   752  							Dependencies: []config.HomebrewDependency{{Name: "zsh"}, {Name: "bash", Type: "recommended"}},
   753  							Conflicts:    []string{"gtk+", "qt"},
   754  							Install:      `bin.install "{{ .ProjectName }}"`,
   755  							Repository: config.RepoRef{
   756  								Owner: "test",
   757  								Name:  "test",
   758  							},
   759  							Homepage: "https://github.com/goreleaser",
   760  						},
   761  					},
   762  					GitHubURLs: config.GitHubURLs{
   763  						Download: "https://github.com",
   764  					},
   765  					Release: config.Release{
   766  						GitHub: config.Repo{
   767  							Owner: "test",
   768  							Name:  "test",
   769  						},
   770  					},
   771  					Env: []string{"FOO=foo_is_bar"},
   772  				},
   773  				testctx.GitHubTokenType,
   774  				testctx.WithVersion("1.0.1"),
   775  				testctx.WithCurrentTag("v1.0.1"),
   776  			)
   777  			fn(ctx)
   778  			for _, a := range []struct {
   779  				name   string
   780  				goos   string
   781  				goarch string
   782  				goarm  string
   783  			}{
   784  				{
   785  					name:   "bin",
   786  					goos:   "darwin",
   787  					goarch: "amd64",
   788  				},
   789  				{
   790  					name:   "arm64",
   791  					goos:   "linux",
   792  					goarch: "arm64",
   793  				},
   794  				{
   795  					name:   "armv5",
   796  					goos:   "linux",
   797  					goarch: "arm",
   798  					goarm:  "5",
   799  				},
   800  				{
   801  					name:   "armv6",
   802  					goos:   "linux",
   803  					goarch: "arm",
   804  					goarm:  "6",
   805  				},
   806  				{
   807  					name:   "armv7",
   808  					goos:   "linux",
   809  					goarch: "arm",
   810  					goarm:  "7",
   811  				},
   812  			} {
   813  				path := filepath.Join(folder, fmt.Sprintf("%s.tar.gz", a.name))
   814  				ctx.Artifacts.Add(&artifact.Artifact{
   815  					Name:   fmt.Sprintf("%s.tar.gz", a.name),
   816  					Path:   path,
   817  					Goos:   a.goos,
   818  					Goarch: a.goarch,
   819  					Goarm:  a.goarm,
   820  					Type:   artifact.UploadableArchive,
   821  					Extra: map[string]interface{}{
   822  						artifact.ExtraID:     a.name,
   823  						artifact.ExtraFormat: "tar.gz",
   824  					},
   825  				})
   826  				f, err := os.Create(path)
   827  				require.NoError(t, err)
   828  				require.NoError(t, f.Close())
   829  			}
   830  
   831  			client := client.NewMock()
   832  			distFile := filepath.Join(folder, "homebrew", name+".rb")
   833  
   834  			require.NoError(t, runAll(ctx, client))
   835  			require.NoError(t, publishAll(ctx, client))
   836  			require.True(t, client.CreatedFile)
   837  			golden.RequireEqualRb(t, []byte(client.Content))
   838  
   839  			distBts, err := os.ReadFile(distFile)
   840  			require.NoError(t, err)
   841  			require.Equal(t, client.Content, string(distBts))
   842  		})
   843  	}
   844  }
   845  
   846  func TestRunPipeNoBuilds(t *testing.T) {
   847  	ctx := testctx.NewWithCfg(config.Project{
   848  		Brews: []config.Homebrew{
   849  			{
   850  				Repository: config.RepoRef{
   851  					Owner: "test",
   852  					Name:  "test",
   853  				},
   854  				IDs: []string{"foo"},
   855  			},
   856  		},
   857  	}, testctx.GitHubTokenType)
   858  	client := client.NewMock()
   859  	require.NoError(t, Pipe{}.Default(ctx))
   860  	require.EqualError(t, runAll(ctx, client), ErrNoArchivesFound{
   861  		ids:     []string{"foo"},
   862  		goarm:   "6",
   863  		goamd64: "v1",
   864  	}.Error())
   865  	require.False(t, client.CreatedFile)
   866  }
   867  
   868  func TestRunPipeMultipleArchivesSameOsBuild(t *testing.T) {
   869  	ctx := testctx.NewWithCfg(config.Project{
   870  		Brews: []config.Homebrew{
   871  			{
   872  				Repository: config.RepoRef{
   873  					Owner: "test",
   874  					Name:  "test",
   875  				},
   876  			},
   877  		},
   878  	}, testctx.GitHubTokenType)
   879  
   880  	f, err := os.CreateTemp(t.TempDir(), "")
   881  	require.NoError(t, err)
   882  	t.Cleanup(func() {
   883  		require.NoError(t, f.Close())
   884  	})
   885  
   886  	tests := []struct {
   887  		expectedError error
   888  		osarchs       []struct {
   889  			goos   string
   890  			goarch string
   891  			goarm  string
   892  		}
   893  	}{
   894  		{
   895  			expectedError: ErrMultipleArchivesSameOS,
   896  			osarchs: []struct {
   897  				goos   string
   898  				goarch string
   899  				goarm  string
   900  			}{
   901  				{
   902  					goos:   "darwin",
   903  					goarch: "amd64",
   904  				},
   905  				{
   906  					goos:   "darwin",
   907  					goarch: "amd64",
   908  				},
   909  			},
   910  		},
   911  		{
   912  			expectedError: ErrMultipleArchivesSameOS,
   913  			osarchs: []struct {
   914  				goos   string
   915  				goarch string
   916  				goarm  string
   917  			}{
   918  				{
   919  					goos:   "linux",
   920  					goarch: "amd64",
   921  				},
   922  				{
   923  					goos:   "linux",
   924  					goarch: "amd64",
   925  				},
   926  			},
   927  		},
   928  		{
   929  			expectedError: ErrMultipleArchivesSameOS,
   930  			osarchs: []struct {
   931  				goos   string
   932  				goarch string
   933  				goarm  string
   934  			}{
   935  				{
   936  					goos:   "linux",
   937  					goarch: "arm64",
   938  				},
   939  				{
   940  					goos:   "linux",
   941  					goarch: "arm64",
   942  				},
   943  			},
   944  		},
   945  		{
   946  			expectedError: ErrMultipleArchivesSameOS,
   947  			osarchs: []struct {
   948  				goos   string
   949  				goarch string
   950  				goarm  string
   951  			}{
   952  				{
   953  					goos:   "linux",
   954  					goarch: "arm",
   955  					goarm:  "6",
   956  				},
   957  				{
   958  					goos:   "linux",
   959  					goarch: "arm",
   960  					goarm:  "6",
   961  				},
   962  			},
   963  		},
   964  		{
   965  			expectedError: ErrMultipleArchivesSameOS,
   966  			osarchs: []struct {
   967  				goos   string
   968  				goarch string
   969  				goarm  string
   970  			}{
   971  				{
   972  					goos:   "linux",
   973  					goarch: "arm",
   974  					goarm:  "5",
   975  				},
   976  				{
   977  					goos:   "linux",
   978  					goarch: "arm",
   979  					goarm:  "6",
   980  				},
   981  				{
   982  					goos:   "linux",
   983  					goarch: "arm",
   984  					goarm:  "7",
   985  				},
   986  			},
   987  		},
   988  	}
   989  
   990  	for _, test := range tests {
   991  		for idx, ttt := range test.osarchs {
   992  			ctx.Artifacts.Add(&artifact.Artifact{
   993  				Name:   fmt.Sprintf("bin%d", idx),
   994  				Path:   f.Name(),
   995  				Goos:   ttt.goos,
   996  				Goarch: ttt.goarch,
   997  				Type:   artifact.UploadableArchive,
   998  				Extra: map[string]interface{}{
   999  					artifact.ExtraID:     fmt.Sprintf("foo%d", idx),
  1000  					artifact.ExtraFormat: "tar.gz",
  1001  				},
  1002  			})
  1003  		}
  1004  		client := client.NewMock()
  1005  		require.Equal(t, test.expectedError, runAll(ctx, client))
  1006  		require.False(t, client.CreatedFile)
  1007  		// clean the artifacts for the next run
  1008  		ctx.Artifacts = artifact.New()
  1009  	}
  1010  }
  1011  
  1012  func TestRunPipeBinaryRelease(t *testing.T) {
  1013  	folder := t.TempDir()
  1014  	ctx := testctx.NewWithCfg(
  1015  		config.Project{
  1016  			Dist:        folder,
  1017  			ProjectName: "foo",
  1018  			Brews: []config.Homebrew{
  1019  				{
  1020  					Name:        "foo",
  1021  					Homepage:    "https://goreleaser.com",
  1022  					Description: "Fake desc",
  1023  					Repository: config.RepoRef{
  1024  						Owner: "foo",
  1025  						Name:  "bar",
  1026  					},
  1027  					ExtraInstall: `man1.install "./man/foo.1.gz"`,
  1028  				},
  1029  			},
  1030  		},
  1031  		testctx.WithVersion("1.2.1"),
  1032  		testctx.WithCurrentTag("v1.2.1"),
  1033  	)
  1034  	path := filepath.Join(folder, "dist/foo_darwin_all/foo")
  1035  	ctx.Artifacts.Add(&artifact.Artifact{
  1036  		Name:   "foo_macos",
  1037  		Path:   path,
  1038  		Goos:   "darwin",
  1039  		Goarch: "all",
  1040  		Type:   artifact.UploadableBinary,
  1041  		Extra: map[string]interface{}{
  1042  			artifact.ExtraID:     "foo",
  1043  			artifact.ExtraFormat: "binary",
  1044  			artifact.ExtraBinary: "foo",
  1045  		},
  1046  	})
  1047  
  1048  	require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755))
  1049  	f, err := os.Create(path)
  1050  	require.NoError(t, err)
  1051  	require.NoError(t, f.Close())
  1052  
  1053  	client := client.NewMock()
  1054  	require.NoError(t, runAll(ctx, client))
  1055  	require.NoError(t, publishAll(ctx, client))
  1056  	require.True(t, client.CreatedFile)
  1057  	golden.RequireEqualRb(t, []byte(client.Content))
  1058  }
  1059  
  1060  func TestRunPipePullRequest(t *testing.T) {
  1061  	folder := t.TempDir()
  1062  	ctx := testctx.NewWithCfg(
  1063  		config.Project{
  1064  			Dist:        folder,
  1065  			ProjectName: "foo",
  1066  			Brews: []config.Homebrew{
  1067  				{
  1068  					Name:         "foo",
  1069  					Homepage:     "https://goreleaser.com",
  1070  					Description:  "Fake desc",
  1071  					ExtraInstall: `man1.install "./man/foo.1.gz"`,
  1072  					Repository: config.RepoRef{
  1073  						Owner:  "foo",
  1074  						Name:   "bar",
  1075  						Branch: "update-{{.Version}}",
  1076  						PullRequest: config.PullRequest{
  1077  							Enabled: true,
  1078  						},
  1079  					},
  1080  				},
  1081  			},
  1082  		},
  1083  		testctx.WithVersion("1.2.1"),
  1084  		testctx.WithCurrentTag("v1.2.1"),
  1085  	)
  1086  	path := filepath.Join(folder, "dist/foo_darwin_all/foo")
  1087  	ctx.Artifacts.Add(&artifact.Artifact{
  1088  		Name:   "foo_macos",
  1089  		Path:   path,
  1090  		Goos:   "darwin",
  1091  		Goarch: "all",
  1092  		Type:   artifact.UploadableBinary,
  1093  		Extra: map[string]interface{}{
  1094  			artifact.ExtraID:     "foo",
  1095  			artifact.ExtraFormat: "binary",
  1096  			artifact.ExtraBinary: "foo",
  1097  		},
  1098  	})
  1099  
  1100  	require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755))
  1101  	f, err := os.Create(path)
  1102  	require.NoError(t, err)
  1103  	require.NoError(t, f.Close())
  1104  
  1105  	client := client.NewMock()
  1106  	require.NoError(t, runAll(ctx, client))
  1107  	require.NoError(t, publishAll(ctx, client))
  1108  	require.True(t, client.CreatedFile)
  1109  	require.True(t, client.OpenedPullRequest)
  1110  	require.True(t, client.SyncedFork)
  1111  	golden.RequireEqualRb(t, []byte(client.Content))
  1112  }
  1113  
  1114  func TestRunPipeNoUpload(t *testing.T) {
  1115  	folder := t.TempDir()
  1116  	ctx := testctx.NewWithCfg(config.Project{
  1117  		Dist:        folder,
  1118  		ProjectName: "foo",
  1119  		Release:     config.Release{},
  1120  		Brews: []config.Homebrew{
  1121  			{
  1122  				Repository: config.RepoRef{
  1123  					Owner: "test",
  1124  					Name:  "test",
  1125  				},
  1126  				Goamd64: "v1",
  1127  			},
  1128  		},
  1129  		Env: []string{"SKIP_UPLOAD=true"},
  1130  	}, testctx.WithCurrentTag("v1.0.1"), testctx.GitHubTokenType)
  1131  	path := filepath.Join(folder, "whatever.tar.gz")
  1132  	f, err := os.Create(path)
  1133  	require.NoError(t, err)
  1134  	require.NoError(t, f.Close())
  1135  	ctx.Artifacts.Add(&artifact.Artifact{
  1136  		Name:    "bin",
  1137  		Path:    path,
  1138  		Goos:    "darwin",
  1139  		Goarch:  "amd64",
  1140  		Goamd64: "v1",
  1141  		Type:    artifact.UploadableArchive,
  1142  		Extra: map[string]interface{}{
  1143  			artifact.ExtraID:     "foo",
  1144  			artifact.ExtraFormat: "tar.gz",
  1145  		},
  1146  	})
  1147  	client := client.NewMock()
  1148  
  1149  	assertNoPublish := func(t *testing.T) {
  1150  		t.Helper()
  1151  		require.NoError(t, runAll(ctx, client))
  1152  		testlib.AssertSkipped(t, publishAll(ctx, client))
  1153  		require.False(t, client.CreatedFile)
  1154  	}
  1155  	t.Run("skip upload true", func(t *testing.T) {
  1156  		ctx.Config.Brews[0].SkipUpload = "true"
  1157  		ctx.Semver.Prerelease = ""
  1158  		assertNoPublish(t)
  1159  	})
  1160  	t.Run("skip upload true set by template", func(t *testing.T) {
  1161  		ctx.Config.Brews[0].SkipUpload = "{{.Env.SKIP_UPLOAD}}"
  1162  		ctx.Semver.Prerelease = ""
  1163  		assertNoPublish(t)
  1164  	})
  1165  	t.Run("skip upload auto", func(t *testing.T) {
  1166  		ctx.Config.Brews[0].SkipUpload = "auto"
  1167  		ctx.Semver.Prerelease = "beta1"
  1168  		assertNoPublish(t)
  1169  	})
  1170  }
  1171  
  1172  func TestRunEmptyTokenType(t *testing.T) {
  1173  	folder := t.TempDir()
  1174  	ctx := testctx.NewWithCfg(config.Project{
  1175  		Dist:        folder,
  1176  		ProjectName: "foo",
  1177  		Release:     config.Release{},
  1178  		Brews: []config.Homebrew{
  1179  			{
  1180  				Repository: config.RepoRef{
  1181  					Owner: "test",
  1182  					Name:  "test",
  1183  				},
  1184  				Goamd64: "v1",
  1185  			},
  1186  		},
  1187  	}, testctx.WithCurrentTag("v1.0.0"))
  1188  	path := filepath.Join(folder, "whatever.tar.gz")
  1189  	f, err := os.Create(path)
  1190  	require.NoError(t, err)
  1191  	require.NoError(t, f.Close())
  1192  	ctx.Artifacts.Add(&artifact.Artifact{
  1193  		Name:    "bin",
  1194  		Path:    path,
  1195  		Goos:    "darwin",
  1196  		Goarch:  "amd64",
  1197  		Goamd64: "v1",
  1198  		Type:    artifact.UploadableArchive,
  1199  		Extra: map[string]interface{}{
  1200  			artifact.ExtraID:     "foo",
  1201  			artifact.ExtraFormat: "tar.gz",
  1202  		},
  1203  	})
  1204  	client := client.NewMock()
  1205  	require.NoError(t, runAll(ctx, client))
  1206  }
  1207  
  1208  func TestDefault(t *testing.T) {
  1209  	testlib.Mktmp(t)
  1210  	repo := config.RepoRef{
  1211  		Owner:  "owner",
  1212  		Name:   "name",
  1213  		Token:  "aaa",
  1214  		Branch: "feat",
  1215  		Git: config.GitRepoRef{
  1216  			URL:        "git@github.com:foo/bar",
  1217  			SSHCommand: "ssh ",
  1218  			PrivateKey: "/fake",
  1219  		},
  1220  		PullRequest: config.PullRequest{
  1221  			Enabled: true,
  1222  			Base: config.PullRequestBase{
  1223  				Owner:  "foo2",
  1224  				Name:   "bar2",
  1225  				Branch: "branch2",
  1226  			},
  1227  			Draft: true,
  1228  		},
  1229  	}
  1230  	ctx := testctx.NewWithCfg(config.Project{
  1231  		ProjectName: "myproject",
  1232  		Brews: []config.Homebrew{
  1233  			{
  1234  				Plist: "<xml>... whatever</xml>",
  1235  				Tap:   repo,
  1236  			},
  1237  		},
  1238  	}, testctx.GitHubTokenType)
  1239  	require.NoError(t, Pipe{}.Default(ctx))
  1240  	require.Equal(t, ctx.Config.ProjectName, ctx.Config.Brews[0].Name)
  1241  	require.NotEmpty(t, ctx.Config.Brews[0].CommitAuthor.Name)
  1242  	require.NotEmpty(t, ctx.Config.Brews[0].CommitAuthor.Email)
  1243  	require.NotEmpty(t, ctx.Config.Brews[0].CommitMessageTemplate)
  1244  	require.Equal(t, repo, ctx.Config.Brews[0].Repository)
  1245  	require.True(t, ctx.Deprecated)
  1246  }
  1247  
  1248  func TestGHFolder(t *testing.T) {
  1249  	require.Equal(t, "bar.rb", buildFormulaPath("", "bar.rb"))
  1250  	require.Equal(t, "fooo/bar.rb", buildFormulaPath("fooo", "bar.rb"))
  1251  }
  1252  
  1253  func TestSkip(t *testing.T) {
  1254  	t.Run("skip", func(t *testing.T) {
  1255  		require.True(t, Pipe{}.Skip(testctx.New()))
  1256  	})
  1257  	t.Run("skip flag", func(t *testing.T) {
  1258  		ctx := testctx.NewWithCfg(config.Project{
  1259  			Brews: []config.Homebrew{
  1260  				{},
  1261  			},
  1262  		}, testctx.Skip(skips.Homebrew))
  1263  		require.True(t, Pipe{}.Skip(ctx))
  1264  	})
  1265  	t.Run("dont skip", func(t *testing.T) {
  1266  		ctx := testctx.NewWithCfg(config.Project{
  1267  			Brews: []config.Homebrew{
  1268  				{},
  1269  			},
  1270  		})
  1271  		require.False(t, Pipe{}.Skip(ctx))
  1272  	})
  1273  }
  1274  
  1275  func TestRunSkipNoName(t *testing.T) {
  1276  	ctx := testctx.NewWithCfg(config.Project{
  1277  		Brews: []config.Homebrew{{}},
  1278  	})
  1279  
  1280  	client := client.NewMock()
  1281  	testlib.AssertSkipped(t, runAll(ctx, client))
  1282  }
  1283  
  1284  func TestInstalls(t *testing.T) {
  1285  	t.Run("provided", func(t *testing.T) {
  1286  		install, err := installs(
  1287  			testctx.New(),
  1288  			config.Homebrew{Install: "bin.install \"foo\"\nbin.install \"bar\""},
  1289  			&artifact.Artifact{},
  1290  		)
  1291  		require.NoError(t, err)
  1292  		require.Equal(t, []string{
  1293  			`bin.install "foo"`,
  1294  			`bin.install "bar"`,
  1295  		}, install)
  1296  	})
  1297  
  1298  	t.Run("from archives", func(t *testing.T) {
  1299  		install, err := installs(
  1300  			testctx.New(),
  1301  			config.Homebrew{},
  1302  			&artifact.Artifact{
  1303  				Type: artifact.UploadableArchive,
  1304  				Extra: map[string]interface{}{
  1305  					artifact.ExtraBinaries: []string{"foo", "bar"},
  1306  				},
  1307  			},
  1308  		)
  1309  		require.NoError(t, err)
  1310  		require.Equal(t, []string{
  1311  			`bin.install "bar"`,
  1312  			`bin.install "foo"`,
  1313  		}, install)
  1314  	})
  1315  
  1316  	t.Run("from binary", func(t *testing.T) {
  1317  		install, err := installs(
  1318  			testctx.New(),
  1319  			config.Homebrew{},
  1320  			&artifact.Artifact{
  1321  				Name: "foo_macos",
  1322  				Type: artifact.UploadableBinary,
  1323  				Extra: map[string]interface{}{
  1324  					artifact.ExtraBinary: "foo",
  1325  				},
  1326  			},
  1327  		)
  1328  		require.NoError(t, err)
  1329  		require.Equal(t, []string{
  1330  			`bin.install "foo_macos" => "foo"`,
  1331  		}, install)
  1332  	})
  1333  
  1334  	t.Run("from template", func(t *testing.T) {
  1335  		install, err := installs(
  1336  			testctx.New(),
  1337  			config.Homebrew{
  1338  				Install: `bin.install "foo_{{.Os}}" => "foo"`,
  1339  			},
  1340  			&artifact.Artifact{
  1341  				Name: "foo_darwin",
  1342  				Goos: "darwin",
  1343  				Type: artifact.UploadableBinary,
  1344  			},
  1345  		)
  1346  		require.NoError(t, err)
  1347  		require.Equal(t, []string{
  1348  			`bin.install "foo_darwin" => "foo"`,
  1349  		}, install)
  1350  	})
  1351  }
  1352  
  1353  func TestRunPipeUniversalBinary(t *testing.T) {
  1354  	folder := t.TempDir()
  1355  	ctx := testctx.NewWithCfg(
  1356  		config.Project{
  1357  			Dist:        folder,
  1358  			ProjectName: "unibin",
  1359  			Brews: []config.Homebrew{
  1360  				{
  1361  					Name:        "unibin",
  1362  					Homepage:    "https://goreleaser.com",
  1363  					Description: "Fake desc",
  1364  					Repository: config.RepoRef{
  1365  						Owner: "unibin",
  1366  						Name:  "bar",
  1367  					},
  1368  					IDs: []string{
  1369  						"unibin",
  1370  					},
  1371  					Install: `bin.install "unibin"`,
  1372  				},
  1373  			},
  1374  		},
  1375  		testctx.WithCurrentTag("v1.0.1"),
  1376  		testctx.WithVersion("1.0.1"),
  1377  	)
  1378  	path := filepath.Join(folder, "bin.tar.gz")
  1379  	ctx.Artifacts.Add(&artifact.Artifact{
  1380  		Name:   "bin.tar.gz",
  1381  		Path:   path,
  1382  		Goos:   "darwin",
  1383  		Goarch: "all",
  1384  		Type:   artifact.UploadableArchive,
  1385  		Extra: map[string]interface{}{
  1386  			artifact.ExtraID:       "unibin",
  1387  			artifact.ExtraFormat:   "tar.gz",
  1388  			artifact.ExtraBinaries: []string{"unibin"},
  1389  			artifact.ExtraReplaces: true,
  1390  		},
  1391  	})
  1392  
  1393  	f, err := os.Create(path)
  1394  	require.NoError(t, err)
  1395  	require.NoError(t, f.Close())
  1396  	client := client.NewMock()
  1397  	distFile := filepath.Join(folder, "homebrew", "unibin.rb")
  1398  
  1399  	require.NoError(t, runAll(ctx, client))
  1400  	require.NoError(t, publishAll(ctx, client))
  1401  	require.True(t, client.CreatedFile)
  1402  	golden.RequireEqualRb(t, []byte(client.Content))
  1403  	distBts, err := os.ReadFile(distFile)
  1404  	require.NoError(t, err)
  1405  	require.Equal(t, client.Content, string(distBts))
  1406  }
  1407  
  1408  func TestRunPipeUniversalBinaryNotReplacing(t *testing.T) {
  1409  	folder := t.TempDir()
  1410  	ctx := testctx.NewWithCfg(
  1411  		config.Project{
  1412  			Dist:        folder,
  1413  			ProjectName: "unibin",
  1414  			Brews: []config.Homebrew{
  1415  				{
  1416  					Name:        "unibin",
  1417  					Homepage:    "https://goreleaser.com",
  1418  					Description: "Fake desc",
  1419  					Repository: config.RepoRef{
  1420  						Owner: "unibin",
  1421  						Name:  "bar",
  1422  					},
  1423  					IDs: []string{
  1424  						"unibin",
  1425  					},
  1426  					Install: `bin.install "unibin"`,
  1427  					Goamd64: "v1",
  1428  				},
  1429  			},
  1430  		},
  1431  
  1432  		testctx.WithCurrentTag("v1.0.1"),
  1433  		testctx.WithVersion("1.0.1"),
  1434  	)
  1435  	path := filepath.Join(folder, "bin.tar.gz")
  1436  	ctx.Artifacts.Add(&artifact.Artifact{
  1437  		Name:    "bin_amd64.tar.gz",
  1438  		Path:    path,
  1439  		Goos:    "darwin",
  1440  		Goarch:  "amd64",
  1441  		Goamd64: "v1",
  1442  		Type:    artifact.UploadableArchive,
  1443  		Extra: map[string]interface{}{
  1444  			artifact.ExtraID:       "unibin",
  1445  			artifact.ExtraFormat:   "tar.gz",
  1446  			artifact.ExtraBinaries: []string{"unibin"},
  1447  		},
  1448  	})
  1449  	ctx.Artifacts.Add(&artifact.Artifact{
  1450  		Name:    "bin_arm64.tar.gz",
  1451  		Path:    path,
  1452  		Goos:    "darwin",
  1453  		Goarch:  "arm64",
  1454  		Goamd64: "v1",
  1455  		Type:    artifact.UploadableArchive,
  1456  		Extra: map[string]interface{}{
  1457  			artifact.ExtraID:       "unibin",
  1458  			artifact.ExtraFormat:   "tar.gz",
  1459  			artifact.ExtraBinaries: []string{"unibin"},
  1460  		},
  1461  	})
  1462  	ctx.Artifacts.Add(&artifact.Artifact{
  1463  		Name:   "bin.tar.gz",
  1464  		Path:   path,
  1465  		Goos:   "darwin",
  1466  		Goarch: "all",
  1467  		Type:   artifact.UploadableArchive,
  1468  		Extra: map[string]interface{}{
  1469  			artifact.ExtraID:       "unibin",
  1470  			artifact.ExtraFormat:   "tar.gz",
  1471  			artifact.ExtraBinaries: []string{"unibin"},
  1472  			artifact.ExtraReplaces: false,
  1473  		},
  1474  	})
  1475  
  1476  	f, err := os.Create(path)
  1477  	require.NoError(t, err)
  1478  	require.NoError(t, f.Close())
  1479  	client := client.NewMock()
  1480  	distFile := filepath.Join(folder, "homebrew", "unibin.rb")
  1481  
  1482  	require.NoError(t, runAll(ctx, client))
  1483  	require.NoError(t, publishAll(ctx, client))
  1484  	require.True(t, client.CreatedFile)
  1485  	golden.RequireEqualRb(t, []byte(client.Content))
  1486  	distBts, err := os.ReadFile(distFile)
  1487  	require.NoError(t, err)
  1488  	require.Equal(t, client.Content, string(distBts))
  1489  }