Skip to content

Commit

Permalink
Merge pull request #16 from carolynvs/ensure-latest-package
Browse files Browse the repository at this point in the history
Support installing latest with EnsurePackage
  • Loading branch information
carolynvs authored Mar 27, 2022
2 parents a5c3ff8 + c6f9ca6 commit 7c11ea4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 27 deletions.
23 changes: 12 additions & 11 deletions pkg/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
32 changes: 24 additions & 8 deletions pkg/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
16 changes: 8 additions & 8 deletions shx/prepared_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 7c11ea4

Please sign in to comment.