mirror of
https://github.com/golang/go
synced 2024-11-17 21:24:55 -07:00
cmd/go: convert TestExecutableGOROOT to the script framework
Part of converting all tests to the script framework, but also just working around flakiness when adding t.Parallel to TestExecutableGOROOT. Also, undo the changes to copyFile in golang.org/cl/220317 because they didn't help. Fixes #37306 Change-Id: I3348fa1d8f2589febe89604ac19a21df20075c4c Reviewed-on: https://go-review.googlesource.com/c/go/+/220319 Run-TryBot: Michael Matloob <matloob@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
6396bc9df7
commit
b28a07e75f
@ -2454,116 +2454,11 @@ func copyFile(src, dst string, perm os.FileMode) error {
|
||||
}
|
||||
|
||||
_, err = io.Copy(df, sf)
|
||||
err2 := df.Sync()
|
||||
err3 := df.Close()
|
||||
err2 := df.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err2 != nil {
|
||||
return err2
|
||||
}
|
||||
return err3
|
||||
}
|
||||
|
||||
// TestExecutableGOROOT verifies that the cmd/go binary itself uses
|
||||
// os.Executable (when available) to locate GOROOT.
|
||||
func TestExecutableGOROOT(t *testing.T) {
|
||||
skipIfGccgo(t, "gccgo has no GOROOT")
|
||||
|
||||
// Note: Must not call tg methods inside subtests: tg is attached to outer t.
|
||||
tg := testgo(t)
|
||||
defer tg.cleanup()
|
||||
tg.parallel()
|
||||
tg.unsetenv("GOROOT")
|
||||
|
||||
check := func(t *testing.T, exe, want string) {
|
||||
cmd := exec.Command(exe, "env", "GOROOT")
|
||||
cmd.Env = tg.env
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("%s env GOROOT: %v, %s", exe, err, out)
|
||||
}
|
||||
goroot, err := filepath.EvalSymlinks(strings.TrimSpace(string(out)))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want, err = filepath.EvalSymlinks(want)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.EqualFold(goroot, want) {
|
||||
t.Errorf("go env GOROOT:\nhave %s\nwant %s", goroot, want)
|
||||
} else {
|
||||
t.Logf("go env GOROOT: %s", goroot)
|
||||
}
|
||||
}
|
||||
|
||||
tg.makeTempdir()
|
||||
tg.tempDir("new/bin")
|
||||
newGoTool := tg.path("new/bin/go" + exeSuffix)
|
||||
tg.must(copyFile(tg.goTool(), newGoTool, 0775))
|
||||
newRoot := tg.path("new")
|
||||
|
||||
t.Run("RelocatedExe", func(t *testing.T) {
|
||||
// Should fall back to default location in binary,
|
||||
// which is the GOROOT we used when building testgo.exe.
|
||||
check(t, newGoTool, testGOROOT)
|
||||
})
|
||||
|
||||
// If the binary is sitting in a bin dir next to ../pkg/tool, that counts as a GOROOT,
|
||||
// so it should find the new tree.
|
||||
tg.tempDir("new/pkg/tool")
|
||||
t.Run("RelocatedTree", func(t *testing.T) {
|
||||
check(t, newGoTool, newRoot)
|
||||
})
|
||||
|
||||
tg.tempDir("other/bin")
|
||||
symGoTool := tg.path("other/bin/go" + exeSuffix)
|
||||
|
||||
// Symlink into go tree should still find go tree.
|
||||
t.Run("SymlinkedExe", func(t *testing.T) {
|
||||
testenv.MustHaveSymlink(t)
|
||||
if err := os.Symlink(newGoTool, symGoTool); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
check(t, symGoTool, newRoot)
|
||||
})
|
||||
|
||||
tg.must(robustio.RemoveAll(tg.path("new/pkg")))
|
||||
|
||||
// Binaries built in the new tree should report the
|
||||
// new tree when they call runtime.GOROOT.
|
||||
t.Run("RuntimeGoroot", func(t *testing.T) {
|
||||
// Build a working GOROOT the easy way, with symlinks.
|
||||
testenv.MustHaveSymlink(t)
|
||||
if err := os.Symlink(filepath.Join(testGOROOT, "src"), tg.path("new/src")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink(filepath.Join(testGOROOT, "pkg"), tg.path("new/pkg")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cmd := exec.Command(newGoTool, "run", "testdata/print_goroot.go")
|
||||
cmd.Env = tg.env
|
||||
cmd.Stderr = os.Stderr
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
t.Fatalf("%s run testdata/print_goroot.go: %v, %s", newGoTool, err, out)
|
||||
}
|
||||
goroot, err := filepath.EvalSymlinks(strings.TrimSpace(string(out)))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want, err := filepath.EvalSymlinks(tg.path("new"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.EqualFold(goroot, want) {
|
||||
t.Errorf("go run testdata/print_goroot.go:\nhave %s\nwant %s", goroot, want)
|
||||
} else {
|
||||
t.Logf("go run testdata/print_goroot.go: %s", goroot)
|
||||
}
|
||||
})
|
||||
return err2
|
||||
}
|
||||
|
||||
func TestNeedVersion(t *testing.T) {
|
||||
|
14
src/cmd/go/testdata/print_goroot.go
vendored
14
src/cmd/go/testdata/print_goroot.go
vendored
@ -1,14 +0,0 @@
|
||||
// Copyright 2017 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(runtime.GOROOT())
|
||||
}
|
104
src/cmd/go/testdata/script/goroot_executable.txt
vendored
Normal file
104
src/cmd/go/testdata/script/goroot_executable.txt
vendored
Normal file
@ -0,0 +1,104 @@
|
||||
[gccgo] skip
|
||||
|
||||
mkdir $WORK/new/bin
|
||||
|
||||
go build -o $WORK/new/bin/go$GOEXE cmd/go &
|
||||
go build -o $WORK/bin/check$GOEXE check.go &
|
||||
wait
|
||||
|
||||
env TESTGOROOT=$GOROOT
|
||||
env GOROOT=
|
||||
|
||||
# Relocated Executable
|
||||
# cp $TESTGOROOT/bin/go$GOEXE $WORK/new/bin/go$GOEXE
|
||||
exec $WORK/bin/check$GOEXE $WORK/new/bin/go$GOEXE $TESTGOROOT
|
||||
|
||||
# Relocated Tree:
|
||||
# If the binary is sitting in a bin dir next to ../pkg/tool, that counts as a GOROOT,
|
||||
# so it should find the new tree.
|
||||
mkdir $WORK/new/pkg/tool
|
||||
exec $WORK/bin/check$GOEXE $WORK/new/bin/go$GOEXE $WORK/new
|
||||
|
||||
[!symlink] stop 'The rest of the test cases require symlinks'
|
||||
|
||||
# Symlinked Executable:
|
||||
# With a symlink into go tree, we should still find the go tree.
|
||||
mkdir $WORK/other/bin
|
||||
symlink $WORK/other/bin/go$GOEXE -> $WORK/new/bin/go$GOEXE
|
||||
exec $WORK/bin/check$GOEXE $WORK/new/bin/go$GOEXE $WORK/new
|
||||
|
||||
rm $WORK/new/pkg
|
||||
|
||||
# Runtime GOROOT:
|
||||
# Binaries built in the new tree should report the
|
||||
# new tree when they call runtime.GOROOT.
|
||||
symlink $WORK/new/src -> $TESTGOROOT/src
|
||||
symlink $WORK/new/pkg -> $TESTGOROOT/pkg
|
||||
exec $WORK/new/bin/go$GOEXE run check_runtime_goroot.go $WORK/new
|
||||
|
||||
-- check.go --
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
exe := os.Args[1]
|
||||
want := os.Args[2]
|
||||
cmd := exec.Command(exe, "env", "GOROOT")
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s env GOROOT: %v, %s\n", exe, err, out)
|
||||
os.Exit(1)
|
||||
}
|
||||
goroot, err := filepath.EvalSymlinks(strings.TrimSpace(string(out)))
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
want, err = filepath.EvalSymlinks(want)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if !strings.EqualFold(goroot, want) {
|
||||
fmt.Fprintf(os.Stderr, "go env GOROOT:\nhave %s\nwant %s\n", goroot, want)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "go env GOROOT: %s\n", goroot)
|
||||
|
||||
}
|
||||
-- check_runtime_goroot.go --
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
goroot, err := filepath.EvalSymlinks(runtime.GOROOT())
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
want, err := filepath.EvalSymlinks(os.Args[1])
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if !strings.EqualFold(goroot, want) {
|
||||
fmt.Fprintf(os.Stderr, "go env GOROOT:\nhave %s\nwant %s\n", goroot, want)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "go env GOROOT: %s\n", goroot)
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user