Skip to content
Myr

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

2 files+72 added−7 removed

modified:doc/man_docs_test.go+65 added
1package doc23import (4 "bufio"5 "bytes"6 "fmt"7 "os"8 "strings"9 "testing"1011 "github.com/spf13/cobra"12)1314var _ = fmt.Println15var _ = os.Stderr
81 unmodified lines
9798 unexpected := translate("#HISTORY")99 checkStringOmits(t, found, unexpected)100}101102func TestGenManSeeAlso(t *testing.T) {103 noop := func(cmd *cobra.Command, args []string) {}104105 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)110111 out := new(bytes.Buffer)112 header := &GenManHeader{}113 if err := GenMan(top, header, out); err != nil {114 t.Fatal(err)115 }116117 scanner := bufio.NewScanner(out)118119 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 }122123 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 }126127 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}131132func AssertLineFound(scanner *bufio.Scanner, expectedLine string) error {133 for scanner.Scan() {134 line := scanner.Text()135 if line == expectedLine {136 return nil137 }138 }139140 if err := scanner.Err(); err != nil {141 return fmt.Errorf("AssertLineFound: scan failed: %s", err.Error())142 }143144 return fmt.Errorf("AssertLineFound: hit EOF before finding %#v", expectedLine)145}146147func 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 }156157 if err := scanner.Err(); err != nil {158 return fmt.Errorf("AssertNextLineEquals: scan failed: %s", err.Error())159 }160161 return fmt.Errorf("AssertNextLineEquals: hit EOF before finding %#v", expectedLine)162}
modified:doc/man_docs.go+7 added-7 removed
186 unmodified lines
187 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