cmd/godoc: fix TestWebIndex test

The godoc in the test was indexing sources in the default GOPATH.
If the default GOPATH pointed to local workspace, test would timeout.
The fix is to supply GOPATH set to non-existing path.

Fixes golang/go#24504

Change-Id: Iedf044cdec78d5c5642105650ad8ec17aa10a5ba
Reviewed-on: https://go-review.googlesource.com/102295
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
diff --git a/cmd/godoc/godoc_test.go b/cmd/godoc/godoc_test.go
index a2552d9..82ed1e8 100644
--- a/cmd/godoc/godoc_test.go
+++ b/cmd/godoc/godoc_test.go
@@ -236,7 +236,12 @@
 	cmd.Stdout = os.Stderr
 	cmd.Stderr = os.Stderr
 	cmd.Args[0] = "godoc"
-	cmd.Env = godocEnv()
+
+	// Set GOPATH variable to non-existing path.
+	// 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(), "GOPATH=does_not_exist")
+
 	if err := cmd.Start(); err != nil {
 		t.Fatalf("failed to start godoc: %s", err)
 	}
@@ -390,14 +395,9 @@
 	defer cleanup()
 	addr := serverAddress(t)
 	cmd := exec.Command(bin, fmt.Sprintf("-http=%s", addr), "-analysis=type")
+	cmd.Env = os.Environ()
 	cmd.Env = append(cmd.Env, fmt.Sprintf("GOROOT=%s", filepath.Join(tmpdir, "goroot")))
 	cmd.Env = append(cmd.Env, fmt.Sprintf("GOPATH=%s", filepath.Join(tmpdir, "gopath")))
-	for _, e := range os.Environ() {
-		if strings.HasPrefix(e, "GOROOT=") || strings.HasPrefix(e, "GOPATH=") {
-			continue
-		}
-		cmd.Env = append(cmd.Env, e)
-	}
 	cmd.Stdout = os.Stderr
 	stderr, err := cmd.StderrPipe()
 	if err != nil {
@@ -475,15 +475,3 @@
 		}
 	}
 }
-
-// godocEnv returns the process environment without the GOPATH variable.
-// (We don't want the indexer looking at the local workspace during tests.)
-func godocEnv() (env []string) {
-	for _, v := range os.Environ() {
-		if strings.HasPrefix(v, "GOPATH=") {
-			continue
-		}
-		env = append(env, v)
-	}
-	return
-}