1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2024-12-31 01:53:50 +02:00

feat: improve template error handling (#4256)

- wrap the multiple errors in an internal error with cleaner messaging
- improve testlib.RequireTemplateError and several tests
This commit is contained in:
Carlos Alexandro Becker 2023-08-24 22:06:12 -03:00 committed by GitHub
parent 53bcbe951a
commit c91c4b7cd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
32 changed files with 254 additions and 102 deletions

View File

@ -7,11 +7,11 @@ import (
"os"
"strings"
"testing"
"text/template"
"code.gitea.io/sdk/gitea"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/jarcoal/httpmock"
@ -102,7 +102,7 @@ func (s *GetInstanceURLSuite) TestTemplateMissingValue() {
})
result, err := getInstanceURL(ctx)
require.ErrorAs(t, err, &template.ExecError{})
require.ErrorAs(t, err, &tmpl.Error{})
require.Empty(t, result)
}

View File

@ -16,6 +16,7 @@ import (
"github.com/google/go-github/v54/github"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require"
)
@ -197,7 +198,7 @@ func TestGitHubCreateReleaseWrongNameTemplate(t *testing.T) {
str, err := client.CreateRelease(ctx, "")
require.Empty(t, str)
require.EqualError(t, err, `template: tmpl:1: unclosed action`)
testlib.RequireTemplateError(t, err)
}
func TestGitHubGetDefaultBranch(t *testing.T) {

View File

@ -10,6 +10,7 @@ import (
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require"
)
@ -88,9 +89,10 @@ func TestExecute(t *testing.T) {
}
testCases := []struct {
name string
publishers []config.Publisher
expectErr error
name string
publishers []config.Publisher
expectErr error
expectErrAs any
}{
{
"filter by IDs",
@ -109,6 +111,7 @@ func TestExecute(t *testing.T) {
},
},
nil,
nil,
},
{
"no filter",
@ -131,6 +134,7 @@ func TestExecute(t *testing.T) {
},
},
nil,
nil,
},
{
"disabled",
@ -143,6 +147,7 @@ func TestExecute(t *testing.T) {
},
},
pipe.ErrSkip{},
nil,
},
{
"disabled invalid tmpl",
@ -154,7 +159,8 @@ func TestExecute(t *testing.T) {
Env: []string{},
},
},
fmt.Errorf(`template: tmpl:1:3: executing "tmpl" at <.NOPE>`),
nil,
&tmpl.Error{},
},
{
"include checksum",
@ -178,6 +184,7 @@ func TestExecute(t *testing.T) {
},
},
nil,
nil,
},
{
"include signatures",
@ -202,6 +209,7 @@ func TestExecute(t *testing.T) {
},
},
nil,
nil,
},
{
"docker",
@ -221,6 +229,7 @@ func TestExecute(t *testing.T) {
},
},
nil,
nil,
},
{
"extra files",
@ -246,6 +255,7 @@ func TestExecute(t *testing.T) {
},
},
nil,
nil,
},
{
"extra files with rename",
@ -274,6 +284,7 @@ func TestExecute(t *testing.T) {
},
},
nil,
nil,
},
{
"try dir templating",
@ -294,6 +305,7 @@ func TestExecute(t *testing.T) {
},
},
nil,
nil,
},
{
"check env templating",
@ -321,6 +333,7 @@ func TestExecute(t *testing.T) {
},
},
nil,
nil,
},
{
"override path",
@ -346,6 +359,7 @@ func TestExecute(t *testing.T) {
},
},
nil,
nil,
},
{
"command error",
@ -373,18 +387,23 @@ func TestExecute(t *testing.T) {
},
// stderr is sent to output via logger
fmt.Errorf(`publishing: %s failed: exit status 1: test error`, MockCmd),
nil,
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("%d-%s", i, tc.name), func(t *testing.T) {
err := Execute(ctx, tc.publishers)
if tc.expectErr == nil {
require.NoError(t, err)
if tc.expectErr != nil {
require.Error(t, err)
require.True(t, strings.HasPrefix(err.Error(), tc.expectErr.Error()), err.Error())
return
}
require.Error(t, err)
require.True(t, strings.HasPrefix(err.Error(), tc.expectErr.Error()), err.Error())
if tc.expectErrAs != nil {
require.ErrorAs(t, err, tc.expectErrAs)
return
}
require.NoError(t, err)
})
}
}

View File

