cmd/godoc: fix TestWeb for versions < 1.11

The test was looking for strings found in specific Go versions
without checking for the actual Go version running the test.

Used ReleaseTags to check whether the current go version should
execute a test or not.

P.S. The version info is inferred from the binary running the test.
But the test builds godoc using the "go" binary in $PATH. In case
one is testing different go versions, please ensure to run tests
by changing the $PATH variable to point to different go versions,
rather than using a custom go binary in a different path.

Fixes golang/go#26531

Change-Id: I16dda81518021e865e79c9c29fc2d9e8a83e7057
Reviewed-on: https://go-review.googlesource.com/125755
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Agniva De Sarker 2018-07-25 13:01:31 +05:30 коммит произвёл Brad Fitzpatrick
Родитель 526516e9c4
Коммит 4d8a0ac9f6
1 изменённых файлов: 21 добавлений и 0 удалений

Просмотреть файл

@ -8,6 +8,7 @@ import (
"bufio"
"bytes"
"fmt"
"go/build"
"io"
"io/ioutil"
"net"
@ -202,6 +203,17 @@ func waitForServer(t *testing.T, url, match string, timeout time.Duration, rever
t.Fatalf("Server failed to respond in %v", timeout)
}
// 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
}
func killAndWait(cmd *exec.Cmd) {
cmd.Process.Kill()
cmd.Wait()
@ -260,6 +272,7 @@ func testWeb(t *testing.T, withIndex bool) {
match []string // regexp
notContains []string
needIndex bool
releaseTag string // optional release tag that must be in go/build.ReleaseTags
}{
{
path: "/",
@ -338,6 +351,7 @@ func testWeb(t *testing.T, withIndex bool) {
match: []string{
`Got1xxResponse.*// Go 1\.11`,
},
releaseTag: "go1.11",
},
// Verify we don't add version info to a struct field added the same time
// as the struct itself:
@ -354,6 +368,7 @@ func testWeb(t *testing.T, withIndex bool) {
"The number of connections currently in use; added in Go 1.11",
"The number of idle connections; added in Go 1.11",
},
releaseTag: "go1.11",
},
}
for _, test := range tests {
@ -374,12 +389,18 @@ func testWeb(t *testing.T, withIndex bool) {
}
isErr := false
for _, substr := range test.contains {
if test.releaseTag != "" && !hasTag(test.releaseTag) {
continue
}
if !bytes.Contains(body, []byte(substr)) {
t.Errorf("GET %s: wanted substring %q in body", url, substr)
isErr = true
}
}
for _, re := range test.match {
if test.releaseTag != "" && !hasTag(test.releaseTag) {
continue
}
if ok, err := regexp.MatchString(re, strBody); !ok || err != nil {
if err != nil {
t.Fatalf("Bad regexp %q: %v", re, err)