1
2
3
4
5 package testenv_test
6
7 import (
8 "internal/platform"
9 "internal/testenv"
10 "os"
11 "path/filepath"
12 "runtime"
13 "strings"
14 "testing"
15 )
16
17 func TestGoToolLocation(t *testing.T) {
18 testenv.MustHaveGoBuild(t)
19
20 var exeSuffix string
21 if runtime.GOOS == "windows" {
22 exeSuffix = ".exe"
23 }
24
25
26
27
28
29
30 relWant := "../../../bin/go" + exeSuffix
31 absWant, err := filepath.Abs(relWant)
32 if err != nil {
33 t.Fatal(err)
34 }
35
36 wantInfo, err := os.Stat(absWant)
37 if err != nil {
38 t.Fatal(err)
39 }
40 t.Logf("found go tool at %q (%q)", relWant, absWant)
41
42 goTool, err := testenv.GoTool()
43 if err != nil {
44 t.Fatalf("testenv.GoTool(): %v", err)
45 }
46 t.Logf("testenv.GoTool() = %q", goTool)
47
48 gotInfo, err := os.Stat(goTool)
49 if err != nil {
50 t.Fatal(err)
51 }
52 if !os.SameFile(wantInfo, gotInfo) {
53 t.Fatalf("%q is not the same file as %q", absWant, goTool)
54 }
55 }
56
57 func TestHasGoBuild(t *testing.T) {
58 if !testenv.HasGoBuild() {
59 switch runtime.GOOS {
60 case "js", "wasip1":
61
62 t.Logf("HasGoBuild is false on %s", runtime.GOOS)
63 return
64 }
65
66 b := testenv.Builder()
67 if b == "" {
68
69
70 t.Skipf("skipping: 'go build' unavailable")
71 }
72
73
74
75
76
77
78
79 switch runtime.GOOS {
80 case "ios":
81 if isCorelliumBuilder(b) {
82
83
84 } else {
85
86
87
88 t.Logf("HasGoBuild is false on %s", b)
89 return
90 }
91 case "android":
92 if isEmulatedBuilder(b) && platform.MustLinkExternal(runtime.GOOS, runtime.GOARCH, false) {
93
94
95 t.Logf("HasGoBuild is false on %s", b)
96 return
97 }
98 }
99
100 if strings.Contains(b, "-noopt") ||
101 strings.Contains(b, "-spectre") {
102
103
104 t.Logf("HasGoBuild is false on %s", b)
105 return
106 }
107
108 t.Fatalf("HasGoBuild unexpectedly false on %s", b)
109 }
110
111 t.Logf("HasGoBuild is true; checking consistency with other functions")
112
113 hasExec := false
114 hasExecGo := false
115 t.Run("MustHaveExec", func(t *testing.T) {
116 testenv.MustHaveExec(t)
117 hasExec = true
118 })
119 t.Run("MustHaveExecPath", func(t *testing.T) {
120 testenv.MustHaveExecPath(t, "go")
121 hasExecGo = true
122 })
123 if !hasExec {
124 t.Errorf(`MustHaveExec(t) skipped unexpectedly`)
125 }
126 if !hasExecGo {
127 t.Errorf(`MustHaveExecPath(t, "go") skipped unexpectedly`)
128 }
129
130 dir := t.TempDir()
131 mainGo := filepath.Join(dir, "main.go")
132 if err := os.WriteFile(mainGo, []byte("package main\nfunc main() {}\n"), 0644); err != nil {
133 t.Fatal(err)
134 }
135 cmd := testenv.Command(t, "go", "build", "-o", os.DevNull, mainGo)
136 out, err := cmd.CombinedOutput()
137 if err != nil {
138 t.Fatalf("%v: %v\n%s", cmd, err, out)
139 }
140 }
141
142 func TestMustHaveExec(t *testing.T) {
143 hasExec := false
144 t.Run("MustHaveExec", func(t *testing.T) {
145 testenv.MustHaveExec(t)
146 t.Logf("MustHaveExec did not skip")
147 hasExec = true
148 })
149
150 switch runtime.GOOS {
151 case "js", "wasip1":
152 if hasExec {
153
154 t.Errorf("expected MustHaveExec to skip on %v", runtime.GOOS)
155 }
156 case "ios":
157 if b := testenv.Builder(); isCorelliumBuilder(b) && !hasExec {
158
159 t.Errorf("expected MustHaveExec not to skip on %v", b)
160 }
161 default:
162 if b := testenv.Builder(); b != "" && !hasExec {
163 t.Errorf("expected MustHaveExec not to skip on %v", b)
164 }
165 }
166 }
167
168 func TestCleanCmdEnvPWD(t *testing.T) {
169
170 switch runtime.GOOS {
171 case "plan9", "windows":
172 t.Skipf("PWD is not used on %s", runtime.GOOS)
173 }
174 dir := t.TempDir()
175 cmd := testenv.Command(t, testenv.GoToolPath(t), "help")
176 cmd.Dir = dir
177 cmd = testenv.CleanCmdEnv(cmd)
178
179 for _, env := range cmd.Env {
180 if strings.HasPrefix(env, "PWD=") {
181 pwd := strings.TrimPrefix(env, "PWD=")
182 if pwd != dir {
183 t.Errorf("unexpected PWD: want %s, got %s", dir, pwd)
184 }
185 return
186 }
187 }
188 t.Error("PWD not set in cmd.Env")
189 }
190
191 func isCorelliumBuilder(builderName string) bool {
192
193
194
195
196
197 return strings.HasSuffix(builderName, "-corellium") || strings.Contains(builderName, "_corellium")
198 }
199
200 func isEmulatedBuilder(builderName string) bool {
201
202
203
204
205
206
207
208 return strings.HasSuffix(builderName, "-emu") || strings.Contains(builderName, "_emu")
209 }
210
View as plain text