You've already forked goreleaser
mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-11-06 09:09:29 +02:00
fix: improved semver parsing
This commit is contained in:
committed by
Carlos Alexandro Becker
parent
a43c653254
commit
df831077e5
@@ -6,10 +6,8 @@ import (
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/Masterminds/semver"
|
||||
"github.com/goreleaser/goreleaser/internal/artifact"
|
||||
"github.com/goreleaser/goreleaser/pkg/context"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Template holds data that can be applied to a template string
|
||||
@@ -57,6 +55,10 @@ func New(ctx *context.Context) *Template {
|
||||
env: ctx.Env,
|
||||
date: time.Now().UTC().Format(time.RFC3339),
|
||||
timestamp: time.Now().UTC().Unix(),
|
||||
major: ctx.Semver.Major,
|
||||
minor: ctx.Semver.Minor,
|
||||
patch: ctx.Semver.Patch,
|
||||
// TODO: no reason not to add prerelease here too I guess
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -90,14 +92,6 @@ func (t *Template) Apply(s string) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
sv, err := semver.NewVersion(t.fields[tag].(string))
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "tmpl")
|
||||
}
|
||||
t.fields[major] = sv.Major()
|
||||
t.fields[minor] = sv.Minor()
|
||||
t.fields[patch] = sv.Patch()
|
||||
|
||||
err = tmpl.Execute(&out, t.fields)
|
||||
return out.String(), err
|
||||
}
|
||||
|
||||
@@ -16,8 +16,13 @@ func TestWithArtifact(t *testing.T) {
|
||||
ctx.Env = map[string]string{
|
||||
"FOO": "bar",
|
||||
}
|
||||
ctx.Version = "1.0.0"
|
||||
ctx.Git.CurrentTag = "v1.0.0"
|
||||
ctx.Version = "1.2.3"
|
||||
ctx.Git.CurrentTag = "v1.2.3"
|
||||
ctx.Semver = context.Semver{
|
||||
Major: 1,
|
||||
Minor: 2,
|
||||
Patch: 3,
|
||||
}
|
||||
ctx.Git.Commit = "commit"
|
||||
ctx.Git.FullCommit = "fullcommit"
|
||||
ctx.Git.ShortCommit = "shortcommit"
|
||||
@@ -26,8 +31,9 @@ func TestWithArtifact(t *testing.T) {
|
||||
"Linux": "{{.Os}}",
|
||||
"amd64": "{{.Arch}}",
|
||||
"6": "{{.Arm}}",
|
||||
"1.0.0": "{{.Version}}",
|
||||
"v1.0.0": "{{.Tag}}",
|
||||
"1.2.3": "{{.Version}}",
|
||||
"v1.2.3": "{{.Tag}}",
|
||||
"1-2-3": "{{.Major}}-{{.Minor}}-{{.Patch}}",
|
||||
"commit": "{{.Commit}}",
|
||||
"fullcommit": "{{.FullCommit}}",
|
||||
"shortcommit": "{{.ShortCommit}}",
|
||||
@@ -136,7 +142,9 @@ func TestFuncMap(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestInvalidTemplate(t *testing.T) {
|
||||
_, err := New(context.New(config.Project{})).Apply("{{{.Foo}")
|
||||
ctx := context.New(config.Project{})
|
||||
ctx.Git.CurrentTag = "v1.1.1"
|
||||
_, err := New(ctx).Apply("{{{.Foo}")
|
||||
assert.EqualError(t, err, "template: tmpl:1: unexpected \"{\" in command")
|
||||
}
|
||||
|
||||
@@ -147,12 +155,3 @@ func TestEnvNotFound(t *testing.T) {
|
||||
assert.Empty(t, result)
|
||||
assert.EqualError(t, err, `template: tmpl:1:6: executing "tmpl" at <.Env.FOO>: map has no entry for key "FOO"`)
|
||||
}
|
||||
|
||||
// This should actually never happen...
|
||||
func TestInvalidSemver(t *testing.T) {
|
||||
var ctx = context.New(config.Project{})
|
||||
ctx.Git.CurrentTag = "v1_2_3"
|
||||
result, err := New(ctx).Apply("{{.Major}}")
|
||||
assert.Empty(t, result)
|
||||
assert.EqualError(t, err, `tmpl: Invalid Semantic Version`)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user