mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-02-03 13:11:48 +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:
parent
5d7f36eadb
commit
25affdd891
@ -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
|
||||
}
|
||||
|
@ -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())
|
||||
}
|
||||
|
@ -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
|
||||
|
3
internal/deprecate/testdata/output_custom.txt.golden
vendored
Normal file
3
internal/deprecate/testdata/output_custom.txt.golden
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
• first
|
||||
• DEPRECATED: some custom template with a url https://goreleaser.com/deprecations#something-else
|
||||
• last
|
@ -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(),
|
||||
|
@ -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())
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user