Skip to content

Commit

Permalink
Add unit test for goutil package
Browse files Browse the repository at this point in the history
  • Loading branch information
KEINOS committed Sep 19, 2022
1 parent 6455870 commit 797b4bd
Show file tree
Hide file tree
Showing 3 changed files with 527 additions and 18 deletions.
3 changes: 2 additions & 1 deletion internal/goutil/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ func ExampleInstall() {
// Install installs an executable from a Go package.
err := goutil.Install("example.com/unknown_user/unknown_package")

// If the package is not found or invalid, Install returns an error
// If the package is not found or invalid, Install returns an error.
// In this case it should be an error.
if err == nil {
log.Fatal("example Install failed. non existing/invalid package should return error")
}
Expand Down
34 changes: 23 additions & 11 deletions internal/goutil/goutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ import (
"github.com/nao1215/gup/internal/print"
)

// Internal variables to mock/monkey-patch behaviors in tests.
var (
// goExe is the executable name for the go command.
goExe = "go"
// keyGoBin is the key name of the env variable for "GOBIN".
keyGoBin = "GOBIN"
// keyGoPath is the key name of the env variable for "GOPATH".
keyGoPath = "GOPATH"
// osMkdirTemp is a copy of os.MkdirTemp to ease testing.
osMkdirTemp = os.MkdirTemp
)

// GoPaths has $GOBIN and $GOPATH
type GoPaths struct {
// GOBIN is $GOBIN
Expand Down Expand Up @@ -100,17 +112,17 @@ func NewGoPaths() *GoPaths {

// StartDryRunMode change the GOBIN or GOPATH settings to install the binaries in the temporary directory.
func (gp *GoPaths) StartDryRunMode() error {
tmpDir, err := os.MkdirTemp("", "")
tmpDir, err := osMkdirTemp("", "")
if err != nil {
return err
}

if gp.GOBIN != "" {
if err := os.Setenv("GOBIN", tmpDir); err != nil {
if err := os.Setenv(keyGoBin, tmpDir); err != nil {
return err
}
} else if gp.GOPATH != "" {
if err := os.Setenv("GOPATH", tmpDir); err != nil {
if err := os.Setenv(keyGoPath, tmpDir); err != nil {
return err
}
} else {
Expand All @@ -122,11 +134,11 @@ func (gp *GoPaths) StartDryRunMode() error {
// EndDryRunMode restore the GOBIN or GOPATH settings.
func (gp *GoPaths) EndDryRunMode() error {
if gp.GOBIN != "" {
if err := os.Setenv("GOBIN", gp.GOBIN); err != nil {
if err := os.Setenv(keyGoBin, gp.GOBIN); err != nil {
return err
}
} else if gp.GOPATH != "" {
if err := os.Setenv("GOPATH", gp.GOPATH); err != nil {
if err := os.Setenv(keyGoPath, gp.GOPATH); err != nil {
return err
}
} else {
Expand All @@ -151,7 +163,7 @@ func (gp *GoPaths) removeTmpDir() error {

// CanUseGoCmd check whether go command install in the system.
func CanUseGoCmd() error {
_, err := exec.LookPath("go")
_, err := exec.LookPath(goExe)
return err
}

Expand All @@ -160,15 +172,15 @@ func Install(importPath string) error {
if importPath == "command-line-arguments" {
return errors.New("is devel-binary copied from local environment")
}
if err := exec.Command("go", "install", importPath+"@latest").Run(); err != nil {
if err := exec.Command(goExe, "install", importPath+"@latest").Run(); err != nil {
return fmt.Errorf("can't install %s: %w", importPath, err)
}
return nil
}

// GetLatestVer execute "$ go list -m -f {{.Version}} <importPath>@latest"
func GetLatestVer(modulePath string) (string, error) {
out, err := exec.Command("go", "list", "-m", "-f", "{{.Version}}", modulePath+"@latest").Output()
out, err := exec.Command(goExe, "list", "-m", "-f", "{{.Version}}", modulePath+"@latest").Output()
if err != nil {
return "", errors.New("can't check " + modulePath)
}
Expand All @@ -177,7 +189,7 @@ func GetLatestVer(modulePath string) (string, error) {

// goPath return GOPATH environment variable.
func goPath() string {
gopath := os.Getenv("GOPATH")
gopath := os.Getenv(keyGoPath)
if gopath != "" {
return gopath
}
Expand All @@ -186,7 +198,7 @@ func goPath() string {

// goBin return GOBIN environment variable.
func goBin() string {
return os.Getenv("GOBIN")
return os.Getenv(keyGoBin)
}

// GoBin return $GOPATH/bin directory path.
Expand All @@ -205,7 +217,7 @@ func GoBin() (string, error) {

// GoVersionWithOptionM return result of "$ go version -m"
func GoVersionWithOptionM(bin string) ([]string, error) {
out, err := exec.Command("go", "version", "-m", bin).Output()
out, err := exec.Command(goExe, "version", "-m", bin).Output()
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 797b4bd

Please sign in to comment.