1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-06 03:13:48 +02:00
goreleaser/internal/tmpl/tmpl_test.go
Carlos Alexandro Becker 64b1f14a86
refactor: better code organization (#757)
* refactor: merging archive in the same repo

* refactor: merging archive in the same repo

* refactor: better organizing packages

* refactor: fixing renames

* fix: new dep version

* fix: makefile

* fix: zip/tar tests

* fix: gitigonore

* fix: s3 tests

* fix: archive test
2018-08-14 23:50:20 -03:00

153 lines
3.5 KiB
Go

package tmpl
import (
"testing"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/assert"
)
func TestWithArtifact(t *testing.T) {
var ctx = context.New(config.Project{
ProjectName: "proj",
})
ctx.Env = map[string]string{
"FOO": "bar",
}
ctx.Version = "1.0.0"
ctx.Git.CurrentTag = "v1.0.0"
for expect, tmpl := range map[string]string{
"bar": "{{.Env.FOO}}",
"Linux": "{{.Os}}",
"amd64": "{{.Arch}}",
"6": "{{.Arm}}",
"1.0.0": "{{.Version}}",
"v1.0.0": "{{.Tag}}",
"binary": "{{.Binary}}",
"proj": "{{.ProjectName}}",
} {
tmpl := tmpl
expect := expect
t.Run(expect, func(tt *testing.T) {
tt.Parallel()
result, err := New(ctx).WithArtifact(
artifact.Artifact{
Name: "not-this-binary",
Goarch: "amd64",
Goos: "linux",
Goarm: "6",
Extra: map[string]string{
"Binary": "binary",
},
},
map[string]string{"linux": "Linux"},
).Apply(tmpl)
assert.NoError(tt, err)
assert.Equal(tt, expect, result)
})
}
t.Run("artifact without binary name", func(tt *testing.T) {
tt.Parallel()
result, err := New(ctx).WithArtifact(
artifact.Artifact{
Name: "another-binary",
Goarch: "amd64",
Goos: "linux",
Goarm: "6",
}, map[string]string{},
).Apply("{{ .Binary }}")
assert.NoError(tt, err)
assert.Equal(tt, ctx.Config.ProjectName, result)
})
t.Run("template using artifact fields with no artifact", func(tt *testing.T) {
tt.Parallel()
result, err := New(ctx).Apply("{{ .Os }}")
assert.EqualError(tt, err, `template: tmpl:1:3: executing "tmpl" at <.Os>: map has no entry for key "Os"`)
assert.Empty(tt, result)
})
}
func TestEnv(t *testing.T) {
testCases := []struct {
desc string
in string
out string
}{
{
desc: "with env",
in: "{{ .Env.FOO }}",
out: "BAR",
},
{
desc: "with env",
in: "{{ .Env.BAR }}",
out: "",
},
}
var ctx = context.New(config.Project{})
ctx.Env = map[string]string{
"FOO": "BAR",
}
ctx.Git.CurrentTag = "v1.2.3"
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
out, _ := New(ctx).Apply(tC.in)
assert.Equal(t, tC.out, out)
})
}
}
func TestFuncMap(t *testing.T) {
var ctx = context.New(config.Project{
ProjectName: "proj",
})
ctx.Git.CurrentTag = "v1.2.4"
for _, tc := range []struct {
Template string
Name string
}{
{
Template: `{{ time "2006-01-02" }}`,
Name: "YYYY-MM-DD",
},
{
Template: `{{ time "01/02/2006" }}`,
Name: "MM/DD/YYYY",
},
{
Template: `{{ time "01/02/2006" }}`,
Name: "MM/DD/YYYY",
},
} {
out, err := New(ctx).Apply(tc.Template)
assert.NoError(t, err)
assert.NotEmpty(t, out)
}
}
func TestInvalidTemplate(t *testing.T) {
_, err := New(context.New(config.Project{})).Apply("{{{.Foo}")
assert.EqualError(t, err, "template: tmpl:1: unexpected \"{\" in command")
}
func TestEnvNotFound(t *testing.T) {
var ctx = context.New(config.Project{})
ctx.Git.CurrentTag = "v1.2.4"
result, err := New(ctx).Apply("{{.Env.FOO}}")
assert.Empty(t, result)
assert.EqualError(t, err, `template: tmpl:1:6: executing "tmpl" at <.Env.FOO>: map has no entry for key "FOO"`)
}
// This should actually never happen...
func TestInvalidSemver(t *testing.T) {
var ctx = context.New(config.Project{})
ctx.Git.CurrentTag = "v1_2_3"
result, err := New(ctx).Apply("{{.Major}}")
assert.Empty(t, result)
assert.EqualError(t, err, `tmpl: Invalid Semantic Version`)
}