Source file
src/cmd/go/go_unix_test.go
1
2
3
4
5
6
7 package main_test
8
9 import (
10 "bufio"
11 "context"
12 "internal/testenv"
13 "io"
14 "os"
15 "os/exec"
16 "slices"
17 "strings"
18 "syscall"
19 "testing"
20 )
21
22 func TestGoBuildUmask(t *testing.T) {
23
24 mask := syscall.Umask(0077)
25 defer syscall.Umask(mask)
26
27 tg := testgo(t)
28 defer tg.cleanup()
29 tg.tempFile("x.go", `package main; func main() {}`)
30
31
32
33
34
35
36
37 control := tg.path("control")
38 tg.creatingTemp(control)
39 if err := os.WriteFile(control, []byte("#!/bin/sh\nexit 0"), 0777); err != nil {
40 t.Fatal(err)
41 }
42 cfi, err := os.Stat(control)
43 if err != nil {
44 t.Fatal(err)
45 }
46
47 exe := tg.path("x")
48 tg.creatingTemp(exe)
49 tg.run("build", "-o", exe, tg.path("x.go"))
50 fi, err := os.Stat(exe)
51 if err != nil {
52 t.Fatal(err)
53 }
54 got, want := fi.Mode(), cfi.Mode()
55 if got == want {
56 t.Logf("wrote x with mode %v", got)
57 } else {
58 t.Fatalf("wrote x with mode %v, wanted no 0077 bits (%v)", got, want)
59 }
60 }
61
62
63
64
65
66
67 func TestTestInterrupt(t *testing.T) {
68 if testing.Short() {
69 t.Skipf("skipping in short mode: test executes many subprocesses")
70 }
71
72
73 tg := testgo(t)
74 defer tg.cleanup()
75 tg.setenv("GOROOT", testGOROOT)
76
77 ctx, cancel := context.WithCancel(context.Background())
78 cmd := testenv.CommandContext(t, ctx, tg.goTool(), "test", "std", "-short", "-count=1")
79 cmd.Dir = tg.execDir
80
81
82
83
84 cmd.Env = append(slices.Clip(tg.env), tempEnvName()+"="+t.TempDir())
85
86 cmd.SysProcAttr = &syscall.SysProcAttr{
87 Setpgid: true,
88 }
89 cmd.Cancel = func() error {
90 pgid := cmd.Process.Pid
91 return syscall.Kill(-pgid, syscall.SIGINT)
92 }
93
94 pipe, err := cmd.StdoutPipe()
95 if err != nil {
96 t.Fatal(err)
97 }
98
99 t.Logf("running %v", cmd)
100 if err := cmd.Start(); err != nil {
101 t.Fatal(err)
102 }
103
104 stdout := new(strings.Builder)
105 r := bufio.NewReader(pipe)
106 line, err := r.ReadString('\n')
107 if err != nil {
108 t.Fatal(err)
109 }
110 stdout.WriteString(line)
111
112
113
114
115
116
117 cancel()
118
119 io.Copy(stdout, r)
120 if stdout.Len() > 0 {
121 t.Logf("stdout:\n%s", stdout)
122 }
123 err = cmd.Wait()
124
125 ee, _ := err.(*exec.ExitError)
126 if ee == nil {
127 t.Fatalf("unexpectedly finished with nonzero status")
128 }
129 if len(ee.Stderr) > 0 {
130 t.Logf("stderr:\n%s", ee.Stderr)
131 }
132 if !ee.Exited() {
133 t.Fatalf("'go test' did not exit after interrupt: %v", err)
134 }
135
136 t.Logf("interrupted tests without deadlocking")
137 }
138
View as plain text