github.com/ahmet2mir/goreleaser@v0.180.3-0.20210927151101-8e5ee5a9b8c5/internal/pipe/brew/brew_test.go (about)

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