11 "github.com/spf13/cobra"
14 func TestGenRSTDoc(t *testing.T) {
15 c := initializeWithRootCmd()
16 // Need two commands to run the command alphabetical sort
17 cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)
18 c.AddCommand(cmdPrint, cmdEcho)
19 cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)
21 out := new(bytes.Buffer)
23 // We generate on s subcommand so we have both subcommands and parents
24 if err := GenReST(cmdEcho, out); err != nil {
30 expected := cmdEcho.Long
31 if !strings.Contains(found, expected) {
32 t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)
35 // Better have our example
36 expected = cmdEcho.Example
37 if !strings.Contains(found, expected) {
38 t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)
43 if !strings.Contains(found, expected) {
44 t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)
47 // persistent flag on parent
49 if !strings.Contains(found, expected) {
50 t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)
53 // We better output info about our parent
54 expected = cmdRootWithRun.Short
55 if !strings.Contains(found, expected) {
56 t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)
59 // And about subcommands
60 expected = cmdEchoSub.Short
61 if !strings.Contains(found, expected) {
62 t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)
65 unexpected := cmdDeprecated.Short
66 if strings.Contains(found, unexpected) {
67 t.Errorf("Unexpected response.\nFound: %v\nBut should not have!!\n", unexpected)
71 func TestGenRSTNoTag(t *testing.T) {
72 c := initializeWithRootCmd()
73 // Need two commands to run the command alphabetical sort
74 cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)
75 c.AddCommand(cmdPrint, cmdEcho)
76 c.DisableAutoGenTag = true
77 cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)
78 out := new(bytes.Buffer)
80 if err := GenReST(c, out); err != nil {
85 unexpected := "Auto generated"
86 checkStringOmits(t, found, unexpected)
90 func TestGenRSTTree(t *testing.T) {
91 cmd := &cobra.Command{
92 Use: "do [OPTIONS] arg1 arg2",
94 tmpdir, err := ioutil.TempDir("", "test-gen-rst-tree")
96 t.Fatalf("Failed to create tmpdir: %s", err.Error())
98 defer os.RemoveAll(tmpdir)
100 if err := GenReSTTree(cmd, tmpdir); err != nil {
101 t.Fatalf("GenReSTTree failed: %s", err.Error())
104 if _, err := os.Stat(filepath.Join(tmpdir, "do.rst")); err != nil {
105 t.Fatalf("Expected file 'do.rst' to exist")
109 func BenchmarkGenReSTToFile(b *testing.B) {
110 c := initializeWithRootCmd()
111 file, err := ioutil.TempFile("", "")
115 defer os.Remove(file.Name())
119 for i := 0; i < b.N; i++ {
120 if err := GenReST(c, file); err != nil {