Skip to content

Commit

Permalink
test: use the version of Go used to run run.go
Browse files Browse the repository at this point in the history
Currently, the top-level testsuite always uses whatever version
of Go is found in the PATH to execute all the tests. This
forces the developers to tweak the PATH to run the testsuite.

Change it to use the same version of Go used to run run.go.
This allows developers to run the testsuite using the tip
compiler by simply saying "../bin/go run run.go".

I think this is a better solution compared to always forcing
"../bin/go", because it allows developers to run the testsuite
using different Go versions, for instance to check if a new
test is fixed in tip compared to the installed compiler.

Fixes golang#24217

Change-Id: I41b299c753b6e77c41e28be9091b2b630efea9d2
Reviewed-on: https://go-review.googlesource.com/98439
Run-TryBot: Giovanni Bajo <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Keith Randall <[email protected]>
  • Loading branch information
rasky committed Mar 3, 2018
1 parent 74a92b8 commit ec0b8c0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
2 changes: 1 addition & 1 deletion test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ They are run as part of all.bash.

To run just these tests, execute:

go run run.go
../bin/go run run.go

Standard library tests should be written as regular Go tests in the appropriate package.

Expand Down
54 changes: 35 additions & 19 deletions test/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,22 @@ func toolPath(name string) string {
return p
}

// goTool reports the path of the go tool to use to run the tests.
// If possible, use the same Go used to run run.go, otherwise
// fallback to the go version found in the PATH.
func goTool() string {
var exeSuffix string
if runtime.GOOS == "windows" {
exeSuffix = ".exe"
}
path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix)
if _, err := os.Stat(path); err == nil {
return path
}
// Just run "go" from PATH
return "go"
}

