cmd/go: make TestScript/gotoolchain more realistic

- Build the fake go1.999testpath binary from Go source instead of
  special-casing a fake command on Windows.

- Skip the part of the test that uses shell scripts served from the
  test GOPROXY if /bin/sh is not present.

This makes the test more expensive, but also more realistic: notably,
it does not require test hooks to determine whether to run a real or
fake binary.

Change-Id: If14fec52186631d7833eba653c91ec5198dede58
Reviewed-on: https://go-review.googlesource.com/c/go/+/486400
Run-TryBot: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Bryan C. Mills 2023-04-19 15:59:19 -04:00 committed by Gopher Robot
parent ebfd80ba6f
commit 308ca75edb
2 changed files with 18 additions and 32 deletions

View File

@ -189,10 +189,6 @@ func execGoToolchain(gotoolchain, dir, exe string) {
// to allow testing this code even when not on Windows.
if godebug.New("#gotoolchainexec").Value() == "0" || runtime.GOOS == "windows" {
cmd := exec.Command(exe, os.Args[1:]...)
if runtime.GOOS == "windows" && strings.Contains(exe, "go1.999test") {
// See testdata/script/gotoolchain.txt.
cmd = exec.Command("cmd", "/c", "echo pretend we ran "+exe)
}
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

View File

@ -1,8 +1,9 @@
[!GOOS:windows] [!exec:/bin/sh] skip
[!GOOS:windows] chmod 0755 $WORK/bin/go1.999testpath
[short] skip
mkdir $WORK/bin
[!GOOS:plan9] env PATH=$WORK/bin${:}$PATH
[GOOS:plan9] env path=$WORK/bin${:}$path
[GOOS:plan9] replace /bin/sh /bin/rc $WORK/bin/go1.999testpath
go build -o $WORK/bin/ ./go1.999testpath.go # adds .exe extension implicitly on Windows
# Plain go version
go version
@ -11,42 +12,31 @@ go version
# GOTOOLCHAIN from PATH
env GOTOOLCHAIN=go1.999testpath
go version
[!GOOS:windows] stdout 'go1.999testpath here!'
[GOOS:windows] stdout 'pretend we ran .*go1.999testpath'
stdout 'go1.999testpath here!'
# GOTOOLCHAIN from PATH, with forced subprocess
env GOTOOLCHAIN=go1.999testpath
env GODEBUG=gotoolchainexec=0
go version
[!GOOS:windows] stdout 'go1.999testpath here!'
[GOOS:windows] stdout 'pretend we ran .*go1.999testpath'
stdout 'go1.999testpath here!'
env GODEBUG=
# GOTOOLCHAIN from network
env GOTOOLCHAIN=go1.999testmod
go version
stderr 'go: downloading go1.999testmod \(.*/.*\)'
[!GOOS:windows] stdout 'go1.999testmod here!'
[GOOS:windows] stdout 'pretend we ran .*go1.999testmod.*\\bin\\go'
# GOTOOLCHAIN from network, does not exist
env GOTOOLCHAIN=go1.9999x
! go version
stderr 'go: download go1.9999x for .*: toolchain not available'
-- $WORK/bin/go1.999testpath --
#!/bin/sh
echo go1.999testpath here!
-- $WORK/bin/go1.999testpath.bat --
This should say:
@echo go1.999testpath here!
but exec.Command does not directly support batch files.
execGoToolchain in cmd/go/toolchain.go picks off versions
named go1.999test and instead of running them just runs
cmd /c "echo pretend we ran <file>".
# GOTOOLCHAIN from network
[!exec:/bin/sh] stop 'the fake proxy serves shell scripts instead of binaries'
env GOTOOLCHAIN=go1.999testmod
go version
stderr 'go: downloading go1.999testmod \(.*/.*\)'
Since the real toolchain will have an exe file and cmd is an
exe file, this seems like a good enough test.
Changing execGoToolchain to use cmd /c to run the batch file
hangs for unknown reasons.
-- go1.999testpath.go --
package main
import "os"
func main() {
os.Stdout.WriteString("go1.999testpath here!")
}