2013-07-17 01:32:45 -06:00
|
|
|
// Copyright 2013 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 main_test
|
|
|
|
|
|
|
|
import (
|
2014-10-27 19:25:46 -06:00
|
|
|
"bufio"
|
2014-03-16 14:17:13 -06:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2018-07-25 01:31:31 -06:00
|
|
|
"go/build"
|
2014-10-27 19:25:46 -06:00
|
|
|
"io"
|
2013-07-17 01:32:45 -06:00
|
|
|
"io/ioutil"
|
2014-03-16 14:17:13 -06:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2013-07-17 01:32:45 -06:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
2013-07-18 18:31:12 -06:00
|
|
|
"runtime"
|
2014-02-26 11:21:44 -07:00
|
|
|
"strings"
|
2013-07-17 01:32:45 -06:00
|
|
|
"testing"
|
2014-03-16 14:17:13 -06:00
|
|
|
"time"
|
2019-08-29 14:22:26 -06:00
|
|
|
|
2019-09-23 13:43:37 -06:00
|
|
|
"golang.org/x/tools/go/packages/packagestest"
|
2019-08-29 14:22:26 -06:00
|
|
|
"golang.org/x/tools/internal/testenv"
|
2013-07-17 01:32:45 -06:00
|
|
|
)
|
|
|
|
|
2014-03-16 14:17:13 -06:00
|
|
|
// buildGodoc builds the godoc executable.
|
|
|
|
// It returns its path, and a cleanup function.
|
|
|
|
//
|
|
|
|
// TODO(adonovan): opt: do this at most once, and do the cleanup
|
|
|
|
// exactly once. How though? There's no atexit.
|
|
|
|
func buildGodoc(t *testing.T) (bin string, cleanup func()) {
|
2019-08-29 14:22:26 -06:00
|
|
|
t.Helper()
|
|
|
|
|
2015-03-12 23:21:50 -06:00
|
|
|
if runtime.GOARCH == "arm" {
|
|
|
|
t.Skip("skipping test on arm platforms; too slow")
|
|
|
|
}
|
2019-03-06 02:28:10 -07:00
|
|
|
if runtime.GOOS == "android" {
|
|
|
|
t.Skipf("the dependencies are not available on android")
|
|
|
|
}
|
2019-08-29 14:22:26 -06:00
|
|
|
testenv.NeedsTool(t, "go")
|
2019-03-06 02:28:10 -07:00
|
|
|
|
2013-07-17 01:32:45 -06:00
|
|
|
tmp, err := ioutil.TempDir("", "godoc-regtest-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-01-15 11:16:57 -07:00
|
|
|
defer func() {
|
|
|
|
if cleanup == nil { // probably, go build failed.
|
|
|
|
os.RemoveAll(tmp)
|
|
|
|
}
|
|
|
|
}()
|
2013-07-17 01:32:45 -06:00
|
|
|
|
2014-03-16 14:17:13 -06:00
|
|
|
bin = filepath.Join(tmp, "godoc")
|
2013-07-18 18:31:12 -06:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
bin += ".exe"
|
|
|
|
}
|
2013-07-17 01:32:45 -06:00
|
|
|
cmd := exec.Command("go", "build", "-o", bin)
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
t.Fatalf("Building godoc: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-03-16 14:17:13 -06:00
|
|
|
return bin, func() { os.RemoveAll(tmp) }
|
|
|
|
}
|
|
|
|
|
|
|
|
func serverAddress(t *testing.T) string {
|
|
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
ln, err = net.Listen("tcp6", "[::1]:0")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer ln.Close()
|
|
|
|
return ln.Addr().String()
|
|
|
|
}
|
|
|
|
|
2019-09-20 13:40:10 -06:00
|
|
|
func waitForServerReady(t *testing.T, cmd *exec.Cmd, addr string) {
|
|
|
|
ch := make(chan error, 1)
|
|
|
|
go func() { ch <- fmt.Errorf("server exited early: %v", cmd.Wait()) }()
|
|
|
|
go waitForServer(t, ch,
|
2015-07-30 23:02:02 -06:00
|
|
|
fmt.Sprintf("http://%v/", addr),
|
|
|
|
"The Go Programming Language",
|
2018-02-09 21:29:54 -07:00
|
|
|
15*time.Second,
|
|
|
|
false)
|
2019-09-20 13:40:10 -06:00
|
|
|
if err := <-ch; err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-30 23:02:02 -06:00
|
|
|
}
|
|
|
|
|
2019-09-20 13:40:10 -06:00
|
|
|
func waitForSearchReady(t *testing.T, cmd *exec.Cmd, addr string) {
|
|
|
|
ch := make(chan error, 1)
|
|
|
|
go func() { ch <- fmt.Errorf("server exited early: %v", cmd.Wait()) }()
|
|
|
|
go waitForServer(t, ch,
|
2015-07-30 23:02:02 -06:00
|
|
|
fmt.Sprintf("http://%v/search?q=FALLTHROUGH", addr),
|
|
|
|
"The list of tokens.",
|
2018-02-09 21:29:54 -07:00
|
|
|
2*time.Minute,
|
|
|
|
false)
|
2019-09-20 13:40:10 -06:00
|
|
|
if err := <-ch; err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-02-09 21:29:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func waitUntilScanComplete(t *testing.T, addr string) {
|
2019-09-20 13:40:10 -06:00
|
|
|
ch := make(chan error)
|
|
|
|
go waitForServer(t, ch,
|
2018-02-09 21:29:54 -07:00
|
|
|
fmt.Sprintf("http://%v/pkg", addr),
|
|
|
|
"Scan is not yet complete",
|
|
|
|
2*time.Minute,
|
2019-09-20 13:40:10 -06:00
|
|
|
// setting reverse as true, which means this waits
|
|
|
|
// until the string is not returned in the response anymore
|
2018-02-09 21:29:54 -07:00
|
|
|
true,
|
|
|
|
)
|
2019-09-20 13:40:10 -06:00
|
|
|
if err := <-ch; err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-30 23:02:02 -06:00
|
|
|
}
|
2015-03-10 05:29:27 -06:00
|
|
|
|
2015-07-30 23:02:02 -06:00
|
|
|
const pollInterval = 200 * time.Millisecond
|
2015-03-10 05:29:27 -06:00
|
|
|
|
2019-09-20 13:40:10 -06:00
|
|
|
// waitForServer waits for server to meet the required condition.
|
|
|
|
// It sends a single error value to ch, unless the test has failed.
|
|
|
|
// The error value is nil if the required condition was met within
|
|
|
|
// timeout, or non-nil otherwise.
|
|
|
|
func waitForServer(t *testing.T, ch chan<- error, url, match string, timeout time.Duration, reverse bool) {
|
2015-07-30 23:02:02 -06:00
|
|
|
deadline := time.Now().Add(timeout)
|
2015-03-10 05:29:27 -06:00
|
|
|
for time.Now().Before(deadline) {
|
2019-09-20 13:40:10 -06:00
|
|
|
if t.Failed() {
|
|
|
|
return
|
|
|
|
}
|
2015-07-30 23:02:02 -06:00
|
|
|
res, err := http.Get(url)
|
2014-03-16 14:17:13 -06:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-09-20 13:40:10 -06:00
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
2015-03-10 05:29:27 -06:00
|
|
|
res.Body.Close()
|
2019-09-20 13:40:10 -06:00
|
|
|
if err != nil || res.StatusCode != http.StatusOK {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case !reverse && bytes.Contains(body, []byte(match)),
|
|
|
|
reverse && !bytes.Contains(body, []byte(match)):
|
|
|
|
ch <- nil
|
|
|
|
return
|
2015-03-10 05:29:27 -06:00
|
|
|
}
|
2019-09-20 13:40:10 -06:00
|
|
|
time.Sleep(pollInterval)
|
2014-03-16 14:17:13 -06:00
|
|
|
}
|
2019-09-20 13:40:10 -06:00
|
|
|
ch <- fmt.Errorf("server failed to respond in %v", timeout)
|
2014-03-16 14:17:13 -06:00
|
|
|
}
|
|
|
|
|
2018-07-25 01:31:31 -06:00
|
|
|
// hasTag checks whether a given release tag is contained in the current version
|
|
|
|
// of the go binary.
|
|
|
|
func hasTag(t string) bool {
|
|
|
|
for _, v := range build.Default.ReleaseTags {
|
|
|
|
if t == v {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-10-27 23:59:58 -06:00
|
|
|
func killAndWait(cmd *exec.Cmd) {
|
|
|
|
cmd.Process.Kill()
|
2019-09-20 13:40:10 -06:00
|
|
|
cmd.Process.Wait()
|
2014-10-27 23:59:58 -06:00
|
|
|
}
|
|
|
|
|
2019-02-15 11:30:09 -07:00
|
|
|
func TestURL(t *testing.T) {
|
|
|
|
if runtime.GOOS == "plan9" {
|
|
|
|
t.Skip("skipping on plan9; fails to start up quickly enough")
|
|
|
|
}
|
|
|
|
bin, cleanup := buildGodoc(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
testcase := func(url string, contents string) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
stdout, stderr := new(bytes.Buffer), new(bytes.Buffer)
|
|
|
|
|
|
|
|
args := []string{fmt.Sprintf("-url=%s", url)}
|
|
|
|
cmd := exec.Command(bin, args...)
|
|
|
|
cmd.Stdout = stdout
|
|
|
|
cmd.Stderr = stderr
|
|
|
|
cmd.Args[0] = "godoc"
|
|
|
|
|
2019-09-23 13:43:37 -06:00
|
|
|
// Set GOPATH variable to non-existing absolute path
|
2019-02-15 11:30:09 -07:00
|
|
|
// and GOPROXY=off to disable module fetches.
|
|
|
|
// We cannot just unset GOPATH variable because godoc would default it to ~/go.
|
|
|
|
// (We don't want the indexer looking at the local workspace during tests.)
|
|
|
|
cmd.Env = append(os.Environ(),
|
2019-09-23 13:43:37 -06:00
|
|
|
"GOPATH=/does_not_exist",
|
2019-02-15 11:30:09 -07:00
|
|
|
"GOPROXY=off",
|
|
|
|
"GO111MODULE=off")
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
t.Fatalf("failed to run godoc -url=%q: %s\nstderr:\n%s", url, err, stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(stdout.String(), contents) {
|
|
|
|
t.Errorf("did not find substring %q in output of godoc -url=%q:\n%s", contents, url, stdout)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("index", testcase("/", "Go is an open source programming language"))
|
|
|
|
t.Run("fmt", testcase("/pkg/fmt", "Package fmt implements formatted I/O"))
|
|
|
|
}
|
|
|
|
|
2014-03-16 14:17:13 -06:00
|
|
|
// Basic integration test for godoc HTTP interface.
|
|
|
|
func TestWeb(t *testing.T) {
|
2019-09-23 13:43:37 -06:00
|
|
|
bin, cleanup := buildGodoc(t)
|
|
|
|
defer cleanup()
|
|
|
|
testWeb(t, packagestest.GOPATH, bin, false)
|
|
|
|
// TODO(golang.org/issue/33655): Add support for module mode, then enable its test coverage.
|
2015-07-30 23:02:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Basic integration test for godoc HTTP interface.
|
|
|
|
func TestWebIndex(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping test in -short mode")
|
|
|
|
}
|
2019-09-23 13:43:37 -06:00
|
|
|
bin, cleanup := buildGodoc(t)
|
|
|
|
defer cleanup()
|
|
|
|
testWeb(t, packagestest.GOPATH, bin, true)
|
2015-07-30 23:02:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Basic integration test for godoc HTTP interface.
|
2019-09-23 13:43:37 -06:00
|
|
|
func testWeb(t *testing.T, x packagestest.Exporter, bin string, withIndex bool) {
|
2017-11-29 12:51:57 -07:00
|
|
|
if runtime.GOOS == "plan9" {
|
2019-02-15 11:30:09 -07:00
|
|
|
t.Skip("skipping on plan9; fails to start up quickly enough")
|
2017-11-29 12:51:57 -07:00
|
|
|
}
|
2019-09-23 13:43:37 -06:00
|
|
|
|
|
|
|
// Write a fake GOROOT/GOPATH with some third party packages.
|
|
|
|
e := packagestest.Export(t, x, []packagestest.Module{
|
|
|
|
{
|
|
|
|
Name: "godoc.test/repo1",
|
|
|
|
Files: map[string]interface{}{
|
|
|
|
"a/a.go": `// Package a is a package in godoc.test/repo1.
|
|
|
|
package a; import _ "godoc.test/repo2/a"; const Name = "repo1a"`,
|
|
|
|
"b/b.go": `package b; const Name = "repo1b"`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "godoc.test/repo2",
|
|
|
|
Files: map[string]interface{}{
|
|
|
|
"a/a.go": `package a; const Name = "repo2a"`,
|
|
|
|
"b/b.go": `package b; const Name = "repo2b"`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
defer e.Cleanup()
|
|
|
|
|
|
|
|
// Start the server.
|
2014-03-16 14:17:13 -06:00
|
|
|
addr := serverAddress(t)
|
2015-07-30 23:02:02 -06:00
|
|
|
args := []string{fmt.Sprintf("-http=%s", addr)}
|
|
|
|
if withIndex {
|
|
|
|
args = append(args, "-index", "-index_interval=-1s")
|
|
|
|
}
|
|
|
|
cmd := exec.Command(bin, args...)
|
2019-09-23 13:43:37 -06:00
|
|
|
cmd.Dir = e.Config.Dir
|
|
|
|
cmd.Env = e.Config.Env
|
2014-03-16 14:17:13 -06:00
|
|
|
cmd.Stdout = os.Stderr
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
cmd.Args[0] = "godoc"
|
2018-03-23 05:59:17 -06:00
|
|
|
|
2014-03-16 14:17:13 -06:00
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
t.Fatalf("failed to start godoc: %s", err)
|
|
|
|
}
|
2014-10-27 23:59:58 -06:00
|
|
|
defer killAndWait(cmd)
|
2015-07-30 23:02:02 -06:00
|
|
|
|
|
|
|
if withIndex {
|
2019-09-20 13:40:10 -06:00
|
|
|
waitForSearchReady(t, cmd, addr)
|
2015-07-30 23:02:02 -06:00
|
|
|
} else {
|
2019-09-20 13:40:10 -06:00
|
|
|
waitForServerReady(t, cmd, addr)
|
2018-02-09 21:29:54 -07:00
|
|
|
waitUntilScanComplete(t, addr)
|
2015-07-30 23:02:02 -06:00
|
|
|
}
|
|
|
|
|
2014-10-13 10:47:02 -06:00
|
|
|
tests := []struct {
|
2018-07-17 15:23:18 -06:00
|
|
|
path string
|
|
|
|
contains []string // substring
|
|
|
|
match []string // regexp
|
|
|
|
notContains []string
|
|
|
|
needIndex bool
|
2018-07-25 01:31:31 -06:00
|
|
|
releaseTag string // optional release tag that must be in go/build.ReleaseTags
|
2014-10-13 10:47:02 -06:00
|
|
|
}{
|
|
|
|
{
|
2018-07-17 15:23:18 -06:00
|
|
|
path: "/",
|
|
|
|
contains: []string{"Go is an open source programming language"},
|
2014-10-13 10:47:02 -06:00
|
|
|
},
|
|
|
|
{
|
2018-07-17 15:23:18 -06:00
|
|
|
path: "/pkg/fmt/",
|
|
|
|
contains: []string{"Package fmt implements formatted I/O"},
|
2014-10-13 10:47:02 -06:00
|
|
|
},
|
|
|
|
{
|
2018-07-17 15:23:18 -06:00
|
|
|
path: "/src/fmt/",
|
|
|
|
contains: []string{"scan_test.go"},
|
2014-10-13 10:47:02 -06:00
|
|
|
},
|
|
|
|
{
|
2018-07-17 15:23:18 -06:00
|
|
|
path: "/src/fmt/print.go",
|
|
|
|
contains: []string{"// Println formats using"},
|
2014-10-13 10:47:02 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/pkg",
|
2018-07-17 15:23:18 -06:00
|
|
|
contains: []string{
|
2014-10-13 10:47:02 -06:00
|
|
|
"Standard library",
|
|
|
|
"Package fmt implements formatted I/O",
|
2019-09-23 13:43:37 -06:00
|
|
|
"Third party",
|
|
|
|
"Package a is a package in godoc.test/repo1.",
|
2014-10-13 10:47:02 -06:00
|
|
|
},
|
2018-07-17 15:23:18 -06:00
|
|
|
notContains: []string{
|
2014-10-13 10:47:02 -06:00
|
|
|
"internal/syscall",
|
|
|
|
"cmd/gc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/pkg/?m=all",
|
2018-07-17 15:23:18 -06:00
|
|
|
contains: []string{
|
2014-10-13 10:47:02 -06:00
|
|
|
"Standard library",
|
|
|
|
"Package fmt implements formatted I/O",
|
2017-01-18 11:52:36 -07:00
|
|
|
"internal/syscall/?m=all",
|
2014-10-13 10:47:02 -06:00
|
|
|
},
|
2018-07-17 15:23:18 -06:00
|
|
|
notContains: []string{
|
2014-10-13 10:47:02 -06:00
|
|
|
"cmd/gc",
|
|
|
|
},
|
|
|
|
},
|
2015-03-10 05:29:27 -06:00
|
|
|
{
|
2016-11-03 12:58:50 -06:00
|
|
|
path: "/search?q=ListenAndServe",
|
2018-07-17 15:23:18 -06:00
|
|
|
contains: []string{
|
2015-03-10 05:29:27 -06:00
|
|
|
"/src",
|
|
|
|
},
|
2018-07-17 15:23:18 -06:00
|
|
|
notContains: []string{
|
2015-03-10 05:29:27 -06:00
|
|
|
"/pkg/bootstrap",
|
|
|
|
},
|
2015-07-30 23:02:02 -06:00
|
|
|
needIndex: true,
|
2015-03-10 05:29:27 -06:00
|
|
|
},
|
2015-08-27 19:00:04 -06:00
|
|
|
{
|
|
|
|
path: "/pkg/strings/",
|
2018-07-17 15:23:18 -06:00
|
|
|
contains: []string{
|
2015-08-27 19:00:04 -06:00
|
|
|
`href="/src/strings/strings.go"`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/cmd/compile/internal/amd64/",
|
2018-07-17 15:23:18 -06:00
|
|
|
contains: []string{
|
2017-03-21 16:45:56 -06:00
|
|
|
`href="/src/cmd/compile/internal/amd64/ssa.go"`,
|
2015-08-27 19:00:04 -06:00
|
|
|
},
|
|
|
|
},
|
2017-12-22 18:30:39 -07:00
|
|
|
{
|
|
|
|
path: "/pkg/math/bits/",
|
2018-07-17 15:23:18 -06:00
|
|
|
contains: []string{
|
2017-12-22 18:30:39 -07:00
|
|
|
`Added in Go 1.9`,
|
|
|
|
},
|
|
|
|
},
|
2018-07-17 15:23:18 -06:00
|
|
|
{
|
|
|
|
path: "/pkg/net/",
|
|
|
|
contains: []string{
|
|
|
|
`// IPv6 scoped addressing zone; added in Go 1.1`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/pkg/net/http/httptrace/",
|
|
|
|
match: []string{
|
|
|
|
`Got1xxResponse.*// Go 1\.11`,
|
|
|
|
},
|
2018-07-25 01:31:31 -06:00
|
|
|
releaseTag: "go1.11",
|
2018-07-17 15:23:18 -06:00
|
|
|
},
|
|
|
|
// Verify we don't add version info to a struct field added the same time
|
|
|
|
// as the struct itself:
|
|
|
|
{
|
|
|
|
path: "/pkg/net/http/httptrace/",
|
|
|
|
match: []string{
|
|
|
|
`(?m)GotFirstResponseByte func\(\)\s*$`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Remove trailing periods before adding semicolons:
|
|
|
|
{
|
|
|
|
path: "/pkg/database/sql/",
|
|
|
|
contains: []string{
|
|
|
|
"The number of connections currently in use; added in Go 1.11",
|
|
|
|
"The number of idle connections; added in Go 1.11",
|
|
|
|
},
|
2018-07-25 01:31:31 -06:00
|
|
|
releaseTag: "go1.11",
|
2018-07-17 15:23:18 -06:00
|
|
|
},
|
2019-09-23 13:43:37 -06:00
|
|
|
|
|
|
|
// Third party packages.
|
|
|
|
{
|
|
|
|
path: "/pkg/godoc.test/repo1/a",
|
|
|
|
contains: []string{`const <span id="Name">Name</span> = "repo1a"`},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/pkg/godoc.test/repo2/b",
|
|
|
|
contains: []string{`const <span id="Name">Name</span> = "repo2b"`},
|
|
|
|
},
|
2014-03-16 14:17:13 -06:00
|
|
|
}
|
|
|
|
for _, test := range tests {
|
2015-07-30 23:02:02 -06:00
|
|
|
if test.needIndex && !withIndex {
|
|
|
|
continue
|
|
|
|
}
|
2014-03-16 14:17:13 -06:00
|
|
|
url := fmt.Sprintf("http://%s%s", addr, test.path)
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("GET %s failed: %s", url, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
2018-07-17 15:23:18 -06:00
|
|
|
strBody := string(body)
|
2014-03-16 14:17:13 -06:00
|
|
|
resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("GET %s: failed to read body: %s (response: %v)", url, err, resp)
|
|
|
|
}
|
2014-10-13 10:47:02 -06:00
|
|
|
isErr := false
|
2018-07-17 15:23:18 -06:00
|
|
|
for _, substr := range test.contains {
|
2018-07-25 01:31:31 -06:00
|
|
|
if test.releaseTag != "" && !hasTag(test.releaseTag) {
|
|
|
|
continue
|
|
|
|
}
|
2014-10-13 10:47:02 -06:00
|
|
|
if !bytes.Contains(body, []byte(substr)) {
|
|
|
|
t.Errorf("GET %s: wanted substring %q in body", url, substr)
|
|
|
|
isErr = true
|
|
|
|
}
|
|
|
|
}
|
2018-07-17 15:23:18 -06:00
|
|
|
for _, re := range test.match {
|
2018-07-25 01:31:31 -06:00
|
|
|
if test.releaseTag != "" && !hasTag(test.releaseTag) {
|
|
|
|
continue
|
|
|
|
}
|
2018-07-17 15:23:18 -06:00
|
|
|
if ok, err := regexp.MatchString(re, strBody); !ok || err != nil {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Bad regexp %q: %v", re, err)
|
|
|
|
}
|
|
|
|
t.Errorf("GET %s: wanted to match %s in body", url, re)
|
|
|
|
isErr = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, substr := range test.notContains {
|
2014-10-13 10:47:02 -06:00
|
|
|
if bytes.Contains(body, []byte(substr)) {
|
|
|
|
t.Errorf("GET %s: didn't want substring %q in body", url, substr)
|
|
|
|
isErr = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isErr {
|
|
|
|
t.Errorf("GET %s: got:\n%s", url, body)
|
2014-03-16 14:17:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-17 13:51:27 -06:00
|
|
|
|
|
|
|
// Basic integration test for godoc -analysis=type (via HTTP interface).
|
|
|
|
func TestTypeAnalysis(t *testing.T) {
|
2019-09-23 13:43:37 -06:00
|
|
|
bin, cleanup := buildGodoc(t)
|
|
|
|
defer cleanup()
|
|
|
|
testTypeAnalysis(t, packagestest.GOPATH, bin)
|
|
|
|
// TODO(golang.org/issue/34473): Add support for type, pointer
|
|
|
|
// analysis in module mode, then enable its test coverage here.
|
|
|
|
}
|
|
|
|
func testTypeAnalysis(t *testing.T, x packagestest.Exporter, bin string) {
|
2015-07-31 16:26:00 -06:00
|
|
|
if runtime.GOOS == "plan9" {
|
|
|
|
t.Skip("skipping test on plan9 (issue #11974)") // see comment re: Plan 9 below
|
|
|
|
}
|
|
|
|
|
2014-04-17 13:51:27 -06:00
|
|
|
// Write a fake GOROOT/GOPATH.
|
2019-09-23 13:43:37 -06:00
|
|
|
// TODO(golang.org/issue/34473): This test uses import paths without a dot in first
|
|
|
|
// path element. This is not viable in module mode; import paths will need to change.
|
|
|
|
e := packagestest.Export(t, x, []packagestest.Module{
|
|
|
|
{
|
|
|
|
Name: "app",
|
|
|
|
Files: map[string]interface{}{
|
|
|
|
"main.go": `
|
|
|
|
package main
|
|
|
|
import "lib"
|
|
|
|
func main() { print(lib.V) }
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "lib",
|
|
|
|
Files: map[string]interface{}{
|
|
|
|
"lib.go": `
|
2014-04-17 13:51:27 -06:00
|
|
|
package lib
|
|
|
|
type T struct{}
|
|
|
|
const C = 3
|
|
|
|
var V T
|
|
|
|
func (T) F() int { return C }
|
2019-09-23 13:43:37 -06:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
goroot := filepath.Join(e.Temp(), "goroot")
|
|
|
|
if err := os.Mkdir(goroot, 0755); err != nil {
|
|
|
|
t.Fatalf("os.Mkdir(%q) failed: %v", goroot, err)
|
2014-04-17 13:51:27 -06:00
|
|
|
}
|
2019-09-23 13:43:37 -06:00
|
|
|
defer e.Cleanup()
|
2014-04-17 13:51:27 -06:00
|
|
|
|
|
|
|
// Start the server.
|
|
|
|
addr := serverAddress(t)
|
|
|
|
cmd := exec.Command(bin, fmt.Sprintf("-http=%s", addr), "-analysis=type")
|
2019-09-23 13:43:37 -06:00
|
|
|
cmd.Dir = e.Config.Dir
|
|
|
|
// Point to an empty GOROOT directory to speed things up
|
|
|
|
// by not doing type analysis for the entire real GOROOT.
|
|
|
|
// TODO(golang.org/issue/34473): This test optimization may not be viable in module mode.
|
|
|
|
cmd.Env = append(e.Config.Env, fmt.Sprintf("GOROOT=%s", goroot))
|
2014-04-17 13:51:27 -06:00
|
|
|
cmd.Stdout = os.Stderr
|
2014-10-27 19:25:46 -06:00
|
|
|
stderr, err := cmd.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-17 13:51:27 -06:00
|
|
|
cmd.Args[0] = "godoc"
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
t.Fatalf("failed to start godoc: %s", err)
|
|
|
|
}
|
2014-10-27 23:59:58 -06:00
|
|
|
defer killAndWait(cmd)
|
2019-09-20 13:40:10 -06:00
|
|
|
waitForServerReady(t, cmd, addr)
|
2014-04-17 13:51:27 -06:00
|
|
|
|
2014-10-27 19:25:46 -06:00
|
|
|
// Wait for type analysis to complete.
|
|
|
|
reader := bufio.NewReader(stderr)
|
|
|
|
for {
|
2015-07-31 16:26:00 -06:00
|
|
|
s, err := reader.ReadString('\n') // on Plan 9 this fails
|
2014-10-27 19:25:46 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Fprint(os.Stderr, s)
|
|
|
|
if strings.Contains(s, "Type analysis complete.") {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go io.Copy(os.Stderr, reader)
|
|
|
|
|
2014-04-21 15:56:06 -06:00
|
|
|
t0 := time.Now()
|
|
|
|
|
2014-04-17 13:51:27 -06:00
|
|
|
// Make an HTTP request and check for a regular expression match.
|
|
|
|
// The patterns are very crude checks that basic type information
|
|
|
|
// has been annotated onto the source view.
|
2014-04-21 15:56:06 -06:00
|
|
|
tryagain:
|
2014-04-17 13:51:27 -06:00
|
|
|
for _, test := range []struct{ url, pattern string }{
|
2014-09-10 07:02:54 -06:00
|
|
|
{"/src/lib/lib.go", "L2.*package .*Package docs for lib.*/lib"},
|
|
|
|
{"/src/lib/lib.go", "L3.*type .*type info for T.*struct"},
|
|
|
|
{"/src/lib/lib.go", "L5.*var V .*type T struct"},
|
|
|
|
{"/src/lib/lib.go", "L6.*func .*type T struct.*T.*return .*const C untyped int.*C"},
|
2014-04-17 13:51:27 -06:00
|
|
|
|
2014-09-10 07:02:54 -06:00
|
|
|
{"/src/app/main.go", "L2.*package .*Package docs for app"},
|
|
|
|
{"/src/app/main.go", "L3.*import .*Package docs for lib.*lib"},
|
|
|
|
{"/src/app/main.go", "L4.*func main.*package lib.*lib.*var lib.V lib.T.*V"},
|
2014-04-17 13:51:27 -06:00
|
|
|
} {
|
|
|
|
url := fmt.Sprintf("http://%s%s", addr, test.url)
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("GET %s failed: %s", url, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("GET %s: failed to read body: %s (response: %v)", url, err, resp)
|
|
|
|
continue
|
|
|
|
}
|
2014-04-21 15:56:06 -06:00
|
|
|
|
|
|
|
if !bytes.Contains(body, []byte("Static analysis features")) {
|
|
|
|
// Type analysis results usually become available within
|
|
|
|
// ~4ms after godoc startup (for this input on my machine).
|
|
|
|
if elapsed := time.Since(t0); elapsed > 500*time.Millisecond {
|
|
|
|
t.Fatalf("type analysis results still unavailable after %s", elapsed)
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
goto tryagain
|
|
|
|
}
|
|
|
|
|
2014-04-17 13:51:27 -06:00
|
|
|
match, err := regexp.Match(test.pattern, body)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("regexp.Match(%q) failed: %s", test.pattern, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !match {
|
|
|
|
// This is a really ugly failure message.
|
|
|
|
t.Errorf("GET %s: body doesn't match %q, got:\n%s",
|
|
|
|
url, test.pattern, string(body))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|