func shardMatch(name string) bool {
if *shards == 0 {
return true
Expand Down Expand Up @@ -194,7 +210,7 @@ func goFiles(dir string) []string {
type runCmd func(...string) ([]byte, error)

func compileFile(runcmd runCmd, longname string, flags []string) (out []byte, err error) {
cmd := []string{"go", "tool", "compile", "-e"}
cmd := []string{goTool(), "tool", "compile", "-e"}
cmd = append(cmd, flags...)
if *linkshared {
cmd = append(cmd, "-dynlink", "-installsuffix=dynlink")
Expand All @@ -204,7 +220,7 @@ func compileFile(runcmd runCmd, longname string, flags []string) (out []byte, er
}

func compileInDir(runcmd runCmd, dir string, flags []string, names ...string) (out []byte, err error) {
cmd := []string{"go", "tool", "compile", "-e", "-D", ".", "-I", "."}
cmd := []string{goTool(), "tool", "compile", "-e", "-D", ".", "-I", "."}
cmd = append(cmd, flags...)
if *linkshared {
cmd = append(cmd, "-dynlink", "-installsuffix=dynlink")
Expand All @@ -217,7 +233,7 @@ func compileInDir(runcmd runCmd, dir string, flags []string, names ...string) (o

func linkFile(runcmd runCmd, goname string) (err error) {
pfile := strings.Replace(goname, ".go", ".o", -1)
cmd := []string{"go", "tool", "link", "-w", "-o", "a.exe", "-L", "."}
cmd := []string{goTool(), "tool", "link", "-w", "-o", "a.exe", "-L", "."}
if *linkshared {
cmd = append(cmd, "-linkshared", "-installsuffix=dynlink")
}
Expand Down Expand Up @@ -599,7 +615,7 @@ func (t *test) run() {
os.Setenv("GOOS", "linux")
os.Setenv("GOARCH", arch)

cmdline := []string{"go", "build", "-gcflags", "-S"}
cmdline := []string{goTool(), "build", "-gcflags", "-S"}
cmdline = append(cmdline, flags...)
cmdline = append(cmdline, long)
out, err := runcmd(cmdline...)
Expand All @@ -616,7 +632,7 @@ func (t *test) run() {

case "errorcheck":
// TODO(gri) remove need for -C (disable printing of columns in error messages)
cmdline := []string{"go", "tool", "compile", "-C", "-e", "-o", "a.o"}
cmdline := []string{goTool(), "tool", "compile", "-C", "-e", "-o", "a.o"}
// No need to add -dynlink even if linkshared if we're just checking for errors...
cmdline = append(cmdline, flags...)
cmdline = append(cmdline, long)
Expand Down Expand Up @@ -730,7 +746,7 @@ func (t *test) run() {
}

case "build":
_, err := runcmd("go", "build", goGcflags(), "-o", "a.exe", long)
_, err := runcmd(goTool(), "build", goGcflags(), "-o", "a.exe", long)
if err != nil {
t.err = err
}
Expand All @@ -756,7 +772,7 @@ func (t *test) run() {

}
var objs []string
cmd := []string{"go", "tool", "compile", "-e", "-D", ".", "-I", ".", "-o", "go.o"}
cmd := []string{goTool(), "tool", "compile", "-e", "-D", ".", "-I", ".", "-o", "go.o"}
if len(asms) > 0 {
cmd = append(cmd, "-asmhdr", "go_asm.h")
}
Expand All @@ -770,7 +786,7 @@ func (t *test) run() {
}
objs = append(objs, "go.o")
if len(asms) > 0 {
cmd = []string{"go", "tool", "asm", "-e", "-I", ".", "-o", "asm.o"}
cmd = []string{goTool(), "tool", "asm", "-e", "-I", ".", "-o", "asm.o"}
for _, file := range asms {
cmd = append(cmd, filepath.Join(longdir, file.Name()))
}
Expand All @@ -781,14 +797,14 @@ func (t *test) run() {
}
objs = append(objs, "asm.o")
}
cmd = []string{"go", "tool", "pack", "c", "all.a"}
cmd = []string{goTool(), "tool", "pack", "c", "all.a"}
cmd = append(cmd, objs...)
_, err = runcmd(cmd...)
if err != nil {
t.err = err
break
}
cmd = []string{"go", "tool", "link", "-o", "a.exe", "all.a"}
cmd = []string{goTool(), "tool", "link", "-o", "a.exe", "all.a"}
_, err = runcmd(cmd...)
if err != nil {
t.err = err
Expand All @@ -809,7 +825,7 @@ func (t *test) run() {
case "buildrun": // build binary, then run binary, instead of go run. Useful for timeout tests where failure mode is infinite loop.
// TODO: not supported on NaCl
useTmp = true
cmd := []string{"go", "build", goGcflags(), "-o", "a.exe"}
cmd := []string{goTool(), "build", goGcflags(), "-o", "a.exe"}
if *linkshared {
cmd = append(cmd, "-linkshared")
}
Expand Down Expand Up @@ -845,20 +861,20 @@ func (t *test) run() {
// Because we run lots of trivial test programs,
// the time adds up.
pkg := filepath.Join(t.tempDir, "pkg.a")
if _, err := runcmd("go", "tool", "compile", "-o", pkg, t.goFileName()); err != nil {
if _, err := runcmd(goTool(), "tool", "compile", "-o", pkg, t.goFileName()); err != nil {
t.err = err
return
}
exe := filepath.Join(t.tempDir, "test.exe")
cmd := []string{"go", "tool", "link", "-s", "-w"}
cmd := []string{goTool(), "tool", "link", "-s", "-w"}
cmd = append(cmd, "-o", exe, pkg)
if _, err := runcmd(cmd...); err != nil {
t.err = err
return
}
out, err = runcmd(append([]string{exe}, args...)...)
} else {
cmd := []string{"go", "run", goGcflags()}
cmd := []string{goTool(), "run", goGcflags()}
if *linkshared {
cmd = append(cmd, "-linkshared")
}
Expand All @@ -880,7 +896,7 @@ func (t *test) run() {
<-rungatec
}()
useTmp = false
cmd := []string{"go", "run", goGcflags()}
cmd := []string{goTool(), "run", goGcflags()}
if *linkshared {
cmd = append(cmd, "-linkshared")
}
Expand All @@ -895,7 +911,7 @@ func (t *test) run() {
t.err = fmt.Errorf("write tempfile:%s", err)
return
}
cmd = []string{"go", "run", goGcflags()}
cmd = []string{goTool(), "run", goGcflags()}
if *linkshared {
cmd = append(cmd, "-linkshared")
}
Expand All @@ -911,7 +927,7 @@ func (t *test) run() {

case "errorcheckoutput":
useTmp = false
cmd := []string{"go", "run", goGcflags()}
cmd := []string{goTool(), "run", goGcflags()}
if *linkshared {
cmd = append(cmd, "-linkshared")
}
Expand All @@ -927,7 +943,7 @@ func (t *test) run() {
t.err = fmt.Errorf("write tempfile:%s", err)
return
}
cmdline := []string{"go", "tool", "compile", "-e", "-o", "a.o"}
cmdline := []string{goTool(), "tool", "compile", "-e", "-o", "a.o"}
cmdline = append(cmdline, flags...)
cmdline = append(cmdline, tfile)
out, err = runcmd(cmdline...)
Expand Down Expand Up @@ -1147,7 +1163,7 @@ func (t *test) updateErrors(out, file string) {
return
}
// Polish.
exec.Command("go", "fmt", file).CombinedOutput()
exec.Command(goTool(), "fmt", file).CombinedOutput()
}

// matchPrefix reports whether s is of the form ^(.*/)?prefix(:|[),
Expand Down

0 comments on commit ec0b8c0

Please sign in to comment.