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

     1  package brew
     2  
     3  import (
     4  	"bytes"
     5  	"flag"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"os"
     9  	"path/filepath"
    10  	"testing"
    11  
    12  	"github.com/goreleaser/goreleaser/config"
    13  	"github.com/goreleaser/goreleaser/context"
    14  	"github.com/goreleaser/goreleaser/internal/artifact"
    15  	"github.com/goreleaser/goreleaser/internal/testlib"
    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 TestNameWithDash(t *testing.T) {
    26  	assert.Equal(t, formulaNameFor("some-binary"), "SomeBinary")
    27  }
    28  
    29  func TestNameWithUnderline(t *testing.T) {
    30  	assert.Equal(t, formulaNameFor("some_binary"), "SomeBinary")
    31  }
    32  
    33  func TestSimpleName(t *testing.T) {
    34  	assert.Equal(t, formulaNameFor("binary"), "Binary")
    35  }
    36  
    37  var defaultTemplateData = templateData{
    38  	Desc:        "Some desc",
    39  	Homepage:    "https://google.com",
    40  	DownloadURL: "https://github.com",
    41  	Name:        "Test",
    42  	Repo: config.Repo{
    43  		Owner: "caarlos0",
    44  		Name:  "test",
    45  	},
    46  	Tag:     "v0.1.3",
    47  	Version: "0.1.3",
    48  	Caveats: []string{},
    49  	File:    "test_Darwin_x86_64.tar.gz",
    50  	SHA256:  "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68",
    51  }
    52  
    53  func assertDefaultTemplateData(t *testing.T, formulae string) {
    54  	assert.Contains(t, formulae, "class Test < Formula")
    55  	assert.Contains(t, formulae, `homepage "https://google.com"`)
    56  	assert.Contains(t, formulae, `url "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Darwin_x86_64.tar.gz"`)
    57  	assert.Contains(t, formulae, `sha256 "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68"`)
    58  	assert.Contains(t, formulae, `version "0.1.3"`)
    59  }
    60  
    61  func TestFullFormulae(t *testing.T) {
    62  	data := defaultTemplateData
    63  	data.Caveats = []string{"Here are some caveats"}
    64  	data.Dependencies = []string{"gtk+"}
    65  	data.Conflicts = []string{"svn"}
    66  	data.Plist = "it works"
    67  	data.Install = []string{"custom install script", "another install script"}
    68  	data.Tests = []string{`system "#{bin}/foo -version"`}
    69  	out, err := doBuildFormula(data)
    70  	assert.NoError(t, err)
    71  	formulae := out.String()
    72  
    73  	var golden = "testdata/test.rb.golden"
    74  	if *update {
    75  		ioutil.WriteFile(golden, []byte(formulae), 0655)
    76  	}
    77  	bts, err := ioutil.ReadFile(golden)
    78  	assert.NoError(t, err)
    79  	assert.Equal(t, string(bts), formulae)
    80  }
    81  
    82  func TestFormulaeSimple(t *testing.T) {
    83  	out, err := doBuildFormula(defaultTemplateData)
    84  	assert.NoError(t, err)
    85  	formulae := out.String()
    86  	assertDefaultTemplateData(t, formulae)
    87  	assert.NotContains(t, formulae, "def caveats")
    88  	assert.NotContains(t, formulae, "depends_on")
    89  	assert.NotContains(t, formulae, "def plist;")
    90  }
    91  
    92  func TestSplit(t *testing.T) {
    93  	var parts = split("system \"true\"\nsystem \"#{bin}/foo -h\"")
    94  	assert.Equal(t, []string{"system \"true\"", "system \"#{bin}/foo -h\""}, parts)
    95  	parts = split("")
    96  	assert.Equal(t, []string{}, parts)
    97  	parts = split("\n  ")
    98  	assert.Equal(t, []string{}, parts)
    99  }
   100  
   101  func TestRunPipe(t *testing.T) {
   102  	for name, fn := range map[string]func(ctx *context.Context){
   103  		"default": func(ctx *context.Context) {},
   104  		"github_enterprise_url": func(ctx *context.Context) {
   105  			ctx.Config.GitHubURLs.Download = "http://github.example.org"
   106  		},
   107  		"custom_download_strategy": func(ctx *context.Context) {
   108  			ctx.Config.Brew.DownloadStrategy = "GitHubPrivateRepositoryReleaseDownloadStrategy"
   109  		},
   110  		"build_from_source": func(ctx *context.Context) {
   111  			ctx.Config.Brew.BuildDependencies = []string{"go"}
   112  		},
   113  	} {
   114  		t.Run(name, func(t *testing.T) {
   115  			folder, err := ioutil.TempDir("", "goreleasertest")
   116  			assert.NoError(t, err)
   117  			var ctx = &context.Context{
   118  				Git: context.GitInfo{
   119  					CurrentTag: "v1.0.1",
   120  				},
   121  				Version:   "1.0.1",
   122  				Artifacts: artifact.New(),
   123  				Config: config.Project{
   124  					Dist:        folder,
   125  					ProjectName: name,
   126  					GitHubURLs: config.GitHubURLs{
   127  						Download: "https://github.com",
   128  					},
   129  					Archive: config.Archive{
   130  						Format: "tar.gz",
   131  					},
   132  					Release: config.Release{
   133  						GitHub: config.Repo{
   134  							Owner: "test",
   135  							Name:  "test",
   136  						},
   137  					},
   138  					Brew: config.Homebrew{
   139  						Name: name,
   140  						GitHub: config.Repo{
   141  							Owner: "test",
   142  							Name:  "test",
   143  						},
   144  						Description:  "A run pipe test formula",
   145  						Homepage:     "https://github.com/goreleaser",
   146  						Caveats:      "don't do this",
   147  						Test:         "system \"true\"\nsystem \"#{bin}/foo -h\"",
   148  						Plist:        `<xml>whatever</xml>`,
   149  						Dependencies: []string{"zsh", "bash"},
   150  						Conflicts:    []string{"gtk+", "qt"},
   151  						Install:      `bin.install "foo"`,
   152  					},
   153  				},
   154  			}
   155  			fn(ctx)
   156  			var path = filepath.Join(folder, "bin.tar.gz")
   157  			ctx.Artifacts.Add(artifact.Artifact{
   158  				Name:   "bin.tar.gz",
   159  				Path:   path,
   160  				Goos:   "darwin",
   161  				Goarch: "amd64",
   162  				Type:   artifact.UploadableArchive,
   163  			})
   164  
   165  			_, err = os.Create(path)
   166  			assert.NoError(t, err)
   167  			client := &DummyClient{}
   168  			var distFile = filepath.Join(folder, name+".rb")
   169  
   170  			assert.NoError(t, doRun(ctx, client))
   171  			assert.True(t, client.CreatedFile)
   172  			var golden = fmt.Sprintf("testdata/%s.rb.golden", name)
   173  			if *update {
   174  				ioutil.WriteFile(golden, []byte(client.Content), 0655)
   175  			}
   176  			bts, err := ioutil.ReadFile(golden)
   177  			assert.NoError(t, err)
   178  			assert.Equal(t, string(bts), client.Content)
   179  
   180  			distBts, err := ioutil.ReadFile(distFile)
   181  			assert.NoError(t, err)
   182  			assert.Equal(t, string(bts), string(distBts))
   183  		})
   184  	}
   185  }
   186  
   187  func TestRunPipeNoDarwin64Build(t *testing.T) {
   188  	var ctx = &context.Context{
   189  		Config: config.Project{
   190  			Archive: config.Archive{
   191  				Format: "tar.gz",
   192  			},
   193  			Brew: config.Homebrew{
   194  				GitHub: config.Repo{
   195  					Owner: "test",
   196  					Name:  "test",
   197  				},
   198  			},
   199  		},
   200  	}
   201  	client := &DummyClient{}
   202  	assert.Equal(t, ErrNoDarwin64Build, doRun(ctx, client))
   203  	assert.False(t, client.CreatedFile)
   204  }
   205  
   206  func TestRunPipeMultipleDarwin64Build(t *testing.T) {
   207  	var ctx = context.New(
   208  		config.Project{
   209  			Archive: config.Archive{
   210  				Format: "tar.gz",
   211  			},
   212  			Brew: config.Homebrew{
   213  				GitHub: config.Repo{
   214  					Owner: "test",
   215  					Name:  "test",
   216  				},
   217  			},
   218  		},
   219  	)
   220  	ctx.Artifacts.Add(artifact.Artifact{
   221  		Name:   "bin1",
   222  		Path:   "doesnt mather",
   223  		Goos:   "darwin",
   224  		Goarch: "amd64",
   225  		Type:   artifact.UploadableArchive,
   226  	})
   227  	ctx.Artifacts.Add(artifact.Artifact{
   228  		Name:   "bin2",
   229  		Path:   "doesnt mather",
   230  		Goos:   "darwin",
   231  		Goarch: "amd64",
   232  		Type:   artifact.UploadableArchive,
   233  	})
   234  	client := &DummyClient{}
   235  	assert.Equal(t, ErrTooManyDarwin64Builds, doRun(ctx, client))
   236  	assert.False(t, client.CreatedFile)
   237  }
   238  
   239  func TestRunPipeBrewNotSetup(t *testing.T) {
   240  	var ctx = &context.Context{
   241  		Config: config.Project{},
   242  	}
   243  	client := &DummyClient{}
   244  	testlib.AssertSkipped(t, doRun(ctx, client))
   245  	assert.False(t, client.CreatedFile)
   246  }
   247  
   248  func TestRunPipeBinaryRelease(t *testing.T) {
   249  	var ctx = context.New(
   250  		config.Project{
   251  			Archive: config.Archive{
   252  				Format: "binary",
   253  			},
   254  			Brew: config.Homebrew{
   255  				GitHub: config.Repo{
   256  					Owner: "test",
   257  					Name:  "test",
   258  				},
   259  			},
   260  		},
   261  	)
   262  	ctx.Artifacts.Add(artifact.Artifact{
   263  		Name:   "bin",
   264  		Path:   "doesnt mather",
   265  		Goos:   "darwin",
   266  		Goarch: "amd64",
   267  		Type:   artifact.Binary,
   268  	})
   269  	client := &DummyClient{}
   270  	testlib.AssertSkipped(t, doRun(ctx, client))
   271  	assert.False(t, client.CreatedFile)
   272  }
   273  
   274  func TestRunPipeNoUpload(t *testing.T) {
   275  	folder, err := ioutil.TempDir("", "goreleasertest")
   276  	assert.NoError(t, err)
   277  	var ctx = context.New(config.Project{
   278  		Dist:        folder,
   279  		ProjectName: "foo",
   280  		Release:     config.Release{},
   281  		Brew: config.Homebrew{
   282  			GitHub: config.Repo{
   283  				Owner: "test",
   284  				Name:  "test",
   285  			},
   286  		},
   287  	})
   288  	var path = filepath.Join(folder, "whatever.tar.gz")
   289  	_, err = os.Create(path)
   290  	assert.NoError(t, err)
   291  	ctx.Artifacts.Add(artifact.Artifact{
   292  		Name:   "bin",
   293  		Path:   path,
   294  		Goos:   "darwin",
   295  		Goarch: "amd64",
   296  		Type:   artifact.UploadableArchive,
   297  	})
   298  	client := &DummyClient{}
   299  
   300  	var assertNoPublish = func(t *testing.T) {
   301  		testlib.AssertSkipped(t, doRun(ctx, client))
   302  		assert.False(t, client.CreatedFile)
   303  	}
   304  	t.Run("skip upload", func(tt *testing.T) {
   305  		ctx.Config.Release.Draft = false
   306  		ctx.Config.Brew.SkipUpload = true
   307  		ctx.SkipPublish = false
   308  		assertNoPublish(tt)
   309  	})
   310  	t.Run("skip publish", func(tt *testing.T) {
   311  		ctx.Config.Release.Draft = false
   312  		ctx.Config.Brew.SkipUpload = false
   313  		ctx.SkipPublish = true
   314  		assertNoPublish(tt)
   315  	})
   316  	t.Run("draft release", func(tt *testing.T) {
   317  		ctx.Config.Release.Draft = true
   318  		ctx.Config.Brew.SkipUpload = false
   319  		ctx.SkipPublish = false
   320  		assertNoPublish(tt)
   321  	})
   322  }
   323  
   324  func TestRunPipeFormatBinary(t *testing.T) {
   325  	var ctx = &context.Context{
   326  		Config: config.Project{
   327  			Archive: config.Archive{
   328  				Format: "binary",
   329  			},
   330  		},
   331  	}
   332  	client := &DummyClient{}
   333  	testlib.AssertSkipped(t, doRun(ctx, client))
   334  	assert.False(t, client.CreatedFile)
   335  }
   336  
   337  func TestDefault(t *testing.T) {
   338  	_, back := testlib.Mktmp(t)
   339  	defer back()
   340  
   341  	var ctx = &context.Context{
   342  		Config: config.Project{
   343  			ProjectName: "myproject",
   344  			Builds: []config.Build{
   345  				{
   346  					Binary: "foo",
   347  					Goos:   []string{"linux", "darwin"},
   348  					Goarch: []string{"386", "amd64"},
   349  				},
   350  				{
   351  					Binary: "bar",
   352  					Goos:   []string{"linux", "darwin"},
   353  					Goarch: []string{"386", "amd64"},
   354  					Ignore: []config.IgnoredBuild{
   355  						{Goos: "darwin", Goarch: "amd64"},
   356  					},
   357  				},
   358  				{
   359  					Binary: "foobar",
   360  					Goos:   []string{"linux"},
   361  					Goarch: []string{"amd64"},
   362  				},
   363  			},
   364  		},
   365  	}
   366  	assert.NoError(t, Pipe{}.Default(ctx))
   367  	assert.Equal(t, ctx.Config.ProjectName, ctx.Config.Brew.Name)
   368  	assert.NotEmpty(t, ctx.Config.Brew.CommitAuthor.Name)
   369  	assert.NotEmpty(t, ctx.Config.Brew.CommitAuthor.Email)
   370  	assert.Equal(t, `bin.install "foo"`, ctx.Config.Brew.Install)
   371  }
   372  
   373  type DummyClient struct {
   374  	CreatedFile bool
   375  	Content     string
   376  }
   377  
   378  func (client *DummyClient) CreateRelease(ctx *context.Context, body string) (releaseID int64, err error) {
   379  	return
   380  }
   381  
   382  func (client *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo config.Repo, content bytes.Buffer, path, msg string) (err error) {
   383  	client.CreatedFile = true
   384  	bts, _ := ioutil.ReadAll(&content)
   385  	client.Content = string(bts)
   386  	return
   387  }
   388  
   389  func (client *DummyClient) Upload(ctx *context.Context, releaseID int64, name string, file *os.File) (err error) {
   390  	return
   391  }