From 4d8a0ac9f66cf9815826183631c4da2e48d982c6 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Wed, 25 Jul 2018 13:01:31 +0530 Subject: [PATCH] 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 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- cmd/godoc/godoc_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/cmd/godoc/godoc_test.go b/cmd/godoc/godoc_test.go index 681818ec..b967e283 100644 --- a/cmd/godoc/godoc_test.go +++ b/cmd/godoc/godoc_test.go @@ -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)