Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion internal/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,38 @@ const (
// The hotfix segment is only valid nested after an rc segment.
var semverRegex = regexp.MustCompile(`^([a-zA-Z]*)(\d+)\.(\d+)\.(\d+)(?:-rc\.(\d+)(?:\.hotfix\.(\d+))?)?$`)

// baseVersionRegex matches a semver core (vX.Y.Z) with any optional
// pre-release suffix (for example -rc.4, -dryrun.13, or -beta.1). Only the
// numeric core and prefix are captured; the suffix is intentionally ignored.
var baseVersionRegex = regexp.MustCompile(`^([a-zA-Z]*)(\d+)\.(\d+)\.(\d+)(?:-.+)?$`)

// ParseBase parses the numeric core (prefix and major.minor.patch) of a version
// string, tolerating and discarding any pre-release suffix such as an -rc.N,
// -dryrun.N, or other exercise tag that the strict Parse rejects. The returned
// Version always has no pre-release or hotfix segment. It errors only when the
// core itself is not a valid vX.Y.Z triple. Version calculations that derive
// their next version solely from a base can use this so a stray suffixed value
// recorded as the latest does not abort the whole calculation.
func ParseBase(s string) (*Version, error) {
matches := baseVersionRegex.FindStringSubmatch(s)
if matches == nil {
return nil, fmt.Errorf("invalid version format: %s", s)
}

major, _ := strconv.Atoi(matches[2])
minor, _ := strconv.Atoi(matches[3])
patch, _ := strconv.Atoi(matches[4])

return &Version{
Major: major,
Minor: minor,
Patch: patch,
PreRelease: -1,
Hotfix: -1,
Prefix: matches[1],
}, nil
}

// Parse parses a version string into a Version struct
func Parse(s string) (*Version, error) {
matches := semverRegex.FindStringSubmatch(s)
Expand Down Expand Up @@ -200,8 +232,13 @@ func (c *Calculator) CalculateNext(currentDevVersion, nextEnvVersion string, com
Prefix: c.prefix,
}
} else {
// Only the numeric core of the next env's version feeds the
// calculation (see BaseVersion below), so tolerate any pre-release
// suffix here. A stray -dryrun.N or -rc.N value recorded as the latest
// must not abort the calculation, matching the discovery-side filtering
// that keeps such exercise tags out of tag lookups.
var err error
baseVersion, err = Parse(nextEnvVersion)
baseVersion, err = ParseBase(nextEnvVersion)
if err != nil {
return nil, fmt.Errorf("parsing next env version: %w", err)
}
Expand Down
79 changes: 79 additions & 0 deletions internal/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,85 @@ func TestCalculateNext_NextEnvHoldsHotfixVersion(t *testing.T) {
assert.Equal(t, -1, got.Hotfix)
}

func TestParseBase(t *testing.T) {
tests := []struct {
name string
input string
want string // Version.String() of the parsed base, or "" when wantErr
wantErr bool
}{
{name: "plain release", input: "v1.2.3", want: "v1.2.3"},
{name: "rc suffix dropped", input: "v1.2.3-rc.4", want: "v1.2.3"},
{name: "hotfix suffix dropped", input: "v1.4.0-rc.2.hotfix.1", want: "v1.4.0"},
{name: "dryrun suffix dropped", input: "v0.6.0-dryrun.13", want: "v0.6.0"},
{name: "opaque suffix dropped", input: "v2.0.0-beta.1", want: "v2.0.0"},
{name: "no prefix", input: "3.4.5-dryrun.1", want: "3.4.5"},
{name: "not a triple", input: "vnightly", wantErr: true},
{name: "empty", input: "", wantErr: true},
{name: "two segments", input: "v1.2-dryrun.1", wantErr: true},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseBase(tt.input)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, got.String())
assert.Equal(t, -1, got.PreRelease)
assert.Equal(t, -1, got.Hotfix)
})
}
}

func TestCalculateNext_NextEnvHoldsDryrunVersion(t *testing.T) {
calc := NewCalculator("v")

commits := []changelog.ConventionalCommit{
{Type: "fix", Description: "bug fix"},
}

tests := []struct {
name string
currentDevVersion string
nextEnvVersion string
want string
}{
{
// A dry-run vector leaves a v0.6.0-dryrun.13 value in state; a
// later real release must still compute the next version from the
// v0.6.0 base rather than aborting the whole calculation.
name: "dryrun latest, fresh release",
currentDevVersion: "",
nextEnvVersion: "v0.6.0-dryrun.13",
want: "v0.6.1-rc.0",
},
{
name: "dryrun latest, just promoted",
currentDevVersion: "v0.6.0-dryrun.13",
nextEnvVersion: "v0.6.0-dryrun.13",
want: "v0.6.1-rc.0",
},
{
name: "opaque prerelease suffix tolerated",
currentDevVersion: "",
nextEnvVersion: "v1.2.3-beta.4",
want: "v1.2.4-rc.0",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := calc.CalculateNext(tt.currentDevVersion, tt.nextEnvVersion, commits)
require.NoError(t, err)
assert.Equal(t, tt.want, got.String())
assert.Equal(t, -1, got.Hotfix)
})
}
}

func TestStripRC_HotfixVersion(t *testing.T) {
got, err := StripRC("v1.4.0-rc.2.hotfix.1")
require.NoError(t, err)
Expand Down
Loading