Merge pull request #68 from tendermint/bugfix/gopath-executes-go
make GoPath a function
This commit is contained in:
commit
0a652499ea
13
common/os.go
13
common/os.go
|
@ -13,11 +13,15 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var gopath string
|
||||||
GoPath = gopath()
|
|
||||||
)
|
// GoPath returns GOPATH env variable value. If it is not set, this function
|
||||||
|
// will try to call `go env GOPATH` subcommand.
|
||||||
|
func GoPath() string {
|
||||||
|
if gopath != "" {
|
||||||
|
return gopath
|
||||||
|
}
|
||||||
|
|
||||||
func gopath() string {
|
|
||||||
path := os.Getenv("GOPATH")
|
path := os.Getenv("GOPATH")
|
||||||
if len(path) == 0 {
|
if len(path) == 0 {
|
||||||
goCmd := exec.Command("go", "env", "GOPATH")
|
goCmd := exec.Command("go", "env", "GOPATH")
|
||||||
|
@ -27,6 +31,7 @@ func gopath() string {
|
||||||
}
|
}
|
||||||
path = string(out)
|
path = string(out)
|
||||||
}
|
}
|
||||||
|
gopath = path
|
||||||
return path
|
return path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -39,14 +39,31 @@ func TestGoPath(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
path = gopath()
|
path = GoPath()
|
||||||
if path != "~/testgopath" {
|
if path != "~/testgopath" {
|
||||||
t.Fatalf("gopath should return GOPATH env var if set, got %v", path)
|
t.Fatalf("should get GOPATH env var value, got %v", path)
|
||||||
}
|
}
|
||||||
os.Unsetenv("GOPATH")
|
os.Unsetenv("GOPATH")
|
||||||
|
|
||||||
path = gopath()
|
path = GoPath()
|
||||||
if path == "~/testgopath" || path == "" {
|
if path != "~/testgopath" {
|
||||||
t.Fatalf("gopath should return go env GOPATH result if env var does not exist, got %v", path)
|
t.Fatalf("subsequent calls should return the same value, got %v", path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGoPathWithoutEnvVar(t *testing.T) {
|
||||||
|
// restore original gopath upon exit
|
||||||
|
path := os.Getenv("GOPATH")
|
||||||
|
defer func() {
|
||||||
|
_ = os.Setenv("GOPATH", path)
|
||||||
|
}()
|
||||||
|
|
||||||
|
os.Unsetenv("GOPATH")
|
||||||
|
// reset cache
|
||||||
|
gopath = ""
|
||||||
|
|
||||||
|
path = GoPath()
|
||||||
|
if path == "" || path == "~/testgopath" {
|
||||||
|
t.Fatalf("should get nonempty result of calling go env GOPATH, got %v", path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue