2017-05-11 16:39:54 +02:00
|
|
|
package doc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGenYamlDoc(t *testing.T) {
|
|
|
|
// We generate on s subcommand so we have both subcommands and parents
|
2018-01-16 14:20:59 +01:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := GenYaml(echoCmd, buf); err != nil {
|
2017-05-11 16:39:54 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-16 14:20:59 +01: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)
|
2017-05-11 16:39:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenYamlNoTag(t *testing.T) {
|
2018-01-16 14:20:59 +01:00
|
|
|
rootCmd.DisableAutoGenTag = true
|
|
|
|
defer func() { rootCmd.DisableAutoGenTag = false }()
|
2017-05-11 16:39:54 +02:00
|
|
|
|
2018-01-16 14:20:59 +01:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := GenYaml(rootCmd, buf); err != nil {
|
2017-05-11 16:39:54 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-16 14:20:59 +01:00
|
|
|
output := buf.String()
|
2017-05-11 16:39:54 +02:00
|
|
|
|
2018-01-16 14:20:59 +01:00
|
|
|
checkStringOmits(t, output, "Auto generated")
|
2017-05-11 16:39:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenYamlTree(t *testing.T) {
|
2018-01-16 14:20:59 +01:00
|
|
|
c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}
|
2017-05-11 16:39:54 +02:00
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "test-gen-yaml-tree")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create tmpdir: %s", err.Error())
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
2018-01-16 14:20:59 +01:00
|
|
|
if err := GenYamlTree(c, tmpdir); err != nil {
|
2017-05-11 16:39:54 +02:00
|
|
|
t.Fatalf("GenYamlTree failed: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(filepath.Join(tmpdir, "do.yaml")); err != nil {
|
|
|
|
t.Fatalf("Expected file 'do.yaml' to exist")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGenYamlToFile(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-01-16 14:20:59 +01:00
|
|
|
if err := GenYaml(rootCmd, file); err != nil {
|
2017-05-11 16:39:54 +02:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|