]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/doc: skip directories like other go tools
authorDaniel Martí <mvdan@mvdan.cc>
Fri, 13 Apr 2018 18:39:32 +0000 (19:39 +0100)
committerDaniel Martí <mvdan@mvdan.cc>
Sat, 14 Apr 2018 21:59:08 +0000 (21:59 +0000)
It was skipping dirs starting with ".", but it was missing the "_"
prefix and the "testdata" name. From "go help packages":

Directory and file names that begin with "." or "_" are ignored
by the go tool, as are directories named "testdata".

Before the change:

$ go doc z # using src/cmd/go/testdata/testvendor/src/q/z
package z // import "."

After the fix, it falls back to the current directory, as expected when
a single argument isn't found as a package in $GOPATH.

TestMain needs a small adjustment to keep the tests working, as now
their use of cmd/doc/testdata would normally not work.

Fixes #24462.

Change-Id: I1f5d6d1eba0fb59aff55db33b3b1147e300284ef
Reviewed-on: https://go-review.googlesource.com/106935
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
src/cmd/doc/dirs.go
src/cmd/doc/doc_test.go

index 5088f880e14222130feb7b3ec302b6c946405b2d..9f7920ecd9641ec91f64f267cee6b55114ae9368 100644 (file)
@@ -97,8 +97,9 @@ func (d *Dirs) bfsWalkRoot(root string) {
                                        continue
                                }
                                // Entry is a directory.
-                               // No .git or other dot nonsense please.
-                               if strings.HasPrefix(name, ".") {
+
+                               // The go tool ignores directories starting with ., _, or named "testdata".
+                               if name[0] == '.' || name[0] == '_' || name == "testdata" {
                                        continue
                                }
                                // Remember this (fully qualified) directory for the next pass.
index e68fb017b96dbacaeac6ffbb1eb022fd2729c6d9..f919857067b7c87adce9ae2b77056d3da41fd9de 100644 (file)
@@ -16,10 +16,20 @@ import (
 )
 
 func TestMain(m *testing.M) {
-       // otherwise the tests are brittle, as they may give unexpected
-       // output or errors when a suffix match with GOPATH takes place
+       // Clear GOPATH so we don't access the user's own packages in the test.
        buildCtx.GOPATH = ""
+
        dirsInit()
+
+       // Add $GOROOT/src/cmd/doc/testdata explicitly so we can access its contents in the test.
+       // Normally testdata directories are ignored, but sending it to dirs.scan directly is
+       // a hack that works around the check.
+       testdataDir, err := filepath.Abs("testdata")
+       if err != nil {
+               panic(err)
+       }
+       go func() { dirs.scan <- testdataDir }()
+
        os.Exit(m.Run())
 }