yaml_docs_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package doc
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "github.com/spf13/cobra"
  9. )
  10. func TestGenYamlDoc(t *testing.T) {
  11. // We generate on s subcommand so we have both subcommands and parents
  12. buf := new(bytes.Buffer)
  13. if err := GenYaml(echoCmd, buf); err != nil {
  14. t.Fatal(err)
  15. }
  16. output := buf.String()
  17. checkStringContains(t, output, echoCmd.Long)
  18. checkStringContains(t, output, echoCmd.Example)
  19. checkStringContains(t, output, "boolone")
  20. checkStringContains(t, output, "rootflag")
  21. checkStringContains(t, output, rootCmd.Short)
  22. checkStringContains(t, output, echoSubCmd.Short)
  23. }
  24. func TestGenYamlNoTag(t *testing.T) {
  25. rootCmd.DisableAutoGenTag = true
  26. defer func() { rootCmd.DisableAutoGenTag = false }()
  27. buf := new(bytes.Buffer)
  28. if err := GenYaml(rootCmd, buf); err != nil {
  29. t.Fatal(err)
  30. }
  31. output := buf.String()
  32. checkStringOmits(t, output, "Auto generated")
  33. }
  34. func TestGenYamlTree(t *testing.T) {
  35. c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}
  36. tmpdir, err := ioutil.TempDir("", "test-gen-yaml-tree")
  37. if err != nil {
  38. t.Fatalf("Failed to create tmpdir: %s", err.Error())
  39. }
  40. defer os.RemoveAll(tmpdir)
  41. if err := GenYamlTree(c, tmpdir); err != nil {
  42. t.Fatalf("GenYamlTree failed: %s", err.Error())
  43. }
  44. if _, err := os.Stat(filepath.Join(tmpdir, "do.yaml")); err != nil {
  45. t.Fatalf("Expected file 'do.yaml' to exist")
  46. }
  47. }
  48. func BenchmarkGenYamlToFile(b *testing.B) {
  49. file, err := ioutil.TempFile("", "")
  50. if err != nil {
  51. b.Fatal(err)
  52. }
  53. defer os.Remove(file.Name())
  54. defer file.Close()
  55. b.ResetTimer()
  56. for i := 0; i < b.N; i++ {
  57. if err := GenYaml(rootCmd, file); err != nil {
  58. b.Fatal(err)
  59. }
  60. }
  61. }