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
102
103 t.Logf("HasGoBuild is false on %s", b)
104 return
105 }
106
107 t.Fatalf("HasGoBuild unexpectedly false on %s", b)
108 }
109
110 t.Logf("HasGoBuild is true; checking consistency with other functions")
111
112 hasExec := false
113 hasExecGo := false
114 t.Run("MustHaveExec", func(t *testing.T) {
115 testenv.MustHaveExec(t)
116 hasExec = true
117 })
118 t.Run("MustHaveExecPath", func(t *testing.T) {
119 testenv.MustHaveExecPath(t, "go")
120 hasExecGo = true
121 })
122 if !hasExec {
123 t.Errorf(`MustHaveExec(t) skipped unexpectedly`)
124 }
125 if !hasExecGo {
126 t.Errorf(`MustHaveExecPath(t, "go") skipped unexpectedly`)
127 }
128
129 dir := t.TempDir()
130 mainGo := filepath.Join(dir, "main.go")
131 if err := os.WriteFile(mainGo, []byte("package main\nfunc main() {}\n"), 0644); err != nil {
132 t.Fatal(err)
133 }
134 cmd := testenv.Command(t, "go", "build", "-o", os.DevNull, mainGo)
135 out, err := cmd.CombinedOutput()
136 if err != nil {
137 t.Fatalf("%v: %v\n%s", cmd, err, out)
138 }
139 }
140
141 func TestMustHaveExec(t *testing.T) {
142 hasExec := false
143 t.Run("MustHaveExec", func(t *testing.T) {
144 testenv.MustHaveExec(t)
145 t.Logf("MustHaveExec did not skip")
146 hasExec = true
147 })
148
149 switch runtime.GOOS {
150 case "js", "wasip1":
151 if hasExec {
152
153 t.Errorf("expected MustHaveExec to skip on %v", runtime.GOOS)
154 }
155 case "ios":
156 if b := testenv.Builder(); isCorelliumBuilder(b) && !hasExec {
157
158 t.Errorf("expected MustHaveExec not to skip on %v", b)
159 }
160 default:
161 if b := testenv.Builder(); b != "" && !hasExec {
162 t.Errorf("expected MustHaveExec not to skip on %v", b)
163 }
164 }
165 }
166
167 func TestCleanCmdEnvPWD(t *testing.T) {
168
169 switch runtime.GOOS {
170 case "plan9", "windows":
171 t.Skipf("PWD is not used on %s", runtime.GOOS)
172 }
173 dir := t.TempDir()
174 cmd := testenv.Command(t, testenv.GoToolPath(t), "help")
175 cmd.Dir = dir
176 cmd = testenv.CleanCmdEnv(cmd)
177
178 for _, env := range cmd.Env {
179 if strings.HasPrefix(env, "PWD=") {
180 pwd := strings.TrimPrefix(env, "PWD=")
181 if pwd != dir {
182 t.Errorf("unexpected PWD: want %s, got %s", dir, pwd)
183 }
184 return
185 }
186 }
187 t.Error("PWD not set in cmd.Env")
188 }
189
190 func isCorelliumBuilder(builderName string) bool {
191
192
193
194
195
196 return strings.HasSuffix(builderName, "-corellium") || strings.Contains(builderName, "_corellium")
197 }
198
199 func isEmulatedBuilder(builderName string) bool {
200
201
202
203
204
205
206
207 return strings.HasSuffix(builderName, "-emu") || strings.Contains(builderName, "_emu")
208 }
209
View as plain text