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