Skip to content

Commit

Permalink
feat: improve template error handling (#4256)
Browse files Browse the repository at this point in the history
- wrap the multiple errors in an internal error with cleaner messaging
- improve testlib.RequireTemplateError and several tests
  • Loading branch information
caarlos0 committed Aug 25, 2023
1 parent 53bcbe9 commit c91c4b7
Show file tree
Hide file tree
Showing 32 changed files with 254 additions and 102 deletions.
4 changes: 2 additions & 2 deletions internal/client/gitea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down
3 changes: 2 additions & 1 deletion internal/client/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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) {
Expand Down
35 changes: 27 additions & 8 deletions internal/exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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",
Expand All @@ -109,6 +111,7 @@ func TestExecute(t *testing.T) {
},
},
nil,
nil,
},
{
"no filter",
Expand All @@ -131,6 +134,7 @@ func TestExecute(t *testing.T) {
},
},
nil,
nil,
},
{
"disabled",
Expand All @@ -143,6 +147,7 @@ func TestExecute(t *testing.T) {
},
},
pipe.ErrSkip{},
nil,
},
{
"disabled invalid tmpl",
Expand All @@ -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",
Expand All @@ -178,6 +184,7 @@ func TestExecute(t *testing.T) {
},
},
nil,
nil,
},
{
"include signatures",
Expand All @@ -202,6 +209,7 @@ func TestExecute(t *testing.T) {
},
},
nil,
nil,
},
{
"docker",
Expand All @@ -221,6 +229,7 @@ func TestExecute(t *testing.T) {
},
},
nil,
nil,
},
{
"extra files",
Expand All @@ -246,6 +255,7 @@ func TestExecute(t *testing.T) {
},
},
nil,
nil,
},
{
"extra files with rename",
Expand Down Expand Up @@ -274,6 +284,7 @@ func TestExecute(t *testing.T) {
},
},
nil,
nil,
},
{
"try dir templating",
Expand All @@ -294,6 +305,7 @@ func TestExecute(t *testing.T) {
},
},
nil,
nil,
},
{
"check env templating",
Expand Down Expand Up @@ -321,6 +333,7 @@ func TestExecute(t *testing.T) {
},
},
nil,
nil,
},
{
"override path",
Expand All @@ -346,6 +359,7 @@ func TestExecute(t *testing.T) {
},
},
nil,
nil,
},
{
"command error",
Expand Down Expand Up @@ -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
}
if tc.expectErrAs != nil {
require.ErrorAs(t, err, tc.expectErrAs)
return
}
require.Error(t, err)
require.True(t, strings.HasPrefix(err.Error(), tc.expectErr.Error()), err.Error())
require.NoError(t, err)
})
}
}
5 changes: 3 additions & 2 deletions internal/extrafiles/extra_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion internal/pipe/archive/archive_meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion internal/pipe/archive/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
33 changes: 19 additions & 14 deletions internal/pipe/aur/aur_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -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
}

Expand Down
5 changes: 3 additions & 2 deletions internal/pipe/before/before_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c91c4b7

Please sign in to comment.