-
-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #672 from yorukot/1.2.0.0-release
chore(release): 1.2.0.0 release
- Loading branch information
Showing
14 changed files
with
270 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// Assuming both a and b are string in format of | ||
// vX.Y.Z.W .... (any number of digits allowed) | ||
// Return 1 if a > b | ||
// Return 0 if a == b | ||
// Return -1 if a < b | ||
// Return non-nil error if string are not correctly formated | ||
func versionCompare(a string, b string) (int, error) { | ||
res := 0 | ||
if len(a) < 2 || len(b) < 2 || a[0] != 'v' || b[0] != 'v' { | ||
return res, fmt.Errorf("Invalid version strings %v and %v", a, b) | ||
} | ||
|
||
a_parts := strings.Split(strings.TrimPrefix(a, "v"), ".") | ||
b_parts := strings.Split(strings.TrimPrefix(b, "v"), ".") | ||
curIdx := 0 | ||
for curIdx < len(a_parts) && curIdx < len(b_parts) { | ||
aVal, bVal := 0, 0 | ||
aVal, err := strconv.Atoi(a_parts[curIdx]) | ||
if err != nil || aVal < 0 { | ||
return res, fmt.Errorf("Non positive integer %v in version : %w", a_parts[curIdx], err) | ||
} | ||
bVal, err = strconv.Atoi(b_parts[curIdx]) | ||
if err != nil || bVal < 0 { | ||
return res, fmt.Errorf("Non positive integer %v in version : %w", b_parts[curIdx], err) | ||
} | ||
if aVal > bVal { | ||
return 1, nil | ||
} else if aVal < bVal { | ||
return -1, nil | ||
} | ||
// Otherwise continue iteration | ||
curIdx++ | ||
} | ||
|
||
if curIdx < len(a_parts) { | ||
// some parts of a are still left, while b is completely iterated | ||
// Just make sure they are all integers | ||
for curIdx < len(a_parts) { | ||
if aVal, err := strconv.Atoi(a_parts[curIdx]); err != nil || aVal < 0 { | ||
return res, fmt.Errorf("Non integer part %v in version : %w", a_parts[curIdx], err) | ||
} | ||
curIdx++ | ||
} | ||
return 1, nil | ||
} | ||
|
||
if curIdx < len(b_parts) { | ||
// some parts of b are still left, while a is completely iterated | ||
// Just make sure they are all integers | ||
for curIdx < len(b_parts) { | ||
if bVal, err := strconv.Atoi(b_parts[curIdx]); err != nil || bVal < 0 { | ||
return res, fmt.Errorf("Non integer part %v in version : %w", b_parts[curIdx], err) | ||
} | ||
curIdx++ | ||
} | ||
return -1, nil | ||
} | ||
|
||
// Both a and b are completely iterated | ||
return 0, nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func Test_versionCompare(t *testing.T) { | ||
data := []struct { | ||
vA string | ||
vB string | ||
expectedError bool | ||
expectedResult int | ||
description string | ||
}{ | ||
{"v1.2.0", "v1.3.0", false, -1, "Basic version comparison (a < b)"}, | ||
{"v1.2.0", "v1.1.7.1", false, 1, "Version with more parts is greater"}, | ||
{"v1.2.0", "v3", false, -1, "Single digit version comparison"}, | ||
{"v1.7.1", "v1.7.1", false, 0, "Exact version match"}, | ||
{"v4", "v4", false, 0, "Single digit version match"}, | ||
{"v4", "v5", false, -1, "Single digit version comparison"}, | ||
{"v5", "v4", false, 1, "Single digit version comparison (reverse)"}, | ||
{"v5.1", "v5", false, 1, "Version with additional part is greater"}, | ||
{"v5", "v5.1", false, -1, "Shorter version is lesser"}, | ||
|
||
// Error cases | ||
{"1.7.1", "v1.7.1", true, 0, "Missing 'v' prefix (first version)"}, | ||
{"v1.7a.1", "v1.7.1", true, 0, "Non-numeric part in version"}, | ||
{"v1.7.1.", "v1.7.1", true, 0, "Trailing dot in version"}, | ||
{"v1.7.1", "v1.7.1.", true, 0, "Trailing dot in second version"}, | ||
{"v", "v1", true, 0, "Incomplete version string"}, | ||
{"v1.-1.0", "v1.0.0", true, 0, "Negative number in version"}, | ||
{"v1.2..3", "v1.2.3", true, 0, "Double dot in version"}, | ||
|
||
{"v1.0.0", "v1.0.1", false, -1, "Smallest difference in last part"}, | ||
{"v10.0.0", "v2.0.0", false, 1, "Multi-digit version comparison"}, | ||
{"v1.2.3.4.5", "v1.2.3.4.6", false, -1, "Many version parts comparison"}, | ||
{"v1.2.3.4.5", "v1.2.3.4.5", false, 0, "Many version parts exact match"}, | ||
{"v0.1.0", "v1.0.0", false, -1, "Zero to non-zero version comparison"}, | ||
{"v0", "v0.0.1", false, -1, "Zero version with additional parts"}, | ||
{"v0.0.0", "v0", false, 1, "Multiple zero representations"}, | ||
{"v01.2.3", "v1.2.3", false, 0, "Leading zero in version part"}, | ||
{"v1.2.03", "v1.2.3", false, 0, "Leading zero in version part"}, | ||
} | ||
|
||
for _, tt := range data { | ||
t.Run(tt.description, func(t *testing.T) { | ||
res, err := versionCompare(tt.vA, tt.vB) | ||
if tt.expectedError { | ||
assert.NotNil(t, err, "Error is expected for %s", tt.description) | ||
} else { | ||
assert.Nil(t, err, "Error should be Nil for %s", tt.description) | ||
assert.Equal(t, tt.expectedResult, res, "Result should be as expected for %s", tt.description) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.