1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-04-15 11:56:56 +02:00

archive format overrides could break brew formula

This commit is contained in:
Carlos Alexandro Becker 2017-07-02 12:01:59 -03:00
parent 28cf506e9e
commit bc64deeb09
No known key found for this signature in database
GPG Key ID: E61E2F7DC14AB940
6 changed files with 122 additions and 43 deletions

View File

@ -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
}

View File

@ -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"))
}

View File

@ -6,11 +6,11 @@ package archive
import ( import (
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/apex/log" "github.com/apex/log"
"github.com/goreleaser/archive" "github.com/goreleaser/archive"
"github.com/goreleaser/goreleaser/context" "github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/archiveformat"
"github.com/goreleaser/goreleaser/internal/ext" "github.com/goreleaser/goreleaser/internal/ext"
"github.com/mattn/go-zglob" "github.com/mattn/go-zglob"
"golang.org/x/sync/errgroup" "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 { func create(ctx *context.Context, platform, name string) error {
var folder = filepath.Join(ctx.Config.Dist, name) 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) file, err := os.Create(folder + "." + format)
if err != nil { if err != nil {
return err return err
@ -76,12 +76,3 @@ func findFiles(ctx *context.Context) (result []string, err error) {
} }
return 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
}

View File

@ -82,25 +82,6 @@ func TestRunPipeDistRemoved(t *testing.T) {
assert.Error(Pipe{}.Run(ctx)) 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) { func TestRunPipeInvalidGlob(t *testing.T) {
var assert = assert.New(t) var assert = assert.New(t)
var ctx = &context.Context{ var ctx = &context.Context{

View File

@ -13,6 +13,7 @@ import (
"github.com/goreleaser/goreleaser/checksum" "github.com/goreleaser/goreleaser/checksum"
"github.com/goreleaser/goreleaser/config" "github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context" "github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/archiveformat"
"github.com/goreleaser/goreleaser/internal/client" "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 const formula = `class {{ .Name }} < Formula
desc "{{ .Desc }}" desc "{{ .Desc }}"
homepage "{{ .Homepage }}" 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 }}" version "{{ .Version }}"
sha256 "{{ .SHA256 }}" sha256 "{{ .SHA256 }}"
@ -72,7 +73,6 @@ type templateData struct {
Binary string Binary string
Caveats string Caveats string
File string File string
Format string
SHA256 string SHA256 string
Plist string Plist string
Install []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) { func dataFor(ctx *context.Context, client client.Client) (result templateData, err error) {
file := ctx.Archives["darwinamd64"] var folder = ctx.Archives["darwinamd64"]
if file == "" { if folder == "" {
return result, ErrNoDarwin64Build return result, ErrNoDarwin64Build
} }
sum, err := checksum.SHA256( var file = folder + "." + archiveformat.For(ctx, "darwinamd64")
filepath.Join( sum, err := checksum.SHA256(filepath.Join(ctx.Config.Dist, file))
ctx.Config.Dist,
file+"."+ctx.Config.Archive.Format,
),
)
if err != nil { if err != nil {
return return
} }
@ -159,7 +155,6 @@ func dataFor(ctx *context.Context, client client.Client) (result templateData, e
Binary: ctx.Config.Build.Binary, Binary: ctx.Config.Build.Binary,
Caveats: ctx.Config.Brew.Caveats, Caveats: ctx.Config.Brew.Caveats,
File: file, File: file,
Format: ctx.Config.Archive.Format, // TODO this can be broken by format_overrides
SHA256: sum, SHA256: sum,
Dependencies: ctx.Config.Brew.Dependencies, Dependencies: ctx.Config.Brew.Dependencies,
Conflicts: ctx.Config.Brew.Conflicts, Conflicts: ctx.Config.Brew.Conflicts,

View File

@ -39,9 +39,8 @@ var defaultTemplateData = templateData{
}, },
Tag: "v0.1.3", Tag: "v0.1.3",
Version: "0.1.3", Version: "0.1.3",
File: "test_Darwin_x86_64", File: "test_Darwin_x86_64.tar.gz",
SHA256: "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68", SHA256: "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68",
Format: "tar.gz",
} }
func assertDefaultTemplateData(t *testing.T, formulae string) { func assertDefaultTemplateData(t *testing.T, formulae string) {
@ -115,6 +114,69 @@ func TestRunPipe(t *testing.T) {
assert.True(client.CreatedFile) 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) { func TestRunPipeNoDarwin64Build(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
var ctx = &context.Context{ var ctx = &context.Context{
@ -188,6 +250,7 @@ func TestRunPipeDraftRelease(t *testing.T) {
type DummyClient struct { type DummyClient struct {
CreatedFile bool CreatedFile bool
Content string
} }
func (client *DummyClient) CreateRelease(ctx *context.Context, body string) (releaseID int, err error) { 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) { func (client *DummyClient) CreateFile(ctx *context.Context, content bytes.Buffer, path string) (err error) {
client.CreatedFile = true client.CreatedFile = true
bts, _ := ioutil.ReadAll(&content)
client.Content = string(bts)
return return
} }