1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-04-15 11:56:56 +02:00

fix: log semver errors when snapshot (#2084)

* fix: log semver errors when snapshot

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* fix: deprecate notice

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
This commit is contained in:
Carlos Alexandro Becker 2021-03-01 14:18:57 -03:00 committed by GitHub
parent 5d7f36eadb
commit 25affdd891
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 79 additions and 31 deletions

View File

@ -3,7 +3,9 @@
package deprecate package deprecate
import ( import (
"bytes"
"strings" "strings"
"text/template"
"github.com/apex/log" "github.com/apex/log"
"github.com/apex/log/handlers/cli" "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. // Notice warns the user about the deprecation of the given property.
func Notice(ctx *context.Context, property string) { 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 ctx.Deprecated = true
cli.Default.Padding += 3 cli.Default.Padding += 3
defer func() { defer func() {
@ -25,9 +32,17 @@ func Notice(ctx *context.Context, property string) {
".", "", ".", "",
"_", "", "_", "",
).Replace(property) ).Replace(property)
log.Warn(color.New(color.Bold, color.FgHiYellow).Sprintf( var out bytes.Buffer
"DEPRECATED: `%s` should not be used anymore, check %s for more info.", if err := template.Must(template.New("deprecation").Parse("DEPRECATED: "+tmpl)).Execute(&out, templateData{
property, URL: 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
} }

View File

@ -1,8 +1,9 @@
package deprecate package deprecate
import ( import (
"bytes"
"flag" "flag"
"io/ioutil" "os"
"testing" "testing"
"github.com/apex/log" "github.com/apex/log"
@ -16,31 +17,47 @@ import (
var update = flag.Bool("update", false, "update .golden files") var update = flag.Bool("update", false, "update .golden files")
func TestNotice(t *testing.T) { func TestNotice(t *testing.T) {
f, err := ioutil.TempFile(t.TempDir(), "output.txt") var w bytes.Buffer
require.NoError(t, err)
t.Cleanup(func() { f.Close() })
color.NoColor = true color.NoColor = true
log.SetHandler(cli.New(f)) log.SetHandler(cli.New(&w))
log.Info("first") log.Info("first")
var ctx = context.New(config.Project{}) ctx := context.New(config.Project{})
Notice(ctx, "foo.bar.whatever") Notice(ctx, "foo.bar.whatever")
log.Info("last") log.Info("last")
require.True(t, ctx.Deprecated) 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" const golden = "testdata/output.txt.golden"
if *update { 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.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())
} }

View File

@ -1,3 +1,3 @@
• first • 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 • last

View File

@ -0,0 +1,3 @@
• first
• DEPRECATED: some custom template with a url https://goreleaser.com/deprecations#something-else
• last

View File

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"github.com/Masterminds/semver/v3" "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/internal/pipe"
"github.com/goreleaser/goreleaser/pkg/context" "github.com/goreleaser/goreleaser/pkg/context"
) )
@ -21,16 +21,20 @@ func (Pipe) String() string {
func (Pipe) Run(ctx *context.Context) error { func (Pipe) Run(ctx *context.Context) error {
sv, err := semver.NewVersion(ctx.Git.CurrentTag) sv, err := semver.NewVersion(ctx.Git.CurrentTag)
if err != nil { 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 { if ctx.Snapshot {
return pipe.ErrSnapshotEnabled return pipe.ErrSnapshotEnabled
} }
if ctx.SkipValidate { if ctx.SkipValidate {
log.WithError(err).
WithField("tag", ctx.Git.CurrentTag).
Warn("current tag is not a semantic tag")
return pipe.ErrSkipValidateEnabled 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{ ctx.Semver = context.Semver{
Major: sv.Major(), Major: sv.Major(),

View File

@ -15,7 +15,7 @@ func TestDescription(t *testing.T) {
} }
func TestValidSemver(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" ctx.Git.CurrentTag = "v1.5.2-rc1"
require.NoError(t, Pipe{}.Run(ctx)) require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, context.Semver{ require.Equal(t, context.Semver{
@ -27,15 +27,15 @@ func TestValidSemver(t *testing.T) {
} }
func TestInvalidSemver(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" ctx.Git.CurrentTag = "aaaav1.5.2-rc1"
var err = Pipe{}.Run(ctx) err := Pipe{}.Run(ctx)
require.Error(t, err) 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) { func TestInvalidSemverOnSnapshots(t *testing.T) {
var ctx = context.New(config.Project{}) ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "aaaav1.5.2-rc1" ctx.Git.CurrentTag = "aaaav1.5.2-rc1"
ctx.Snapshot = true ctx.Snapshot = true
require.EqualError(t, Pipe{}.Run(ctx), pipe.ErrSnapshotEnabled.Error()) require.EqualError(t, Pipe{}.Run(ctx), pipe.ErrSnapshotEnabled.Error())
@ -48,7 +48,7 @@ func TestInvalidSemverOnSnapshots(t *testing.T) {
} }
func TestInvalidSemverSkipValidate(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.Git.CurrentTag = "aaaav1.5.2-rc1"
ctx.SkipValidate = true ctx.SkipValidate = true
require.EqualError(t, Pipe{}.Run(ctx), pipe.ErrSkipValidateEnabled.Error()) require.EqualError(t, Pipe{}.Run(ctx), pipe.ErrSkipValidateEnabled.Error())

View File

@ -15,6 +15,15 @@ goreleaser check
## Active deprecation notices ## 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 ### builds for darwin/arm64
> since 2021-02-17 (v0.157.0) > since 2021-02-17 (v0.157.0)