@ -4,6 +4,7 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require"
)
@ -30,7 +31,7 @@ func TestBadTemplate(t *testing.T) {
ctx := testctx.New()
files, err := Find(ctx, globs)
require.Empty(t, files)
require.EqualError(t, err, `failed to apply template to glob "./testdata/file{{ .Env.NOPE }}.golden": template: tmpl:1:22: executing "tmpl" at <.Env.NOPE>: map has no entry for key "NOPE"`)
testlib.RequireTemplateError(t, err)
}
func TestShouldGetSpecificFile(t *testing.T) {
@ -123,7 +124,7 @@ func TestTargetInvalidNameTemplate(t *testing.T) {
ctx := testctx.New()
files, err := Find(ctx, globs)
require.Empty(t, files)
require.EqualError(t, err, `failed to apply template to name "file1_{{.Env.HONK}}.golden": template: tmpl:1:12: executing "tmpl" at <.Env.HONK>: map has no entry for key "HONK"`)
testlib.RequireTemplateError(t, err)
}
func TestTargetNameMatchesMultipleFiles(t *testing.T) {

View File

@ -51,7 +51,7 @@ func TestMeta(t *testing.T) {
})
require.NoError(t, Pipe{}.Default(ctx))
require.EqualError(t, Pipe{}.Run(ctx), `template: tmpl:1:5: executing "tmpl" at <.Os>: map has no entry for key "Os"`)
testlib.RequireTemplateError(t, Pipe{}.Run(ctx))
})
t.Run("no files", func(t *testing.T) {

View File

@ -655,7 +655,7 @@ func TestRunPipeInvalidWrapInDirectoryTemplate(t *testing.T) {
artifact.ExtraID: "default",
},
})
require.EqualError(t, Pipe{}.Run(ctx), `template: tmpl:1: unexpected "}" in operand`)
testlib.RequireTemplateError(t, Pipe{}.Run(ctx))
}
func TestRunPipeWrap(t *testing.T) {

View File

@ -150,11 +150,12 @@ func TestSrcInfoSimple(t *testing.T) {
func TestFullPipe(t *testing.T) {
type testcase struct {
prepare func(ctx *context.Context)
expectedRunError string
expectedPublishError string
expectedPublishErrorIs error
expectedErrorCheck func(testing.TB, error)
prepare func(ctx *context.Context)
expectedRunError string
expectedRunErrorCheck func(testing.TB, error)
expectedPublishError string
expectedPublishErrorIs error
expectedPublishErrorCheck func(testing.TB, error)
}
for name, tt := range map[string]testcase{
"default": {
@ -185,25 +186,25 @@ func TestFullPipe(t *testing.T) {
prepare: func(ctx *context.Context) {
ctx.Config.AURs[0].Name = "{{ .Asdsa }"
},
expectedRunError: `template: tmpl:1: unexpected "}" in operand`,
expectedRunErrorCheck: testlib.RequireTemplateError,
},
"invalid-package-template": {
prepare: func(ctx *context.Context) {
ctx.Config.AURs[0].Package = "{{ .Asdsa }"
},
expectedRunError: `template: tmpl:1: unexpected "}" in operand`,
expectedRunErrorCheck: testlib.RequireTemplateError,
},
"invalid-commit-template": {
prepare: func(ctx *context.Context) {
ctx.Config.AURs[0].CommitMessageTemplate = "{{ .Asdsa }"
},
expectedErrorCheck: testlib.RequireTemplateError,
expectedPublishErrorCheck: testlib.RequireTemplateError,
},
"invalid-key-template": {
prepare: func(ctx *context.Context) {
ctx.Config.AURs[0].PrivateKey = "{{ .Asdsa }"
},
expectedErrorCheck: testlib.RequireTemplateError,
expectedPublishErrorCheck: testlib.RequireTemplateError,
},
"no-key": {
prepare: func(ctx *context.Context) {
@ -221,7 +222,7 @@ func TestFullPipe(t *testing.T) {
prepare: func(ctx *context.Context) {
ctx.Config.AURs[0].GitURL = "{{ .Asdsa }"
},
expectedErrorCheck: testlib.RequireTemplateError,
expectedPublishErrorCheck: testlib.RequireTemplateError,
},
"no-git-url": {
prepare: func(ctx *context.Context) {
@ -233,13 +234,13 @@ func TestFullPipe(t *testing.T) {
prepare: func(ctx *context.Context) {
ctx.Config.AURs[0].GitSSHCommand = "{{ .Asdsa }"
},
expectedErrorCheck: testlib.RequireTemplateError,
expectedPublishErrorCheck: testlib.RequireTemplateError,
},
"invalid-commit-author-template": {
prepare: func(ctx *context.Context) {
ctx.Config.AURs[0].CommitAuthor.Name = "{{ .Asdsa }"
},
expectedErrorCheck: testlib.RequireTemplateError,
expectedPublishErrorCheck: testlib.RequireTemplateError,
},
} {
t.Run(name, func(t *testing.T) {
@ -321,6 +322,10 @@ func TestFullPipe(t *testing.T) {
require.EqualError(t, runAll(ctx, client), tt.expectedRunError)
return
}
if tt.expectedRunErrorCheck != nil {
tt.expectedRunErrorCheck(t, runAll(ctx, client))
return
}
require.NoError(t, runAll(ctx, client))
if tt.expectedPublishError != "" {
@ -333,8 +338,8 @@ func TestFullPipe(t *testing.T) {
return
}
if tt.expectedErrorCheck != nil {
tt.expectedErrorCheck(t, Pipe{}.Publish(ctx))
if tt.expectedPublishErrorCheck != nil {
tt.expectedPublishErrorCheck(t, Pipe{}.Publish(ctx))
return
}

View File

@ -7,6 +7,7 @@ import (
"github.com/caarlos0/log"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require"
)
@ -83,13 +84,13 @@ func TestRunWithEnv(t *testing.T) {
}
func TestInvalidTemplate(t *testing.T) {
require.EqualError(t, Pipe{}.Run(testctx.NewWithCfg(
testlib.RequireTemplateError(t, Pipe{}.Run(testctx.NewWithCfg(
config.Project{
Before: config.Before{
Hooks: []string{"touch {{ .fasdsd }"},
},
},
)), `template: tmpl:1: unexpected "}" in operand`)
)))
}
func TestSkip(t *testing.T) {

View File

@ -12,6 +12,7 @@ import (
"github.com/goreleaser/goreleaser/internal/golden"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
@ -161,9 +162,11 @@ func TestSplit(t *testing.T) {
func TestFullPipe(t *testing.T) {
type testcase struct {
prepare func(ctx *context.Context)
expectedRunError string
expectedPublishError string
prepare func(ctx *context.Context)
expectedRunError string
expectedRunErrorAs any
expectedPublishError string
expectedPublishErrorAs any
}
for name, tt := range map[string]testcase{
"default": {
@ -247,7 +250,7 @@ func TestFullPipe(t *testing.T) {
ctx.Config.Brews[0].Repository.Name = "test"
ctx.Config.Brews[0].CommitMessageTemplate = "{{ .Asdsa }"
},
expectedPublishError: `template: tmpl:1: unexpected "}" in operand`,
expectedPublishErrorAs: &tmpl.Error{},
},
"valid_repository_templates": {
prepare: func(ctx *context.Context) {
@ -264,14 +267,14 @@ func TestFullPipe(t *testing.T) {
ctx.Config.Brews[0].Repository.Owner = "test"
ctx.Config.Brews[0].Repository.Name = "{{ .Asdsa }"
},
expectedRunError: `template: tmpl:1: unexpected "}" in operand`,
expectedRunErrorAs: &tmpl.Error{},
},
"invalid_repository_owner_template": {
prepare: func(ctx *context.Context) {
ctx.Config.Brews[0].Repository.Owner = "{{ .Asdsa }"
ctx.Config.Brews[0].Repository.Name = "test"
},
expectedRunError: `template: tmpl:1: unexpected "}" in operand`,
expectedRunErrorAs: &tmpl.Error{},
},
"invalid_repository_skip_upload_template": {
prepare: func(ctx *context.Context) {
@ -279,7 +282,7 @@ func TestFullPipe(t *testing.T) {
ctx.Config.Brews[0].Repository.Owner = "test"
ctx.Config.Brews[0].Repository.Name = "test"
},
expectedRunError: `template: tmpl:1: unexpected "}" in operand`,
expectedRunErrorAs: &tmpl.Error{},
},
"invalid_install_template": {
prepare: func(ctx *context.Context) {
@ -287,7 +290,7 @@ func TestFullPipe(t *testing.T) {
ctx.Config.Brews[0].Repository.Name = "test"
ctx.Config.Brews[0].Install = "{{ .aaaa }"
},
expectedRunError: `template: tmpl:1: unexpected "}" in operand`,
expectedRunErrorAs: &tmpl.Error{},
},
} {
t.Run(name, func(t *testing.T) {
@ -381,18 +384,27 @@ func TestFullPipe(t *testing.T) {
require.NoError(t, Pipe{}.Default(ctx))
if tt.expectedRunError == "" {
require.NoError(t, runAll(ctx, client))
} else {
require.EqualError(t, runAll(ctx, client), tt.expectedRunError)
err = runAll(ctx, client)
if tt.expectedRunError != "" {
require.EqualError(t, err, tt.expectedRunError)
return
}
if tt.expectedPublishError != "" {
require.EqualError(t, publishAll(ctx, client), tt.expectedPublishError)
if tt.expectedRunErrorAs != nil {
require.ErrorAs(t, err, tt.expectedRunErrorAs)
return
}
require.NoError(t, err)
require.NoError(t, publishAll(ctx, client))
err = publishAll(ctx, client)
if tt.expectedPublishError != "" {
require.EqualError(t, err, tt.expectedPublishError)
return
}
if tt.expectedPublishErrorAs != nil {
require.ErrorAs(t, err, tt.expectedPublishErrorAs)
return
}
require.NoError(t, err)
content := []byte(client.Content)
if url := ctx.Config.Brews[0].Repository.Git.URL; url == "" {

View File

@ -562,7 +562,7 @@ func TestPipeOnBuild_invalidBinaryTpl(t *testing.T) {
})
g := semerrgroup.New(ctx.Parallelism)
runPipeOnBuild(ctx, g, build)
require.EqualError(t, g.Wait(), `template: tmpl:1:11: executing "tmpl" at <.XYZ>: map has no entry for key "XYZ"`)
testlib.RequireTemplateError(t, g.Wait())
}
func TestBuildOptionsForTarget(t *testing.T) {

View File

@ -9,6 +9,7 @@ import (
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require"
)
@ -159,9 +160,9 @@ func TestPipeInvalidNameTemplate(t *testing.T) {
_, err = binFile.WriteString("fake artifact")
require.NoError(t, err)
for template, eerr := range map[string]string{
"{{ .Pro }_checksums.txt": `template: tmpl:1: unexpected "}" in operand`,
"{{.Env.NOPE}}": `template: tmpl:1:6: executing "tmpl" at <.Env.NOPE>: map has no entry for key "NOPE"`,
for _, template := range []string{
"{{ .Pro }_checksums.txt",
"{{.Env.NOPE}}",
} {
t.Run(template, func(t *testing.T) {
folder := t.TempDir()
@ -181,9 +182,7 @@ func TestPipeInvalidNameTemplate(t *testing.T) {
Type: artifact.UploadableBinary,
Path: binFile.Name(),
})
err = Pipe{}.Run(ctx)
require.Error(t, err)
require.Equal(t, eerr, err.Error())
testlib.RequireTemplateError(t, Pipe{}.Run(ctx))
})
}
}

View File

@ -4,6 +4,7 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require"
)
@ -26,7 +27,7 @@ func TestAnnounceInvalidTemplate(t *testing.T) {
},
},
})
require.EqualError(t, Pipe{}.Announce(ctx), `discord: template: tmpl:1: unexpected "}" in operand`)
testlib.RequireTemplateError(t, Pipe{}.Announce(ctx))
}
func TestAnnounceMissingEnv(t *testing.T) {

View File

@ -78,7 +78,7 @@ func TestSetDefaultTokenFiles(t *testing.T) {
"FOO={{ .Asss }",
},
})
require.EqualError(t, Pipe{}.Run(ctx), `template: tmpl:1: unexpected "}" in operand`)
testlib.RequireTemplateError(t, Pipe{}.Run(ctx))
})
t.Run("no token", func(t *testing.T) {

View File

@ -15,6 +15,7 @@ import (
"github.com/goreleaser/goreleaser/internal/golden"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
@ -134,9 +135,11 @@ func TestSimple(t *testing.T) {
func TestFullPipe(t *testing.T) {
type testcase struct {
prepare func(ctx *context.Context)
expectedRunError string
expectedPublishError string
prepare func(ctx *context.Context)
expectedRunError string
expectedRunErrorAs any
expectedPublishError string
expectedPublishErrorAs any
}
for name, tt := range map[string]testcase{
"default": {
@ -175,7 +178,7 @@ func TestFullPipe(t *testing.T) {
ctx.Config.Krews[0].Repository.Name = "test"
ctx.Config.Krews[0].CommitMessageTemplate = "{{ .Asdsa }"
},
expectedPublishError: `template: tmpl:1: unexpected "}" in operand`,
expectedPublishErrorAs: &tmpl.Error{},
},
"invalid desc": {
prepare: func(ctx *context.Context) {
@ -183,7 +186,7 @@ func TestFullPipe(t *testing.T) {
ctx.Config.Krews[0].Repository.Name = "test"
ctx.Config.Krews[0].Description = "{{ .Asdsa }"
},
expectedRunError: `failed to apply template: {{ .Asdsa }: template: tmpl:1: unexpected "}" in operand`,
expectedRunErrorAs: &tmpl.Error{},
},
"invalid short desc": {
prepare: func(ctx *context.Context) {
@ -191,7 +194,7 @@ func TestFullPipe(t *testing.T) {
ctx.Config.Krews[0].Repository.Name = "test"
ctx.Config.Krews[0].ShortDescription = "{{ .Asdsa }"
},
expectedRunError: `failed to apply template: {{ .Asdsa }: template: tmpl:1: unexpected "}" in operand`,
expectedRunErrorAs: &tmpl.Error{},
},
"invalid homepage": {
prepare: func(ctx *context.Context) {
@ -199,7 +202,7 @@ func TestFullPipe(t *testing.T) {
ctx.Config.Krews[0].Repository.Name = "test"
ctx.Config.Krews[0].Homepage = "{{ .Asdsa }"
},
expectedRunError: `failed to apply template: {{ .Asdsa }: template: tmpl:1: unexpected "}" in operand`,
expectedRunErrorAs: &tmpl.Error{},
},
"invalid name": {
prepare: func(ctx *context.Context) {
@ -207,7 +210,7 @@ func TestFullPipe(t *testing.T) {
ctx.Config.Krews[0].Repository.Name = "test"
ctx.Config.Krews[0].Name = "{{ .Asdsa }"
},
expectedRunError: `failed to apply template: {{ .Asdsa }: template: tmpl:1: unexpected "}" in operand`,
expectedRunErrorAs: &tmpl.Error{},
},
"invalid caveats": {
prepare: func(ctx *context.Context) {
@ -215,7 +218,7 @@ func TestFullPipe(t *testing.T) {
ctx.Config.Krews[0].Repository.Name = "test"
ctx.Config.Krews[0].Caveats = "{{ .Asdsa }"
},
expectedRunError: `failed to apply template: {{ .Asdsa }: template: tmpl:1: unexpected "}" in operand`,
expectedRunErrorAs: &tmpl.Error{},
},
"no short desc": {
prepare: func(ctx *context.Context) {
@ -304,11 +307,16 @@ func TestFullPipe(t *testing.T) {
distFile := filepath.Join(folder, "krew", name+".yaml")
require.NoError(t, Pipe{}.Default(ctx))
err = runAll(ctx, client)
if tt.expectedRunError != "" {
require.EqualError(t, err, tt.expectedRunError)
return
}
if tt.expectedRunErrorAs != nil {
require.ErrorAs(t, err, tt.expectedRunErrorAs)
return
}
require.NoError(t, err)
err = publishAll(ctx, client)
@ -316,7 +324,10 @@ func TestFullPipe(t *testing.T) {
require.EqualError(t, err, tt.expectedPublishError)
return
}
if tt.expectedPublishErrorAs != nil {
require.ErrorAs(t, err, tt.expectedPublishErrorAs)
return
}
require.NoError(t, err)
content := []byte(client.Content)

View File

@ -4,6 +4,7 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require"
)
@ -33,7 +34,7 @@ func TestAnnounceInvalidTemplate(t *testing.T) {
},
},
})
require.EqualError(t, Pipe{}.Announce(ctx), `linkedin: template: tmpl:1: unexpected "}" in operand`)
testlib.RequireTemplateError(t, Pipe{}.Announce(ctx))
}
func TestAnnounceMissingEnv(t *testing.T) {

View File

@ -4,6 +4,7 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require"
)
@ -26,7 +27,7 @@ func TestAnnounceInvalidTemplate(t *testing.T) {
},
},
})
require.EqualError(t, Pipe{}.Announce(ctx), `mastodon: template: tmpl:1: unexpected "}" in operand`)
testlib.RequireTemplateError(t, Pipe{}.Announce(ctx))
}
func TestAnnounceMissingEnv(t *testing.T) {

View File

@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
)
@ -31,7 +32,7 @@ func TestAnnounceInvalidTemplate(t *testing.T) {
},
},
})
require.EqualError(t, Pipe{}.Announce(ctx), `mattermost: template: tmpl:1: unexpected "}" in operand`)
testlib.RequireTemplateError(t, Pipe{}.Announce(ctx))
}
func TestAnnounceMissingEnv(t *testing.T) {

View File

@ -4,6 +4,7 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require"
)
@ -27,7 +28,7 @@ func TestAnnounceInvalidTemplate(t *testing.T) {
},
},
})
require.EqualError(t, Pipe{}.Announce(ctx), `opencollective: template: tmpl:1: unexpected "}" in operand`)
testlib.RequireTemplateError(t, Pipe{}.Announce(ctx))
}
func TestAnnounceMissingEnv(t *testing.T) {

View File

@ -3,6 +3,7 @@ package prebuild
import (
"testing"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
@ -31,7 +32,7 @@ func TestRun(t *testing.T) {
ctx := context.New(config.Project{
Builds: []config.Build{{Main: "{{ .Env.FOO }}"}},
})
require.EqualError(t, Pipe{}.Run(ctx), `template: tmpl:1:7: executing "tmpl" at <.Env.FOO>: map has no entry for key "FOO"`)
testlib.RequireTemplateError(t, Pipe{}.Run(ctx))
})
}

View File

@ -4,6 +4,7 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require"
)
@ -26,7 +27,7 @@ func TestAnnounceInvalidURLTemplate(t *testing.T) {
},
},
})
require.EqualError(t, Pipe{}.Announce(ctx), `reddit: template: tmpl:1: unexpected "}" in operand`)
testlib.RequireTemplateError(t, Pipe{}.Announce(ctx))
}
func TestAnnounceInvalidTitleTemplate(t *testing.T) {
@ -37,7 +38,7 @@ func TestAnnounceInvalidTitleTemplate(t *testing.T) {
},
},
})
require.EqualError(t, Pipe{}.Announce(ctx), `reddit: template: tmpl:1: unexpected "}" in operand`)
testlib.RequireTemplateError(t, Pipe{}.Announce(ctx))
}
func TestAnnounceMissingEnv(t *testing.T) {

View File

@ -8,6 +8,7 @@ import (
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/golden"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
@ -85,7 +86,7 @@ func TestDescribeBodyWithInvalidHeaderTemplate(t *testing.T) {
},
})
_, err := describeBody(ctx)
require.EqualError(t, err, `template: tmpl:1: unexpected "}" in operand`)
testlib.RequireTemplateError(t, err)
}
func TestDescribeBodyWithInvalidFooterTemplate(t *testing.T) {
@ -95,5 +96,5 @@ func TestDescribeBodyWithInvalidFooterTemplate(t *testing.T) {
},
})
_, err := describeBody(ctx)
require.EqualError(t, err, `template: tmpl:1: unexpected "}" in operand`)
testlib.RequireTemplateError(t, err)
}

View File

@ -11,6 +11,7 @@ import (
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/assert"
@ -221,6 +222,7 @@ func TestSBOMCatalogArtifacts(t *testing.T) {
ctx *context.Context
sbomPaths []string
sbomNames []string
expectedErrAs any
expectedErrMsg string
}{
{
@ -237,8 +239,8 @@ func TestSBOMCatalogArtifacts(t *testing.T) {
}),
},
{
desc: "invalid args template",
expectedErrMsg: `cataloging artifacts failed: arg "${FOO}-{{ .foo }{{}}{": invalid template: template: tmpl:1: unexpected "}" in operand`,
desc: "invalid args template",
expectedErrAs: &tmpl.Error{},
ctx: testctx.NewWithCfg(config.Project{
SBOMs: []config.SBOM{
{
@ -415,12 +417,25 @@ func TestSBOMCatalogArtifacts(t *testing.T) {
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
testSBOMCataloging(t, test.ctx, test.sbomPaths, test.sbomNames, test.expectedErrMsg)
testSBOMCataloging(
t,
test.ctx,
test.sbomPaths,
test.sbomNames,
test.expectedErrAs,
test.expectedErrMsg,
)
})
}
}
func testSBOMCataloging(tb testing.TB, ctx *context.Context, sbomPaths, sbomNames []string, expectedErrMsg string) {
func testSBOMCataloging(
tb testing.TB,
ctx *context.Context,
sbomPaths, sbomNames []string,
expectedErrAs any,
expectedErrMsg string,
) {
tb.Helper()
testlib.CheckPath(tb, "syft")
tmpdir := tb.TempDir()
@ -501,6 +516,10 @@ func testSBOMCataloging(tb testing.TB, ctx *context.Context, sbomPaths, sbomName
require.Contains(tb, err.Error(), expectedErrMsg)
return
}
if expectedErrAs != nil {
require.ErrorAs(tb, Pipe{}.Run(ctx), expectedErrAs)
return
}
require.NoError(tb, Pipe{}.Run(ctx))

View File

@ -18,6 +18,7 @@ import (
"github.com/goreleaser/goreleaser/internal/git"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
@ -104,6 +105,7 @@ func TestSignArtifacts(t *testing.T) {
certificateNames []string
expectedErrMsg string
expectedErrIs error
expectedErrAs any
user string
}{
{
@ -132,8 +134,8 @@ func TestSignArtifacts(t *testing.T) {
}),
},
{
desc: "invalid certificate template",
expectedErrMsg: `sign failed: artifact1: template: tmpl:1:3: executing "tmpl" at <.blah>: map has no entry for key "blah"`,
desc: "invalid certificate template",
expectedErrAs: &tmpl.Error{},
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
@ -145,8 +147,8 @@ func TestSignArtifacts(t *testing.T) {
}),
},
{
desc: "invalid signature template",
expectedErrMsg: `sign failed: artifact1: template: tmpl:1:3: executing "tmpl" at <.blah>: map has no entry for key "blah"`,
desc: "invalid signature template",
expectedErrAs: &tmpl.Error{},
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
@ -158,8 +160,8 @@ func TestSignArtifacts(t *testing.T) {
}),
},
{
desc: "invalid args template",
expectedErrMsg: `sign failed: artifact1: template: tmpl:1: unexpected "}" in operand`,
desc: "invalid args template",
expectedErrAs: &tmpl.Error{},
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
@ -174,8 +176,8 @@ func TestSignArtifacts(t *testing.T) {
}),
},
{
desc: "invalid env template",
expectedErrMsg: `sign failed: artifact1: template: tmpl:1:5: executing "tmpl" at <.blah>: map has no entry for key "blah"`,
desc: "invalid env template",
expectedErrAs: &tmpl.Error{},
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
@ -524,12 +526,29 @@ func TestSignArtifacts(t *testing.T) {
}
t.Run(test.desc, func(t *testing.T) {
testSign(t, test.ctx, test.certificateNames, test.signaturePaths, test.signatureNames, test.user, test.expectedErrMsg, test.expectedErrIs)
testSign(
t,
test.ctx,
test.certificateNames,
test.signaturePaths,
test.signatureNames,
test.user,
test.expectedErrMsg,
test.expectedErrIs,
test.expectedErrAs,
)
})
}
}
func testSign(tb testing.TB, ctx *context.Context, certificateNames, signaturePaths, signatureNames []string, user, expectedErrMsg string, expectedErrIs error) {
func testSign(
tb testing.TB,
ctx *context.Context,
certificateNames, signaturePaths, signatureNames []string,
user, expectedErrMsg string,
expectedErrIs error,
expectedErrAs any,
) {
tb.Helper()
tmpdir := tb.TempDir()
@ -639,6 +658,11 @@ func testSign(tb testing.TB, ctx *context.Context, certificateNames, signaturePa
return
}
if expectedErrAs != nil {
require.ErrorAs(tb, err, expectedErrAs)
return
}
require.NoError(tb, err)
// ensure all artifacts have an ID

View File

@ -5,6 +5,7 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/internal/yaml"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/slack-go/slack"
@ -29,7 +30,7 @@ func TestAnnounceInvalidTemplate(t *testing.T) {
},
},
})
require.EqualError(t, Pipe{}.Announce(ctx), `slack: template: tmpl:1: unexpected "}" in operand`)
testlib.RequireTemplateError(t, Pipe{}.Announce(ctx))
}
func TestAnnounceMissingEnv(t *testing.T) {

View File

@ -4,6 +4,7 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require"
)
@ -27,7 +28,7 @@ func TestAnnounceInvalidTemplate(t *testing.T) {
},
},
})
require.EqualError(t, Pipe{}.Announce(ctx), `teams: template: tmpl:1: unexpected "}" in operand`)
testlib.RequireTemplateError(t, Pipe{}.Announce(ctx))
}
func TestAnnounceMissingEnv(t *testing.T) {

View File

@ -4,6 +4,7 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require"
)
@ -26,7 +27,7 @@ func TestAnnounceInvalidTemplate(t *testing.T) {
},
},
})
require.EqualError(t, Pipe{}.Announce(ctx), `twitter: template: tmpl:1: unexpected "}" in operand`)
testlib.RequireTemplateError(t, Pipe{}.Announce(ctx))
}
func TestAnnounceMissingEnv(t *testing.T) {

View File

@ -68,7 +68,7 @@ func (p Pipe) Announce(ctx *context.Context) error {
msg, err := tmpl.New(ctx).Apply(ctx.Config.Announce.Webhook.MessageTemplate)
if err != nil {
return fmt.Errorf("webhook: %s", err)
return fmt.Errorf("webhook: %w", err)
}
log.Infof("posting: '%s'", msg)

View File

@ -11,6 +11,7 @@ import (
"github.com/google/uuid"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require"
)
@ -48,7 +49,7 @@ func TestAnnounceInvalidMessageTemplate(t *testing.T) {
},
},
})
require.EqualError(t, Pipe{}.Announce(ctx), `webhook: template: tmpl:1: unexpected "}" in operand`)
testlib.RequireTemplateError(t, Pipe{}.Announce(ctx))
}
type WebHookServerMockMessage struct {

View File

@ -3,6 +3,7 @@ package testlib
import (
"testing"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/stretchr/testify/require"
)
@ -11,6 +12,5 @@ func RequireTemplateError(tb testing.TB, err error) {
tb.Helper()
require.Error(tb, err)
require.Contains(tb, err.Error(), "template:")
require.Regexp(tb, "bad character|map has no entry|unexpected \"}\" in operand", err.Error())
require.ErrorAs(tb, err, &tmpl.Error{})
}

45
internal/tmpl/errors.go Normal file
View File

@ -0,0 +1,45 @@
package tmpl
import (
"fmt"
"regexp"
)
var res = []*regexp.Regexp{
regexp.MustCompile(`^template: tmpl:\d+:\d+: executing ".+" at .+: `),
regexp.MustCompile(`^template: tmpl:\d+:\d+: `),
regexp.MustCompile(`^template: tmpl:\d+: `),
}
func newTmplError(str string, err error) error {
if err == nil {
return nil
}
details := err.Error()
for _, re := range res {
if re.MatchString(details) {
details = re.ReplaceAllString(details, "")
break
}
}
return Error{str, details, err}
}
// Error is returned on any template error.
type Error struct {
str string
details string
err error
}
func (e Error) Unwrap() error {
return e.err
}
func (e Error) Error() string {
return fmt.Sprintf(
"template: failed to apply %q: %s",
e.str,
e.details,
)
}

View File

@ -223,11 +223,11 @@ func (t *Template) Apply(s string) (string, error) {
}).
Parse(s)
if err != nil {
return "", err
return "", newTmplError(s, err)
}
err = tmpl.Execute(&out, t.fields)
return out.String(), err
return out.String(), newTmplError(s, err)
}
// ApplyAll applies all the given strings against the Fields stored in the
@ -237,7 +237,7 @@ func (t *Template) ApplyAll(sps ...*string) error {
s := *sp
result, err := t.Apply(s)
if err != nil {
return fmt.Errorf("failed to apply template: %s: %w", s, err)
return newTmplError(s, err)
}
*sp = result
}

