1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-17 20:47:50 +02:00

Merge pull request #474 from goreleaser/archive-name

fix: default archive name
This commit is contained in:
Carlos Alexandro Becker 2017-12-18 21:45:26 -02:00 committed by GitHub
commit 12c6633bb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 49 deletions

View File

@ -19,6 +19,8 @@ import (
"github.com/goreleaser/goreleaser/internal/nametemplate"
)
const defaultNameTemplate = "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
// Pipe for archive
type Pipe struct{}
@ -45,7 +47,7 @@ func (Pipe) Run(ctx *context.Context) error {
// Default sets the pipe defaults
func (Pipe) Default(ctx *context.Context) error {
if ctx.Config.Archive.NameTemplate == "" {
ctx.Config.Archive.NameTemplate = "{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
ctx.Config.Archive.NameTemplate = defaultNameTemplate
}
if ctx.Config.Archive.Format == "" {
ctx.Config.Archive.Format = "tar.gz"
@ -109,8 +111,7 @@ func create(ctx *context.Context, artifacts []artifact.Artifact) error {
func skip(ctx *context.Context, artifacts []artifact.Artifact) error {
for _, a := range artifacts {
log.WithField("binary", a.Name).Info("skip archiving")
// TODO: this should not happen here, maybe add another extra field for the extension and/or name without extension?
name, err := nametemplate.Apply(ctx, a, strings.TrimSuffix(a.Name, ".exe"))
name, err := nametemplate.Apply(ctx, a, a.Extra["Binary"])
if err != nil {
return err
}

View File

@ -33,52 +33,61 @@ func TestRunPipe(t *testing.T) {
assert.NoError(t, err)
_, err = os.Create(filepath.Join(folder, "README.md"))
assert.NoError(t, err)
var ctx = context.New(
config.Project{
Dist: dist,
Archive: config.Archive{
NameTemplate: "whatever",
Files: []string{
"README.*",
},
FormatOverrides: []config.FormatOverride{
{
Goos: "windows",
Format: "zip",
for _, format := range []string{"tar.gz", "zip"} {
t.Run("Archive format "+format, func(tt *testing.T) {
var ctx = context.New(
config.Project{
Dist: dist,
ProjectName: "foobar",
Archive: config.Archive{
NameTemplate: defaultNameTemplate,
Files: []string{
"README.*",
},
FormatOverrides: []config.FormatOverride{
{
Goos: "windows",
Format: "zip",
},
},
},
},
},
},
)
ctx.Artifacts.Add(artifact.Artifact{
Goos: "darwin",
Goarch: "amd64",
Name: "mybin",
Path: filepath.Join(dist, "darwinamd64", "mybin"),
Type: artifact.Binary,
Extra: map[string]string{
"Binary": "mybin",
},
})
ctx.Artifacts.Add(artifact.Artifact{
Goos: "windows",
Goarch: "amd64",
Name: "mybin.exe",
Path: filepath.Join(dist, "windowsamd64", "mybin.exe"),
Type: artifact.Binary,
Extra: map[string]string{
"Binary": "mybin",
},
})
for _, format := range []string{"tar.gz", "zip"} {
t.Run("Archive format "+format, func(t *testing.T) {
)
ctx.Artifacts.Add(artifact.Artifact{
Goos: "darwin",
Goarch: "amd64",
Name: "mybin",
Path: filepath.Join(dist, "darwinamd64", "mybin"),
Type: artifact.Binary,
Extra: map[string]string{
"Binary": "mybin",
},
})
ctx.Artifacts.Add(artifact.Artifact{
Goos: "windows",
Goarch: "amd64",
Name: "mybin.exe",
Path: filepath.Join(dist, "windowsamd64", "mybin.exe"),
Type: artifact.Binary,
Extra: map[string]string{
"Binary": "mybin",
"Extension": ".exe",
},
})
ctx.Version = "0.0.1"
ctx.Config.Archive.Format = format
assert.NoError(t, Pipe{}.Run(ctx))
assert.NoError(tt, Pipe{}.Run(ctx))
var archives = ctx.Artifacts.Filter(artifact.ByType(artifact.UploadableArchive))
darwin := archives.Filter(artifact.ByGoos("darwin")).List()[0]
windows := archives.Filter(artifact.ByGoos("windows")).List()[0]
assert.Equal(tt, "foobar_0.0.1_darwin_amd64."+format, darwin.Name)
assert.Equal(tt, "foobar_0.0.1_windows_amd64.zip", windows.Name)
assert.Len(tt, archives.List(), 2)
})
}
// Check archive contents
f, err := os.Open(filepath.Join(dist, "whatever.tar.gz"))
f, err := os.Open(filepath.Join(dist, "foobar_0.0.1_darwin_amd64.tar.gz"))
assert.NoError(t, err)
defer func() { assert.NoError(t, f.Close()) }()
gr, err := gzip.NewReader(f)
@ -111,14 +120,13 @@ func TestRunPipeBinary(t *testing.T) {
var ctx = context.New(
config.Project{
Dist: dist,
Builds: []config.Build{
{Binary: "mybin"},
},
Archive: config.Archive{
Format: "binary",
Format: "binary",
NameTemplate: defaultNameTemplate,
},
},
)
ctx.Version = "0.0.1"
ctx.Artifacts.Add(artifact.Artifact{
Goos: "darwin",
Goarch: "amd64",
@ -137,12 +145,15 @@ func TestRunPipeBinary(t *testing.T) {
Type: artifact.Binary,
Extra: map[string]string{
"Binary": "mybin",
"Ext": ".exe",
},
})
assert.NoError(t, Pipe{}.Run(ctx))
var binaries = ctx.Artifacts.Filter(artifact.ByType(artifact.UploadableBinary))
assert.Len(t, binaries.Filter(artifact.ByGoos("darwin")).List(), 1)
assert.Len(t, binaries.Filter(artifact.ByGoos("windows")).List(), 1)
darwin := binaries.Filter(artifact.ByGoos("darwin")).List()[0]
windows := binaries.Filter(artifact.ByGoos("windows")).List()[0]
assert.Equal(t, "mybin_0.0.1_darwin_amd64", darwin.Name)
assert.Equal(t, "mybin_0.0.1_windows_amd64.exe", windows.Name)
assert.Len(t, binaries.List(), 2)
}
@ -163,7 +174,8 @@ func TestRunPipeDistRemoved(t *testing.T) {
Path: filepath.Join("/path/to/nope", "windowsamd64", "mybin.exe"),
Type: artifact.Binary,
Extra: map[string]string{
"Binary": "mybin",
"Binary": "mybin",
"Extension": ".exe",
},
})
assert.EqualError(t, Pipe{}.Run(ctx), `failed to create directory /path/nope/nope.zip: open /path/nope/nope.zip: no such file or directory`)