@@ -22,6 +22,7 @@ import (
22
22
"testing"
23
23
"time"
24
24
25
+ "golang.org/x/tools/go/packages/packagestest"
25
26
"golang.org/x/tools/internal/testenv"
26
27
)
27
28
@@ -181,12 +182,12 @@ func TestURL(t *testing.T) {
181
182
cmd .Stderr = stderr
182
183
cmd .Args [0 ] = "godoc"
183
184
184
- // Set GOPATH variable to non-existing path
185
+ // Set GOPATH variable to non-existing absolute path
185
186
// and GOPROXY=off to disable module fetches.
186
187
// We cannot just unset GOPATH variable because godoc would default it to ~/go.
187
188
// (We don't want the indexer looking at the local workspace during tests.)
188
189
cmd .Env = append (os .Environ (),
189
- "GOPATH=does_not_exist" ,
190
+ "GOPATH=/ does_not_exist" ,
190
191
"GOPROXY=off" ,
191
192
"GO111MODULE=off" )
192
193
@@ -206,43 +207,61 @@ func TestURL(t *testing.T) {
206
207
207
208
// Basic integration test for godoc HTTP interface.
208
209
func TestWeb (t * testing.T ) {
209
- testWeb (t , false )
210
+ bin , cleanup := buildGodoc (t )
211
+ defer cleanup ()
212
+ testWeb (t , packagestest .GOPATH , bin , false )
213
+ // TODO(golang.org/issue/33655): Add support for module mode, then enable its test coverage.
210
214
}
211
215
212
216
// Basic integration test for godoc HTTP interface.
213
217
func TestWebIndex (t * testing.T ) {
214
218
if testing .Short () {
215
219
t .Skip ("skipping test in -short mode" )
216
220
}
217
- testWeb (t , true )
221
+ bin , cleanup := buildGodoc (t )
222
+ defer cleanup ()
223
+ testWeb (t , packagestest .GOPATH , bin , true )
218
224
}
219
225
220
226
// Basic integration test for godoc HTTP interface.
221
- func testWeb (t * testing.T , withIndex bool ) {
227
+ func testWeb (t * testing.T , x packagestest. Exporter , bin string , withIndex bool ) {
222
228
if runtime .GOOS == "plan9" {
223
229
t .Skip ("skipping on plan9; fails to start up quickly enough" )
224
230
}
225
- bin , cleanup := buildGodoc (t )
226
- defer cleanup ()
231
+
232
+ // Write a fake GOROOT/GOPATH with some third party packages.
233
+ e := packagestest .Export (t , x , []packagestest.Module {
234
+ {
235
+ Name : "godoc.test/repo1" ,
236
+ Files : map [string ]interface {}{
237
+ "a/a.go" : `// Package a is a package in godoc.test/repo1.
238
+ package a; import _ "godoc.test/repo2/a"; const Name = "repo1a"` ,
239
+ "b/b.go" : `package b; const Name = "repo1b"` ,
240
+ },
241
+ },
242
+ {
243
+ Name : "godoc.test/repo2" ,
244
+ Files : map [string ]interface {}{
245
+ "a/a.go" : `package a; const Name = "repo2a"` ,
246
+ "b/b.go" : `package b; const Name = "repo2b"` ,
247
+ },
248
+ },
249
+ })
250
+ defer e .Cleanup ()
251
+
252
+ // Start the server.
227
253
addr := serverAddress (t )
228
254
args := []string {fmt .Sprintf ("-http=%s" , addr )}
229
255
if withIndex {
230
256
args = append (args , "-index" , "-index_interval=-1s" )
231
257
}
232
258
cmd := exec .Command (bin , args ... )
259
+ cmd .Dir = e .Config .Dir
260
+ cmd .Env = e .Config .Env
233
261
cmd .Stdout = os .Stderr
234
262
cmd .Stderr = os .Stderr
235
263
cmd .Args [0 ] = "godoc"
236
264
237
- // Set GOPATH variable to non-existing path
238
- // and GOPROXY=off to disable module fetches.
239
- // We cannot just unset GOPATH variable because godoc would default it to ~/go.
240
- // (We don't want the indexer looking at the local workspace during tests.)
241
- cmd .Env = append (os .Environ (),
242
- "GOPATH=does_not_exist" ,
243
- "GOPROXY=off" ,
244
- "GO111MODULE=off" )
245
-
246
265
if err := cmd .Start (); err != nil {
247
266
t .Fatalf ("failed to start godoc: %s" , err )
248
267
}
@@ -284,6 +303,8 @@ func testWeb(t *testing.T, withIndex bool) {
284
303
contains : []string {
285
304
"Standard library" ,
286
305
"Package fmt implements formatted I/O" ,
306
+ "Third party" ,
307
+ "Package a is a package in godoc.test/repo1." ,
287
308
},
288
309
notContains : []string {
289
310
"internal/syscall" ,
@@ -359,6 +380,16 @@ func testWeb(t *testing.T, withIndex bool) {
359
380
},
360
381
releaseTag : "go1.11" ,
361
382
},
383
+
384
+ // Third party packages.
385
+ {
386
+ path : "/pkg/godoc.test/repo1/a" ,
387
+ contains : []string {`const <span id="Name">Name</span> = "repo1a"` },
388
+ },
389
+ {
390
+ path : "/pkg/godoc.test/repo2/b" ,
391
+ contains : []string {`const <span id="Name">Name</span> = "repo2b"` },
392
+ },
362
393
}
363
394
for _ , test := range tests {
364
395
if test .needIndex && ! withIndex {
@@ -412,49 +443,58 @@ func testWeb(t *testing.T, withIndex bool) {
412
443
413
444
// Basic integration test for godoc -analysis=type (via HTTP interface).
414
445
func TestTypeAnalysis (t * testing.T ) {
446
+ bin , cleanup := buildGodoc (t )
447
+ defer cleanup ()
448
+ testTypeAnalysis (t , packagestest .GOPATH , bin )
449
+ // TODO(golang.org/issue/34473): Add support for type, pointer
450
+ // analysis in module mode, then enable its test coverage here.
451
+ }
452
+ func testTypeAnalysis (t * testing.T , x packagestest.Exporter , bin string ) {
415
453
if runtime .GOOS == "plan9" {
416
454
t .Skip ("skipping test on plan9 (issue #11974)" ) // see comment re: Plan 9 below
417
455
}
418
456
419
457
// Write a fake GOROOT/GOPATH.
420
- tmpdir , err := ioutil .TempDir ("" , "godoc-analysis" )
421
- if err != nil {
422
- t .Fatalf ("ioutil.TempDir failed: %s" , err )
423
- }
424
- defer os .RemoveAll (tmpdir )
425
- for _ , f := range []struct { file , content string }{
426
- {"goroot/src/lib/lib.go" , `
458
+ // TODO(golang.org/issue/34473): This test uses import paths without a dot in first
459
+ // path element. This is not viable in module mode; import paths will need to change.
460
+ e := packagestest .Export (t , x , []packagestest.Module {
461
+ {
462
+ Name : "app" ,
463
+ Files : map [string ]interface {}{
464
+ "main.go" : `
465
+ package main
466
+ import "lib"
467
+ func main() { print(lib.V) }
468
+ ` ,
469
+ },
470
+ },
471
+ {
472
+ Name : "lib" ,
473
+ Files : map [string ]interface {}{
474
+ "lib.go" : `
427
475
package lib
428
476
type T struct{}
429
477
const C = 3
430
478
var V T
431
479
func (T) F() int { return C }
432
- ` },
433
- {"gopath/src/app/main.go" , `
434
- package main
435
- import "lib"
436
- func main() { print(lib.V) }
437
- ` },
438
- } {
439
- file := filepath .Join (tmpdir , f .file )
440
- if err := os .MkdirAll (filepath .Dir (file ), 0755 ); err != nil {
441
- t .Fatalf ("MkdirAll(%s) failed: %s" , filepath .Dir (file ), err )
442
- }
443
- if err := ioutil .WriteFile (file , []byte (f .content ), 0644 ); err != nil {
444
- t .Fatal (err )
445
- }
480
+ ` ,
481
+ },
482
+ },
483
+ })
484
+ goroot := filepath .Join (e .Temp (), "goroot" )
485
+ if err := os .Mkdir (goroot , 0755 ); err != nil {
486
+ t .Fatalf ("os.Mkdir(%q) failed: %v" , goroot , err )
446
487
}
488
+ defer e .Cleanup ()
447
489
448
490
// Start the server.
449
- bin , cleanup := buildGodoc (t )
450
- defer cleanup ()
451
491
addr := serverAddress (t )
452
492
cmd := exec .Command (bin , fmt .Sprintf ("-http=%s" , addr ), "-analysis=type" )
453
- cmd .Env = os . Environ ()
454
- cmd . Env = append ( cmd . Env , fmt . Sprintf ( " GOROOT=%s" , filepath . Join ( tmpdir , "goroot" )))
455
- cmd . Env = append ( cmd . Env , fmt . Sprintf ( "GOPATH=%s" , filepath . Join ( tmpdir , "gopath" )))
456
- cmd . Env = append ( cmd . Env , "GO111MODULE=off" )
457
- cmd .Env = append (cmd . Env , "GOPROXY=off" )
493
+ cmd .Dir = e . Config . Dir
494
+ // Point to an empty GOROOT directory to speed things up
495
+ // by not doing type analysis for the entire real GOROOT.
496
+ // TODO(golang.org/issue/34473): This test optimization may not be viable in module mode.
497
+ cmd .Env = append (e . Config . Env , fmt . Sprintf ( "GOROOT=%s" , goroot ) )
458
498
cmd .Stdout = os .Stderr
459
499
stderr , err := cmd .StderrPipe ()
460
500
if err != nil {
0 commit comments