Skip to content

Commit

Permalink
Fix bug in the GetPrevMinor implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmacknz committed Aug 23, 2023
1 parent 34d5a22 commit 1aa3953
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pkg/version/version_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,17 @@ func (psv ParsedSemVer) GetPreviousMinor() (*ParsedSemVer, error) {

if minor > 0 {
// We have at least one previous minor version in the current
// major version series
return NewParsedSemVer(major, minor-1, 0, "", ""), nil
// major version series. Set the patch to zero to guarnatee the
// version exists, the number of patch releases varies.
return NewParsedSemVer(major, minor-1, 0, psv.Prerelease(), psv.BuildMetadata()), nil
}

// We are at the first minor of the current major version series. To
// figure out the previous minor, we need to rely on knowledge of
// the release versions from the past major series'.
switch major {
case 8:
return NewParsedSemVer(7, 17, 10, "", ""), nil
return NewParsedSemVer(7, 17, 10, psv.Prerelease(), psv.BuildMetadata()), nil
}

return nil, fmt.Errorf("unable to determine previous minor version for [%s]", psv.String())
Expand Down
51 changes: 51 additions & 0 deletions pkg/version/version_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,54 @@ func TestLess(t *testing.T) {
})
}
}

func TestPreviousMinor(t *testing.T) {
testcases := []struct {
name string
version string
prevMinorVersion string
}{
{
name: "basic release version",
version: "8.7.0",
prevMinorVersion: "8.6.0",
},
{
name: "snapshot release version",
version: "8.9.3-SNAPSHOT",
prevMinorVersion: "8.8.0-SNAPSHOT",
},
{
name: "emergency release version",
version: "8.9.0-er1",
prevMinorVersion: "8.8.0-er1",
},
{
name: "previous major version",
version: "8.0.0",
prevMinorVersion: "7.17.10",
},
{
name: "previous major snapshot",
version: "8.0.0-SNAPSHOT",
prevMinorVersion: "7.17.10-SNAPSHOT",
},
{
name: "snapshot version with metadata",
version: "8.9.1-SNAPSHOT+aaaaaa",
prevMinorVersion: "8.8.0-SNAPSHOT+aaaaaa",
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
parsed, err := ParseVersion(tc.version)
require.NoError(t, err)
require.NotNil(t, parsed)

prev, err := parsed.GetPreviousMinor()
require.NoError(t, err)
require.Equal(t, tc.prevMinorVersion, prev.String())
})
}
}

0 comments on commit 1aa3953

Please sign in to comment.