From 1a83a0b54880dd62aaf82968d7cdd83007ecc60e Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Sun, 15 Apr 2018 03:03:16 +0530 Subject: [PATCH] godoc: fix counting of package files in GetPageInfo CL 107155 introduced this bug due to oversight. There can be a scenario where a folder has no source .go or .c files, but has an ignored .go file. In that case, both the if conditions to check the length of pkgfiles slice will evaluate to true. Added a test case for it to prevent this happening in future. Change-Id: I7181699bbf7580888a6f7923c5aeb842356941a8 Reviewed-on: https://go-review.googlesource.com/107195 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- godoc/server.go | 5 ++++- godoc/server_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 godoc/server_test.go diff --git a/godoc/server.go b/godoc/server.go index 3e271bdd..9240746d 100644 --- a/godoc/server.go +++ b/godoc/server.go @@ -113,7 +113,10 @@ func (h *handlerServer) GetPageInfo(abspath, relpath string, mode PageInfoMode, // documentation (historic). pkgname = "main" // assume package main since pkginfo.Name == "" pkgfiles = pkginfo.IgnoredGoFiles - } else { + } + + // get package information, if any + if len(pkgfiles) > 0 { // build package AST fset := token.NewFileSet() files, err := h.c.parseFiles(fset, relpath, abspath, pkgfiles) diff --git a/godoc/server_test.go b/godoc/server_test.go new file mode 100644 index 00000000..656e5f58 --- /dev/null +++ b/godoc/server_test.go @@ -0,0 +1,48 @@ +// Copyright 2018 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. + +package godoc + +import ( + "testing" + + "golang.org/x/tools/godoc/vfs/mapfs" +) + +// TestIgnoredGoFiles tests the scenario where a folder has no .go or .c files, +// but has an ignored go file. +func TestIgnoredGoFiles(t *testing.T) { + packagePath := "github.com/package" + packageComment := "main is documented in an ignored .go file" + + c := NewCorpus(mapfs.New(map[string]string{ + "src/" + packagePath + "/ignored.go": `// +build ignore + +// ` + packageComment + ` +package main`})) + srv := &handlerServer{ + p: &Presentation{ + Corpus: c, + }, + c: c, + } + pInfo := srv.GetPageInfo("/src/"+packagePath, packagePath, NoFiltering, "linux", "amd64") + + if pInfo.PDoc == nil { + t.Error("pInfo.PDoc = nil; want non-nil.") + } else { + if got, want := pInfo.PDoc.Doc, packageComment+"\n"; got != want { + t.Errorf("pInfo.PDoc.Doc = %q; want %q.", got, want) + } + if got, want := pInfo.PDoc.Name, "main"; got != want { + t.Errorf("pInfo.PDoc.Name = %q; want %q.", got, want) + } + if got, want := pInfo.PDoc.ImportPath, packagePath; got != want { + t.Errorf("pInfo.PDoc.ImportPath = %q; want %q.", got, want) + } + } + if pInfo.FSet == nil { + t.Error("pInfo.FSet = nil; want non-nil.") + } +}