View File

@ -138,7 +138,8 @@ func TestWithArtifact(t *testing.T) {
t.Run("template using artifact Fields with no artifact", func(t *testing.T) {
t.Parallel()
result, err := New(ctx).Apply("{{ .Os }}")
require.EqualError(t, err, `template: tmpl:1:3: executing "tmpl" at <.Os>: map has no entry for key "Os"`)
require.ErrorAs(t, err, &Error{})
require.EqualError(t, err, `template: failed to apply "{{ .Os }}": map has no entry for key "Os"`)
require.Empty(t, result)
})
}
@ -357,16 +358,18 @@ func TestApplySingleEnvOnly(t *testing.T) {
}
func TestInvalidTemplate(t *testing.T) {
ctx := testctx.New(testctx.WithCurrentTag("v1.1.1"))
ctx := testctx.New()
_, err := New(ctx).Apply("{{{.Foo}")
require.EqualError(t, err, "template: tmpl:1: unexpected \"{\" in command")
require.ErrorAs(t, err, &Error{})
require.EqualError(t, err, `template: failed to apply "{{{.Foo}": unexpected "{" in command`)
}
func TestEnvNotFound(t *testing.T) {
ctx := testctx.New(testctx.WithCurrentTag("v1.2.4"))
result, err := New(ctx).Apply("{{.Env.FOO}}")
require.Empty(t, result)
require.EqualError(t, err, `template: tmpl:1:6: executing "tmpl" at <.Env.FOO>: map has no entry for key "FOO"`)
require.ErrorAs(t, err, &Error{})
require.EqualError(t, err, `template: failed to apply "{{.Env.FOO}}": map has no entry for key "FOO"`)
}
func TestWithExtraFields(t *testing.T) {