}
}
+// Test that we don't print spurious package clauses
+// when there should be no output at all. Issue 37969.
+func TestNoPackageClauseWhenNoMatch(t *testing.T) {
+ maybeSkip(t)
+ var b bytes.Buffer
+ var flagSet flag.FlagSet
+ err := do(&b, &flagSet, []string{"template.ZZZ"})
+ // Expect an error.
+ if err == nil {
+ t.Error("expect an error for template.zzz")
+ }
+ // And the output should not contain any package clauses.
+ const dontWant = `package template // import `
+ output := b.String()
+ if strings.Contains(output, dontWant) {
+ t.Fatalf("improper package clause printed:\n%s", output)
+ }
+}
+
type trimTest struct {
path string
prefix string
}
func (pkg *Package) flush() {
- // Print the package clause in case it wasn't written already.
- pkg.buf.packageClause()
_, err := pkg.writer.Write(pkg.buf.Bytes())
if err != nil {
log.Fatal(err)
// allDoc prints all the docs for the package.
func (pkg *Package) allDoc() {
- defer pkg.flush()
-
+ pkg.Printf("") // Trigger the package clause; we know the package exists.
doc.ToText(&pkg.buf, pkg.doc.Doc, "", indent, indentedWidth)
pkg.newlines(1)
// packageDoc prints the docs for the package (package doc plus one-liners of the rest).
func (pkg *Package) packageDoc() {
- defer pkg.flush()
-
+ pkg.Printf("") // Trigger the package clause; we know the package exists.
if !short {
doc.ToText(&pkg.buf, pkg.doc.Doc, "", indent, indentedWidth)
pkg.newlines(1)
// If symbol matches a type, output includes its methods factories and associated constants.
// If there is no top-level symbol, symbolDoc looks for methods that match.
func (pkg *Package) symbolDoc(symbol string) bool {
- defer pkg.flush()
found := false
// Functions.
for _, fun := range pkg.findFuncs(symbol) {
// If symbol is empty, it prints all methods for any concrete type
// that match the name. It reports whether it found any methods.
func (pkg *Package) printMethodDoc(symbol, method string) bool {
- defer pkg.flush()
types := pkg.findTypes(symbol)
if types == nil {
if symbol == "" {
if symbol == "" || fieldName == "" {
return false
}
- defer pkg.flush()
types := pkg.findTypes(symbol)
if types == nil {
pkg.Fatalf("symbol %s is not a type in package %s installed in %q", symbol, pkg.name, pkg.build.ImportPath)
// methodDoc prints the docs for matches of symbol.method.
func (pkg *Package) methodDoc(symbol, method string) bool {
- defer pkg.flush()
return pkg.printMethodDoc(symbol, method)
}
// fieldDoc prints the docs for matches of symbol.field.
func (pkg *Package) fieldDoc(symbol, field string) bool {
- defer pkg.flush()
return pkg.printFieldDoc(symbol, field)
}