2018-07-09 05:47:30 +02:00
|
|
|
package tmpl
|
|
|
|
|
|
|
|
import (
|
2020-05-10 18:03:49 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-02-01 03:36:22 +02:00
|
|
|
"runtime"
|
2018-07-09 05:47:30 +02:00
|
|
|
"testing"
|
2020-07-06 22:12:41 +02:00
|
|
|
"text/template"
|
2018-07-09 05:47:30 +02:00
|
|
|
|
|
|
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
2018-08-15 04:50:20 +02:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/config"
|
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2020-05-10 18:03:49 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-07-09 05:47:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWithArtifact(t *testing.T) {
|
2021-04-25 19:20:49 +02:00
|
|
|
t.Parallel()
|
2021-03-22 13:55:01 +02:00
|
|
|
ctx := context.New(config.Project{
|
2018-07-09 05:47:30 +02:00
|
|
|
ProjectName: "proj",
|
|
|
|
})
|
2021-03-22 13:55:01 +02:00
|
|
|
ctx.ModulePath = "github.com/goreleaser/goreleaser"
|
2018-07-09 05:47:30 +02:00
|
|
|
ctx.Env = map[string]string{
|
2022-02-25 03:57:43 +02:00
|
|
|
"FOO": "bar",
|
|
|
|
"MULTILINE": "something with\nmultiple lines\nremove this\nto test things",
|
2018-07-09 05:47:30 +02:00
|
|
|
}
|
2019-01-19 20:57:58 +02:00
|
|
|
ctx.Version = "1.2.3"
|
2021-11-24 14:12:24 +02:00
|
|
|
ctx.Git.PreviousTag = "v1.2.2"
|
2019-01-19 20:57:58 +02:00
|
|
|
ctx.Git.CurrentTag = "v1.2.3"
|
|
|
|
ctx.Semver = context.Semver{
|
|
|
|
Major: 1,
|
|
|
|
Minor: 2,
|
|
|
|
Patch: 3,
|
|
|
|
}
|
2020-11-18 20:50:31 +02:00
|
|
|
ctx.Git.Branch = "test-branch"
|
2018-10-03 22:44:31 +02:00
|
|
|
ctx.Git.Commit = "commit"
|
|
|
|
ctx.Git.FullCommit = "fullcommit"
|
|
|
|
ctx.Git.ShortCommit = "shortcommit"
|
2021-12-06 21:52:26 +02:00
|
|
|
ctx.Git.TagSubject = "awesome release"
|
|
|
|
ctx.Git.TagContents = "awesome release\n\nanother line"
|
2022-02-25 03:57:51 +02:00
|
|
|
ctx.Git.TagBody = "another line"
|
2021-10-07 19:19:19 +02:00
|
|
|
ctx.ReleaseNotes = "test release notes"
|
2018-07-09 05:47:30 +02:00
|
|
|
for expect, tmpl := range map[string]string{
|
2021-03-22 13:55:01 +02:00
|
|
|
"bar": "{{.Env.FOO}}",
|
|
|
|
"Linux": "{{.Os}}",
|
|
|
|
"amd64": "{{.Arch}}",
|
|
|
|
"6": "{{.Arm}}",
|
|
|
|
"softfloat": "{{.Mips}}",
|
|
|
|
"1.2.3": "{{.Version}}",
|
|
|
|
"v1.2.3": "{{.Tag}}",
|
|
|
|
"1-2-3": "{{.Major}}-{{.Minor}}-{{.Patch}}",
|
|
|
|
"test-branch": "{{.Branch}}",
|
|
|
|
"commit": "{{.Commit}}",
|
|
|
|
"fullcommit": "{{.FullCommit}}",
|
|
|
|
"shortcommit": "{{.ShortCommit}}",
|
|
|
|
"binary": "{{.Binary}}",
|
|
|
|
"proj": "{{.ProjectName}}",
|
|
|
|
"github.com/goreleaser/goreleaser": "{{ .ModulePath }}",
|
2021-07-24 15:13:05 +02:00
|
|
|
"v2.0.0": "{{.Tag | incmajor }}",
|
|
|
|
"2.0.0": "{{.Version | incmajor }}",
|
|
|
|
"v1.3.0": "{{.Tag | incminor }}",
|
|
|
|
"1.3.0": "{{.Version | incminor }}",
|
|
|
|
"v1.2.4": "{{.Tag | incpatch }}",
|
|
|
|
"1.2.4": "{{.Version | incpatch }}",
|
2021-10-07 19:19:19 +02:00
|
|
|
"test release notes": "{{ .ReleaseNotes }}",
|
2021-11-24 14:12:24 +02:00
|
|
|
"v1.2.2": "{{ .PreviousTag }}",
|
2021-12-06 21:52:26 +02:00
|
|
|
"awesome release": "{{ .TagSubject }}",
|
|
|
|
"awesome release\n\nanother line": "{{ .TagContents }}",
|
2022-02-25 03:57:51 +02:00
|
|
|
"another line": "{{ .TagBody }}",
|
2022-02-01 03:36:22 +02:00
|
|
|
"runtime: " + runtime.GOOS: "runtime: {{ .Runtime.Goos }}",
|
|
|
|
"runtime: " + runtime.GOARCH: "runtime: {{ .Runtime.Goarch }}",
|
2022-02-25 03:57:43 +02:00
|
|
|
|
|
|
|
"remove this": "{{ filter .Env.MULTILINE \".*remove.*\" }}",
|
|
|
|
"something with\nmultiple lines\nto test things": "{{ reverseFilter .Env.MULTILINE \".*remove.*\" }}",
|
2018-07-09 05:47:30 +02:00
|
|
|
} {
|
|
|
|
tmpl := tmpl
|
|
|
|
expect := expect
|
2021-01-07 14:15:32 +02:00
|
|
|
t.Run(expect, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-07-09 06:28:26 +02:00
|
|
|
result, err := New(ctx).WithArtifact(
|
2019-08-12 22:44:48 +02:00
|
|
|
&artifact.Artifact{
|
2018-07-09 06:28:26 +02:00
|
|
|
Name: "not-this-binary",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarm: "6",
|
2020-02-06 03:08:18 +02:00
|
|
|
Gomips: "softfloat",
|
2019-01-01 18:40:17 +02:00
|
|
|
Extra: map[string]interface{}{
|
2021-10-17 03:46:11 +02:00
|
|
|
artifact.ExtraBinary: "binary",
|
2018-07-09 06:28:26 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
map[string]string{"linux": "Linux"},
|
|
|
|
).Apply(tmpl)
|
2021-01-07 14:15:32 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expect, result)
|
2018-07-09 05:47:30 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-07 14:15:32 +02:00
|
|
|
t.Run("artifact without binary name", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-07-09 05:47:30 +02:00
|
|
|
result, err := New(ctx).WithArtifact(
|
2019-08-12 22:44:48 +02:00
|
|
|
&artifact.Artifact{
|
2018-07-09 05:47:30 +02:00
|
|
|
Name: "another-binary",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarm: "6",
|
|
|
|
}, map[string]string{},
|
|
|
|
).Apply("{{ .Binary }}")
|
2021-01-07 14:15:32 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, ctx.Config.ProjectName, result)
|
2018-07-09 05:47:30 +02:00
|
|
|
})
|
2018-07-09 07:32:21 +02:00
|
|
|
|
2021-01-07 14:15:32 +02:00
|
|
|
t.Run("template using artifact Fields with no artifact", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-07-09 07:32:21 +02:00
|
|
|
result, err := New(ctx).Apply("{{ .Os }}")
|
2021-01-07 14:15:32 +02:00
|
|
|
require.EqualError(t, err, `template: tmpl:1:3: executing "tmpl" at <.Os>: map has no entry for key "Os"`)
|
|
|
|
require.Empty(t, result)
|
2018-07-09 07:32:21 +02:00
|
|
|
})
|
2018-07-09 05:47:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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: "",
|
|
|
|
},
|
|
|
|
}
|
2021-03-22 13:55:01 +02:00
|
|
|
ctx := context.New(config.Project{})
|
2018-07-09 05:47:30 +02:00
|
|
|
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)
|
2020-10-06 14:48:04 +02:00
|
|
|
require.Equal(t, tC.out, out)
|
2018-07-09 05:47:30 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 14:15:05 +02:00
|
|
|
func TestWithEnv(t *testing.T) {
|
2021-03-22 13:55:01 +02:00
|
|
|
ctx := context.New(config.Project{})
|
2019-04-09 14:15:05 +02:00
|
|
|
ctx.Env = map[string]string{
|
|
|
|
"FOO": "BAR",
|
|
|
|
}
|
|
|
|
ctx.Git.CurrentTag = "v1.2.3"
|
|
|
|
out, err := New(ctx).WithEnvS([]string{
|
|
|
|
"FOO=foo",
|
|
|
|
"BAR=bar",
|
|
|
|
}).Apply("{{ .Env.FOO }}-{{ .Env.BAR }}")
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "foo-bar", out)
|
2019-04-09 14:15:05 +02:00
|
|
|
}
|
|
|
|
|
2018-07-09 05:47:30 +02:00
|
|
|
func TestFuncMap(t *testing.T) {
|
2021-03-22 13:55:01 +02:00
|
|
|
ctx := context.New(config.Project{
|
2018-07-09 05:47:30 +02:00
|
|
|
ProjectName: "proj",
|
2021-05-31 03:53:40 +02:00
|
|
|
Env: []string{
|
|
|
|
"FOO=bar",
|
|
|
|
},
|
2018-07-09 05:47:30 +02:00
|
|
|
})
|
2020-05-10 18:03:49 +02:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-11-03 01:03:23 +02:00
|
|
|
ctx.Git.URL = "https://github.com/foo/bar.git"
|
2021-11-06 21:54:01 +02:00
|
|
|
ctx.ReleaseURL = "https://github.com/foo/bar/releases/tag/v1.0.0"
|
2018-07-09 05:47:30 +02:00
|
|
|
ctx.Git.CurrentTag = "v1.2.4"
|
|
|
|
for _, tc := range []struct {
|
|
|
|
Template string
|
|
|
|
Name string
|
2019-11-07 19:49:36 +02:00
|
|
|
Expected string
|
2018-07-09 05:47:30 +02:00
|
|
|
}{
|
2019-11-07 19:49:36 +02:00
|
|
|
{
|
|
|
|
Template: `{{ replace "v1.24" "v" "" }}`,
|
|
|
|
Name: "replace",
|
|
|
|
Expected: "1.24",
|
|
|
|
},
|
2021-05-31 03:53:40 +02:00
|
|
|
{
|
|
|
|
Template: `{{ if index .Env "SOME_ENV" }}{{ .Env.SOME_ENV }}{{ else }}default value{{ end }}`,
|
|
|
|
Name: "default value",
|
|
|
|
Expected: "default value",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Template: `{{ if index .Env "FOO" }}{{ .Env.FOO }}{{ else }}default value{{ end }}`,
|
|
|
|
Name: "default value set",
|
|
|
|
Expected: "bar",
|
|
|
|
},
|
2018-07-09 05:47:30 +02:00
|
|
|
{
|
|
|
|
Template: `{{ time "2006-01-02" }}`,
|
2019-11-07 19:49:36 +02:00
|
|
|
Name: "time YYYY-MM-DD",
|
2018-07-09 05:47:30 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Template: `{{ time "01/02/2006" }}`,
|
2019-11-07 19:49:36 +02:00
|
|
|
Name: "time MM/DD/YYYY",
|
2018-07-09 05:47:30 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Template: `{{ time "01/02/2006" }}`,
|
2019-11-07 19:49:36 +02:00
|
|
|
Name: "time MM/DD/YYYY",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Template: `{{ tolower "TEST" }}`,
|
|
|
|
Name: "tolower",
|
|
|
|
Expected: "test",
|
|
|
|
},
|
2021-03-17 19:19:32 +02:00
|
|
|
{
|
|
|
|
Template: `{{ trimprefix "v1.2.4" "v" }}`,
|
|
|
|
Name: "trimprefix",
|
|
|
|
Expected: "1.2.4",
|
|
|
|
},
|
2021-11-03 01:03:23 +02:00
|
|
|
{
|
|
|
|
Template: `{{ trimsuffix .GitURL ".git" }}`,
|
|
|
|
Name: "trimsuffix",
|
|
|
|
Expected: "https://github.com/foo/bar",
|
|
|
|
},
|
2021-11-06 21:54:01 +02:00
|
|
|
{
|
|
|
|
Template: `{{ .ReleaseURL }}`,
|
|
|
|
Name: "trimsuffix",
|
|
|
|
Expected: "https://github.com/foo/bar/releases/tag/v1.0.0",
|
|
|
|
},
|
2019-11-07 19:49:36 +02:00
|
|
|
{
|
|
|
|
Template: `{{ toupper "test" }}`,
|
|
|
|
Name: "toupper",
|
|
|
|
Expected: "TEST",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Template: `{{ trim " test " }}`,
|
|
|
|
Name: "trim",
|
|
|
|
Expected: "test",
|
2018-07-09 05:47:30 +02:00
|
|
|
},
|
2020-05-10 18:03:49 +02:00
|
|
|
{
|
|
|
|
Template: `{{ abs "file" }}`,
|
|
|
|
Name: "abs",
|
|
|
|
Expected: filepath.Join(wd, "file"),
|
|
|
|
},
|
2018-07-09 05:47:30 +02:00
|
|
|
} {
|
|
|
|
out, err := New(ctx).Apply(tc.Template)
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, err)
|
2019-11-07 19:49:36 +02:00
|
|
|
if tc.Expected != "" {
|
2020-10-06 14:48:04 +02:00
|
|
|
require.Equal(t, tc.Expected, out)
|
2019-11-07 19:49:36 +02:00
|
|
|
} else {
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NotEmpty(t, out)
|
2019-11-07 19:49:36 +02:00
|
|
|
}
|
2018-07-09 05:47:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 22:12:41 +02:00
|
|
|
func TestApplySingleEnvOnly(t *testing.T) {
|
|
|
|
ctx := context.New(config.Project{
|
|
|
|
Env: []string{
|
|
|
|
"FOO=value",
|
|
|
|
"BAR=another",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
tpl string
|
|
|
|
expectedErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"empty tpl",
|
|
|
|
"",
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"whitespaces",
|
|
|
|
" ",
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"plain-text only",
|
|
|
|
"raw-token",
|
|
|
|
ExpectedSingleEnvErr{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"variable with spaces",
|
|
|
|
"{{ .Env.FOO }}",
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"variable without spaces",
|
|
|
|
"{{.Env.FOO}}",
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"variable with outer spaces",
|
|
|
|
" {{ .Env.FOO }} ",
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"unknown variable",
|
|
|
|
"{{ .Env.UNKNOWN }}",
|
|
|
|
template.ExecError{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"other interpolation",
|
|
|
|
"{{ .ProjectName }}",
|
|
|
|
ExpectedSingleEnvErr{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
_, err := New(ctx).ApplySingleEnvOnly(tc.tpl)
|
|
|
|
if tc.expectedErr != nil {
|
2020-10-06 14:48:04 +02:00
|
|
|
require.Error(t, err)
|
2020-07-06 22:12:41 +02:00
|
|
|
} else {
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, err)
|
2020-07-06 22:12:41 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-09 05:47:30 +02:00
|
|
|
func TestInvalidTemplate(t *testing.T) {
|
2019-01-19 20:57:58 +02:00
|
|
|
ctx := context.New(config.Project{})
|
|
|
|
ctx.Git.CurrentTag = "v1.1.1"
|
|
|
|
_, err := New(ctx).Apply("{{{.Foo}")
|
2020-10-06 14:48:04 +02:00
|
|
|
require.EqualError(t, err, "template: tmpl:1: unexpected \"{\" in command")
|
2018-07-09 05:47:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvNotFound(t *testing.T) {
|
2021-03-22 13:55:01 +02:00
|
|
|
ctx := context.New(config.Project{})
|
2018-07-09 05:47:30 +02:00
|
|
|
ctx.Git.CurrentTag = "v1.2.4"
|
|
|
|
result, err := New(ctx).Apply("{{.Env.FOO}}")
|
2020-10-06 14:48:04 +02:00
|
|
|
require.Empty(t, result)
|
|
|
|
require.EqualError(t, err, `template: tmpl:1:6: executing "tmpl" at <.Env.FOO>: map has no entry for key "FOO"`)
|
2018-07-09 05:47:30 +02:00
|
|
|
}
|
2020-03-22 18:54:47 +02:00
|
|
|
|
|
|
|
func TestWithExtraFields(t *testing.T) {
|
2021-03-22 13:55:01 +02:00
|
|
|
ctx := context.New(config.Project{})
|
2020-03-22 18:54:47 +02:00
|
|
|
out, _ := New(ctx).WithExtraFields(Fields{
|
|
|
|
"MyCustomField": "foo",
|
|
|
|
}).Apply("{{ .MyCustomField }}")
|
2020-10-06 14:48:04 +02:00
|
|
|
require.Equal(t, "foo", out)
|
2020-03-22 18:54:47 +02:00
|
|
|
}
|