...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package doc
16
17 import (
18 "bytes"
19 "fmt"
20 "io/ioutil"
21 "os"
22 "path/filepath"
23 "testing"
24
25 "github.com/spf13/cobra"
26 )
27
28 func TestGenYamlDoc(t *testing.T) {
29
30 buf := new(bytes.Buffer)
31 if err := GenYaml(echoCmd, buf); err != nil {
32 t.Fatal(err)
33 }
34 output := buf.String()
35
36 checkStringContains(t, output, echoCmd.Long)
37 checkStringContains(t, output, echoCmd.Example)
38 checkStringContains(t, output, "boolone")
39 checkStringContains(t, output, "rootflag")
40 checkStringContains(t, output, rootCmd.Short)
41 checkStringContains(t, output, echoSubCmd.Short)
42 checkStringContains(t, output, fmt.Sprintf("- %s - %s", echoSubCmd.CommandPath(), echoSubCmd.Short))
43 }
44
45 func TestGenYamlNoTag(t *testing.T) {
46 rootCmd.DisableAutoGenTag = true
47 defer func() { rootCmd.DisableAutoGenTag = false }()
48
49 buf := new(bytes.Buffer)
50 if err := GenYaml(rootCmd, buf); err != nil {
51 t.Fatal(err)
52 }
53 output := buf.String()
54
55 checkStringOmits(t, output, "Auto generated")
56 }
57
58 func TestGenYamlTree(t *testing.T) {
59 c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}
60
61 tmpdir, err := ioutil.TempDir("", "test-gen-yaml-tree")
62 if err != nil {
63 t.Fatalf("Failed to create tmpdir: %s", err.Error())
64 }
65 defer os.RemoveAll(tmpdir)
66
67 if err := GenYamlTree(c, tmpdir); err != nil {
68 t.Fatalf("GenYamlTree failed: %s", err.Error())
69 }
70
71 if _, err := os.Stat(filepath.Join(tmpdir, "do.yaml")); err != nil {
72 t.Fatalf("Expected file 'do.yaml' to exist")
73 }
74 }
75
76 func TestGenYamlDocRunnable(t *testing.T) {
77
78 buf := new(bytes.Buffer)
79 if err := GenYaml(rootCmd, buf); err != nil {
80 t.Fatal(err)
81 }
82 output := buf.String()
83
84 checkStringContains(t, output, "usage: "+rootCmd.Use)
85 }
86
87 func BenchmarkGenYamlToFile(b *testing.B) {
88 file, err := ioutil.TempFile("", "")
89 if err != nil {
90 b.Fatal(err)
91 }
92 defer os.Remove(file.Name())
93 defer file.Close()
94
95 b.ResetTimer()
96 for i := 0; i < b.N; i++ {
97 if err := GenYaml(rootCmd, file); err != nil {
98 b.Fatal(err)
99 }
100 }
101 }
102
View as plain text