Merge pull request #230 from garthk/fix-man-see-also
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Ensure SEE ALSO list has no leading comma.
authored by Eric Paris · committed by Eric Paris · parents 57021c6, 57f4732 · Browse files · 2 files·+72 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+72 added−7 removed
- other:+72 added−7 removed
Provenance
- Commit
966e604- Tree
9f1cba0- Author
Eric Paris <eparis@redhat.com>- Committer
Eric Paris <eparis@redhat.com>- Parent 1
57021c6- Parent 2
57f4732
2 files changed+72 added−7 removed
modified:doc/man_docs_test.go+65 added
1 package doc2 3 import (4+ "bufio"5 "bytes"6 "fmt"7 "os"8 "strings"9 "testing"10+11+ "github.com/spf13/cobra"12 )13 14 var _ = fmt.Println15 var _ = os.Stderr81 unmodified lines97 98 unexpected := translate("#HISTORY")99 checkStringOmits(t, found, unexpected)100 }101+102+func TestGenManSeeAlso(t *testing.T) {103+ noop := func(cmd *cobra.Command, args []string) {}104+105+ top := &cobra.Command{Use: "top", Run: noop}106+ aaa := &cobra.Command{Use: "aaa", Run: noop, Hidden: true} // #229107+ bbb := &cobra.Command{Use: "bbb", Run: noop}108+ ccc := &cobra.Command{Use: "ccc", Run: noop}109+ top.AddCommand(aaa, bbb, ccc)110+111+ out := new(bytes.Buffer)112+ header := &GenManHeader{}113+ if err := GenMan(top, header, out); err != nil {114+ t.Fatal(err)115+ }116+117+ scanner := bufio.NewScanner(out)118+119+ if err := AssertLineFound(scanner, ".SH SEE ALSO"); err != nil {120+ t.Fatal(fmt.Errorf("Couldn't find SEE ALSO section header: %s", err.Error()))121+ }122+123+ if err := AssertNextLineEquals(scanner, ".PP"); err != nil {124+ t.Fatal(fmt.Errorf("First line after SEE ALSO wasn't break-indent: %s", err.Error()))125+ }126+127+ if err := AssertNextLineEquals(scanner, `\fBtop\-bbb(1)\fP, \fBtop\-ccc(1)\fP`); err != nil {128+ t.Fatal(fmt.Errorf("Second line after SEE ALSO wasn't correct: %s", err.Error()))129+ }130+}131+132+func AssertLineFound(scanner *bufio.Scanner, expectedLine string) error {133+ for scanner.Scan() {134+ line := scanner.Text()135+ if line == expectedLine {136+ return nil137+ }138+ }139+140+ if err := scanner.Err(); err != nil {141+ return fmt.Errorf("AssertLineFound: scan failed: %s", err.Error())142+ }143+144+ return fmt.Errorf("AssertLineFound: hit EOF before finding %#v", expectedLine)145+}146+147+func AssertNextLineEquals(scanner *bufio.Scanner, expectedLine string) error {148+ if scanner.Scan() {149+ line := scanner.Text()150+ if line == expectedLine {151+ return nil152+ } else {153+ return fmt.Errorf("AssertNextLineEquals: got %#v, not %#v", line, expectedLine)154+ }155+ }156+157+ if err := scanner.Err(); err != nil {158+ return fmt.Errorf("AssertNextLineEquals: scan failed: %s", err.Error())159+ }160+161+ return fmt.Errorf("AssertNextLineEquals: hit EOF before finding %#v", expectedLine)162+}modified:doc/man_docs.go+7 added-7 removed
186 unmodified lines187 fmt.Fprintf(buf, "```\n%s\n```\n", cmd.Example)188 }189 if hasSeeAlso(cmd) {190 fmt.Fprintf(buf, "# SEE ALSO\n")191+ seealsos := make([]string, 0)192 if cmd.HasParent() {193 parentPath := cmd.Parent().CommandPath()194 dashParentPath := strings.Replace(parentPath, " ", "-", -1)194- fmt.Fprintf(buf, "**%s(%s)**", dashParentPath, header.Section)195+ seealso := fmt.Sprintf("**%s(%s)**", dashParentPath, header.Section)196+ seealsos = append(seealsos, seealso)197 cmd.VisitParents(func(c *cobra.Command) {198 if c.DisableAutoGenTag {199 cmd.DisableAutoGenTag = c.DisableAutoGenTag200 }201 })202 }203 children := cmd.Commands()204 sort.Sort(byName(children))203- for i, c := range children {205+ for _, c := range children {206 if !c.IsAvailableCommand() || c.IsHelpCommand() {207 continue208 }207- if cmd.HasParent() || i > 0 {208- fmt.Fprintf(buf, ", ")209- }210- fmt.Fprintf(buf, "**%s-%s(%s)**", dashCommandName, c.Name(), header.Section)209+ seealso := fmt.Sprintf("**%s-%s(%s)**", dashCommandName, c.Name(), header.Section)210+ seealsos = append(seealsos, seealso)211 }212- fmt.Fprintf(buf, "\n")212+ fmt.Fprintf(buf, "%s\n", strings.Join(seealsos, ", "))213 }214 if !cmd.DisableAutoGenTag {215 fmt.Fprintf(buf, "# HISTORY\n%s Auto generated by spf13/cobra\n", header.Date.Format("2-Jan-2006"))216 }2 unmodified lines