diff --git a/pkg/install.go b/pkg/install.go index dc0e6c9..a620a00 100644 --- a/pkg/install.go +++ b/pkg/install.go @@ -64,24 +64,22 @@ func getCommandName(pkg string) string { // InstallPackage installs the latest version of a package. // // When version is specified, install that version. Otherwise install the most -// recent code from the default branch. +// recent version. func InstallPackage(pkg string, version string) error { gopath.EnsureGopathBin() cmd := getCommandName(pkg) - // Optionally install a specific version of the package - moduleVersion := "" - if version != "" { - if strings.HasPrefix(version, "v") { - moduleVersion = "@" + version - } else { - moduleVersion = "@v" + version + if version == "" { + version = "latest" + } else { + if version != "latest" && !strings.HasPrefix(version, "v") { + version = "v" + version } } - fmt.Printf("Installing %s%s\n", cmd, moduleVersion) - err := shx.Command("go", "install", pkg+moduleVersion). + fmt.Printf("Installing %s@%s\n", cmd, version) + err := shx.Command("go", "install", pkg+"@"+version). Env("GO111MODULE=on").In(os.TempDir()).RunE() if err != nil { return err @@ -132,10 +130,13 @@ func IsCommandAvailable(cmd string, version string, versionArgs ...string) (bool } // If no version is specified, report that it is installed - if version == "" { + if version == "" || version == "latest" { return true, nil } + // Trim the leading v prefix if present so that we are more likely to get a hit on the version + version = strings.TrimPrefix(version, "v") + // Get the installed version versionOutput, err := shx.OutputE(cmd, versionArgs...) if err != nil { diff --git a/pkg/install_test.go b/pkg/install_test.go index ca0393a..47a77dd 100644 --- a/pkg/install_test.go +++ b/pkg/install_test.go @@ -42,14 +42,30 @@ func TestGetCommandName(t *testing.T) { }) } -func TestEnsurePackage_MajorVersion(t *testing.T) { +func TestEnsurePackage(t *testing.T) { os.Setenv(mg.VerboseEnv, "true") - err, cleanup := gopath.UseTempGopath() - require.NoError(t, err, "Failed to set up a temporary GOPATH") - defer cleanup() + defer os.Unsetenv(mg.VerboseEnv) - hasCmd, err := IsCommandAvailable("testpkg", "") - require.False(t, hasCmd) - err = EnsurePackage("github.com/carolynvs/testpkg/v2", "v2.0.1", "--version") - require.NoError(t, err) + testcases := []struct { + name string + version string + }{ + {name: "with prefix", version: "v2.0.1"}, + {name: "without prefix", version: "2.0.1"}, + {name: "no version", version: ""}, + {name: "latest version", version: "latest"}, + } + + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + err, cleanup := gopath.UseTempGopath() + require.NoError(t, err, "Failed to set up a temporary GOPATH") + defer cleanup() + + hasCmd, err := IsCommandAvailable("testpkg", "") + require.False(t, hasCmd) + err = EnsurePackage("github.com/carolynvs/testpkg/v2", tc.version, "--version") + require.NoError(t, err) + }) + } } diff --git a/shx/prepared_command_test.go b/shx/prepared_command_test.go index b1e75ef..798e8df 100644 --- a/shx/prepared_command_test.go +++ b/shx/prepared_command_test.go @@ -38,8 +38,8 @@ func TestPreparedCommand_Run_Fail(t *testing.T) { t.Fatal("expected shx.Command to fail") } - wantStderr := "go run: no go files listed\n" - assert.Equal(t, wantStderr, gotStderr) + wantStderr := "no go files listed" + assert.Contains(t, gotStderr, wantStderr) } func TestPreparedCommand_Run_Verbose(t *testing.T) { @@ -91,8 +91,8 @@ func TestPreparedCommand_RunE_Fail(t *testing.T) { t.Fatal("expected the shx.Command to fail") } - wantStderr := "go run: no go files listed\n" - assert.Equal(t, wantStderr, gotStderr) + wantStderr := "no go files listed" + assert.Contains(t, gotStderr, wantStderr) } func TestPreparedCommand_RunE_Verbose(t *testing.T) { @@ -217,8 +217,8 @@ func TestPreparedCommand_Output_Fail(t *testing.T) { t.Fatal("expected shx.Command to fail") } - wantStderr := "go run: no go files listed\n" - assert.Equal(t, wantStderr, gotStderr) + wantStderr := "no go files listed" + assert.Contains(t, gotStderr, wantStderr) assert.Empty(t, gotOutput) } @@ -276,8 +276,8 @@ func TestPreparedCommand_OutputE_Fail(t *testing.T) { t.Fatal("expected the shx.Command to fail") } - wantStderr := "go run: no go files listed\n" - assert.Equal(t, wantStderr, gotStderr) + wantStderr := "no go files listed" + assert.Contains(t, gotStderr, wantStderr) assert.Empty(t, gotOutput) }