1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-08 03:31:59 +02:00

Merge pull request #480 from goreleaser/name

fix: re-added .Binary to supported fields
This commit is contained in:
Carlos Alexandro Becker 2017-12-20 10:31:20 -02:00 committed by GitHub
commit 37e377718c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 0 deletions

View File

@ -2,15 +2,22 @@ package nametemplate
import (
"bytes"
"regexp"
"text/template"
"github.com/apex/log"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/artifact"
)
var deprecatedBinary = regexp.MustCompile(`\{\{ ?\.Binary ?\}\}`)
// Apply applies the name template to the given artifact and name
// TODO: this should be refactored alongside with other name template related todos
func Apply(ctx *context.Context, a artifact.Artifact, name string) (string, error) {
if deprecatedBinary.MatchString(ctx.Config.Archive.NameTemplate) {
log.WithField("field", "{{.Binary}}").Warn("you are using a deprecated field on your template, please check the documentation")
}
var out bytes.Buffer
t, err := template.New("archive_name").Parse(ctx.Config.Archive.NameTemplate)
if err != nil {
@ -26,6 +33,7 @@ func Apply(ctx *context.Context, a artifact.Artifact, name string) (string, erro
Version: ctx.Version,
Tag: ctx.Git.CurrentTag,
ProjectName: name,
Binary: name, // TODO: deprecated, remove soon
Env: ctx.Env,
}
err = t.Execute(&out, data)

View File

@ -0,0 +1,83 @@
package nametemplate
import (
"bytes"
"testing"
"github.com/apex/log"
"github.com/apex/log/handlers/cli"
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/tj/assert"
)
func TestNameTemplate(t *testing.T) {
var ctx = context.New(config.Project{
ProjectName: "proj",
Archive: config.Archive{
NameTemplate: "{{.Binary}}_{{.ProjectName}}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}",
Replacements: map[string]string{
"windows": "windao",
},
},
})
s, err := Apply(ctx, artifact.Artifact{
Goos: "windows",
Goarch: "amd64",
Name: "winbin",
}, "bin")
assert.NoError(t, err)
assert.Equal(t, "bin_bin_windao_amd64", s)
s, err = Apply(ctx, artifact.Artifact{
Goos: "darwin",
Goarch: "amd64",
Name: "winbin",
}, "bin")
assert.NoError(t, err)
assert.Equal(t, "bin_bin_darwin_amd64", s)
}
func TestInvalidNameTemplate(t *testing.T) {
var ctx = context.New(config.Project{
ProjectName: "proj",
Archive: config.Archive{
NameTemplate: "{{.Binary}",
},
})
s, err := Apply(ctx, artifact.Artifact{
Goos: "windows",
Goarch: "amd64",
Name: "winbin",
}, "bin")
assert.EqualError(t, err, `template: archive_name:1: unexpected "}" in operand`)
assert.Empty(t, s)
}
func TestDeprecatedFieldOnNameTemplate(t *testing.T) {
for _, temp := range []string{
"{{.Binary}}",
"{{ .Binary}}",
"{{.Binary }}",
"{{ .Binary }}",
} {
t.Run(temp, func(tt *testing.T) {
var out bytes.Buffer
log.SetHandler(cli.New(&out))
var ctx = context.New(config.Project{
ProjectName: "proj",
Archive: config.Archive{
NameTemplate: temp,
},
})
s, err := Apply(ctx, artifact.Artifact{
Goos: "windows",
Goarch: "amd64",
Name: "winbin",
}, "bin")
assert.NoError(tt, err)
assert.Equal(tt, "bin", s)
assert.Contains(tt, out.String(), "you are using a deprecated field on your template")
})
}
}