Source file
misc/reboot/experiment_toolid_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13 package reboot_test
14
15 import (
16 "bytes"
17 "os"
18 "os/exec"
19 "path/filepath"
20 "runtime"
21 "testing"
22 )
23
24 func TestExperimentToolID(t *testing.T) {
25
26 goroot, err := os.MkdirTemp("", "experiment-goroot")
27 if err != nil {
28 t.Fatal(err)
29 }
30 defer os.RemoveAll(goroot)
31
32 gorootSrc := filepath.Join(goroot, "src")
33 if err := overlayDir(gorootSrc, filepath.Join(runtime.GOROOT(), "src")); err != nil {
34 t.Fatal(err)
35 }
36
37 if err := os.WriteFile(filepath.Join(goroot, "VERSION"), []byte("go1.999"), 0666); err != nil {
38 t.Fatal(err)
39 }
40 env := append(os.Environ(), "GOROOT=", "GOROOT_BOOTSTRAP="+runtime.GOROOT())
41
42
43 gocache, err := os.MkdirTemp("", "experiment-gocache")
44 if err != nil {
45 t.Fatal(err)
46 }
47 defer os.RemoveAll(gocache)
48 env = append(env, "GOCACHE="+gocache)
49
50
51 var makeScript string
52 switch runtime.GOOS {
53 case "windows":
54 makeScript = "make.bat"
55 case "plan9":
56 makeScript = "make.rc"
57 default:
58 makeScript = "make.bash"
59 }
60 makeScriptPath := filepath.Join(runtime.GOROOT(), "src", makeScript)
61 runCmd(t, gorootSrc, env, makeScriptPath)
62
63
64 goCmdPath := filepath.Join(goroot, "bin", "go")
65 if runtime.GOOS == "windows" {
66 goCmdPath += ".exe"
67 }
68 gotVersion := bytes.TrimSpace(runCmd(t, gorootSrc, env, goCmdPath, "tool", "compile", "-V=full"))
69 wantVersion := []byte(`compile version go1.999`)
70 if !bytes.Equal(gotVersion, wantVersion) {
71 t.Errorf("compile version without experiment: got %q, want %q", gotVersion, wantVersion)
72 }
73
74
75 runCmd(t, gorootSrc, env, goCmdPath, "build", "-race", "archive/tar")
76
77
78 env = append(env, "GOEXPERIMENT=fieldtrack")
79 runCmd(t, gorootSrc, env, makeScriptPath)
80
81
82 gotVersion = bytes.TrimSpace(runCmd(t, gorootSrc, env, goCmdPath, "tool", "compile", "-V=full"))
83 wantVersion = []byte(`compile version go1.999 X:fieldtrack,framepointer`)
84 if !bytes.Equal(gotVersion, wantVersion) {
85 t.Errorf("compile version with experiment: got %q, want %q", gotVersion, wantVersion)
86 }
87
88
89 runCmd(t, gorootSrc, env, goCmdPath, "build", "-race", "archive/tar")
90 }
91
92 func runCmd(t *testing.T, dir string, env []string, path string, args ...string) []byte {
93 cmd := exec.Command(path, args...)
94 cmd.Dir = dir
95 cmd.Env = env
96 out, err := cmd.Output()
97 if err != nil {
98 t.Fatal(err)
99 }
100 return out
101 }
102
View as plain text