1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-07-15 01:34:21 +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
32 changed files with 254 additions and 102 deletions

View File

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

View File

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

View File

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

View File

@ -4,6 +4,7 @@ import (
"testing" "testing"
"github.com/goreleaser/goreleaser/internal/testctx" "github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config" "github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -30,7 +31,7 @@ func TestBadTemplate(t *testing.T) {
ctx := testctx.New() ctx := testctx.New()
files, err := Find(ctx, globs) files, err := Find(ctx, globs)
require.Empty(t, files) 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) { func TestShouldGetSpecificFile(t *testing.T) {
@ -123,7 +124,7 @@ func TestTargetInvalidNameTemplate(t *testing.T) {
ctx := testctx.New() ctx := testctx.New()
files, err := Find(ctx, globs) files, err := Find(ctx, globs)
require.Empty(t, files) 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) { func TestTargetNameMatchesMultipleFiles(t *testing.T) {

View File

@ -51,7 +51,7 @@ func TestMeta(t *testing.T) {
}) })
require.NoError(t, Pipe{}.Default(ctx)) 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) { t.Run("no files", func(t *testing.T) {

View File

@ -655,7 +655,7 @@ func TestRunPipeInvalidWrapInDirectoryTemplate(t *testing.T) {
artifact.ExtraID: "default", 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) { func TestRunPipeWrap(t *testing.T) {

View File

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

View File

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

View File

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

View File

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

View File

@ -4,6 +4,7 @@ import (
"testing" "testing"
"github.com/goreleaser/goreleaser/internal/testctx" "github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config" "github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require" "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) { func TestAnnounceMissingEnv(t *testing.T) {

View File

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

View File

@ -4,6 +4,7 @@ import (
"testing" "testing"
"github.com/goreleaser/goreleaser/internal/testctx" "github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config" "github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require" "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) { func TestAnnounceMissingEnv(t *testing.T) {

View File

@ -4,6 +4,7 @@ import (
"testing" "testing"
"github.com/goreleaser/goreleaser/internal/testctx" "github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config" "github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require" "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) { func TestAnnounceMissingEnv(t *testing.T) {

View File

@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/goreleaser/goreleaser/internal/testctx" "github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config" "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) { func TestAnnounceMissingEnv(t *testing.T) {

View File

@ -4,6 +4,7 @@ import (
"testing" "testing"
"github.com/goreleaser/goreleaser/internal/testctx" "github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config" "github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require" "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) { func TestAnnounceMissingEnv(t *testing.T) {

View File

@ -3,6 +3,7 @@ package prebuild
import ( import (
"testing" "testing"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config" "github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context" "github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -31,7 +32,7 @@ func TestRun(t *testing.T) {
ctx := context.New(config.Project{ ctx := context.New(config.Project{
Builds: []config.Build{{Main: "{{ .Env.FOO }}"}}, 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" "testing"
"github.com/goreleaser/goreleaser/internal/testctx" "github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config" "github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require" "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) { 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) { func TestAnnounceMissingEnv(t *testing.T) {

View File

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

View File

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

View File

@ -5,6 +5,7 @@ import (
"testing" "testing"
"github.com/goreleaser/goreleaser/internal/testctx" "github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/internal/yaml" "github.com/goreleaser/goreleaser/internal/yaml"
"github.com/goreleaser/goreleaser/pkg/config" "github.com/goreleaser/goreleaser/pkg/config"
"github.com/slack-go/slack" "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) { func TestAnnounceMissingEnv(t *testing.T) {

View File

@ -4,6 +4,7 @@ import (
"testing" "testing"
"github.com/goreleaser/goreleaser/internal/testctx" "github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config" "github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require" "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) { func TestAnnounceMissingEnv(t *testing.T) {

View File

@ -4,6 +4,7 @@ import (
"testing" "testing"
"github.com/goreleaser/goreleaser/internal/testctx" "github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config" "github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require" "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) { 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) msg, err := tmpl.New(ctx).Apply(ctx.Config.Announce.Webhook.MessageTemplate)
if err != nil { if err != nil {
return fmt.Errorf("webhook: %s", err) return fmt.Errorf("webhook: %w", err)
} }
log.Infof("posting: '%s'", msg) log.Infof("posting: '%s'", msg)

View File

@ -11,6 +11,7 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
"github.com/goreleaser/goreleaser/internal/testctx" "github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config" "github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require" "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 { type WebHookServerMockMessage struct {

View File

@ -3,6 +3,7 @@ package testlib
import ( import (
"testing" "testing"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -11,6 +12,5 @@ func RequireTemplateError(tb testing.TB, err error) {
tb.Helper() tb.Helper()
require.Error(tb, err) require.Error(tb, err)
require.Contains(tb, err.Error(), "template:") require.ErrorAs(tb, err, &tmpl.Error{})
require.Regexp(tb, "bad character|map has no entry|unexpected \"}\" in operand", err.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) Parse(s)
if err != nil { if err != nil {
return "", err return "", newTmplError(s, err)
} }
err = tmpl.Execute(&out, t.fields) 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 // 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 s := *sp
result, err := t.Apply(s) result, err := t.Apply(s)
if err != nil { if err != nil {
return fmt.Errorf("failed to apply template: %s: %w", s, err) return newTmplError(s, err)
} }
*sp = result *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.Run("template using artifact Fields with no artifact", func(t *testing.T) {
t.Parallel() t.Parallel()
result, err := New(ctx).Apply("{{ .Os }}") 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) require.Empty(t, result)
}) })
} }
@ -357,16 +358,18 @@ func TestApplySingleEnvOnly(t *testing.T) {
} }
func TestInvalidTemplate(t *testing.T) { func TestInvalidTemplate(t *testing.T) {
ctx := testctx.New(testctx.WithCurrentTag("v1.1.1")) ctx := testctx.New()
_, err := New(ctx).Apply("{{{.Foo}") _, 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) { func TestEnvNotFound(t *testing.T) {
ctx := testctx.New(testctx.WithCurrentTag("v1.2.4")) ctx := testctx.New(testctx.WithCurrentTag("v1.2.4"))
result, err := New(ctx).Apply("{{.Env.FOO}}") result, err := New(ctx).Apply("{{.Env.FOO}}")
require.Empty(t, result) 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) { func TestWithExtraFields(t *testing.T) {