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/buildcfg"
14 "internal/testenv"
15 "os"
16 "os/exec"
17 "path/filepath"
18 "regexp"
19 "runtime"
20 "runtime/debug"
21 "sync"
22 "testing"
23
24 "golang.org/x/mod/semver"
25 )
26
27 func scriptConditions(t *testing.T) map[string]script.Cond {
28 conds := scripttest.DefaultConds()
29
30 scripttest.AddToolChainScriptConditions(t, conds, goHostOS, goHostArch)
31
32 add := func(name string, cond script.Cond) {
33 if _, ok := conds[name]; ok {
34 panic(fmt.Sprintf("condition %q is already registered", name))
35 }
36 conds[name] = cond
37 }
38
39 lazyBool := func(summary string, f func() bool) script.Cond {
40 return script.OnceCondition(summary, func() (bool, error) { return f(), nil })
41 }
42
43 add("abscc", script.Condition("default $CC path is absolute and exists", defaultCCIsAbsolute))
44 add("case-sensitive", script.OnceCondition("$WORK filesystem is case-sensitive", isCaseSensitive))
45 add("cc", script.PrefixCondition("go env CC = <suffix> (ignoring the go/env file)", ccIs))
46 add("git", lazyBool("the 'git' executable exists and provides the standard CLI", hasWorkingGit))
47 add("git-sha256", script.OnceCondition("the local 'git' version is recent enough to support sha256 object/commit hashes", gitSupportsSHA256))
48 add("net", script.PrefixCondition("can connect to external network host <suffix>", hasNet))
49 add("trimpath", script.OnceCondition("test binary was built with -trimpath", isTrimpath))
50 add("default-cgo", lazyBool("when CGO_ENABLED=1|0 was set in make.bash", defaultCgo))
51
52 return conds
53 }
54
55 func defaultCCIsAbsolute(s *script.State) (bool, error) {
56 GOOS, _ := s.LookupEnv("GOOS")
57 GOARCH, _ := s.LookupEnv("GOARCH")
58 defaultCC := cfg.DefaultCC(GOOS, GOARCH)
59 if filepath.IsAbs(defaultCC) {
60 if _, err := exec.LookPath(defaultCC); err == nil {
61 return true, nil
62 }
63 }
64 return false, nil
65 }
66
67 func ccIs(s *script.State, want string) (bool, error) {
68 CC, _ := s.LookupEnv("CC")
69 if CC != "" {
70 return CC == want, nil
71 }
72 GOOS, _ := s.LookupEnv("GOOS")
73 GOARCH, _ := s.LookupEnv("GOARCH")
74 return cfg.DefaultCC(GOOS, GOARCH) == want, nil
75 }
76
77 var scriptNetEnabled sync.Map
78
79 func hasNet(s *script.State, host string) (bool, error) {
80 if !testenv.HasExternalNetwork() {
81 return false, nil
82 }
83
84
85
86
87 t, ok := tbFromContext(s.Context())
88 if !ok {
89 return false, errors.New("script Context unexpectedly missing testing.TB key")
90 }
91
92 if netTestSem != nil {
93
94
95
96 _, dup := scriptNetEnabled.LoadOrStore(t, true)
97 if !dup {
98
99 netTestSem <- struct{}{}
100 t.Cleanup(func() {
101 <-netTestSem
102 scriptNetEnabled.Delete(t)
103 })
104 }
105 }
106
107
108
109 s.Setenv("TESTGONETWORK", "")
110 return true, nil
111 }
112
113 func isCaseSensitive() (bool, error) {
114 tmpdir, err := os.MkdirTemp(testTmpDir, "case-sensitive")
115 if err != nil {
116 return false, fmt.Errorf("failed to create directory to determine case-sensitivity: %w", err)
117 }
118 defer os.RemoveAll(tmpdir)
119
120 fcap := filepath.Join(tmpdir, "FILE")
121 if err := os.WriteFile(fcap, []byte{}, 0644); err != nil {
122 return false, fmt.Errorf("error writing file to determine case-sensitivity: %w", err)
123 }
124
125 flow := filepath.Join(tmpdir, "file")
126 _, err = os.ReadFile(flow)
127 switch {
128 case err == nil:
129 return false, nil
130 case os.IsNotExist(err):
131 return true, nil
132 default:
133 return false, fmt.Errorf("unexpected error reading file when determining case-sensitivity: %w", err)
134 }
135 }
136
137 func isTrimpath() (bool, error) {
138 info, _ := debug.ReadBuildInfo()
139 if info == nil {
140 return false, errors.New("missing build info")
141 }
142
143 for _, s := range info.Settings {
144 if s.Key == "-trimpath" && s.Value == "true" {
145 return true, nil
146 }
147 }
148 return false, nil
149 }
150
151 func hasWorkingGit() bool {
152 if runtime.GOOS == "plan9" {
153
154
155 return false
156 }
157 _, err := exec.LookPath("git")
158 return err == nil
159 }
160
161
162 var gitVersLineExtract = regexp.MustCompile(`git version\s+(\d+\.\d+(?:\.\d+)?)`)
163
164 func gitVersion() (string, error) {
165 gitOut, runErr := exec.Command("git", "version").CombinedOutput()
166 if runErr != nil {
167 return "v0", fmt.Errorf("failed to execute git version: %w", runErr)
168 }
169 matches := gitVersLineExtract.FindSubmatch(gitOut)
170 if len(matches) < 2 {
171 return "v0", fmt.Errorf("git version extraction regexp did not match version line: %q", gitOut)
172 }
173 return "v" + string(matches[1]), nil
174 }
175
176 func hasAtLeastGitVersion(minVers string) (bool, error) {
177 gitVers, gitVersErr := gitVersion()
178 if gitVersErr != nil {
179 return false, gitVersErr
180 }
181 return semver.Compare(minVers, gitVers) <= 0, nil
182 }
183
184 func gitSupportsSHA256() (bool, error) {
185 return hasAtLeastGitVersion("v2.29")
186 }
187
188 func defaultCgo() bool {
189 return buildcfg.DefaultCGO_ENABLED == "1" || buildcfg.DefaultCGO_ENABLED == "0"
190 }
191
View as plain text