@@ -6,8 +6,10 @@ import (
6
6
"go/types"
7
7
"io/ioutil"
8
8
"os"
9
+ "os/exec"
9
10
"path/filepath"
10
11
"reflect"
12
+ "runtime"
11
13
"sort"
12
14
"strings"
13
15
"testing"
@@ -26,22 +28,24 @@ func TestChanges(t *testing.T) {
26
28
sort .Strings (wanti )
27
29
sort .Strings (wantc )
28
30
29
- oldpkg , err := load ("apidiff/old" , dir )
31
+ oldpkg , err := load (t , "apidiff/old" , dir )
30
32
if err != nil {
31
33
t .Fatal (err )
32
34
}
33
- newpkg , err := load ("apidiff/new" , dir )
35
+ newpkg , err := load (t , "apidiff/new" , dir )
34
36
if err != nil {
35
37
t .Fatal (err )
36
38
}
37
39
38
40
report := Changes (oldpkg .Types , newpkg .Types )
39
41
40
- if ! reflect .DeepEqual (report .Incompatible , wanti ) {
41
- t .Errorf ("incompatibles: got %v\n want %v\n " , report .Incompatible , wanti )
42
+ got := report .messages (false )
43
+ if ! reflect .DeepEqual (got , wanti ) {
44
+ t .Errorf ("incompatibles: got %v\n want %v\n " , got , wanti )
42
45
}
43
- if ! reflect .DeepEqual (report .Compatible , wantc ) {
44
- t .Errorf ("compatibles: got %v\n want %v\n " , report .Compatible , wantc )
46
+ got = report .messages (true )
47
+ if ! reflect .DeepEqual (got , wantc ) {
48
+ t .Errorf ("compatibles: got %v\n want %v\n " , got , wantc )
45
49
}
46
50
}
47
51
@@ -113,7 +117,9 @@ func splitIntoPackages(t *testing.T, dir string) (incompatibles, compatibles []s
113
117
return
114
118
}
115
119
116
- func load (importPath , goPath string ) (* packages.Package , error ) {
120
+ func load (t * testing.T , importPath , goPath string ) (* packages.Package , error ) {
121
+ needsGoPackages (t )
122
+
117
123
cfg := & packages.Config {
118
124
Mode : packages .LoadTypes ,
119
125
}
@@ -132,7 +138,7 @@ func load(importPath, goPath string) (*packages.Package, error) {
132
138
}
133
139
134
140
func TestExportedFields (t * testing.T ) {
135
- pkg , err := load ("golang.org/x/exp/apidiff/testdata/exported_fields" , "" )
141
+ pkg , err := load (t , "golang.org/x/exp/apidiff/testdata/exported_fields" , "" )
136
142
if err != nil {
137
143
t .Fatal (err )
138
144
}
@@ -164,3 +170,69 @@ func TestExportedFields(t *testing.T) {
164
170
}
165
171
}
166
172
}
173
+
174
+ // needsGoPackages skips t if the go/packages driver (or 'go' tool) implied by
175
+ // the current process environment is not present in the path.
176
+ //
177
+ // Copied and adapted from golang.org/x/tools/internal/testenv.
178
+ func needsGoPackages (t * testing.T ) {
179
+ t .Helper ()
180
+
181
+ tool := os .Getenv ("GOPACKAGESDRIVER" )
182
+ switch tool {
183
+ case "off" :
184
+ // "off" forces go/packages to use the go command.
185
+ tool = "go"
186
+ case "" :
187
+ if _ , err := exec .LookPath ("gopackagesdriver" ); err == nil {
188
+ tool = "gopackagesdriver"
189
+ } else {
190
+ tool = "go"
191
+ }
192
+ }
193
+
194
+ needsTool (t , tool )
195
+ }
196
+
197
+ // needsTool skips t if the named tool is not present in the path.
198
+ //
199
+ // Copied and adapted from golang.org/x/tools/internal/testenv.
200
+ func needsTool (t * testing.T , tool string ) {
201
+ _ , err := exec .LookPath (tool )
202
+ if err == nil {
203
+ return
204
+ }
205
+
206
+ t .Helper ()
207
+ if allowMissingTool (tool ) {
208
+ t .Skipf ("skipping because %s tool not available: %v" , tool , err )
209
+ } else {
210
+ t .Fatalf ("%s tool not available: %v" , tool , err )
211
+ }
212
+ }
213
+
214
+ func allowMissingTool (tool string ) bool {
215
+ if runtime .GOOS == "android" {
216
+ // Android builds generally run tests on a separate machine from the build,
217
+ // so don't expect any external tools to be available.
218
+ return true
219
+ }
220
+
221
+ if tool == "go" && os .Getenv ("GO_BUILDER_NAME" ) == "illumos-amd64-joyent" {
222
+ // Work around a misconfigured builder (see https://golang.org/issue/33950).
223
+ return true
224
+ }
225
+
226
+ // If a developer is actively working on this test, we expect them to have all
227
+ // of its dependencies installed. However, if it's just a dependency of some
228
+ // other module (for example, being run via 'go test all'), we should be more
229
+ // tolerant of unusual environments.
230
+ return ! packageMainIsDevel ()
231
+ }
232
+
233
+ // packageMainIsDevel reports whether the module containing package main
234
+ // is a development version (if module information is available).
235
+ //
236
+ // Builds in GOPATH mode and builds that lack module information are assumed to
237
+ // be development versions.
238
+ var packageMainIsDevel = func () bool { return true }
0 commit comments