OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / spf13 / cobra / doc / man_docs_test.go
1 package doc
2
3 import (
4         "bufio"
5         "bytes"
6         "fmt"
7         "io/ioutil"
8         "os"
9         "path/filepath"
10         "strings"
11         "testing"
12
13         "github.com/spf13/cobra"
14 )
15
16 func translate(in string) string {
17         return strings.Replace(in, "-", "\\-", -1)
18 }
19
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)
26
27         out := new(bytes.Buffer)
28
29         header := &GenManHeader{
30                 Title:   "Project",
31                 Section: "2",
32         }
33         // We generate on a subcommand so we have both subcommands and parents
34         if err := GenMan(cmdEcho, header, out); err != nil {
35                 t.Fatal(err)
36         }
37         found := out.String()
38
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)
45
46         // Our description
47         expected = translate(cmdEcho.Name())
48         checkStringContains(t, found, expected)
49
50         // Better have our example
51         expected = translate(cmdEcho.Name())
52         checkStringContains(t, found, expected)
53
54         // A local flag
55         expected = "boolone"
56         checkStringContains(t, found, expected)
57
58         // persistent flag on parent
59         expected = "rootflag"
60         checkStringContains(t, found, expected)
61
62         // We better output info about our parent
63         expected = translate(cmdRootWithRun.Name())
64         checkStringContains(t, found, expected)
65
66         // And about subcommands
67         expected = translate(cmdEchoSub.Name())
68         checkStringContains(t, found, expected)
69
70         unexpected := translate(cmdDeprecated.Name())
71         checkStringOmits(t, found, unexpected)
72
73         // auto generated
74         expected = translate("Auto generated")
75         checkStringContains(t, found, expected)
76 }
77
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)
86
87         header := &GenManHeader{
88                 Title:   "Project",
89                 Section: "2",
90         }
91         // We generate on a subcommand so we have both subcommands and parents
92         if err := GenMan(cmdEcho, header, out); err != nil {
93                 t.Fatal(err)
94         }
95         found := out.String()
96
97         unexpected := translate("#HISTORY")
98         checkStringOmits(t, found, unexpected)
99 }
100
101 func TestGenManSeeAlso(t *testing.T) {
102         noop := func(cmd *cobra.Command, args []string) {}
103
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)
109
110         out := new(bytes.Buffer)
111         header := &GenManHeader{}
112         if err := GenMan(top, header, out); err != nil {
113                 t.Fatal(err)
114         }
115
116         scanner := bufio.NewScanner(out)
117
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()))
120         }
121
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()))
124         }
125
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()))
128         }
129 }
130
131 func TestManPrintFlagsHidesShortDeperecated(t *testing.T) {
132         cmd := &cobra.Command{}
133         flags := cmd.Flags()
134         flags.StringP("foo", "f", "default", "Foo flag")
135         flags.MarkShorthandDeprecated("foo", "don't use it no more")
136
137         out := new(bytes.Buffer)
138         manPrintFlags(out, flags)
139
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())
143         }
144 }
145
146 func TestGenManTree(t *testing.T) {
147         cmd := &cobra.Command{
148                 Use: "do [OPTIONS] arg1 arg2",
149         }
150         header := &GenManHeader{Section: "2"}
151         tmpdir, err := ioutil.TempDir("", "test-gen-man-tree")
152         if err != nil {
153                 t.Fatalf("Failed to create tmpdir: %s", err.Error())
154         }
155         defer os.RemoveAll(tmpdir)
156
157         if err := GenManTree(cmd, header, tmpdir); err != nil {
158                 t.Fatalf("GenManTree failed: %s", err.Error())
159         }
160
161         if _, err := os.Stat(filepath.Join(tmpdir, "do.2")); err != nil {
162                 t.Fatalf("Expected file 'do.2' to exist")
163         }
164
165         if header.Title != "" {
166                 t.Fatalf("Expected header.Title to be unmodified")
167         }
168 }
169
170 func AssertLineFound(scanner *bufio.Scanner, expectedLine string) error {
171         for scanner.Scan() {
172                 line := scanner.Text()
173                 if line == expectedLine {
174                         return nil
175                 }
176         }
177
178         if err := scanner.Err(); err != nil {
179                 return fmt.Errorf("AssertLineFound: scan failed: %s", err.Error())
180         }
181
182         return fmt.Errorf("AssertLineFound: hit EOF before finding %#v", expectedLine)
183 }
184
185 func AssertNextLineEquals(scanner *bufio.Scanner, expectedLine string) error {
186         if scanner.Scan() {
187                 line := scanner.Text()
188                 if line == expectedLine {
189                         return nil
190                 }
191                 return fmt.Errorf("AssertNextLineEquals: got %#v, not %#v", line, expectedLine)
192         }
193
194         if err := scanner.Err(); err != nil {
195                 return fmt.Errorf("AssertNextLineEquals: scan failed: %s", err.Error())
196         }
197
198         return fmt.Errorf("AssertNextLineEquals: hit EOF before finding %#v", expectedLine)
199 }
200
201 func BenchmarkGenManToFile(b *testing.B) {
202         c := initializeWithRootCmd()
203         file, err := ioutil.TempFile("", "")
204         if err != nil {
205                 b.Fatal(err)
206         }
207         defer os.Remove(file.Name())
208         defer file.Close()
209
210         b.ResetTimer()
211         for i := 0; i < b.N; i++ {
212                 if err := GenMan(c, nil, file); err != nil {
213                         b.Fatal(err)
214                 }
215         }
216 }