cuelang.org/go@v0.13.0/internal/golangorgx/tools/testenv/testenv.go (about) 1 // Copyright 2019 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package testenv contains helper functions for skipping tests 6 // based on which tools are present in the environment. 7 package testenv 8 9 import ( 10 "bytes" 11 "fmt" 12 "go/build" 13 "os" 14 "os/exec" 15 "path/filepath" 16 "runtime" 17 "runtime/debug" 18 "strings" 19 "sync" 20 "testing" 21 "time" 22 ) 23 24 // packageMainIsDevel reports whether the module containing package main 25 // is a development version (if module information is available). 26 func packageMainIsDevel() bool { 27 info, ok := debug.ReadBuildInfo() 28 if !ok { 29 // Most test binaries currently lack build info, but this should become more 30 // permissive once https://golang.org/issue/33976 is fixed. 31 return true 32 } 33 34 // Note: info.Main.Version describes the version of the module containing 35 // package main, not the version of “the main module”. 36 // See https://golang.org/issue/33975. 37 return info.Main.Version == "(devel)" 38 } 39 40 var checkGoBuild struct { 41 once sync.Once 42 err error 43 } 44 45 func hasTool(tool string) error { 46 if tool == "cgo" { 47 enabled, err := cgoEnabled(false) 48 if err != nil { 49 return fmt.Errorf("checking cgo: %v", err) 50 } 51 if !enabled { 52 return fmt.Errorf("cgo not enabled") 53 } 54 return nil 55 } 56 57 _, err := exec.LookPath(tool) 58 if err != nil { 59 return err 60 } 61 62 switch tool { 63 case "patch": 64 // check that the patch tools supports the -o argument 65 temp, err := os.CreateTemp("", "patch-test") 66 if err != nil { 67 return err 68 } 69 temp.Close() 70 defer os.Remove(temp.Name()) 71 cmd := exec.Command(tool, "-o", temp.Name()) 72 if err := cmd.Run(); err != nil { 73 return err 74 } 75 76 case "go": 77 checkGoBuild.once.Do(func() { 78 if runtime.GOROOT() != "" { 79 // Ensure that the 'go' command found by exec.LookPath is from the correct 80 // GOROOT. Otherwise, 'some/path/go test ./...' will test against some 81 // version of the 'go' binary other than 'some/path/go', which is almost 82 // certainly not what the user intended. 83 out, err := exec.Command(tool, "env", "GOROOT").CombinedOutput() 84 if err != nil { 85 checkGoBuild.err = err 86 return 87 } 88 GOROOT := strings.TrimSpace(string(out)) 89 if GOROOT != runtime.GOROOT() { 90 checkGoBuild.err = fmt.Errorf("'go env GOROOT' does not match runtime.GOROOT:\n\tgo env: %s\n\tGOROOT: %s", GOROOT, runtime.GOROOT()) 91 return 92 } 93 } 94 95 dir, err := os.MkdirTemp("", "testenv-*") 96 if err != nil { 97 checkGoBuild.err = err 98 return 99 } 100 defer os.RemoveAll(dir) 101 102 mainGo := filepath.Join(dir, "main.go") 103 if err := os.WriteFile(mainGo, []byte("package main\nfunc main() {}\n"), 0644); err != nil { 104 checkGoBuild.err = err 105 return 106 } 107 cmd := exec.Command("go", "build", "-o", os.DevNull, mainGo) 108 cmd.Dir = dir 109 if out, err := cmd.CombinedOutput(); err != nil { 110 if len(out) > 0 { 111 checkGoBuild.err = fmt.Errorf("%v: %v\n%s", cmd, err, out) 112 } else { 113 checkGoBuild.err = fmt.Errorf("%v: %v", cmd, err) 114 } 115 } 116 }) 117 if checkGoBuild.err != nil { 118 return checkGoBuild.err 119 } 120 121 case "diff": 122 // Check that diff is the GNU version, needed for the -u argument and 123 // to report missing newlines at the end of files. 124 out, err := exec.Command(tool, "-version").Output() 125 if err != nil { 126 return err 127 } 128 if !bytes.Contains(out, []byte("GNU diffutils")) { 129 return fmt.Errorf("diff is not the GNU version") 130 } 131 } 132 133 return nil 134 } 135 136 func cgoEnabled(bypassEnvironment bool) (bool, error) { 137 cmd := exec.Command("go", "env", "CGO_ENABLED") 138 if bypassEnvironment { 139 cmd.Env = append(append([]string(nil), os.Environ()...), "CGO_ENABLED=") 140 } 141 out, err := cmd.CombinedOutput() 142 if err != nil { 143 return false, err 144 } 145 enabled := strings.TrimSpace(string(out)) 146 return enabled == "1", nil 147 } 148 149 func allowMissingTool(tool string) bool { 150 switch runtime.GOOS { 151 case "aix", "darwin", "dragonfly", "freebsd", "illumos", "linux", "netbsd", "openbsd", "plan9", "solaris", "windows": 152 // Known non-mobile OS. Expect a reasonably complete environment. 153 default: 154 return true 155 } 156 157 switch tool { 158 case "cgo": 159 if strings.HasSuffix(os.Getenv("GO_BUILDER_NAME"), "-nocgo") { 160 // Explicitly disabled on -nocgo builders. 161 return true 162 } 163 if enabled, err := cgoEnabled(true); err == nil && !enabled { 164 // No platform support. 165 return true 166 } 167 case "go": 168 if os.Getenv("GO_BUILDER_NAME") == "illumos-amd64-joyent" { 169 // Work around a misconfigured builder (see https://golang.org/issue/33950). 170 return true 171 } 172 case "diff": 173 if os.Getenv("GO_BUILDER_NAME") != "" { 174 return true 175 } 176 case "patch": 177 if os.Getenv("GO_BUILDER_NAME") != "" { 178 return true 179 } 180 } 181 182 // If a developer is actively working on this test, we expect them to have all 183 // of its dependencies installed. However, if it's just a dependency of some 184 // other module (for example, being run via 'go test all'), we should be more 185 // tolerant of unusual environments. 186 return !packageMainIsDevel() 187 } 188 189 // NeedsTool skips t if the named tool is not present in the path. 190 // As a special case, "cgo" means "go" is present and can compile cgo programs. 191 func NeedsTool(t testing.TB, tool string) { 192 err := hasTool(tool) 193 if err == nil { 194 return 195 } 196 197 t.Helper() 198 if allowMissingTool(tool) { 199 t.Skipf("skipping because %s tool not available: %v", tool, err) 200 } else { 201 t.Fatalf("%s tool not available: %v", tool, err) 202 } 203 } 204 205 // NeedsGoBuild skips t if the current system can't build programs with “go build” 206 // and then run them with os.StartProcess or exec.Command. 207 // Android doesn't have the userspace go build needs to run, 208 // and js/wasm doesn't support running subprocesses. 209 func NeedsGoBuild(t testing.TB) { 210 t.Helper() 211 212 // This logic was derived from internal/testing.HasGoBuild and 213 // may need to be updated as that function evolves. 214 215 NeedsTool(t, "go") 216 } 217 218 // Go1Point returns the x in Go 1.x. 219 func Go1Point() int { 220 for i := len(build.Default.ReleaseTags) - 1; i >= 0; i-- { 221 var version int 222 if _, err := fmt.Sscanf(build.Default.ReleaseTags[i], "go1.%d", &version); err != nil { 223 continue 224 } 225 return version 226 } 227 panic("bad release tags") 228 } 229 230 // NeedsGo1Point skips t if the Go version used to run the test is older than 231 // 1.x. 232 func NeedsGo1Point(t testing.TB, x int) { 233 if Go1Point() < x { 234 t.Helper() 235 t.Skipf("running Go version %q is version 1.%d, older than required 1.%d", runtime.Version(), Go1Point(), x) 236 } 237 } 238 239 // SkipAfterGo1Point skips t if the Go version used to run the test is newer than 240 // 1.x. 241 func SkipAfterGo1Point(t testing.TB, x int) { 242 if Go1Point() > x { 243 t.Helper() 244 t.Skipf("running Go version %q is version 1.%d, newer than maximum 1.%d", runtime.Version(), Go1Point(), x) 245 } 246 } 247 248 // Deadline returns the deadline of t, if known, 249 // using the Deadline method added in Go 1.15. 250 func Deadline(t testing.TB) (time.Time, bool) { 251 td, ok := t.(interface { 252 Deadline() (time.Time, bool) 253 }) 254 if !ok { 255 return time.Time{}, false 256 } 257 return td.Deadline() 258 } 259 260 // NeedsGoExperiment skips t if the current process environment does not 261 // have a GOEXPERIMENT flag set. 262 func NeedsGoExperiment(t testing.TB, flag string) { 263 t.Helper() 264 265 goexp := os.Getenv("GOEXPERIMENT") 266 set := false 267 for _, f := range strings.Split(goexp, ",") { 268 if f == "" { 269 continue 270 } 271 if f == "none" { 272 // GOEXPERIMENT=none disables all experiment flags. 273 set = false 274 break 275 } 276 val := true 277 if strings.HasPrefix(f, "no") { 278 f, val = f[2:], false 279 } 280 if f == flag { 281 set = val 282 } 283 } 284 if !set { 285 t.Skipf("skipping test: flag %q is not set in GOEXPERIMENT=%q", flag, goexp) 286 } 287 }