Skip to content

Commit 7da961d

Browse files
committed
cmd/godoc: convert tests to packagestest, cover third party packages
This change converts cmd/godoc tests to use the packagestest package in its basic integration tests. For now, those tests continue to run in GOPATH mode only. When module support is added to cmd/godoc, then the same tests will be made to run in module mode too. Previously, the basic integration test covered godoc functionality on Go packages in GOROOT only. This change also adds some third party packages to increase test coverage. This is easy to do with the packagestest API. Updates golang/go#33655 Change-Id: If3fce913140b81ed9340556d6bb4b963f5f98813 Reviewed-on: https://go-review.googlesource.com/c/tools/+/196981 Run-TryBot: Dmitri Shuralyov <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Andrew Bonventre <[email protected]>
1 parent d04c33d commit 7da961d

File tree

1 file changed

+84
-44
lines changed

1 file changed

+84
-44
lines changed

cmd/godoc/godoc_test.go

Lines changed: 84 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"testing"
2323
"time"
2424

25+
"golang.org/x/tools/go/packages/packagestest"
2526
"golang.org/x/tools/internal/testenv"
2627
)
2728

@@ -181,12 +182,12 @@ func TestURL(t *testing.T) {
181182
cmd.Stderr = stderr
182183
cmd.Args[0] = "godoc"
183184

184-
// Set GOPATH variable to non-existing path
185+
// Set GOPATH variable to non-existing absolute path
185186
// and GOPROXY=off to disable module fetches.
186187
// We cannot just unset GOPATH variable because godoc would default it to ~/go.
187188
// (We don't want the indexer looking at the local workspace during tests.)
188189
cmd.Env = append(os.Environ(),
189-
"GOPATH=does_not_exist",
190+
"GOPATH=/does_not_exist",
190191
"GOPROXY=off",
191192
"GO111MODULE=off")
192193

@@ -206,43 +207,61 @@ func TestURL(t *testing.T) {
206207

207208
// Basic integration test for godoc HTTP interface.
208209
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.
210214
}
211215

212216
// Basic integration test for godoc HTTP interface.
213217
func TestWebIndex(t *testing.T) {
214218
if testing.Short() {
215219
t.Skip("skipping test in -short mode")
216220
}
217-
testWeb(t, true)
221+
bin, cleanup := buildGodoc(t)
222+
defer cleanup()
223+
testWeb(t, packagestest.GOPATH, bin, true)
218224
}
219225

220226
// 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) {
222228
if runtime.GOOS == "plan9" {
223229
t.Skip("skipping on plan9; fails to start up quickly enough")
224230
}
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.
227253
addr := serverAddress(t)
228254
args := []string{fmt.Sprintf("-http=%s", addr)}
229255
if withIndex {
230256
args = append(args, "-index", "-index_interval=-1s")
231257
}
232258
cmd := exec.Command(bin, args...)
259+
cmd.Dir = e.Config.Dir
260+
cmd.Env = e.Config.Env
233261
cmd.Stdout = os.Stderr
234262
cmd.Stderr = os.Stderr
235263
cmd.Args[0] = "godoc"
236264

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-
246265
if err := cmd.Start(); err != nil {
247266
t.Fatalf("failed to start godoc: %s", err)
248267
}
@@ -284,6 +303,8 @@ func testWeb(t *testing.T, withIndex bool) {
284303
contains: []string{
285304
"Standard library",
286305
"Package fmt implements formatted I/O",
306+
"Third party",
307+
"Package a is a package in godoc.test/repo1.",
287308
},
288309
notContains: []string{
289310
"internal/syscall",
@@ -359,6 +380,16 @@ func testWeb(t *testing.T, withIndex bool) {
359380
},
360381
releaseTag: "go1.11",
361382
},
383+
384+
// Third party packages.
385+
{
386+
path: "/pkg/godoc.test/repo1/a",
387+
contains: []string{`const <span id="Name">Name</span> = &#34;repo1a&#34;`},
388+
},
389+
{
390+
path: "/pkg/godoc.test/repo2/b",
391+
contains: []string{`const <span id="Name">Name</span> = &#34;repo2b&#34;`},
392+
},
362393
}
363394
for _, test := range tests {
364395
if test.needIndex && !withIndex {
@@ -412,49 +443,58 @@ func testWeb(t *testing.T, withIndex bool) {
412443

413444
// Basic integration test for godoc -analysis=type (via HTTP interface).
414445
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) {
415453
if runtime.GOOS == "plan9" {
416454
t.Skip("skipping test on plan9 (issue #11974)") // see comment re: Plan 9 below
417455
}
418456

419457
// 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": `
427475
package lib
428476
type T struct{}
429477
const C = 3
430478
var V T
431479
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)
446487
}
488+
defer e.Cleanup()
447489

448490
// Start the server.
449-
bin, cleanup := buildGodoc(t)
450-
defer cleanup()
451491
addr := serverAddress(t)
452492
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))
458498
cmd.Stdout = os.Stderr
459499
stderr, err := cmd.StderrPipe()
460500
if err != nil {

0 commit comments

Comments
 (0)