Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit d7e206d

Browse files
authored
fix: cmd/update: do not use NewStrictVersion for user-provided version
* fix: cmd/update: use `semver.NewVersion` instead of `semver.StrictNewVersion` as otherwise users must type the explicit trailing zeros e.g. coder update --version 1.21.0 instead of coder update --version 1.21. * chore: cmd/update: add unit tests for getDesiredVersion
1 parent 426b18c commit d7e206d

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

internal/cmd/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func getDesiredVersion(httpClient getter, coderURLArg string, versionArg string)
258258
}
259259

260260
if versionArg != "" {
261-
desiredVersion, err = semver.StrictNewVersion(versionArg)
261+
desiredVersion, err = semver.NewVersion(versionArg)
262262
if err != nil {
263263
return &semver.Version{}, xerrors.Errorf("parse desired version arg: %w", err)
264264
}

internal/cmd/update_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"testing"
1616

1717
"cdr.dev/slog/sloggers/slogtest/assert"
18+
"github.com/Masterminds/semver/v3"
1819
"github.com/manifoldco/promptui"
1920
"github.com/spf13/afero"
2021
"golang.org/x/xerrors"
@@ -390,6 +391,38 @@ func Test_updater_run(t *testing.T) {
390391
}
391392
}
392393

394+
func Test_getDesiredVersion(t *testing.T) {
395+
t.Parallel()
396+
397+
t.Run("invalid version specified by user", func(t *testing.T) {
398+
t.Parallel()
399+
400+
expected := &semver.Version{}
401+
actual, err := getDesiredVersion(nil, "", "not a valid version")
402+
assert.ErrorContains(t, "error should be nil", err, "Invalid Semantic Version")
403+
assert.Equal(t, "expected should equal actual", expected, actual)
404+
})
405+
406+
t.Run("underspecified version from user", func(t *testing.T) {
407+
t.Parallel()
408+
409+
expected, err := semver.StrictNewVersion("1.23.0")
410+
assert.Success(t, "error should be nil", err)
411+
actual, err := getDesiredVersion(nil, "", "1.23")
412+
assert.Success(t, "error should be nil", err)
413+
assert.True(t, "should handle versions without trailing zero", expected.Equal(actual))
414+
})
415+
416+
t.Run("empty coder URL", func(t *testing.T) {
417+
t.Parallel()
418+
419+
expected := &semver.Version{}
420+
actual, err := getDesiredVersion(nil, "", "")
421+
assert.ErrorContains(t, "error should be nil", err, "get coder url")
422+
assert.True(t, "should handle versions without trailing zero", expected.Equal(actual))
423+
})
424+
}
425+
393426
// fakeGetter mocks HTTP requests.
394427
type fakeGetter struct {
395428
M map[string]*fakeGetterResponse

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy