mirror of
https://github.com/fluencelabs/tendermint
synced 2025-05-21 18:41:18 +00:00
Merge pull request #61 from tendermint/60-cmn-gopath-needs-a-default
call go env GOPATH if env var is not found (Refs #60)
This commit is contained in:
commit
1f15ade60d
16
common/os.go
16
common/os.go
@ -6,6 +6,7 @@ import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@ -13,9 +14,22 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
GoPath = os.Getenv("GOPATH")
|
||||
GoPath = gopath()
|
||||
)
|
||||
|
||||
func gopath() string {
|
||||
path := os.Getenv("GOPATH")
|
||||
if len(path) == 0 {
|
||||
goCmd := exec.Command("go", "env", "GOPATH")
|
||||
out, err := goCmd.Output()
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("failed to determine gopath: %v", err))
|
||||
}
|
||||
path = string(out)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func TrapSignal(cb func()) {
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||
|
@ -27,3 +27,26 @@ func TestWriteFileAtomic(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoPath(t *testing.T) {
|
||||
// restore original gopath upon exit
|
||||
path := os.Getenv("GOPATH")
|
||||
defer func() {
|
||||
_ = os.Setenv("GOPATH", path)
|
||||
}()
|
||||
|
||||
err := os.Setenv("GOPATH", "~/testgopath")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
path = gopath()
|
||||
if path != "~/testgopath" {
|
||||
t.Fatalf("gopath should return GOPATH env var if set, got %v", path)
|
||||
}
|
||||
os.Unsetenv("GOPATH")
|
||||
|
||||
path = gopath()
|
||||
if path == "~/testgopath" || path == "" {
|
||||
t.Fatalf("gopath should return go env GOPATH result if env var does not exist, got %v", path)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user