2018-01-29 19:44:18 +00:00
|
|
|
package doc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGenRSTDoc(t *testing.T) {
|
2018-09-05 15:36:14 +00:00
|
|
|
// We generate on a subcommand so we have both subcommands and parents
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := GenReST(echoCmd, buf); err != nil {
|
2018-01-29 19:44:18 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-09-05 15:36:14 +00:00
|
|
|
output := buf.String()
|
|
|
|
|
|
|
|
checkStringContains(t, output, echoCmd.Long)
|
|
|
|
checkStringContains(t, output, echoCmd.Example)
|
|
|
|
checkStringContains(t, output, "boolone")
|
|
|
|
checkStringContains(t, output, "rootflag")
|
|
|
|
checkStringContains(t, output, rootCmd.Short)
|
|
|
|
checkStringContains(t, output, echoSubCmd.Short)
|
|
|
|
checkStringOmits(t, output, deprecatedCmd.Short)
|
|
|
|
}
|
2018-01-29 19:44:18 +00:00
|
|
|
|
2018-09-05 15:36:14 +00:00
|
|
|
func TestGenRSTNoHiddenParents(t *testing.T) {
|
|
|
|
// We generate on a subcommand so we have both subcommands and parents
|
|
|
|
for _, name := range []string{"rootflag", "strtwo"} {
|
|
|
|
f := rootCmd.PersistentFlags().Lookup(name)
|
|
|
|
f.Hidden = true
|
|
|
|
defer func() { f.Hidden = false }()
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
2018-09-05 15:36:14 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := GenReST(echoCmd, buf); err != nil {
|
|
|
|
t.Fatal(err)
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
2018-09-05 15:36:14 +00:00
|
|
|
output := buf.String()
|
|
|
|
|
|
|
|
checkStringContains(t, output, echoCmd.Long)
|
|
|
|
checkStringContains(t, output, echoCmd.Example)
|
|
|
|
checkStringContains(t, output, "boolone")
|
|
|
|
checkStringOmits(t, output, "rootflag")
|
|
|
|
checkStringContains(t, output, rootCmd.Short)
|
|
|
|
checkStringContains(t, output, echoSubCmd.Short)
|
|
|
|
checkStringOmits(t, output, deprecatedCmd.Short)
|
|
|
|
checkStringOmits(t, output, "Options inherited from parent commands")
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenRSTNoTag(t *testing.T) {
|
2018-09-05 15:36:14 +00:00
|
|
|
rootCmd.DisableAutoGenTag = true
|
|
|
|
defer func() { rootCmd.DisableAutoGenTag = false }()
|
2018-01-29 19:44:18 +00:00
|
|
|
|
2018-09-05 15:36:14 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := GenReST(rootCmd, buf); err != nil {
|
2018-01-29 19:44:18 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-09-05 15:36:14 +00:00
|
|
|
output := buf.String()
|
2018-01-29 19:44:18 +00:00
|
|
|
|
|
|
|
unexpected := "Auto generated"
|
2018-09-05 15:36:14 +00:00
|
|
|
checkStringOmits(t, output, unexpected)
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenRSTTree(t *testing.T) {
|
2018-09-05 15:36:14 +00:00
|
|
|
c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}
|
|
|
|
|
2018-01-29 19:44:18 +00:00
|
|
|
tmpdir, err := ioutil.TempDir("", "test-gen-rst-tree")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create tmpdir: %s", err.Error())
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
2018-09-05 15:36:14 +00:00
|
|
|
if err := GenReSTTree(c, tmpdir); err != nil {
|
2018-01-29 19:44:18 +00:00
|
|
|
t.Fatalf("GenReSTTree failed: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(filepath.Join(tmpdir, "do.rst")); err != nil {
|
|
|
|
t.Fatalf("Expected file 'do.rst' to exist")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGenReSTToFile(b *testing.B) {
|
|
|
|
file, err := ioutil.TempFile("", "")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2018-09-05 15:36:14 +00:00
|
|
|
if err := GenReST(rootCmd, file); err != nil {
|
2018-01-29 19:44:18 +00:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|