Skip to content

Commit 6ef25c2

Browse files
committed
cmd/go: convert testCDAndGOPATHAreDifferent to the script framework
This is a bit complex. There's a driver program to run go with modifications to the GOPATH used to test Windows. Also remove the cd method on testgoData, because this was the last function that used it. Part of converting all tests to script framework to improve test parallelism. Updates #36320 Updates #17751 Change-Id: I3e8e27f37fd3701bd36b6365b128dd73b69181c0 Reviewed-on: https://go-review.googlesource.com/c/go/+/214578 Reviewed-by: Jay Conrod <[email protected]>
1 parent e674972 commit 6ef25c2

File tree

2 files changed

+72
-52
lines changed

2 files changed

+72
-52
lines changed

src/cmd/go/go_test.go

-52
Original file line numberDiff line numberDiff line change
@@ -388,24 +388,6 @@ func (tg *testgoData) pwd() string {
388388
return wd
389389
}
390390

391-
// cd changes the current directory to the named directory. Note that
392-
// using this means that the test must not be run in parallel with any
393-
// other tests.
394-
func (tg *testgoData) cd(dir string) {
395-
tg.t.Helper()
396-
if tg.inParallel {
397-
tg.t.Fatal("internal testsuite error: changing directory when running in parallel")
398-
}
399-
if tg.wd == "" {
400-
tg.wd = tg.pwd()
401-
}
402-
abs, err := filepath.Abs(dir)
403-
tg.must(os.Chdir(dir))
404-
if err == nil {
405-
tg.setenv("PWD", abs)
406-
}
407-
}
408-
409391
// sleep sleeps for one tick, where a tick is a conservative estimate
410392
// of how long it takes for a file modification to get a different
411393
// mtime.
@@ -2872,40 +2854,6 @@ func TestLinkerTmpDirIsDeleted(t *testing.T) {
28722854
}
28732855
}
28742856

2875-
func testCDAndGOPATHAreDifferent(tg *testgoData, cd, gopath string) {
2876-
skipIfGccgo(tg.t, "gccgo does not support -ldflags -X")
2877-
tg.setenv("GOPATH", gopath)
2878-
2879-
tg.tempDir("dir")
2880-
exe := tg.path("dir/a.exe")
2881-
2882-
tg.cd(cd)
2883-
2884-
tg.run("build", "-o", exe, "-ldflags", "-X=my.pkg.Text=linkXworked")
2885-
out, err := exec.Command(exe).CombinedOutput()
2886-
if err != nil {
2887-
tg.t.Fatal(err)
2888-
}
2889-
if string(out) != "linkXworked\n" {
2890-
tg.t.Errorf(`incorrect output with GOPATH=%q and CD=%q: expected "linkXworked\n", but have %q`, gopath, cd, string(out))
2891-
}
2892-
}
2893-
2894-
func TestCDAndGOPATHAreDifferent(t *testing.T) {
2895-
tg := testgo(t)
2896-
defer tg.cleanup()
2897-
2898-
gopath := filepath.Join(tg.pwd(), "testdata")
2899-
cd := filepath.Join(gopath, "src/my.pkg/main")
2900-
2901-
testCDAndGOPATHAreDifferent(tg, cd, gopath)
2902-
if runtime.GOOS == "windows" {
2903-
testCDAndGOPATHAreDifferent(tg, cd, strings.ReplaceAll(gopath, `\`, `/`))
2904-
testCDAndGOPATHAreDifferent(tg, cd, strings.ToUpper(gopath))
2905-
testCDAndGOPATHAreDifferent(tg, cd, strings.ToLower(gopath))
2906-
}
2907-
}
2908-
29092857
// Issue 25093.
29102858
func TestCoverpkgTestOnly(t *testing.T) {
29112859
skipIfGccgo(t, "gccgo has no cover tool")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
[gccgo] skip 'gccgo does not support -ldflags -X'
2+
go build run_go.go
3+
4+
# Apply identity function to GOPATH
5+
exec ./run_go$GOEXE $GOPATH/src/my.pkg/main $GOPATH IDENTITY build -o $WORK/tmp/a.exe -ldflags -X=my.pkg.Text=linkXworked
6+
exec $WORK/tmp/a.exe
7+
stderr 'linkXworked'
8+
rm $WORK/tmp/a.exe
9+
10+
[!windows] stop 'rest of the tests only apply to Windows'
11+
12+
# Replace '\' with '/' in GOPATH
13+
exec ./run_go$GOEXE $GOPATH/src/my.pkg/main $GOPATH REPLACE_SLASH build -o $WORK/tmp/a.exe -ldflags -X=my.pkg.Text=linkXworked
14+
exec $WORK/tmp/a.exe
15+
stderr 'linkXworked'
16+
rm $WORK/tmp/a.exe
17+
18+
# Apply identity function to GOPATH
19+
exec ./run_go$GOEXE $GOPATH/src/my.pkg/main $GOPATH UPPER build -o $WORK/tmp/a.exe -ldflags -X=my.pkg.Text=linkXworked
20+
exec $WORK/tmp/a.exe
21+
stderr 'linkXworked'
22+
rm $WORK/tmp/a.exe
23+
24+
# Apply identity function to GOPATH
25+
exec ./run_go$GOEXE $GOPATH/src/my.pkg/main $GOPATH LOWER build -o $WORK/tmp/a.exe -ldflags -X=my.pkg.Text=linkXworked
26+
exec $WORK/tmp/a.exe
27+
stderr 'linkXworked'
28+
rm $WORK/tmp/a.exe
29+
30+
-- run_go.go --
31+
package main
32+
33+
import (
34+
"fmt"
35+
"os"
36+
"os/exec"
37+
"strings"
38+
)
39+
40+
func main() {
41+
dir := os.Args[1]
42+
gopath := os.Args[2]
43+
switch os.Args[3] {
44+
case "IDENTITY":
45+
case "REPLACE_SLASH": gopath = strings.ReplaceAll(gopath, `\`, `/`)
46+
case "UPPER": gopath = strings.ToUpper(gopath)
47+
case "LOWER": gopath = strings.ToLower(gopath)
48+
default: fmt.Fprintln(os.Stderr, "bad op"); os.Exit(1)
49+
}
50+
cmd := exec.Command("go", os.Args[4:]...)
51+
cmd.Dir = dir
52+
cmd.Env = append(os.Environ(), "GOPATH="+gopath)
53+
cmd.Stdout = os.Stdout
54+
cmd.Stderr = os.Stderr
55+
if err := cmd.Run(); err != nil {
56+
fmt.Fprintln(os.Stderr, err)
57+
os.Exit(1)
58+
}
59+
}
60+
61+
-- my.pkg/main/main.go --
62+
package main
63+
64+
import "my.pkg"
65+
66+
func main() {
67+
println(pkg.Text)
68+
}
69+
-- my.pkg/pkg.go --
70+
package pkg
71+
72+
var Text = "unset"

0 commit comments

Comments
 (0)