Skip to content

Commit

Permalink
Merge pull request #133 from rkscv/use-go-env
Browse files Browse the repository at this point in the history
Get `GOPATH` with `go env`
  • Loading branch information
nao1215 authored Mar 23, 2024
2 parents 7a4a982 + b78ea8b commit 7f46f0d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 68 deletions.
42 changes: 0 additions & 42 deletions cmd/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"bytes"
"go/build"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -136,47 +135,6 @@ func Test_remove(t *testing.T) {
}
}

func Test_remove_gobin_is_empty(t *testing.T) {
t.Run("GOPATH and GOBIN is empty", func(t *testing.T) {
t.Setenv("GOBIN", "")
t.Setenv("GOPATH", "")

oldBuildGopath := build.Default.GOPATH
build.Default.GOPATH = ""
defer func() { build.Default.GOPATH = oldBuildGopath }()

orgStdout := print.Stdout
orgStderr := print.Stderr
pr, pw, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
print.Stdout = pw
print.Stderr = pw

cmd := &cobra.Command{}
cmd.Flags().BoolP("force", "f", false, "Forcibly remove the file")
if got := remove(cmd, []string{"dummy"}); got != 1 {
t.Errorf("remove() = %v, want %v", got, 1)
}
pw.Close()
print.Stdout = orgStdout
print.Stderr = orgStderr

buf := bytes.Buffer{}
_, err = io.Copy(&buf, pr)
if err != nil {
t.Error(err)
}
defer pr.Close()
got := strings.Split(buf.String(), "\n")

if diff := cmp.Diff([]string{"gup:ERROR: $GOPATH is not set", ""}, got); diff != "" {
t.Errorf("value is mismatch (-want +got):\n%s", diff)
}
})
}

func Test_removeLoop(t *testing.T) {
type args struct {
gobin string
Expand Down
4 changes: 4 additions & 0 deletions internal/goutil/goutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ func goPath() string {
if gopath != "" {
return gopath
}
out, err := exec.Command(goExe, "env", keyGoPath).Output()
if err == nil {
return strings.TrimSpace(string(out))
}
return build.Default.GOPATH
}

Expand Down
32 changes: 6 additions & 26 deletions internal/goutil/goutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package goutil
import (
"bytes"
"errors"
"go/build"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -135,13 +134,13 @@ func TestGetPackageVersion_getting_error_from_gobin(t *testing.T) {
t.Setenv("GOPATH", "")

// Backup and defer restore
oldBuildDefaultGOPATH := build.Default.GOPATH
oldKeyGoPath := keyGoPath
defer func() {
build.Default.GOPATH = oldBuildDefaultGOPATH
keyGoPath = oldKeyGoPath
}()

// Mock the value
build.Default.GOPATH = ""
keyGoPath = t.Name()

// Setting GOBIN, GOPATH and build.Default.GOPATH to empty string
// should be an error internally and return "unknown" as a version.
Expand Down Expand Up @@ -179,13 +178,13 @@ func TestGoBin_gobin_and_gopath_is_empty(t *testing.T) {
t.Setenv("GOPATH", "")

// Backup and defer restore
oldBuildDefaultGOPATH := build.Default.GOPATH
oldKeyGoPath := keyGoPath
defer func() {
build.Default.GOPATH = oldBuildDefaultGOPATH
keyGoPath = oldKeyGoPath
}()

// Mock the value
build.Default.GOPATH = ""
keyGoPath = t.Name()

wantContain := "$GOPATH is not set"
result, got := GoBin()
Expand Down Expand Up @@ -224,25 +223,6 @@ func TestGoBin_golden(t *testing.T) {
}
}

func Test_goPath_get_from_build_default_gopath(t *testing.T) {
// Backup and defer restore
oldKeyGoPath := keyGoPath
defer func() {
keyGoPath = oldKeyGoPath
}()

// Mock the private global variable.
// os.Getenv() in the goPath() shuld return empty since it doesn't exist.
keyGoPath = t.Name()

// Assert to be equal
want := build.Default.GOPATH
got := goPath()
if want != got {
t.Errorf("goPath() should return default GOPATH. got: %v, want: %v", got, want)
}
}

func TestInstall_arg_is_command_line_arguments(t *testing.T) {
err := InstallLatest("command-line-arguments")

Expand Down

0 comments on commit 7f46f0d

Please sign in to comment.