Skip to content

Commit a855f37

Browse files
authored
Merge branch 'main' into feat/add-coin-type
2 parents b36d41c + 96c04ab commit a855f37

File tree

2 files changed

+67
-18
lines changed

2 files changed

+67
-18
lines changed

cosmosutils/binary.go

+46-6
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,13 @@ func SortVersions(versions BinaryVersionWithDownloadURL) []string {
216216
return versionTags
217217
}
218218

219-
// CompareSemVer compares two semantic version strings and returns true if v1 should be ordered before v2
219+
// CompareSemVer compares two semantic version strings and returns true if v1 is greater than v2.
220+
// Examples:
221+
// - CompareSemVer("v2.0.0", "v1.0.0") returns true
222+
// - CompareSemVer("v1.2.0", "v1.1.0") returns true
223+
// - CompareSemVer("v1.0.0", "v1.0.0-beta") returns true
224+
// - CompareSemVer("v1.0.0-beta.2", "v1.0.0-beta.1") returns true
225+
// - CompareSemVer("v1.0.0", "v2.0.0") returns false
220226
func CompareSemVer(v1, v2 string) bool {
221227
// Trim "v" prefix
222228
v1 = strings.TrimPrefix(v1, "v")
@@ -237,15 +243,49 @@ func CompareSemVer(v1, v2 string) bool {
237243
}
238244
}
239245

240-
// Compare pre-release parts if main versions are equal
241-
// A pre-release version is always ordered greater than the normal version
246+
// If main versions are equal, handle pre-release versions
247+
// A pre-release version has lower precedence than the normal version
242248
if v1Pre == "" && v2Pre != "" {
243-
return false
249+
return true // v1 (normal version) is greater
244250
}
245251
if v1Pre != "" && v2Pre == "" {
246-
return true
252+
return false // v2 (normal version) is greater
247253
}
248-
return v1Pre > v2Pre
254+
255+
// If both have pre-release versions, compare them
256+
if v1Pre != "" && v2Pre != "" {
257+
v1PreParts := strings.Split(v1Pre, ".")
258+
v2PreParts := strings.Split(v2Pre, ".")
259+
260+
// Compare each pre-release identifier
261+
minLen := len(v1PreParts)
262+
if len(v2PreParts) < minLen {
263+
minLen = len(v2PreParts)
264+
}
265+
266+
for i := 0; i < minLen; i++ {
267+
// Try to compare as integers first
268+
v1Int, v1IsInt := strconv.Atoi(v1PreParts[i])
269+
v2Int, v2IsInt := strconv.Atoi(v2PreParts[i])
270+
271+
if v1IsInt == nil && v2IsInt == nil {
272+
// Both are integers
273+
if v1Int != v2Int {
274+
return v1Int > v2Int
275+
}
276+
} else {
277+
// Compare as strings if not both integers
278+
if v1PreParts[i] != v2PreParts[i] {
279+
return v1PreParts[i] > v2PreParts[i]
280+
}
281+
}
282+
}
283+
284+
// If all parts so far are equal, longer one is greater
285+
return len(v1PreParts) > len(v2PreParts)
286+
}
287+
288+
return false // versions are exactly equal
249289
}
250290

251291
// splitVersion separates the main version (e.g., "0.4.11") from the pre-release (e.g., "Binarytion.1")

cosmosutils/binary_test.go

+21-12
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,6 @@ func TestCompareSemVer(t *testing.T) {
2828
v2: "1.0.0",
2929
expected: false,
3030
},
31-
{
32-
v1: "1.0.0",
33-
v2: "1.0.0-1",
34-
expected: false,
35-
},
36-
{
37-
v1: "1.0.0-2",
38-
v2: "1.0.0-1",
39-
expected: true,
40-
},
4131
{
4232
v1: "1.2.0",
4333
v2: "1.1.9",
@@ -54,10 +44,29 @@ func TestCompareSemVer(t *testing.T) {
5444
expected: false,
5545
},
5646
{
57-
v1: "1.0.0",
58-
v2: "1.0.0-2",
47+
name: "complex prerelease identifiers",
48+
v1: "1.0.0-alpha.2",
49+
v2: "1.0.0-alpha.1",
50+
expected: true,
51+
},
52+
{
53+
name: "complex prerelease identifiers reverse",
54+
v1: "1.0.0-alpha.1",
55+
v2: "1.0.0-alpha.2",
5956
expected: false,
6057
},
58+
{
59+
name: "different prerelease identifiers",
60+
v1: "1.0.0-beta.1",
61+
v2: "1.0.0-alpha.2",
62+
expected: true,
63+
},
64+
{
65+
name: "release vs complex prerelease",
66+
v1: "1.0.0",
67+
v2: "1.0.0-beta.1",
68+
expected: true,
69+
},
6170
}
6271

6372
for _, tt := range tests {

0 commit comments

Comments
 (0)