From 25affdd8910fb3f1032abf78f23b791e9c87ef89 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Mon, 1 Mar 2021 14:18:57 -0300 Subject: [PATCH] fix: log semver errors when snapshot (#2084) * fix: log semver errors when snapshot Signed-off-by: Carlos Alexandro Becker * fix: deprecate notice Signed-off-by: Carlos Alexandro Becker --- internal/deprecate/deprecate.go | 25 ++++++++--- internal/deprecate/deprecate_test.go | 45 +++++++++++++------ internal/deprecate/testdata/output.txt.golden | 2 +- .../testdata/output_custom.txt.golden | 3 ++ internal/pipe/semver/semver.go | 14 +++--- internal/pipe/semver/semver_test.go | 12 ++--- www/docs/deprecations.md | 9 ++++ 7 files changed, 79 insertions(+), 31 deletions(-) create mode 100644 internal/deprecate/testdata/output_custom.txt.golden diff --git a/internal/deprecate/deprecate.go b/internal/deprecate/deprecate.go index 759adfd91..6f347ba73 100644 --- a/internal/deprecate/deprecate.go +++ b/internal/deprecate/deprecate.go @@ -3,7 +3,9 @@ package deprecate import ( + "bytes" "strings" + "text/template" "github.com/apex/log" "github.com/apex/log/handlers/cli" @@ -15,6 +17,11 @@ const baseURL = "https://goreleaser.com/deprecations#" // Notice warns the user about the deprecation of the given property. func Notice(ctx *context.Context, property string) { + NoticeCustom(ctx, property, "`{{ .Property }}` should not be used anymore, check {{ .URL }} for more info") +} + +// Notice warns the user about the deprecation of the given property. +func NoticeCustom(ctx *context.Context, property, tmpl string) { ctx.Deprecated = true cli.Default.Padding += 3 defer func() { @@ -25,9 +32,17 @@ func Notice(ctx *context.Context, property string) { ".", "", "_", "", ).Replace(property) - log.Warn(color.New(color.Bold, color.FgHiYellow).Sprintf( - "DEPRECATED: `%s` should not be used anymore, check %s for more info.", - property, - url, - )) + var out bytes.Buffer + if err := template.Must(template.New("deprecation").Parse("DEPRECATED: "+tmpl)).Execute(&out, templateData{ + URL: url, + Property: property, + }); err != nil { + panic(err) // this should never happen + } + log.Warn(color.New(color.Bold, color.FgHiYellow).Sprintf(out.String())) +} + +type templateData struct { + URL string + Property string } diff --git a/internal/deprecate/deprecate_test.go b/internal/deprecate/deprecate_test.go index 920013ccd..d0bbc6623 100644 --- a/internal/deprecate/deprecate_test.go +++ b/internal/deprecate/deprecate_test.go @@ -1,8 +1,9 @@ package deprecate import ( + "bytes" "flag" - "io/ioutil" + "os" "testing" "github.com/apex/log" @@ -16,31 +17,47 @@ import ( var update = flag.Bool("update", false, "update .golden files") func TestNotice(t *testing.T) { - f, err := ioutil.TempFile(t.TempDir(), "output.txt") - require.NoError(t, err) - t.Cleanup(func() { f.Close() }) + var w bytes.Buffer color.NoColor = true - log.SetHandler(cli.New(f)) + log.SetHandler(cli.New(&w)) log.Info("first") - var ctx = context.New(config.Project{}) + ctx := context.New(config.Project{}) Notice(ctx, "foo.bar.whatever") log.Info("last") require.True(t, ctx.Deprecated) - require.NoError(t, f.Close()) - - bts, err := ioutil.ReadFile(f.Name()) - require.NoError(t, err) - const golden = "testdata/output.txt.golden" if *update { - require.NoError(t, ioutil.WriteFile(golden, bts, 0655)) + require.NoError(t, os.WriteFile(golden, w.Bytes(), 0o655)) } - gbts, err := ioutil.ReadFile(golden) + gbts, err := os.ReadFile(golden) require.NoError(t, err) - require.Equal(t, string(gbts), string(bts)) + require.Equal(t, string(gbts), w.String()) +} + +func TestNoticeCustom(t *testing.T) { + var w bytes.Buffer + + color.NoColor = true + log.SetHandler(cli.New(&w)) + + log.Info("first") + ctx := context.New(config.Project{}) + NoticeCustom(ctx, "something-else", "some custom template with a url {{ .URL }}") + log.Info("last") + require.True(t, ctx.Deprecated) + + const golden = "testdata/output_custom.txt.golden" + if *update { + require.NoError(t, os.WriteFile(golden, w.Bytes(), 0o655)) + } + + gbts, err := os.ReadFile(golden) + require.NoError(t, err) + + require.Equal(t, string(gbts), w.String()) } diff --git a/internal/deprecate/testdata/output.txt.golden b/internal/deprecate/testdata/output.txt.golden index 589885c24..bd4a5aab2 100644 --- a/internal/deprecate/testdata/output.txt.golden +++ b/internal/deprecate/testdata/output.txt.golden @@ -1,3 +1,3 @@ • first - • DEPRECATED: `foo.bar.whatever` should not be used anymore, check https://goreleaser.com/deprecations#foobarwhatever for more info. + • DEPRECATED: `foo.bar.whatever` should not be used anymore, check https://goreleaser.com/deprecations#foobarwhatever for more info • last diff --git a/internal/deprecate/testdata/output_custom.txt.golden b/internal/deprecate/testdata/output_custom.txt.golden new file mode 100644 index 000000000..dad95691c --- /dev/null +++ b/internal/deprecate/testdata/output_custom.txt.golden @@ -0,0 +1,3 @@ + • first + • DEPRECATED: some custom template with a url https://goreleaser.com/deprecations#something-else + • last diff --git a/internal/pipe/semver/semver.go b/internal/pipe/semver/semver.go index 9d0f931d6..d7ef1887e 100644 --- a/internal/pipe/semver/semver.go +++ b/internal/pipe/semver/semver.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/Masterminds/semver/v3" - "github.com/apex/log" + "github.com/goreleaser/goreleaser/internal/deprecate" "github.com/goreleaser/goreleaser/internal/pipe" "github.com/goreleaser/goreleaser/pkg/context" ) @@ -21,16 +21,20 @@ func (Pipe) String() string { func (Pipe) Run(ctx *context.Context) error { sv, err := semver.NewVersion(ctx.Git.CurrentTag) if err != nil { + if ctx.Snapshot || ctx.SkipValidate { + deprecate.NoticeCustom( + ctx, + "skipping-semver-validations", + fmt.Sprintf("'%s' is not SemVer-compatible and may cause other issues in the pipeline, check {{ .URL }} for more info", ctx.Git.CurrentTag), + ) + } if ctx.Snapshot { return pipe.ErrSnapshotEnabled } if ctx.SkipValidate { - log.WithError(err). - WithField("tag", ctx.Git.CurrentTag). - Warn("current tag is not a semantic tag") return pipe.ErrSkipValidateEnabled } - return fmt.Errorf("failed to parse tag %s as semver: %w", ctx.Git.CurrentTag, err) + return fmt.Errorf("failed to parse tag '%s' as semver: %w", ctx.Git.CurrentTag, err) } ctx.Semver = context.Semver{ Major: sv.Major(), diff --git a/internal/pipe/semver/semver_test.go b/internal/pipe/semver/semver_test.go index 2a2ae3a81..9d833a7dd 100644 --- a/internal/pipe/semver/semver_test.go +++ b/internal/pipe/semver/semver_test.go @@ -15,7 +15,7 @@ func TestDescription(t *testing.T) { } func TestValidSemver(t *testing.T) { - var ctx = context.New(config.Project{}) + ctx := context.New(config.Project{}) ctx.Git.CurrentTag = "v1.5.2-rc1" require.NoError(t, Pipe{}.Run(ctx)) require.Equal(t, context.Semver{ @@ -27,15 +27,15 @@ func TestValidSemver(t *testing.T) { } func TestInvalidSemver(t *testing.T) { - var ctx = context.New(config.Project{}) + ctx := context.New(config.Project{}) ctx.Git.CurrentTag = "aaaav1.5.2-rc1" - var err = Pipe{}.Run(ctx) + err := Pipe{}.Run(ctx) require.Error(t, err) - require.Contains(t, err.Error(), "failed to parse tag aaaav1.5.2-rc1 as semver") + require.Contains(t, err.Error(), "failed to parse tag 'aaaav1.5.2-rc1' as semver") } func TestInvalidSemverOnSnapshots(t *testing.T) { - var ctx = context.New(config.Project{}) + ctx := context.New(config.Project{}) ctx.Git.CurrentTag = "aaaav1.5.2-rc1" ctx.Snapshot = true require.EqualError(t, Pipe{}.Run(ctx), pipe.ErrSnapshotEnabled.Error()) @@ -48,7 +48,7 @@ func TestInvalidSemverOnSnapshots(t *testing.T) { } func TestInvalidSemverSkipValidate(t *testing.T) { - var ctx = context.New(config.Project{}) + ctx := context.New(config.Project{}) ctx.Git.CurrentTag = "aaaav1.5.2-rc1" ctx.SkipValidate = true require.EqualError(t, Pipe{}.Run(ctx), pipe.ErrSkipValidateEnabled.Error()) diff --git a/www/docs/deprecations.md b/www/docs/deprecations.md index 005835f34..3266d7b40 100644 --- a/www/docs/deprecations.md +++ b/www/docs/deprecations.md @@ -15,6 +15,15 @@ goreleaser check ## Active deprecation notices +### Skipping SemVer Validations + +> since 2021-02-28 (v0.158.0) + +GoReleaser skips SemVer validations when run with `--skip-validations` or `--snapshot`. +This causes other problems later, such as [invalid Linux packages](https://github.com/goreleaser/goreleaser/issues/2081). +Because of that, once this deprecation expires, GoReleaser will hard fail on non-semver versions, as stated on our +[limitations page](https://goreleaser.com/limitations/semver/). + ### builds for darwin/arm64 > since 2021-02-17 (v0.157.0)