github.com/goreleaser/goreleaser@v1.25.1/internal/pipe/scoop/scoop_test.go (about)

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