From bc64deeb09a70cbf662dc3a49b4856ee96885430 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sun, 2 Jul 2017 12:01:59 -0300 Subject: [PATCH] archive format overrides could break brew formula --- internal/archiveformat/format.go | 19 ++++++++ internal/archiveformat/format_test.go | 28 +++++++++++ pipeline/archive/archive.go | 13 +---- pipeline/archive/archive_test.go | 19 -------- pipeline/brew/brew.go | 17 +++---- pipeline/brew/brew_test.go | 69 ++++++++++++++++++++++++++- 6 files changed, 122 insertions(+), 43 deletions(-) create mode 100644 internal/archiveformat/format.go create mode 100644 internal/archiveformat/format_test.go diff --git a/internal/archiveformat/format.go b/internal/archiveformat/format.go new file mode 100644 index 000000000..ce87f215a --- /dev/null +++ b/internal/archiveformat/format.go @@ -0,0 +1,19 @@ +// Package archiveformat provides functions to get the format of given package +// based on the config +package archiveformat + +import ( + "strings" + + "github.com/goreleaser/goreleaser/context" +) + +// For return the archive format, considering overrides and all that +func For(ctx *context.Context, platform string) string { + for _, override := range ctx.Config.Archive.FormatOverrides { + if strings.HasPrefix(platform, override.Goos) { + return override.Format + } + } + return ctx.Config.Archive.Format +} diff --git a/internal/archiveformat/format_test.go b/internal/archiveformat/format_test.go new file mode 100644 index 000000000..76396648d --- /dev/null +++ b/internal/archiveformat/format_test.go @@ -0,0 +1,28 @@ +package archiveformat + +import ( + "testing" + + "github.com/goreleaser/goreleaser/config" + "github.com/goreleaser/goreleaser/context" + "github.com/stretchr/testify/assert" +) + +func TestFormatFor(t *testing.T) { + var assert = assert.New(t) + var ctx = &context.Context{ + Config: config.Project{ + Archive: config.Archive{ + Format: "tar.gz", + FormatOverrides: []config.FormatOverride{ + { + Goos: "windows", + Format: "zip", + }, + }, + }, + }, + } + assert.Equal("zip", For(ctx, "windowsamd64")) + assert.Equal("tar.gz", For(ctx, "linux386")) +} diff --git a/pipeline/archive/archive.go b/pipeline/archive/archive.go index b6b4791c5..108b2d68e 100644 --- a/pipeline/archive/archive.go +++ b/pipeline/archive/archive.go @@ -6,11 +6,11 @@ package archive import ( "os" "path/filepath" - "strings" "github.com/apex/log" "github.com/goreleaser/archive" "github.com/goreleaser/goreleaser/context" + "github.com/goreleaser/goreleaser/internal/archiveformat" "github.com/goreleaser/goreleaser/internal/ext" "github.com/mattn/go-zglob" "golang.org/x/sync/errgroup" @@ -39,7 +39,7 @@ func (Pipe) Run(ctx *context.Context) error { func create(ctx *context.Context, platform, name string) error { var folder = filepath.Join(ctx.Config.Dist, name) - var format = formatFor(ctx, platform) + var format = archiveformat.For(ctx, platform) file, err := os.Create(folder + "." + format) if err != nil { return err @@ -76,12 +76,3 @@ func findFiles(ctx *context.Context) (result []string, err error) { } return } - -func formatFor(ctx *context.Context, platform string) string { - for _, override := range ctx.Config.Archive.FormatOverrides { - if strings.HasPrefix(platform, override.Goos) { - return override.Format - } - } - return ctx.Config.Archive.Format -} diff --git a/pipeline/archive/archive_test.go b/pipeline/archive/archive_test.go index fc4797ab2..62ce31e42 100644 --- a/pipeline/archive/archive_test.go +++ b/pipeline/archive/archive_test.go @@ -82,25 +82,6 @@ func TestRunPipeDistRemoved(t *testing.T) { assert.Error(Pipe{}.Run(ctx)) } -func TestFormatFor(t *testing.T) { - var assert = assert.New(t) - var ctx = &context.Context{ - Config: config.Project{ - Archive: config.Archive{ - Format: "tar.gz", - FormatOverrides: []config.FormatOverride{ - { - Goos: "windows", - Format: "zip", - }, - }, - }, - }, - } - assert.Equal("zip", formatFor(ctx, "windowsamd64")) - assert.Equal("tar.gz", formatFor(ctx, "linux386")) -} - func TestRunPipeInvalidGlob(t *testing.T) { var assert = assert.New(t) var ctx = &context.Context{ diff --git a/pipeline/brew/brew.go b/pipeline/brew/brew.go index fb385793e..5fd94b695 100644 --- a/pipeline/brew/brew.go +++ b/pipeline/brew/brew.go @@ -13,6 +13,7 @@ import ( "github.com/goreleaser/goreleaser/checksum" "github.com/goreleaser/goreleaser/config" "github.com/goreleaser/goreleaser/context" + "github.com/goreleaser/goreleaser/internal/archiveformat" "github.com/goreleaser/goreleaser/internal/client" ) @@ -23,7 +24,7 @@ var ErrNoDarwin64Build = errors.New("brew tap requires a darwin amd64 build") const formula = `class {{ .Name }} < Formula desc "{{ .Desc }}" homepage "{{ .Homepage }}" - url "https://github.com/{{ .Repo.Owner }}/{{ .Repo.Name }}/releases/download/{{ .Tag }}/{{ .File }}.{{ .Format }}" + url "https://github.com/{{ .Repo.Owner }}/{{ .Repo.Name }}/releases/download/{{ .Tag }}/{{ .File }}" version "{{ .Version }}" sha256 "{{ .SHA256 }}" @@ -72,7 +73,6 @@ type templateData struct { Binary string Caveats string File string - Format string SHA256 string Plist string Install []string @@ -136,16 +136,12 @@ func doBuildFormula(data templateData) (bytes.Buffer, error) { } func dataFor(ctx *context.Context, client client.Client) (result templateData, err error) { - file := ctx.Archives["darwinamd64"] - if file == "" { + var folder = ctx.Archives["darwinamd64"] + if folder == "" { return result, ErrNoDarwin64Build } - sum, err := checksum.SHA256( - filepath.Join( - ctx.Config.Dist, - file+"."+ctx.Config.Archive.Format, - ), - ) + var file = folder + "." + archiveformat.For(ctx, "darwinamd64") + sum, err := checksum.SHA256(filepath.Join(ctx.Config.Dist, file)) if err != nil { return } @@ -159,7 +155,6 @@ func dataFor(ctx *context.Context, client client.Client) (result templateData, e Binary: ctx.Config.Build.Binary, Caveats: ctx.Config.Brew.Caveats, File: file, - Format: ctx.Config.Archive.Format, // TODO this can be broken by format_overrides SHA256: sum, Dependencies: ctx.Config.Brew.Dependencies, Conflicts: ctx.Config.Brew.Conflicts, diff --git a/pipeline/brew/brew_test.go b/pipeline/brew/brew_test.go index 2da710082..146b099d7 100644 --- a/pipeline/brew/brew_test.go +++ b/pipeline/brew/brew_test.go @@ -39,9 +39,8 @@ var defaultTemplateData = templateData{ }, Tag: "v0.1.3", Version: "0.1.3", - File: "test_Darwin_x86_64", + File: "test_Darwin_x86_64.tar.gz", SHA256: "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68", - Format: "tar.gz", } func assertDefaultTemplateData(t *testing.T, formulae string) { @@ -115,6 +114,69 @@ func TestRunPipe(t *testing.T) { assert.True(client.CreatedFile) } +func TestRunPipeFormatOverride(t *testing.T) { + assert := assert.New(t) + folder, err := ioutil.TempDir("", "goreleasertest") + assert.NoError(err) + _, err = os.Create(filepath.Join(folder, "bin.zip")) + assert.NoError(err) + var ctx = &context.Context{ + Config: config.Project{ + Dist: folder, + Archive: config.Archive{ + Format: "tar.gz", + FormatOverrides: []config.FormatOverride{ + { + Format: "zip", + Goos: "darwin", + }, + }, + }, + Brew: config.Homebrew{ + GitHub: config.Repo{ + Owner: "test", + Name: "test", + }, + }, + }, + Archives: map[string]string{ + "darwinamd64": "bin", + }, + Publish: true, + } + client := &DummyClient{} + assert.NoError(doRun(ctx, client)) + assert.True(client.CreatedFile) + assert.Contains(client.Content, "bin.zip") +} + +func TestRunPipeArchiveDoesntExist(t *testing.T) { + assert := assert.New(t) + folder, err := ioutil.TempDir("", "goreleasertest") + assert.NoError(err) + var ctx = &context.Context{ + Config: config.Project{ + Dist: folder, + Archive: config.Archive{ + Format: "tar.gz", + }, + Brew: config.Homebrew{ + GitHub: config.Repo{ + Owner: "test", + Name: "test", + }, + }, + }, + Archives: map[string]string{ + "darwinamd64": "bin", + }, + Publish: true, + } + client := &DummyClient{} + assert.Error(doRun(ctx, client)) + assert.False(client.CreatedFile) +} + func TestRunPipeNoDarwin64Build(t *testing.T) { assert := assert.New(t) var ctx = &context.Context{ @@ -188,6 +250,7 @@ func TestRunPipeDraftRelease(t *testing.T) { type DummyClient struct { CreatedFile bool + Content string } func (client *DummyClient) CreateRelease(ctx *context.Context, body string) (releaseID int, err error) { @@ -196,6 +259,8 @@ func (client *DummyClient) CreateRelease(ctx *context.Context, body string) (rel func (client *DummyClient) CreateFile(ctx *context.Context, content bytes.Buffer, path string) (err error) { client.CreatedFile = true + bts, _ := ioutil.ReadAll(&content) + client.Content = string(bts) return }