gotestrunner: Make GOROOT absolute before chdir

If GOROOT is a relative path (like it is in Android), then changing
directories to run the test will cause GOROOT to become invalid. So make
it absolute if we're going to change to a different directory.

This allows tests to use the go tools without searching for a valid go
installation.

Change-Id: Ifab0a8533d236054ccf363dfb68b12e0bf66f6f8
This commit is contained in:
Dan Willemsen 2017-01-18 14:42:09 -08:00
parent 10e4357dbb
commit b53f7beb72

View file

@ -24,6 +24,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"syscall"
)
@ -56,12 +57,20 @@ func main() {
test, err := filepath.Abs(flag.Arg(0))
if err != nil {
fmt.Fprintf(os.Stderr, "error: Failed to locate test binary: %s", err)
fmt.Fprintln(os.Stderr, "error: Failed to locate test binary:", err)
}
cmd := exec.Command(test, flag.Args()[1:]...)
if *chdir != "" {
cmd.Dir = *chdir
// GOROOT is commonly a relative path in Android, make it
// absolute if we're changing directories.
if absRoot, err := filepath.Abs(runtime.GOROOT()); err == nil {
os.Setenv("GOROOT", absRoot)
} else {
fmt.Fprintln(os.Stderr, "error: Failed to locate GOROOT:", err)
}
}
cmd.Stderr = os.Stderr