godoc: skip build tag annotations when displaying examples
After moving the filepath.Walk example to a standalone example file
in CL 122237 (so it could use a standalone function), godoc includes
the build tag annotation ("// +build !windows,!plan9" in this case)
in the runnable example. The example runs correctly, but the
annotation might be confusing for new users.
Change the behavior so that godoc skips these annotations when
displaying examples.
To avoid false positives in older versions of "go vet", which are still used
on the build dashboard, we avoid using a multiline string in the test.
Fixes golang/go#26490.
Change-Id: I1da4b3b7e1e5a85a76773e25d9355b3f92479c19
GitHub-Last-Rev: bc5ed29bd368e5bcf11fed7a0c7f14b872fef065
GitHub-Pull-Request: golang/tools#42
Reviewed-on: https://go-review.googlesource.com/126256
Run-TryBot: Daniel Martà <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Daniel Martà <mvdan@mvdan.cc>
Reviewed-by: Andrew Bonventre <andybons@golang.org>
diff --git a/godoc/godoc_test.go b/godoc/godoc_test.go
index c1d631c..33dbe3f 100644
--- a/godoc/godoc_test.go
+++ b/godoc/godoc_test.go
@@ -321,3 +321,50 @@
}
}
}
+
+func TestFilterOutBuildAnnotations(t *testing.T) {
+ // TODO: simplify this by using a multiline string once we stop
+ // using go vet from 1.10 on the build dashboard.
+ // https://golang.org/issue/26627
+ src := []byte("// +build !foo\n" +
+ "// +build !anothertag\n" +
+ "\n" +
+ "// non-tag comment\n" +
+ "\n" +
+ "package foo\n" +
+ "\n" +
+ "func bar() int {\n" +
+ " return 42\n" +
+ "}\n")
+
+ fset := token.NewFileSet()
+ af, err := parser.ParseFile(fset, "foo.go", src, parser.ParseComments)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ var found bool
+ for _, cg := range af.Comments {
+ if strings.HasPrefix(cg.Text(), "+build ") {
+ found = true
+ break
+ }
+ }
+ if !found {
+ t.Errorf("TestFilterOutBuildAnnotations is broken: missing build tag in test input")
+ }
+
+ found = false
+ for _, cg := range filterOutBuildAnnotations(af.Comments) {
+ if strings.HasPrefix(cg.Text(), "+build ") {
+ t.Errorf("filterOutBuildAnnotations failed to filter build tag")
+ }
+
+ if strings.Contains(cg.Text(), "non-tag comment") {
+ found = true
+ }
+ }
+ if !found {
+ t.Errorf("filterOutBuildAnnotations should not remove non-build tag comment")
+ }
+}