cmd/godoc: fix expected test output for Go 1.8 and older

A go/build change in Go 1.9 (CL 33158) allowed for a better expected
error message for "nonexistingpkg" case. CL 37768 did that. However,
that shouldn't be done for Go 1.8 and older, since they don't have
the corresponding go/build change.

So, if the version is Go 1.8 or older, allow matching those previous
expected outputs (before CL 37768) for "nonexistingpkg" case.

Move test cases for TestCLI from a package-level variable into TestCLI
itself. This is more readable, and isGo19's value is ready in time.

Fixes golang/go#20122.

Change-Id: I5e80600a75176d9e54ffebb7730849e381568316
Reviewed-on: https://go-review.googlesource.com/41810
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/godoc19_test.go b/cmd/godoc/godoc19_test.go
new file mode 100644
index 0000000..655a623
--- /dev/null
+++ b/cmd/godoc/godoc19_test.go
@@ -0,0 +1,9 @@
+// Copyright 2017 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.
+
+// +build go1.9
+
+package main_test
+
+func init() { isGo19 = true }
diff --git a/cmd/godoc/godoc_test.go b/cmd/godoc/godoc_test.go
index 3adaf37..8b0c24e 100644
--- a/cmd/godoc/godoc_test.go
+++ b/cmd/godoc/godoc_test.go
@@ -22,48 +22,6 @@
 	"time"
 )
 
-var godocTests = []struct {
-	args      []string
-	matches   []string // regular expressions
-	dontmatch []string // regular expressions
-}{
-	{
-		args: []string{"fmt"},
-		matches: []string{
-			`import "fmt"`,
-			`Package fmt implements formatted I/O`,
-		},
-	},
-	{
-		args: []string{"io", "WriteString"},
-		matches: []string{
-			`func WriteString\(`,
-			`WriteString writes the contents of the string s to w`,
-		},
-	},
-	{
-		args: []string{"nonexistingpkg"},
-		matches: []string{
-			`cannot find package`,
-		},
-	},
-	{
-		args: []string{"fmt", "NonexistentSymbol"},
-		matches: []string{
-			`No match found\.`,
-		},
-	},
-	{
-		args: []string{"-src", "syscall", "Open"},
-		matches: []string{
-			`func Open\(`,
-		},
-		dontmatch: []string{
-			`No match found\.`,
-		},
-	},
-}
-
 // buildGodoc builds the godoc executable.
 // It returns its path, and a cleanup function.
 //
@@ -95,11 +53,69 @@
 	return bin, func() { os.RemoveAll(tmp) }
 }
 
+var isGo19 bool // godoc19_test.go sets it to true.
+
 // Basic regression test for godoc command-line tool.
 func TestCLI(t *testing.T) {
 	bin, cleanup := buildGodoc(t)
 	defer cleanup()
-	for _, test := range godocTests {
+
+	// condStr returns s if cond is true, otherwise empty string.
+	condStr := func(cond bool, s string) string {
+		if !cond {
+			return ""
+		}
+		return s
+	}
+
+	tests := []struct {
+		args      []string
+		matches   []string // regular expressions
+		dontmatch []string // regular expressions
+	}{
+		{
+			args: []string{"fmt"},
+			matches: []string{
+				`import "fmt"`,
+				`Package fmt implements formatted I/O`,
+			},
+		},
+		{
+			args: []string{"io", "WriteString"},
+			matches: []string{
+				`func WriteString\(`,
+				`WriteString writes the contents of the string s to w`,
+			},
+		},
+		{
+			args: []string{"nonexistingpkg"},
+			matches: []string{
+				`cannot find package` +
+					// TODO: Remove this when support for Go 1.8 is dropped.
+					condStr(!isGo19,
+						// For Go 1.8 and older, because it doesn't have CL 33158 change applied to go/build.
+						// The last pattern (does not e) is for plan9:
+						// http://build.golang.org/log/2d8e5e14ed365bfa434b37ec0338cd9e6f8dd9bf
+						`|no such file or directory|does not exist|cannot find the file|(?:' does not e)`),
+			},
+		},
+		{
+			args: []string{"fmt", "NonexistentSymbol"},
+			matches: []string{
+				`No match found\.`,
+			},
+		},
+		{
+			args: []string{"-src", "syscall", "Open"},
+			matches: []string{
+				`func Open\(`,
+			},
+			dontmatch: []string{
+				`No match found\.`,
+			},
+		},
+	}
+	for _, test := range tests {
 		cmd := exec.Command(bin, test.args...)
 		cmd.Args[0] = "godoc"
 		out, err := cmd.CombinedOutput()