github.com/joselitofilho/goreleaser@v0.155.1-0.20210123221854-e4891856c593/internal/pipe/scoop/scoop_test.go (about)

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