13 "github.com/spf13/cobra"
16 func translate(in string) string {
17 return strings.Replace(in, "-", "\\-", -1)
20 func TestGenManDoc(t *testing.T) {
21 c := initializeWithRootCmd()
22 // Need two commands to run the command alphabetical sort
23 cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)
24 c.AddCommand(cmdPrint, cmdEcho)
25 cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)
27 out := new(bytes.Buffer)
29 header := &GenManHeader{
33 // We generate on a subcommand so we have both subcommands and parents
34 if err := GenMan(cmdEcho, header, out); err != nil {
39 // Make sure parent has - in CommandPath() in SEE ALSO:
40 parentPath := cmdEcho.Parent().CommandPath()
41 dashParentPath := strings.Replace(parentPath, " ", "-", -1)
42 expected := translate(dashParentPath)
43 expected = expected + "(" + header.Section + ")"
44 checkStringContains(t, found, expected)
47 expected = translate(cmdEcho.Name())
48 checkStringContains(t, found, expected)
50 // Better have our example
51 expected = translate(cmdEcho.Name())
52 checkStringContains(t, found, expected)
56 checkStringContains(t, found, expected)
58 // persistent flag on parent
60 checkStringContains(t, found, expected)
62 // We better output info about our parent
63 expected = translate(cmdRootWithRun.Name())
64 checkStringContains(t, found, expected)
66 // And about subcommands
67 expected = translate(cmdEchoSub.Name())
68 checkStringContains(t, found, expected)
70 unexpected := translate(cmdDeprecated.Name())
71 checkStringOmits(t, found, unexpected)
74 expected = translate("Auto generated")
75 checkStringContains(t, found, expected)
78 func TestGenManNoGenTag(t *testing.T) {
79 c := initializeWithRootCmd()
80 // Need two commands to run the command alphabetical sort
81 cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)
82 c.AddCommand(cmdPrint, cmdEcho)
83 cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)
84 cmdEcho.DisableAutoGenTag = true
85 out := new(bytes.Buffer)
87 header := &GenManHeader{
91 // We generate on a subcommand so we have both subcommands and parents
92 if err := GenMan(cmdEcho, header, out); err != nil {
97 unexpected := translate("#HISTORY")
98 checkStringOmits(t, found, unexpected)
101 func TestGenManSeeAlso(t *testing.T) {
102 noop := func(cmd *cobra.Command, args []string) {}
104 top := &cobra.Command{Use: "top", Run: noop}
105 aaa := &cobra.Command{Use: "aaa", Run: noop, Hidden: true} // #229
106 bbb := &cobra.Command{Use: "bbb", Run: noop}
107 ccc := &cobra.Command{Use: "ccc", Run: noop}
108 top.AddCommand(aaa, bbb, ccc)
110 out := new(bytes.Buffer)
111 header := &GenManHeader{}
112 if err := GenMan(top, header, out); err != nil {
116 scanner := bufio.NewScanner(out)
118 if err := AssertLineFound(scanner, ".SH SEE ALSO"); err != nil {
119 t.Fatal(fmt.Errorf("Couldn't find SEE ALSO section header: %s", err.Error()))
122 if err := AssertNextLineEquals(scanner, ".PP"); err != nil {
123 t.Fatal(fmt.Errorf("First line after SEE ALSO wasn't break-indent: %s", err.Error()))
126 if err := AssertNextLineEquals(scanner, `\fBtop\-bbb(1)\fP, \fBtop\-ccc(1)\fP`); err != nil {
127 t.Fatal(fmt.Errorf("Second line after SEE ALSO wasn't correct: %s", err.Error()))
131 func TestManPrintFlagsHidesShortDeperecated(t *testing.T) {
132 cmd := &cobra.Command{}
134 flags.StringP("foo", "f", "default", "Foo flag")
135 flags.MarkShorthandDeprecated("foo", "don't use it no more")
137 out := new(bytes.Buffer)
138 manPrintFlags(out, flags)
140 expected := "**--foo**=\"default\"\n\tFoo flag\n\n"
141 if out.String() != expected {
142 t.Fatalf("Expected %s, but got %s", expected, out.String())
146 func TestGenManTree(t *testing.T) {
147 cmd := &cobra.Command{
148 Use: "do [OPTIONS] arg1 arg2",
150 header := &GenManHeader{Section: "2"}
151 tmpdir, err := ioutil.TempDir("", "test-gen-man-tree")
153 t.Fatalf("Failed to create tmpdir: %s", err.Error())
155 defer os.RemoveAll(tmpdir)
157 if err := GenManTree(cmd, header, tmpdir); err != nil {
158 t.Fatalf("GenManTree failed: %s", err.Error())
161 if _, err := os.Stat(filepath.Join(tmpdir, "do.2")); err != nil {
162 t.Fatalf("Expected file 'do.2' to exist")
165 if header.Title != "" {
166 t.Fatalf("Expected header.Title to be unmodified")
170 func AssertLineFound(scanner *bufio.Scanner, expectedLine string) error {
172 line := scanner.Text()
173 if line == expectedLine {
178 if err := scanner.Err(); err != nil {
179 return fmt.Errorf("AssertLineFound: scan failed: %s", err.Error())
182 return fmt.Errorf("AssertLineFound: hit EOF before finding %#v", expectedLine)
185 func AssertNextLineEquals(scanner *bufio.Scanner, expectedLine string) error {
187 line := scanner.Text()
188 if line == expectedLine {
191 return fmt.Errorf("AssertNextLineEquals: got %#v, not %#v", line, expectedLine)
194 if err := scanner.Err(); err != nil {
195 return fmt.Errorf("AssertNextLineEquals: scan failed: %s", err.Error())
198 return fmt.Errorf("AssertNextLineEquals: hit EOF before finding %#v", expectedLine)
201 func BenchmarkGenManToFile(b *testing.B) {
202 c := initializeWithRootCmd()
203 file, err := ioutil.TempFile("", "")
207 defer os.Remove(file.Name())
211 for i := 0; i < b.N; i++ {
212 if err := GenMan(c, nil, file); err != nil {