mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-02-05 13:15:26 +02:00
archive format overrides could break brew formula
This commit is contained in:
parent
28cf506e9e
commit
bc64deeb09
19
internal/archiveformat/format.go
Normal file
19
internal/archiveformat/format.go
Normal 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
|
||||
}
|
28
internal/archiveformat/format_test.go
Normal file
28
internal/archiveformat/format_test.go
Normal 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"))
|
||||
}
|
@ -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
|
||||
}
|
||||
|
@ -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{
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user