OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / spf13 / cobra / doc / yaml_docs_test.go
1 package doc
2
3 import (
4         "bytes"
5         "io/ioutil"
6         "os"
7         "path/filepath"
8         "strings"
9         "testing"
10
11         "github.com/spf13/cobra"
12 )
13
14 func TestGenYamlDoc(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)
20
21         out := new(bytes.Buffer)
22
23         // We generate on s subcommand so we have both subcommands and parents
24         if err := GenYaml(cmdEcho, out); err != nil {
25                 t.Fatal(err)
26         }
27         found := out.String()
28
29         // Our description
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)
33         }
34
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)
39         }
40
41         // A local flag
42         expected = "boolone"
43         if !strings.Contains(found, expected) {
44                 t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)
45         }
46
47         // persistent flag on parent
48         expected = "rootflag"
49         if !strings.Contains(found, expected) {
50                 t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)
51         }
52
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)
57         }
58
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)
63         }
64
65         unexpected := cmdDeprecated.Short
66         if strings.Contains(found, unexpected) {
67                 t.Errorf("Unexpected response.\nFound: %v\nBut should not have!!\n", unexpected)
68         }
69 }
70
71 func TestGenYamlNoTag(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)
79
80         if err := GenYaml(c, out); err != nil {
81                 t.Fatal(err)
82         }
83         found := out.String()
84
85         unexpected := "Auto generated"
86         checkStringOmits(t, found, unexpected)
87
88 }
89
90 func TestGenYamlTree(t *testing.T) {
91         cmd := &cobra.Command{
92                 Use: "do [OPTIONS] arg1 arg2",
93         }
94
95         tmpdir, err := ioutil.TempDir("", "test-gen-yaml-tree")
96         if err != nil {
97                 t.Fatalf("Failed to create tmpdir: %s", err.Error())
98         }
99         defer os.RemoveAll(tmpdir)
100
101         if err := GenYamlTree(cmd, tmpdir); err != nil {
102                 t.Fatalf("GenYamlTree failed: %s", err.Error())
103         }
104
105         if _, err := os.Stat(filepath.Join(tmpdir, "do.yaml")); err != nil {
106                 t.Fatalf("Expected file 'do.yaml' to exist")
107         }
108 }
109
110 func BenchmarkGenYamlToFile(b *testing.B) {
111         c := initializeWithRootCmd()
112         file, err := ioutil.TempFile("", "")
113         if err != nil {
114                 b.Fatal(err)
115         }
116         defer os.Remove(file.Name())
117         defer file.Close()
118
119         b.ResetTimer()
120         for i := 0; i < b.N; i++ {
121                 if err := GenYaml(c, file); err != nil {
122                         b.Fatal(err)
123                 }
124         }
125 }