Skip to content

Commit c6f9ca6

Browse files
committed
Support installing latest with EnsurePackage
Originally EnsurePackage was written for the older go get behavior. Now that we are using go install, the required syntax is a bit different, for example when a version is not specified, we should use @latest. I've also made it easier for people to specify the version both with and without the v prefix, and allow for them specifying either an empty version or latest. Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>
1 parent a5c3ff8 commit c6f9ca6

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

pkg/install.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,22 @@ func getCommandName(pkg string) string {
6464
// InstallPackage installs the latest version of a package.
6565
//
6666
// When version is specified, install that version. Otherwise install the most
67-
// recent code from the default branch.
67+
// recent version.
6868
func InstallPackage(pkg string, version string) error {
6969
gopath.EnsureGopathBin()
7070

7171
cmd := getCommandName(pkg)
7272

73-
// Optionally install a specific version of the package
74-
moduleVersion := ""
75-
if version != "" {
76-
if strings.HasPrefix(version, "v") {
77-
moduleVersion = "@" + version
78-
} else {
79-
moduleVersion = "@v" + version
73+
if version == "" {
74+
version = "latest"
75+
} else {
76+
if version != "latest" && !strings.HasPrefix(version, "v") {
77+
version = "v" + version
8078
}
8179
}
8280

83-
fmt.Printf("Installing %s%s\n", cmd, moduleVersion)
84-
err := shx.Command("go", "install", pkg+moduleVersion).
81+
fmt.Printf("Installing %s@%s\n", cmd, version)
82+
err := shx.Command("go", "install", pkg+"@"+version).
8583
Env("GO111MODULE=on").In(os.TempDir()).RunE()
8684
if err != nil {
8785
return err
@@ -132,10 +130,13 @@ func IsCommandAvailable(cmd string, version string, versionArgs ...string) (bool
132130
}
133131

134132
// If no version is specified, report that it is installed
135-
if version == "" {
133+
if version == "" || version == "latest" {
136134
return true, nil
137135
}
138136

137+
// Trim the leading v prefix if present so that we are more likely to get a hit on the version
138+
version = strings.TrimPrefix(version, "v")
139+
139140
// Get the installed version
140141
versionOutput, err := shx.OutputE(cmd, versionArgs...)
141142
if err != nil {

pkg/install_test.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,30 @@ func TestGetCommandName(t *testing.T) {
4242
})
4343
}
4444

45-
func TestEnsurePackage_MajorVersion(t *testing.T) {
45+
func TestEnsurePackage(t *testing.T) {
4646
os.Setenv(mg.VerboseEnv, "true")
47-
err, cleanup := gopath.UseTempGopath()
48-
require.NoError(t, err, "Failed to set up a temporary GOPATH")
49-
defer cleanup()
47+
defer os.Unsetenv(mg.VerboseEnv)
5048

51-
hasCmd, err := IsCommandAvailable("testpkg", "")
52-
require.False(t, hasCmd)
53-
err = EnsurePackage("github.com/carolynvs/testpkg/v2", "v2.0.1", "--version")
54-
require.NoError(t, err)
49+
testcases := []struct {
50+
name string
51+
version string
52+
}{
53+
{name: "with prefix", version: "v2.0.1"},
54+
{name: "without prefix", version: "2.0.1"},
55+
{name: "no version", version: ""},
56+
{name: "latest version", version: "latest"},
57+
}
58+
59+
for _, tc := range testcases {
60+
t.Run(tc.name, func(t *testing.T) {
61+
err, cleanup := gopath.UseTempGopath()
62+
require.NoError(t, err, "Failed to set up a temporary GOPATH")
63+
defer cleanup()
64+
65+
hasCmd, err := IsCommandAvailable("testpkg", "")
66+
require.False(t, hasCmd)
67+
err = EnsurePackage("github.com/carolynvs/testpkg/v2", tc.version, "--version")
68+
require.NoError(t, err)
69+
})
70+
}
5571
}

shx/prepared_command_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ func TestPreparedCommand_Run_Fail(t *testing.T) {
3838
t.Fatal("expected shx.Command to fail")
3939
}
4040

41-
wantStderr := "go run: no go files listed\n"
42-
assert.Equal(t, wantStderr, gotStderr)
41+
wantStderr := "no go files listed"
42+
assert.Contains(t, gotStderr, wantStderr)
4343
}
4444

4545
func TestPreparedCommand_Run_Verbose(t *testing.T) {
@@ -91,8 +91,8 @@ func TestPreparedCommand_RunE_Fail(t *testing.T) {
9191
t.Fatal("expected the shx.Command to fail")
9292
}
9393

94-
wantStderr := "go run: no go files listed\n"
95-
assert.Equal(t, wantStderr, gotStderr)
94+
wantStderr := "no go files listed"
95+
assert.Contains(t, gotStderr, wantStderr)
9696
}
9797

9898
func TestPreparedCommand_RunE_Verbose(t *testing.T) {
@@ -217,8 +217,8 @@ func TestPreparedCommand_Output_Fail(t *testing.T) {
217217
t.Fatal("expected shx.Command to fail")
218218
}
219219

220-
wantStderr := "go run: no go files listed\n"
221-
assert.Equal(t, wantStderr, gotStderr)
220+
wantStderr := "no go files listed"
221+
assert.Contains(t, gotStderr, wantStderr)
222222
assert.Empty(t, gotOutput)
223223
}
224224

@@ -276,8 +276,8 @@ func TestPreparedCommand_OutputE_Fail(t *testing.T) {
276276
t.Fatal("expected the shx.Command to fail")
277277
}
278278

279-
wantStderr := "go run: no go files listed\n"
280-
assert.Equal(t, wantStderr, gotStderr)
279+
wantStderr := "no go files listed"
280+
assert.Contains(t, gotStderr, wantStderr)
281281
assert.Empty(t, gotOutput)
282282
}
283283

0 commit comments

Comments
 (0)