2017-01-31 00:45:31 +03:00
|
|
|
package doc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-04-19 15:39:58 +03:00
|
|
|
"io/ioutil"
|
2017-01-31 00:45:31 +03:00
|
|
|
"os"
|
2017-04-19 15:39:58 +03:00
|
|
|
"path/filepath"
|
2017-01-31 00:45:31 +03:00
|
|
|
"testing"
|
|
|
|
|
2017-04-19 15:39:58 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
2017-01-31 00:45:31 +03:00
|
|
|
|
|
|
|
func TestGenYamlDoc(t *testing.T) {
|
|
|
|
// We generate on s subcommand so we have both subcommands and parents
|
2017-11-02 18:22:38 +03:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := GenYaml(echoCmd, buf); err != nil {
|
2017-01-31 00:45:31 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-11-02 18:22:38 +03: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-01-31 00:45:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenYamlNoTag(t *testing.T) {
|
2017-11-02 18:22:38 +03:00
|
|
|
rootCmd.DisableAutoGenTag = true
|
|
|
|
defer func() { rootCmd.DisableAutoGenTag = false }()
|
2017-01-31 00:45:31 +03:00
|
|
|
|
2017-11-02 18:22:38 +03:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := GenYaml(rootCmd, buf); err != nil {
|
2017-01-31 00:45:31 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-11-02 18:22:38 +03:00
|
|
|
output := buf.String()
|
2017-01-31 00:45:31 +03:00
|
|
|
|
2017-11-02 18:22:38 +03:00
|
|
|
checkStringOmits(t, output, "Auto generated")
|
2017-01-31 00:45:31 +03:00
|
|
|
}
|
2017-04-19 15:39:58 +03:00
|
|
|
|
|
|
|
func TestGenYamlTree(t *testing.T) {
|
2017-11-02 18:22:38 +03:00
|
|
|
c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}
|
2017-04-19 15:39:58 +03: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)
|
|
|
|
|
2017-11-02 18:22:38 +03:00
|
|
|
if err := GenYamlTree(c, tmpdir); err != nil {
|
2017-04-19 15:39:58 +03: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")
|
|
|
|
}
|
|
|
|
}
|
2017-04-24 21:44:32 +03:00
|
|
|
|
2022-04-23 22:46:57 +03:00
|
|
|
func TestGenYamlTreeSlashCommands(t *testing.T) {
|
|
|
|
c := &cobra.Command{Use: "run/first [OPTIONS] arg1 arg2"}
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "test-gen-yaml-tree-slash-commands")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create tmpdir: %s", err.Error())
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
|
|
|
if err := GenYamlTree(c, tmpdir); err != nil {
|
|
|
|
t.Fatalf("GenYamlTree failed: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(filepath.Join(tmpdir, "run_first.yaml")); err != nil {
|
|
|
|
t.Fatalf("Expected file 'run_first.yaml' to exist")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 01:51:03 +03:00
|
|
|
func TestGenYamlDocRunnable(t *testing.T) {
|
|
|
|
// Testing a runnable command: should contain the "usage" field
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := GenYaml(rootCmd, buf); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
output := buf.String()
|
|
|
|
|
|
|
|
checkStringContains(t, output, "usage: "+rootCmd.Use)
|
|
|
|
}
|
|
|
|
|
2017-04-24 21:44:32 +03:00
|
|
|
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++ {
|
2017-11-02 18:22:38 +03:00
|
|
|
if err := GenYaml(rootCmd, file); err != nil {
|
2017-04-24 21:44:32 +03:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|