/
testenv.go
110 lines (97 loc) · 2.82 KB
/
testenv.go
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
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package testenv contains helper functions for skipping tests
// based on which tools are present in the environment.
package testenv
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"runtime"
"runtime/debug"
"testing"
)
// packageMainIsDevel reports whether the module containing package main
// is a development version (if module information is available).
func packageMainIsDevel() bool {
info, ok := debug.ReadBuildInfo()
if !ok {
// Most test binaries currently lack build info, but this should become more
// permissive once https://golang.org/issue/33976 is fixed.
return true
}
// Note: info.Main.Version describes the version of the module containing
// package main, not the version of “the main module”.
// See https://golang.org/issue/33975.
return info.Main.Version == "(devel)"
}
func hasTool(tool string) error {
_, err := exec.LookPath(tool)
if err != nil {
return err
}
switch tool {
case "patch":
// check that the patch tools supports the -o argument
temp, err := ioutil.TempFile("", "patch-test")
if err != nil {
return err
}
temp.Close()
defer os.Remove(temp.Name())
cmd := exec.Command(tool, "-o", temp.Name())
if err := cmd.Run(); err != nil {
return err
}
case "diff":
// Check that diff is the GNU version, needed for the -u argument and
// to report missing newlines at the end of files.
out, err := exec.Command(tool, "-version").Output()
if err != nil {
return err
}
if !bytes.Contains(out, []byte("GNU diffutils")) {
return fmt.Errorf("diff is not the GNU version")
}
}
return nil
}
func allowMissingTool(tool string) bool {
if runtime.GOOS == "android" {
// Android builds generally run tests on a separate machine from the build,
// so don't expect any external tools to be available.
return true
}
switch tool {
case "diff":
if os.Getenv("GO_BUILDER_NAME") != "" {
return true
}
case "patch":
if os.Getenv("GO_BUILDER_NAME") != "" {
return true
}
}
// If a developer is actively working on this test, we expect them to have all
// of its dependencies installed. However, if it's just a dependency of some
// other module (for example, being run via 'go test all'), we should be more
// tolerant of unusual environments.
return !packageMainIsDevel()
}
// NeedsTool skips t if the named tool is not present in the path.
// As a special case, "cgo" means "go" is present and can compile cgo programs.
func NeedsTool(t testing.TB, tool string) {
err := hasTool(tool)
if err == nil {
return
}
t.Helper()
if allowMissingTool(tool) {
t.Skipf("skipping because %s tool not available: %v", tool, err)
} else {
t.Fatalf("%s tool not available: %v", tool, err)
}
}