Skip to content

Commit e13307c

Browse files
authored
fix: version parsing when -rc is in curVersion (#991)
* fix: version parsing when -rc is in curVersion * chore: preserve original version string for debug prints
1 parent d602b92 commit e13307c

2 files changed

Lines changed: 68 additions & 4 deletions

File tree

runner/cmd/nexa-cli/version.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,24 @@ func version() *cobra.Command {
4646
// Returns: -1 if v1 < v2, 0 if v1 == v2, 1 if v1 > v2
4747
func compareVersion(v1, v2 string) (int, error) {
4848
parseVersion := func(v string) ([3]int, error) {
49-
parts := strings.Split(strings.TrimPrefix(v, "v"), ".")
49+
origV := v
50+
v = strings.TrimPrefix(v, "v")
51+
// Strip pre-release suffixes like -rc2, -beta, -alpha, etc.
52+
if idx := strings.IndexAny(v, "-+"); idx != -1 {
53+
v = v[:idx]
54+
}
55+
parts := strings.Split(v, ".")
5056
if len(parts) != 3 {
51-
return [3]int{}, fmt.Errorf("invalid format: %s", v)
57+
return [3]int{}, fmt.Errorf("invalid format: %s", origV)
5258
}
5359
var nums [3]int
5460
for i, p := range parts {
5561
n, err := strconv.Atoi(p)
5662
if err != nil {
57-
return [3]int{}, fmt.Errorf("invalid format: %s", v)
63+
return [3]int{}, fmt.Errorf("invalid format: %s", origV)
5864
}
5965
if n < 0 {
60-
return [3]int{}, fmt.Errorf("invalid format: %s", v)
66+
return [3]int{}, fmt.Errorf("invalid format: %s", origV)
6167
}
6268
nums[i] = n
6369
}

runner/cmd/nexa-cli/version_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,64 @@ func TestCompareVersion(t *testing.T) {
160160
wantErr: false,
161161
},
162162

163+
// Pre-release versions (should compare base version only)
164+
{
165+
name: "rc version equals stable",
166+
v1: "v0.2.68-rc2",
167+
v2: "v0.2.68",
168+
expected: 0,
169+
wantErr: false,
170+
},
171+
{
172+
name: "rc version greater than older stable",
173+
v1: "v0.2.68-rc2",
174+
v2: "v0.2.67",
175+
expected: 1,
176+
wantErr: false,
177+
},
178+
{
179+
name: "rc version less than newer stable",
180+
v1: "v0.2.68-rc2",
181+
v2: "v0.2.69",
182+
expected: -1,
183+
wantErr: false,
184+
},
185+
{
186+
name: "beta version comparison",
187+
v1: "v1.0.0-beta",
188+
v2: "v1.0.0",
189+
expected: 0,
190+
wantErr: false,
191+
},
192+
{
193+
name: "alpha version comparison",
194+
v1: "v2.5.0-alpha.1",
195+
v2: "v2.4.9",
196+
expected: 1,
197+
wantErr: false,
198+
},
199+
{
200+
name: "build metadata with plus",
201+
v1: "v1.0.0+build123",
202+
v2: "v1.0.0",
203+
expected: 0,
204+
wantErr: false,
205+
},
206+
{
207+
name: "complex prerelease version",
208+
v1: "v3.2.1-rc1+build456",
209+
v2: "v3.2.1",
210+
expected: 0,
211+
wantErr: false,
212+
},
213+
{
214+
name: "two rc versions",
215+
v1: "v0.2.68-rc1",
216+
v2: "v0.2.68-rc2",
217+
expected: 0,
218+
wantErr: false,
219+
},
220+
163221
// Invalid format cases
164222
{
165223
name: "invalid format - missing parts",

0 commit comments

Comments
 (0)