1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/pipeline/brew/brew_test.go

404 lines
10 KiB
Go
Raw Normal View History

2016-12-29 14:55:35 +02:00
package brew
import (
2017-03-26 20:30:21 +02:00
"bytes"
2017-12-25 23:58:07 +02:00
"flag"
2017-03-26 20:30:21 +02:00
"io/ioutil"
"os"
"path/filepath"
2016-12-29 14:55:35 +02:00
"testing"
2016-12-29 14:56:51 +02:00
2017-03-23 02:01:29 +02:00
"github.com/goreleaser/goreleaser/config"
2017-03-26 20:30:21 +02:00
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/artifact"
2017-08-20 21:50:34 +02:00
"github.com/goreleaser/goreleaser/internal/testlib"
2017-01-06 15:13:17 +02:00
"github.com/stretchr/testify/assert"
2016-12-29 14:55:35 +02:00
)
2017-12-25 23:58:07 +02:00
var update = flag.Bool("update", false, "update .golden files")
2017-03-26 01:43:42 +02:00
func TestDescription(t *testing.T) {
assert.NotEmpty(t, Pipe{}.String())
2017-03-26 01:43:42 +02:00
}
2016-12-29 14:55:35 +02:00
func TestNameWithDash(t *testing.T) {
assert.Equal(t, formulaNameFor("some-binary"), "SomeBinary")
}
func TestNameWithUnderline(t *testing.T) {
assert.Equal(t, formulaNameFor("some_binary"), "SomeBinary")
}
func TestSimpleName(t *testing.T) {
assert.Equal(t, formulaNameFor("binary"), "Binary")
}
2016-12-29 17:14:52 +02:00
2016-12-31 21:02:25 +02:00
var defaultTemplateData = templateData{
2017-09-27 00:00:24 +02:00
Desc: "Some desc",
Homepage: "https://google.com",
DownloadURL: "https://github.com",
Name: "Test",
2017-03-23 02:29:14 +02:00
Repo: config.Repo{
Owner: "caarlos0",
Name: "test",
},
Tag: "v0.1.3",
Version: "0.1.3",
2017-07-02 01:59:16 +02:00
File: "test_Darwin_x86_64.tar.gz",
2017-03-23 02:29:14 +02:00
SHA256: "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68",
2016-12-31 21:02:25 +02:00
}
func assertDefaultTemplateData(t *testing.T, formulae string) {
assert.Contains(t, formulae, "class Test < Formula")
2017-09-27 13:43:52 +02:00
assert.Contains(t, formulae, `homepage "https://google.com"`)
assert.Contains(t, formulae, `url "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Darwin_x86_64.tar.gz"`)
assert.Contains(t, formulae, `sha256 "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68"`)
assert.Contains(t, formulae, `version "0.1.3"`)
2016-12-31 21:02:25 +02:00
}
func TestFullFormulae(t *testing.T) {
data := defaultTemplateData
data.Caveats = "Here are some caveats"
2017-07-16 20:06:32 +02:00
data.Dependencies = []string{"gtk+"}
data.Conflicts = []string{"svn"}
2017-03-09 19:24:49 +02:00
data.Plist = "it works"
data.Install = []string{"custom install script", "another install script"}
2017-07-16 20:06:32 +02:00
data.Tests = []string{`system "#{bin}/foo -version"`}
2017-01-19 11:04:41 +02:00
out, err := doBuildFormula(data)
assert.NoError(t, err)
2016-12-31 21:02:25 +02:00
formulae := out.String()
2017-07-16 20:06:32 +02:00
2017-12-26 00:09:55 +02:00
var golden = "testdata/test.rb.golden"
if *update {
ioutil.WriteFile(golden, []byte(formulae), 0655)
}
bts, err := ioutil.ReadFile(golden)
assert.NoError(t, err)
assert.Equal(t, string(bts), formulae)
2016-12-31 21:02:25 +02:00
}
2017-02-01 19:40:27 +02:00
func TestFormulaeSimple(t *testing.T) {
2017-01-19 11:04:41 +02:00
out, err := doBuildFormula(defaultTemplateData)
assert.NoError(t, err)
2016-12-31 21:02:25 +02:00
formulae := out.String()
assertDefaultTemplateData(t, formulae)
assert.NotContains(t, formulae, "def caveats")
assert.NotContains(t, formulae, "depends_on")
assert.NotContains(t, formulae, "def plist;")
2016-12-29 17:14:52 +02:00
}
2017-03-26 20:30:21 +02:00
2017-07-16 21:01:20 +02:00
func TestSplit(t *testing.T) {
var parts = split("system \"true\"\nsystem \"#{bin}/foo -h\"")
assert.Equal(t, []string{"system \"true\"", "system \"#{bin}/foo -h\""}, parts)
2017-07-16 21:01:20 +02:00
}
2017-07-16 21:02:18 +02:00
2017-03-26 20:30:21 +02:00
func TestRunPipe(t *testing.T) {
2017-04-15 19:18:17 +02:00
folder, err := ioutil.TempDir("", "goreleasertest")
assert.NoError(t, err)
2017-03-26 20:30:21 +02:00
var ctx = &context.Context{
2017-07-16 21:01:20 +02:00
Git: context.GitInfo{
CurrentTag: "v1.0.1",
},
Version: "1.0.1",
Artifacts: artifact.New(),
2017-03-26 20:30:21 +02:00
Config: config.Project{
2017-07-16 21:01:20 +02:00
Dist: folder,
ProjectName: "run-pipe",
2017-03-26 20:30:21 +02:00
Archive: config.Archive{
Format: "tar.gz",
},
2017-09-26 00:01:10 +02:00
Release: config.Release{
GitHub: config.Repo{
Owner: "test",
Name: "test",
},
},
2017-03-26 20:30:21 +02:00
Brew: config.Homebrew{
GitHub: config.Repo{
Owner: "test",
Name: "test",
},
2017-07-16 21:01:20 +02:00
Description: "A run pipe test formula",
Homepage: "https://github.com/goreleaser",
Caveats: "don't do this",
Test: "system \"true\"\nsystem \"#{bin}/foo -h\"",
Plist: `<xml>whatever</xml>`,
Dependencies: []string{"zsh", "bash"},
Conflicts: []string{"gtk+", "qt"},
2017-07-16 21:01:20 +02:00
Install: `bin.install "foo"`,
2017-03-26 20:30:21 +02:00
},
},
Publish: true,
2017-03-26 20:30:21 +02:00
}
2017-07-14 03:46:21 +02:00
var path = filepath.Join(folder, "bin.tar.gz")
ctx.Artifacts.Add(artifact.Artifact{
Name: "bin.tar.gz",
Path: path,
Goos: "darwin",
Goarch: "amd64",
Type: artifact.UploadableArchive,
})
2017-03-26 20:30:21 +02:00
client := &DummyClient{}
assert.Error(t, doRun(ctx, client))
assert.False(t, client.CreatedFile)
2017-07-07 01:13:02 +02:00
2017-07-14 03:46:21 +02:00
_, err = os.Create(path)
assert.NoError(t, err)
2017-07-16 21:01:20 +02:00
2018-01-10 01:31:18 +02:00
var distFile = filepath.Join(folder, "run-pipe.rb")
t.Run("default git url", func(tt *testing.T) {
assert.NoError(tt, doRun(ctx, client))
assert.True(tt, client.CreatedFile)
2017-12-25 23:58:07 +02:00
var golden = "testdata/run_pipe.rb.golden"
if *update {
ioutil.WriteFile(golden, []byte(client.Content), 0655)
}
bts, err := ioutil.ReadFile(golden)
assert.NoError(tt, err)
assert.Equal(tt, string(bts), client.Content)
2018-01-10 01:31:18 +02:00
distBts, err := ioutil.ReadFile(distFile)
assert.NoError(tt, err)
assert.Equal(tt, string(bts), string(distBts))
})
2017-09-27 13:43:52 +02:00
t.Run("github enterprise url", func(tt *testing.T) {
ctx.Config.GitHubURLs.Download = "http://github.example.org"
assert.NoError(tt, doRun(ctx, client))
assert.True(tt, client.CreatedFile)
2017-12-25 23:58:07 +02:00
var golden = "testdata/run_pipe_enterprise.rb.golden"
if *update {
ioutil.WriteFile(golden, []byte(client.Content), 0644)
}
bts, err := ioutil.ReadFile(golden)
assert.NoError(tt, err)
assert.Equal(tt, string(bts), client.Content)
2018-01-10 01:31:18 +02:00
distBts, err := ioutil.ReadFile(distFile)
assert.NoError(tt, err)
assert.Equal(tt, string(bts), string(distBts))
})
t.Run("custom download strategy", func(tt *testing.T) {
ctx.Config.Brew.DownloadStrategy = "GitHubPrivateRepositoryReleaseDownloadStrategy"
assert.NoError(tt, doRun(ctx, client))
assert.True(tt, client.CreatedFile)
var golden = "testdata/run_pipe_download_strategy.rb.golden"
if *update {
ioutil.WriteFile(golden, []byte(client.Content), 0655)
}
bts, err := ioutil.ReadFile(golden)
assert.NoError(tt, err)
assert.Equal(tt, string(bts), client.Content)
distBts, err := ioutil.ReadFile(distFile)
assert.NoError(tt, err)
assert.Equal(tt, string(bts), string(distBts))
})
2017-03-26 20:30:21 +02:00
}
2017-04-21 17:02:48 +02:00
func TestRunPipeNoDarwin64Build(t *testing.T) {
2017-04-14 20:49:37 +02:00
var ctx = &context.Context{
Config: config.Project{
Archive: config.Archive{
Format: "tar.gz",
},
Brew: config.Homebrew{
GitHub: config.Repo{
Owner: "test",
Name: "test",
},
},
},
Publish: true,
2017-04-14 20:49:37 +02:00
}
client := &DummyClient{}
assert.Equal(t, ErrNoDarwin64Build, doRun(ctx, client))
assert.False(t, client.CreatedFile)
2017-04-14 20:49:37 +02:00
}
func TestRunPipeMultipleDarwin64Build(t *testing.T) {
var ctx = context.New(
config.Project{
Archive: config.Archive{
Format: "tar.gz",
},
Brew: config.Homebrew{
GitHub: config.Repo{
Owner: "test",
Name: "test",
},
},
},
)
ctx.Publish = true
ctx.Artifacts.Add(artifact.Artifact{
Name: "bin1",
Path: "doesnt mather",
Goos: "darwin",
Goarch: "amd64",
Type: artifact.UploadableArchive,
})
ctx.Artifacts.Add(artifact.Artifact{
Name: "bin2",
Path: "doesnt mather",
Goos: "darwin",
Goarch: "amd64",
Type: artifact.UploadableArchive,
})
client := &DummyClient{}
assert.Equal(t, ErrTooManyDarwin64Builds, doRun(ctx, client))
assert.False(t, client.CreatedFile)
}
2017-04-21 17:02:48 +02:00
func TestRunPipeBrewNotSetup(t *testing.T) {
var ctx = &context.Context{
Config: config.Project{},
Publish: true,
}
client := &DummyClient{}
2017-08-20 21:50:34 +02:00
testlib.AssertSkipped(t, doRun(ctx, client))
assert.False(t, client.CreatedFile)
2017-04-21 17:02:48 +02:00
}
2017-07-03 06:24:26 +02:00
func TestRunPipeBinaryRelease(t *testing.T) {
var ctx = context.New(
config.Project{
2017-07-03 06:24:26 +02:00
Archive: config.Archive{
Format: "binary",
},
Brew: config.Homebrew{
GitHub: config.Repo{
Owner: "test",
Name: "test",
},
},
},
)
ctx.Publish = true
ctx.Artifacts.Add(artifact.Artifact{
Name: "bin",
Path: "doesnt mather",
Goos: "darwin",
Goarch: "amd64",
Type: artifact.Binary,
})
2017-04-14 20:49:37 +02:00
client := &DummyClient{}
2017-08-20 21:50:34 +02:00
testlib.AssertSkipped(t, doRun(ctx, client))
assert.False(t, client.CreatedFile)
2017-04-14 20:49:37 +02:00
}
2018-01-10 23:22:37 +02:00
func TestRunPipeNoUpload(t *testing.T) {
folder, err := ioutil.TempDir("", "goreleasertest")
assert.NoError(t, err)
var ctx = context.New(config.Project{
Dist: folder,
ProjectName: "foo",
Release: config.Release{},
Brew: config.Homebrew{
GitHub: config.Repo{
Owner: "test",
Name: "test",
2017-04-22 00:55:25 +02:00
},
2017-04-22 00:50:09 +02:00
},
2018-01-10 23:22:37 +02:00
})
var path = filepath.Join(folder, "whatever.tar.gz")
_, err = os.Create(path)
assert.NoError(t, err)
ctx.Artifacts.Add(artifact.Artifact{
Name: "bin",
Path: path,
Goos: "darwin",
Goarch: "amd64",
Type: artifact.UploadableArchive,
})
2017-04-22 00:50:09 +02:00
client := &DummyClient{}
2018-01-10 23:22:37 +02:00
var assertNoPublish = func(t *testing.T) {
testlib.AssertSkipped(t, doRun(ctx, client))
assert.False(t, client.CreatedFile)
}
t.Run("skip upload", func(tt *testing.T) {
ctx.Publish = true
ctx.Config.Brew.SkipUpload = true
assertNoPublish(tt)
})
t.Run("skip publish", func(tt *testing.T) {
ctx.Publish = false
assertNoPublish(tt)
})
t.Run("draft release", func(tt *testing.T) {
ctx.Publish = true
ctx.Config.Release.Draft = true
assertNoPublish(tt)
})
2017-04-22 00:50:09 +02:00
}
func TestRunPipeFormatBinary(t *testing.T) {
var ctx = &context.Context{
Config: config.Project{
Archive: config.Archive{
Format: "binary",
},
},
}
client := &DummyClient{}
2017-08-20 21:50:34 +02:00
testlib.AssertSkipped(t, doRun(ctx, client))
assert.False(t, client.CreatedFile)
}
func TestDefault(t *testing.T) {
_, back := testlib.Mktmp(t)
defer back()
var ctx = &context.Context{
Config: config.Project{
Builds: []config.Build{
{
Binary: "foo",
Goos: []string{"linux", "darwin"},
Goarch: []string{"386", "amd64"},
},
{
Binary: "bar",
Goos: []string{"linux", "darwin"},
Goarch: []string{"386", "amd64"},
Ignore: []config.IgnoredBuild{
{Goos: "darwin", Goarch: "amd64"},
},
},
{
Binary: "foobar",
Goos: []string{"linux"},
Goarch: []string{"amd64"},
},
},
},
}
assert.NoError(t, Pipe{}.Default(ctx))
assert.NotEmpty(t, ctx.Config.Brew.CommitAuthor.Name)
assert.NotEmpty(t, ctx.Config.Brew.CommitAuthor.Email)
assert.Equal(t, `bin.install "foo"`, ctx.Config.Brew.Install)
}
2017-03-26 20:30:21 +02:00
type DummyClient struct {
CreatedFile bool
Content string
2017-03-26 20:30:21 +02:00
}
2018-01-26 19:45:38 +02:00
func (client *DummyClient) CreateRelease(ctx *context.Context, body string) (releaseID int64, err error) {
2017-03-26 20:30:21 +02:00
return
}
func (client *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo config.Repo, content bytes.Buffer, path string) (err error) {
2017-03-26 20:30:21 +02:00
client.CreatedFile = true
bts, _ := ioutil.ReadAll(&content)
client.Content = string(bts)
2017-03-26 20:30:21 +02:00
return
}
2018-01-26 19:45:38 +02:00
func (client *DummyClient) Upload(ctx *context.Context, releaseID int64, name string, file *os.File) (err error) {
2017-03-26 20:30:21 +02:00
return
}