Skip to content

Commit

Permalink
Prevent decrease of higher position version while auto incrementing
Browse files Browse the repository at this point in the history
Resolves #4
  • Loading branch information
René Galle committed Mar 1, 2021
1 parent 9529528 commit 960007b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const positionMajor = 0
const positionMinor = 1
const positionPatch = 2

var versionDecreaseError = fmt.Errorf("version decrease in combination with auto increment is not supported")

func Next(current string, patternNext string) (string, error) {
_, current = splitPrefix("v", current)
currentCore, _ := coreAndExtension(current)
Expand Down Expand Up @@ -63,10 +65,16 @@ func Next(current string, patternNext string) (string, error) {
case positionMinor:
if currentMajor == nextMajor {
nextMinor = currentMinor + 1
} else if currentMajor > nextMajor {
return "", versionDecreaseError
}
case positionPatch:
if currentMajor == nextMajor && currentMinor == nextMinor {
nextPatch = currentPatch + 1
} else if currentMajor > nextMajor {
return "", versionDecreaseError
} else if currentMinor > nextMinor {
return "", versionDecreaseError
}
}

Expand Down
27 changes: 27 additions & 0 deletions pkg/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,33 @@ func TestNext(t *testing.T) {
want: "2.0.0",
wantErr: false,
},
{
name: "reject decreasing major version when incrementing minor version",
args: args{
current: "1.2.3",
incrementPattern: "0.x.0",
},
want: "",
wantErr: true,
},
{
name: "reject decreasing major version when incrementing patch version",
args: args{
current: "1.2.3",
incrementPattern: "0.2.x",
},
want: "",
wantErr: true,
},
{
name: "reject decreasing minor version when incrementing patch version",
args: args{
current: "1.2.3",
incrementPattern: "1.0.x",
},
want: "",
wantErr: true,
},
{
name: "increment minor not knowing major and patch",
args: args{
Expand Down

0 comments on commit 960007b

Please sign in to comment.