Source file
src/cmd/go/scriptconds_test.go
1
2
3
4
5 package main_test
6
7 import (
8 "cmd/go/internal/cfg"
9 "cmd/internal/script"
10 "cmd/internal/script/scripttest"
11 "errors"
12 "fmt"
13 "internal/testenv"
14 "os"
15 "os/exec"
16 "path/filepath"
17 "runtime"
18 "runtime/debug"
19 "sync"
20 "testing"
21 )
22
23 func scriptConditions(t *testing.T) map[string]script.Cond {
24 conds := scripttest.DefaultConds()
25
26 scripttest.AddToolChainScriptConditions(t, conds, goHostOS, goHostArch)
27
28 add := func(name string, cond script.Cond) {
29 if _, ok := conds[name]; ok {
30 panic(fmt.Sprintf("condition %q is already registered", name))
31 }
32 conds[name] = cond
33 }
34
35 lazyBool := func(summary string, f func() bool) script.Cond {
36 return script.OnceCondition(summary, func() (bool, error) { return f(), nil })
37 }
38
39 add("abscc", script.Condition("default $CC path is absolute and exists", defaultCCIsAbsolute))
40 add("bzr", lazyBool("the 'bzr' executable exists and provides the standard CLI", hasWorkingBzr))
41 add("case-sensitive", script.OnceCondition("$WORK filesystem is case-sensitive", isCaseSensitive))
42 add("cc", script.PrefixCondition("go env CC = <suffix> (ignoring the go/env file)", ccIs))
43 add("git", lazyBool("the 'git' executable exists and provides the standard CLI", hasWorkingGit))
44 add("net", script.PrefixCondition("can connect to external network host <suffix>", hasNet))
45 add("trimpath", script.OnceCondition("test binary was built with -trimpath", isTrimpath))
46
47 return conds
48 }
49
50 func defaultCCIsAbsolute(s *script.State) (bool, error) {
51 GOOS, _ := s.LookupEnv("GOOS")
52 GOARCH, _ := s.LookupEnv("GOARCH")
53 defaultCC := cfg.DefaultCC(GOOS, GOARCH)
54 if filepath.IsAbs(defaultCC) {
55 if _, err := exec.LookPath(defaultCC); err == nil {
56 return true, nil
57 }
58 }
59 return false, nil
60 }
61
62 func ccIs(s *script.State, want string) (bool, error) {
63 CC, _ := s.LookupEnv("CC")
64 if CC != "" {
65 return CC == want, nil
66 }
67 GOOS, _ := s.LookupEnv("GOOS")
68 GOARCH, _ := s.LookupEnv("GOARCH")
69 return cfg.DefaultCC(GOOS, GOARCH) == want, nil
70 }
71
72 var scriptNetEnabled sync.Map
73
74 func hasNet(s *script.State, host string) (bool, error) {
75 if !testenv.HasExternalNetwork() {
76 return false, nil
77 }
78
79
80
81
82 t, ok := tbFromContext(s.Context())
83 if !ok {
84 return false, errors.New("script Context unexpectedly missing testing.TB key")
85 }
86
87 if netTestSem != nil {
88
89
90
91 _, dup := scriptNetEnabled.LoadOrStore(t, true)
92 if !dup {
93
94 netTestSem <- struct{}{}
95 t.Cleanup(func() {
96 <-netTestSem
97 scriptNetEnabled.Delete(t)
98 })
99 }
100 }
101
102
103
104 s.Setenv("TESTGONETWORK", "")
105 return true, nil
106 }
107
108 func isCaseSensitive() (bool, error) {
109 tmpdir, err := os.MkdirTemp(testTmpDir, "case-sensitive")
110 if err != nil {
111 return false, fmt.Errorf("failed to create directory to determine case-sensitivity: %w", err)
112 }
113 defer os.RemoveAll(tmpdir)
114
115 fcap := filepath.Join(tmpdir, "FILE")
116 if err := os.WriteFile(fcap, []byte{}, 0644); err != nil {
117 return false, fmt.Errorf("error writing file to determine case-sensitivity: %w", err)
118 }
119
120 flow := filepath.Join(tmpdir, "file")
121 _, err = os.ReadFile(flow)
122 switch {
123 case err == nil:
124 return false, nil
125 case os.IsNotExist(err):
126 return true, nil
127 default:
128 return false, fmt.Errorf("unexpected error reading file when determining case-sensitivity: %w", err)
129 }
130 }
131
132 func isTrimpath() (bool, error) {
133 info, _ := debug.ReadBuildInfo()
134 if info == nil {
135 return false, errors.New("missing build info")
136 }
137
138 for _, s := range info.Settings {
139 if s.Key == "-trimpath" && s.Value == "true" {
140 return true, nil
141 }
142 }
143 return false, nil
144 }
145
146 func hasWorkingGit() bool {
147 if runtime.GOOS == "plan9" {
148
149
150 return false
151 }
152 _, err := exec.LookPath("git")
153 return err == nil
154 }
155
156 func hasWorkingBzr() bool {
157 bzr, err := exec.LookPath("bzr")
158 if err != nil {
159 return false
160 }
161
162
163 err = exec.Command(bzr, "help").Run()
164 return err == nil
165 }
166
View as plain text