github.com/szyn/goreleaser@v0.76.1-0.20180517112710-333da09a1297/pipeline/scoop/scoop_test.go (about)

     1  package scoop
     2  
     3  import (
     4  	"bytes"
     5  	"flag"
     6  	"io/ioutil"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/goreleaser/goreleaser/config"
    11  	"github.com/goreleaser/goreleaser/context"
    12  	"github.com/goreleaser/goreleaser/internal/artifact"
    13  	"github.com/goreleaser/goreleaser/internal/client"
    14  	"github.com/goreleaser/goreleaser/internal/testlib"
    15  	"github.com/goreleaser/goreleaser/pipeline"
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  var update = flag.Bool("update", false, "update .golden files")
    20  
    21  func TestDescription(t *testing.T) {
    22  	assert.NotEmpty(t, Pipe{}.String())
    23  }
    24  
    25  func TestDefault(t *testing.T) {
    26  	_, back := testlib.Mktmp(t)
    27  	defer back()
    28  
    29  	var ctx = &context.Context{
    30  		Config: config.Project{
    31  			Builds: []config.Build{
    32  				{
    33  					Binary: "foo",
    34  					Goos:   []string{"linux", "darwin"},
    35  					Goarch: []string{"386", "amd64"},
    36  				},
    37  				{
    38  					Binary: "bar",
    39  					Goos:   []string{"linux", "darwin"},
    40  					Goarch: []string{"386", "amd64"},
    41  					Ignore: []config.IgnoredBuild{
    42  						{Goos: "darwin", Goarch: "amd64"},
    43  					},
    44  				},
    45  				{
    46  					Binary: "foobar",
    47  					Goos:   []string{"linux"},
    48  					Goarch: []string{"amd64"},
    49  				},
    50  			},
    51  		},
    52  	}
    53  	assert.NoError(t, Pipe{}.Default(ctx))
    54  	assert.NotEmpty(t, ctx.Config.Scoop.CommitAuthor.Name)
    55  	assert.NotEmpty(t, ctx.Config.Scoop.CommitAuthor.Email)
    56  }
    57  
    58  func Test_doRun(t *testing.T) {
    59  	type errChecker func(*testing.T, error)
    60  	var shouldErr = func(msg string) errChecker {
    61  		return func(t *testing.T, err error) {
    62  			assert.Error(t, err)
    63  			assert.EqualError(t, err, msg)
    64  		}
    65  	}
    66  	var shouldNotErr = func(t *testing.T, err error) {
    67  		assert.NoError(t, err)
    68  	}
    69  	type args struct {
    70  		ctx    *context.Context
    71  		client client.Client
    72  	}
    73  	tests := []struct {
    74  		name        string
    75  		args        args
    76  		artifacts   []artifact.Artifact
    77  		assertError errChecker
    78  	}{
    79  		{
    80  			"valid",
    81  			args{
    82  				&context.Context{
    83  					Git: context.GitInfo{
    84  						CurrentTag: "v1.0.1",
    85  					},
    86  					Version:   "1.0.1",
    87  					Artifacts: artifact.New(),
    88  					Config: config.Project{
    89  						Builds: []config.Build{
    90  							{Binary: "test", Goarch: []string{"amd64"}, Goos: []string{"windows"}},
    91  						},
    92  						Dist:        ".",
    93  						ProjectName: "run-pipe",
    94  						Archive: config.Archive{
    95  							Format: "tar.gz",
    96  						},
    97  						Release: config.Release{
    98  							GitHub: config.Repo{
    99  								Owner: "test",
   100  								Name:  "test",
   101  							},
   102  						},
   103  						Scoop: config.Scoop{
   104  							Bucket: config.Repo{
   105  								Owner: "test",
   106  								Name:  "test",
   107  							},
   108  							Description: "A run pipe test formula",
   109  							Homepage:    "https://github.com/goreleaser",
   110  						},
   111  					},
   112  				},
   113  				&DummyClient{},
   114  			},
   115  			[]artifact.Artifact{
   116  				{Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64"},
   117  				{Name: "foo_1.0.1_windows_386.tar.gz", Goos: "windows", Goarch: "386"},
   118  			},
   119  			shouldNotErr,
   120  		},
   121  		{
   122  			"valid",
   123  			args{
   124  				&context.Context{
   125  					Git: context.GitInfo{
   126  						CurrentTag: "v1.0.1",
   127  					},
   128  					Version:   "1.0.1",
   129  					Artifacts: artifact.New(),
   130  					Config: config.Project{
   131  						GitHubURLs: config.GitHubURLs{Download: "https://api.custom.github.enterprise.com"},
   132  						Builds: []config.Build{
   133  							{Binary: "test", Goarch: []string{"amd64"}, Goos: []string{"windows"}},
   134  						},
   135  						Dist:        ".",
   136  						ProjectName: "run-pipe",
   137  						Archive: config.Archive{
   138  							Format: "tar.gz",
   139  						},
   140  						Release: config.Release{
   141  							GitHub: config.Repo{
   142  								Owner: "test",
   143  								Name:  "test",
   144  							},
   145  						},
   146  						Scoop: config.Scoop{
   147  							Bucket: config.Repo{
   148  								Owner: "test",
   149  								Name:  "test",
   150  							},
   151  							Description: "A run pipe test formula",
   152  							Homepage:    "https://github.com/goreleaser",
   153  						},
   154  					},
   155  				},
   156  				&DummyClient{},
   157  			},
   158  			[]artifact.Artifact{
   159  				{Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64"},
   160  				{Name: "foo_1.0.1_windows_386.tar.gz", Goos: "windows", Goarch: "386"},
   161  			},
   162  			shouldNotErr,
   163  		},
   164  		{
   165  			"no windows build",
   166  			args{
   167  				&context.Context{
   168  					Git: context.GitInfo{
   169  						CurrentTag: "v1.0.1",
   170  					},
   171  					Version:   "1.0.1",
   172  					Artifacts: artifact.New(),
   173  					Config: config.Project{
   174  						Builds: []config.Build{
   175  							{Binary: "test"},
   176  						},
   177  						Dist:        ".",
   178  						ProjectName: "run-pipe",
   179  						Archive: config.Archive{
   180  							Format: "tar.gz",
   181  						},
   182  						Release: config.Release{
   183  							GitHub: config.Repo{
   184  								Owner: "test",
   185  								Name:  "test",
   186  							},
   187  						},
   188  						Scoop: config.Scoop{
   189  							Bucket: config.Repo{
   190  								Owner: "test",
   191  								Name:  "test",
   192  							},
   193  							Description: "A run pipe test formula",
   194  							Homepage:    "https://github.com/goreleaser",
   195  						},
   196  					},
   197  				},
   198  				&DummyClient{},
   199  			},
   200  			[]artifact.Artifact{
   201  				{Name: "foo_1.0.1_linux_amd64.tar.gz", Goos: "linux", Goarch: "amd64"},
   202  				{Name: "foo_1.0.1_linux_386.tar.gz", Goos: "linux", Goarch: "386"},
   203  			},
   204  			shouldErr("scoop requires a windows build"),
   205  		},
   206  		{
   207  			"no scoop",
   208  			args{
   209  				&context.Context{
   210  					Git: context.GitInfo{
   211  						CurrentTag: "v1.0.1",
   212  					},
   213  					Version:   "1.0.1",
   214  					Artifacts: artifact.New(),
   215  					Config: config.Project{
   216  						Builds: []config.Build{
   217  							{Binary: "test", Goarch: []string{"amd64"}, Goos: []string{"windows"}},
   218  						},
   219  						Dist:        ".",
   220  						ProjectName: "run-pipe",
   221  						Archive: config.Archive{
   222  							Format: "tar.gz",
   223  						},
   224  						Release: config.Release{
   225  							GitHub: config.Repo{
   226  								Owner: "test",
   227  								Name:  "test",
   228  							},
   229  						},
   230  					},
   231  				},
   232  				&DummyClient{},
   233  			},
   234  			[]artifact.Artifact{
   235  				{Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64"},
   236  				{Name: "foo_1.0.1_windows_386.tar.gz", Goos: "windows", Goarch: "386"},
   237  			},
   238  			shouldErr("scoop section is not configured"),
   239  		},
   240  		{
   241  			"no publish",
   242  			args{
   243  				&context.Context{
   244  					Git: context.GitInfo{
   245  						CurrentTag: "v1.0.1",
   246  					},
   247  					Version:   "1.0.1",
   248  					Artifacts: artifact.New(),
   249  					Config: config.Project{
   250  						Builds: []config.Build{
   251  							{Binary: "test", Goarch: []string{"amd64"}, Goos: []string{"windows"}},
   252  						},
   253  						Dist:        ".",
   254  						ProjectName: "run-pipe",
   255  						Archive: config.Archive{
   256  							Format: "tar.gz",
   257  						},
   258  						Release: config.Release{
   259  							GitHub: config.Repo{
   260  								Owner: "test",
   261  								Name:  "test",
   262  							},
   263  						},
   264  						Scoop: config.Scoop{
   265  							Bucket: config.Repo{
   266  								Owner: "test",
   267  								Name:  "test",
   268  							},
   269  							Description: "A run pipe test formula",
   270  							Homepage:    "https://github.com/goreleaser",
   271  						},
   272  					},
   273  					SkipPublish: true,
   274  				},
   275  				&DummyClient{},
   276  			},
   277  			[]artifact.Artifact{
   278  				{Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64"},
   279  				{Name: "foo_1.0.1_windows_386.tar.gz", Goos: "windows", Goarch: "386"},
   280  			},
   281  			shouldErr(pipeline.ErrSkipPublishEnabled.Error()),
   282  		},
   283  		{
   284  			"is draft",
   285  			args{
   286  				&context.Context{
   287  					Git: context.GitInfo{
   288  						CurrentTag: "v1.0.1",
   289  					},
   290  					Version:   "1.0.1",
   291  					Artifacts: artifact.New(),
   292  					Config: config.Project{
   293  						Builds: []config.Build{
   294  							{Binary: "test", Goarch: []string{"amd64"}, Goos: []string{"windows"}},
   295  						},
   296  						Dist:        ".",
   297  						ProjectName: "run-pipe",
   298  						Archive: config.Archive{
   299  							Format: "tar.gz",
   300  						},
   301  						Release: config.Release{
   302  							Draft: true,
   303  						},
   304  						Scoop: config.Scoop{
   305  							Bucket: config.Repo{
   306  								Owner: "test",
   307  								Name:  "test",
   308  							},
   309  							Description: "A run pipe test formula",
   310  							Homepage:    "https://github.com/goreleaser",
   311  						},
   312  					},
   313  				},
   314  				&DummyClient{},
   315  			},
   316  			[]artifact.Artifact{
   317  				{Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64"},
   318  				{Name: "foo_1.0.1_windows_386.tar.gz", Goos: "windows", Goarch: "386"},
   319  			},
   320  			shouldErr("release is marked as draft"),
   321  		},
   322  		{
   323  			"no archive",
   324  			args{
   325  				&context.Context{
   326  					Git: context.GitInfo{
   327  						CurrentTag: "v1.0.1",
   328  					},
   329  					Version:   "1.0.1",
   330  					Artifacts: artifact.New(),
   331  					Config: config.Project{
   332  						Builds: []config.Build{
   333  							{Binary: "test", Goarch: []string{"amd64"}, Goos: []string{"windows"}},
   334  						},
   335  						Dist:        ".",
   336  						ProjectName: "run-pipe",
   337  						Archive: config.Archive{
   338  							Format: "binary",
   339  						},
   340  						Release: config.Release{
   341  							Draft: true,
   342  						},
   343  						Scoop: config.Scoop{
   344  							Bucket: config.Repo{
   345  								Owner: "test",
   346  								Name:  "test",
   347  							},
   348  							Description: "A run pipe test formula",
   349  							Homepage:    "https://github.com/goreleaser",
   350  						},
   351  					},
   352  				},
   353  				&DummyClient{},
   354  			},
   355  			[]artifact.Artifact{
   356  				{Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64"},
   357  				{Name: "foo_1.0.1_windows_386.tar.gz", Goos: "windows", Goarch: "386"},
   358  			},
   359  			shouldErr("archive format is binary"),
   360  		},
   361  	}
   362  	for _, tt := range tests {
   363  		t.Run(tt.name, func(t *testing.T) {
   364  			for _, a := range tt.artifacts {
   365  				tt.args.ctx.Artifacts.Add(a)
   366  			}
   367  			tt.assertError(t, doRun(tt.args.ctx, tt.args.client))
   368  		})
   369  	}
   370  }
   371  
   372  func Test_buildManifest(t *testing.T) {
   373  	var ctx = &context.Context{
   374  		Git: context.GitInfo{
   375  			CurrentTag: "v1.0.1",
   376  		},
   377  		Version:   "1.0.1",
   378  		Artifacts: artifact.New(),
   379  		Config: config.Project{
   380  			GitHubURLs: config.GitHubURLs{
   381  				Download: "https://github.com",
   382  			},
   383  			Builds: []config.Build{
   384  				{Binary: "test"},
   385  			},
   386  			Dist:        ".",
   387  			ProjectName: "run-pipe",
   388  			Archive: config.Archive{
   389  				Format: "tar.gz",
   390  			},
   391  			Release: config.Release{
   392  				GitHub: config.Repo{
   393  					Owner: "test",
   394  					Name:  "test",
   395  				},
   396  			},
   397  			Scoop: config.Scoop{
   398  				Bucket: config.Repo{
   399  					Owner: "test",
   400  					Name:  "test",
   401  				},
   402  				Description: "A run pipe test formula",
   403  				Homepage:    "https://github.com/goreleaser",
   404  			},
   405  		},
   406  	}
   407  	out, err := buildManifest(ctx, &DummyClient{}, []artifact.Artifact{
   408  		{Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64"},
   409  		{Name: "foo_1.0.1_windows_386.tar.gz", Goos: "windows", Goarch: "386"},
   410  	})
   411  	assert.NoError(t, err)
   412  	var golden = "testdata/test_buildmanifest.json.golden"
   413  	if *update {
   414  		ioutil.WriteFile(golden, out.Bytes(), 0655)
   415  	}
   416  	bts, err := ioutil.ReadFile(golden)
   417  	assert.NoError(t, err)
   418  	assert.Equal(t, string(bts), out.String())
   419  }
   420  
   421  func Test_getDownloadURL(t *testing.T) {
   422  	type args struct {
   423  		ctx       *context.Context
   424  		githubURL string
   425  		file      string
   426  	}
   427  	tests := []struct {
   428  		name    string
   429  		args    args
   430  		wantURL string
   431  	}{
   432  		{
   433  			"simple",
   434  			args{
   435  				&context.Context{
   436  					Version: "1.0.0",
   437  					Git: context.GitInfo{
   438  						CurrentTag: "v1.0.0",
   439  					},
   440  					Config: config.Project{
   441  						Release: config.Release{
   442  							GitHub: config.Repo{
   443  								Owner: "user",
   444  								Name:  "repo",
   445  							},
   446  						},
   447  					},
   448  				},
   449  				"https://github.com",
   450  				"file.tar.gz",
   451  			},
   452  			"https://github.com/user/repo/releases/download/v1.0.0/file.tar.gz",
   453  		},
   454  		{
   455  			"custom",
   456  			args{
   457  				&context.Context{
   458  					Version: "1.0.0",
   459  					Git: context.GitInfo{
   460  						CurrentTag: "v1.0.0",
   461  					},
   462  					Config: config.Project{
   463  						Release: config.Release{
   464  							GitHub: config.Repo{
   465  								Owner: "user",
   466  								Name:  "repo",
   467  							},
   468  						},
   469  					},
   470  				},
   471  				"https://git.my.company.com",
   472  				"file.tar.gz",
   473  			},
   474  			"https://git.my.company.com/user/repo/releases/download/v1.0.0/file.tar.gz",
   475  		},
   476  	}
   477  	for _, tt := range tests {
   478  		t.Run(tt.name, func(t *testing.T) {
   479  			gotURL := getDownloadURL(tt.args.ctx, tt.args.githubURL, tt.args.file)
   480  			assert.Equal(t, tt.wantURL, gotURL)
   481  		})
   482  	}
   483  }
   484  
   485  type DummyClient struct {
   486  	CreatedFile bool
   487  	Content     string
   488  }
   489  
   490  func (client *DummyClient) CreateRelease(ctx *context.Context, body string) (releaseID int64, err error) {
   491  	return
   492  }
   493  
   494  func (client *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo config.Repo, content bytes.Buffer, path, msg string) (err error) {
   495  	client.CreatedFile = true
   496  	bts, _ := ioutil.ReadAll(&content)
   497  	client.Content = string(bts)
   498  	return
   499  }
   500  
   501  func (client *DummyClient) Upload(ctx *context.Context, releaseID int64, name string, file *os.File) (err error) {
   502  	return
   503  }