Repository navigation
Expand file tree
/
Copy pathmakefile_test.go
More file actions
457 lines (422 loc) · 14.1 KB
/
Copy pathmakefile_test.go
File metadata and controls
457 lines (422 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package main
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestMakeCleanRemovesReleaseDirectory(t *testing.T) {
workspaceDir := t.TempDir()
releaseDir := filepath.Join(workspaceDir, "release")
staleArtifact := filepath.Join(releaseDir, "stale-artifact")
if err := os.MkdirAll(releaseDir, 0o755); err != nil {
t.Fatalf("mkdir release dir: %v", err)
}
if err := os.WriteFile(staleArtifact, []byte("stale"), 0o644); err != nil {
t.Fatalf("write stale artifact: %v", err)
}
repoRoot, err := os.Getwd()
if err != nil {
t.Fatalf("getwd: %v", err)
}
cmd := exec.Command("make", "-f", filepath.Join(repoRoot, "Makefile"), "-C", workspaceDir, "clean")
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("make clean failed: %v\n%s", err, output)
}
if _, err := os.Stat(staleArtifact); !os.IsNotExist(err) {
t.Fatalf("expected make clean to remove %s, stat err=%v\n%s", staleArtifact, err, output)
}
}
// makeDryRun returns the commands make would execute for a target without
// running them.
func makeDryRun(t *testing.T, target string) string {
t.Helper()
repoRoot, err := os.Getwd()
if err != nil {
t.Fatalf("getwd: %v", err)
}
cmd := exec.Command("make", "-n", "-f", filepath.Join(repoRoot, "Makefile"), "-C", t.TempDir(), target)
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("make -n %s failed: %v\n%s", target, err, output)
}
return string(output)
}
func TestMakeBuildAllUsesStrippedTrimmedReleaseFlags(t *testing.T) {
output := makeDryRun(t, "build-all")
for _, want := range []string{"-trimpath", `-ldflags "-s -w `} {
if !strings.Contains(output, want) {
t.Fatalf("expected build-all recipe to contain %q, got:\n%s", want, output)
}
}
}
func TestMakeBuildAllSetsCGOPerTarget(t *testing.T) {
output := makeDryRun(t, "build-all")
for _, want := range []string{
`if [ "$os" = "darwin" ]; then cgo="1"; else cgo="0"; fi`,
`CGO_ENABLED="$cgo" GOOS="$os" GOARCH="$arch"`,
} {
if !strings.Contains(output, want) {
t.Fatalf("expected build-all recipe to contain %q, got:\n%s", want, output)
}
}
}
func TestMakeBuildAllFailsWhenAnyTargetFails(t *testing.T) {
repoRoot, err := os.Getwd()
if err != nil {
t.Fatalf("getwd: %v", err)
}
workspaceDir := t.TempDir()
fakeGo := filepath.Join(workspaceDir, "fake-go")
script := `#!/bin/sh
if [ "$1" = "env" ]; then
exit 0
fi
if [ "$GOOS" = "darwin" ]; then
echo "simulated Darwin Cgo failure" >&2
exit 42
fi
exit 0
`
if err := os.WriteFile(fakeGo, []byte(script), 0o755); err != nil {
t.Fatalf("write fake go: %v", err)
}
cmd := exec.Command(
"make",
"-f",
filepath.Join(repoRoot, "Makefile"),
"-C",
workspaceDir,
"build-all",
"GO="+fakeGo,
"VERSION=1.2.3",
)
output, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("make build-all succeeded despite a failed target:\n%s", output)
}
if strings.Contains(string(output), "Release binaries written") {
t.Fatalf("make build-all reported success after a failed target:\n%s", output)
}
}
func TestMakeBuildAllQuotesCustomReleaseDirectory(t *testing.T) {
repoRoot, err := os.Getwd()
if err != nil {
t.Fatalf("getwd: %v", err)
}
workspaceDir := t.TempDir()
releaseDir := filepath.Join(workspaceDir, "release output")
cmd := exec.Command(
"make",
"-n",
"-f",
filepath.Join(repoRoot, "Makefile"),
"-C",
workspaceDir,
"build-all",
"VERSION=1.2.3",
"RELEASE_DIR="+releaseDir,
)
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("make -n build-all with custom output failed: %v\n%s", err, output)
}
commands := string(output)
for _, want := range []string{
`rm -rf build dist "` + releaseDir + `"`,
`mkdir -p "` + releaseDir + `"`,
`-o "` + releaseDir + `/asc_1.2.3_`,
} {
if !strings.Contains(commands, want) {
t.Fatalf("custom release directory is not safely quoted in %q; got:\n%s", want, commands)
}
}
}
func TestMakeBuildKeepsDebugSymbolsForDevBuilds(t *testing.T) {
output := makeDryRun(t, "build")
for _, unwanted := range []string{"-trimpath", "-s -w"} {
if strings.Contains(output, unwanted) {
t.Fatalf("expected dev build recipe to stay unstripped, found %q in:\n%s", unwanted, output)
}
}
if !strings.Contains(output, "-X main.version=") {
t.Fatalf("expected dev build recipe to inject version metadata, got:\n%s", output)
}
}
func TestMakeBuildRebuildsBinaryWhenSourceChanges(t *testing.T) {
workspaceDir := t.TempDir()
repoRoot, err := os.Getwd()
if err != nil {
t.Fatalf("getwd: %v", err)
}
writeWorkspaceFile := func(path, contents string) {
t.Helper()
fullPath := filepath.Join(workspaceDir, path)
if err := os.WriteFile(fullPath, []byte(contents), 0o644); err != nil {
t.Fatalf("write %s: %v", path, err)
}
}
writeWorkspaceFile("go.mod", "module example.com/makebuildtest\n\ngo 1.24.0\n")
writeWorkspaceFile("main.go", "package main\n\nimport \"fmt\"\n\nfunc main() { fmt.Print(\"first\") }\n")
runMakeBuild := func() {
t.Helper()
cmd := exec.Command("make", "-f", filepath.Join(repoRoot, "Makefile"), "-C", workspaceDir, "build")
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("make build failed: %v\n%s", err, output)
}
}
runBinary := func() string {
t.Helper()
cmd := exec.Command(filepath.Join(workspaceDir, "asc"))
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("run built binary failed: %v\n%s", err, output)
}
return string(output)
}
runMakeBuild()
if got := runBinary(); got != "first" {
t.Fatalf("expected initial binary output %q, got %q", "first", got)
}
writeWorkspaceFile("main.go", "package main\n\nimport \"fmt\"\n\nfunc main() { fmt.Print(\"second\") }\n")
runMakeBuild()
if got := runBinary(); got != "second" {
t.Fatalf("expected rebuilt binary output %q, got %q", "second", got)
}
}
// copyTestRunner gives a temporary workspace the script that make test runs.
func copyTestRunner(t *testing.T, repoRoot, workspaceDir string) {
t.Helper()
data, err := os.ReadFile(filepath.Join(repoRoot, "scripts", "go_test_shard.py"))
if err != nil {
t.Fatalf("read test runner: %v", err)
}
if err := os.Mkdir(filepath.Join(workspaceDir, "scripts"), 0o700); err != nil {
t.Fatalf("mkdir scripts: %v", err)
}
if err := os.WriteFile(filepath.Join(workspaceDir, "scripts", "go_test_shard.py"), data, 0o600); err != nil {
t.Fatalf("write test runner: %v", err)
}
}
func TestMakeTestTargetsIsolateDeveloperEnvironment(t *testing.T) {
repoRoot, err := os.Getwd()
if err != nil {
t.Fatalf("getwd: %v", err)
}
for _, target := range []string{"test", "test-short", "test-parallel", "test-coverage"} {
t.Run(target, func(t *testing.T) {
workspaceDir := t.TempDir()
tempDir := filepath.Join(workspaceDir, "tmp")
if err := os.Mkdir(tempDir, 0o700); err != nil {
t.Fatalf("mkdir tmp: %v", err)
}
envLog := filepath.Join(workspaceDir, "test-env")
stateLog := filepath.Join(workspaceDir, "test-state")
copyTestRunner(t, repoRoot, workspaceDir)
fakeGo := filepath.Join(workspaceDir, "fake-go")
script := `#!/bin/sh
if [ "$1" = "test" ]; then
env > "$FAKE_GO_ENV_LOG"
config_dir=$(dirname "$ASC_CONFIG_PATH")
[ -d "$config_dir" ] && echo "config-dir-exists" >> "$FAKE_GO_STATE_LOG"
[ -e "$ASC_CONFIG_PATH" ] && echo "config-exists" >> "$FAKE_GO_STATE_LOG"
[ -w "$config_dir" ] && echo "config-dir-writable" >> "$FAKE_GO_STATE_LOG"
fi
exit 0
`
if err := os.WriteFile(fakeGo, []byte(script), 0o755); err != nil {
t.Fatalf("write fake go: %v", err)
}
cmd := exec.Command("make", "-f", filepath.Join(repoRoot, "Makefile"), "-C", workspaceDir, target, "GO="+fakeGo)
cmd.Env = append(
os.Environ(),
"TMPDIR="+tempDir,
"FAKE_GO_ENV_LOG="+envLog,
"FAKE_GO_STATE_LOG="+stateLog,
"ASC_APP_ID=developer-app",
"ASC_CONFIG_PATH=/developer/.asc/config.json",
"ASC_PROFILE=developer-profile",
"ASC_TELEMETRY_DISABLED=1",
"ASC_BYPASS_KEYCHAIN=0",
"DO_NOT_TRACK=1",
"ASC_UPDATE_GOLDEN=1",
"ASC_SIGNING_RUN_LIVE_TEST=1",
"ASC_SIGNING_KEYCHAIN_INSTALL_LIVE_TEST=1",
)
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("make %s failed: %v\n%s", target, err, output)
}
data, err := os.ReadFile(envLog)
if err != nil {
t.Fatalf("read test environment: %v", err)
}
got := map[string]string{}
for _, line := range strings.Split(string(data), "\n") {
if name, value, ok := strings.Cut(line, "="); ok {
got[name] = value
}
}
for name, want := range map[string]string{
"ASC_BYPASS_KEYCHAIN": "1",
// Opt-in test switches pass through so an explicitly requested
// golden update or gated live test still runs under make.
"ASC_UPDATE_GOLDEN": "1",
"ASC_SIGNING_RUN_LIVE_TEST": "1",
"ASC_SIGNING_KEYCHAIN_INSTALL_LIVE_TEST": "1",
} {
if got[name] != want {
t.Errorf("%s = %q, want %q", name, got[name], want)
}
}
for _, name := range []string{"ASC_APP_ID", "ASC_PROFILE", "ASC_TELEMETRY_DISABLED", "DO_NOT_TRACK"} {
if value, ok := got[name]; ok {
t.Errorf("%s leaked into the test environment as %q", name, value)
}
}
// The config path is a missing file in a fresh per-run directory
// that tests cannot write to and that is removed afterwards.
configPath := got["ASC_CONFIG_PATH"]
configDir := filepath.Dir(configPath)
if filepath.Base(configPath) != "config.json" || filepath.Dir(configDir) != tempDir ||
!strings.HasPrefix(filepath.Base(configDir), "asc-test-config.") {
t.Fatalf("ASC_CONFIG_PATH = %q, want config.json in a fresh directory under %q", configPath, tempDir)
}
state, err := os.ReadFile(stateLog)
if err != nil {
t.Fatalf("read test state: %v", err)
}
want := "config-dir-exists\n"
if os.Geteuid() == 0 {
// Root bypasses the read-only mode; the directory is still
// fresh, empty, and private to this run.
want += "config-dir-writable\n"
}
if string(state) != want {
t.Fatalf("config state during the run = %q, want %q", state, want)
}
if _, err := os.Stat(configDir); !os.IsNotExist(err) {
t.Fatalf("config directory %q still exists after the run (stat error %v)", configDir, err)
}
})
}
}
func TestMakeTestFailsWhenTestsWriteSharedConfig(t *testing.T) {
repoRoot, err := os.Getwd()
if err != nil {
t.Fatalf("getwd: %v", err)
}
workspaceDir := t.TempDir()
tempDir := filepath.Join(workspaceDir, "tmp")
if err := os.Mkdir(tempDir, 0o700); err != nil {
t.Fatalf("mkdir tmp: %v", err)
}
copyTestRunner(t, repoRoot, workspaceDir)
fakeGo := filepath.Join(workspaceDir, "fake-go")
// Simulate a test that writes the inherited config path, as a root runner
// could despite the read-only directory.
script := `#!/bin/sh
if [ "$1" = "test" ]; then
chmod 700 "$(dirname "$ASC_CONFIG_PATH")"
echo "{}" > "$ASC_CONFIG_PATH"
fi
exit 0
`
if err := os.WriteFile(fakeGo, []byte(script), 0o755); err != nil {
t.Fatalf("write fake go: %v", err)
}
cmd := exec.Command("make", "-f", filepath.Join(repoRoot, "Makefile"), "-C", workspaceDir, "test-short", "GO="+fakeGo)
cmd.Env = append(os.Environ(), "TMPDIR="+tempDir)
output, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("make test-short succeeded although tests wrote the shared config:\n%s", output)
}
if !strings.Contains(string(output), "tests wrote to the shared test config directory") {
t.Fatalf("make test-short output does not explain the failure:\n%s", output)
}
entries, err := os.ReadDir(tempDir)
if err != nil {
t.Fatalf("read tmp: %v", err)
}
if len(entries) != 0 {
t.Fatalf("config directory left behind: %v", entries)
}
}
func TestMakeTestRemovesConfigDirectoryWhenInterrupted(t *testing.T) {
repoRoot, err := os.Getwd()
if err != nil {
t.Fatalf("getwd: %v", err)
}
workspaceDir := t.TempDir()
tempDir := filepath.Join(workspaceDir, "tmp")
if err := os.Mkdir(tempDir, 0o700); err != nil {
t.Fatalf("mkdir tmp: %v", err)
}
copyTestRunner(t, repoRoot, workspaceDir)
fakeGo := filepath.Join(workspaceDir, "fake-go")
// Terminate the recipe shell while the test command runs, as Ctrl-C or a
// cancelled CI job would.
script := `#!/bin/sh
if [ "$1" = "test" ]; then
kill -TERM "$PPID"
fi
exit 0
`
if err := os.WriteFile(fakeGo, []byte(script), 0o755); err != nil {
t.Fatalf("write fake go: %v", err)
}
cmd := exec.Command("make", "-f", filepath.Join(repoRoot, "Makefile"), "-C", workspaceDir, "test-short", "GO="+fakeGo)
cmd.Env = append(os.Environ(), "TMPDIR="+tempDir)
if output, err := cmd.CombinedOutput(); err == nil {
t.Fatalf("make test-short succeeded although the run was terminated:\n%s", output)
}
entries, err := os.ReadDir(tempDir)
if err != nil {
t.Fatalf("read tmp: %v", err)
}
if len(entries) != 0 {
t.Fatalf("config directory left behind after termination: %v", entries)
}
}
func TestMakeBuildsPinMacOSDeploymentTarget(t *testing.T) {
repoRoot, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
for _, target := range []string{"build", "build-debug", "build-all"} {
for _, source := range []string{"environment", "make-argument"} {
t.Run(target+"/"+source, func(t *testing.T) {
workspace := t.TempDir()
fakeGo := filepath.Join(workspace, "fake-go")
script := `#!/bin/sh
if [ "$1" = "env" ]; then
[ "$2" != "GOHOSTOS" ] || echo darwin
exit 0
fi
if [ "$1" = "build" ]; then
echo "deployment-target=$MACOSX_DEPLOYMENT_TARGET"
[ "$MACOSX_DEPLOYMENT_TARGET" = "13.0" ] || exit 42
[ "$CGO_CFLAGS" = "-O0 -g -mmacosx-version-min=13.0" ] || exit 43
[ "$CGO_LDFLAGS" = "-O0 -g -mmacosx-version-min=13.0" ] || exit 44
fi
`
if err := os.WriteFile(fakeGo, []byte(script), 0o755); err != nil {
t.Fatal(err)
}
cmd := exec.Command("make", "-f", filepath.Join(repoRoot, "Makefile"), "-C", workspace, target, "GO="+fakeGo)
settings := []string{"MACOSX_DEPLOYMENT_TARGET=27.0", "CGO_CFLAGS=-O0 -g -mmacosx-version-min=27.0", "CGO_LDFLAGS=-O0 -g -mmacosx-version-min=27.0"}
if source == "make-argument" {
cmd.Args = append(cmd.Args, settings...)
} else {
cmd.Env = append(os.Environ(), settings...)
}
output, err := cmd.CombinedOutput()
if err != nil || !strings.Contains(string(output), "deployment-target=13.0") {
t.Fatalf("make %s must pin the supported macOS minimum despite the host default: %v\n%s", target, err, output)
}
})
}
}
}