rest_docs_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 TestGenRSTDoc(t *testing.T) {
  11. // We generate on a subcommand so we have both subcommands and parents
  12. buf := new(bytes.Buffer)
  13. if err := GenReST(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. checkStringOmits(t, output, deprecatedCmd.Short)
  24. }
  25. func TestGenRSTNoTag(t *testing.T) {
  26. rootCmd.DisableAutoGenTag = true
  27. defer func() { rootCmd.DisableAutoGenTag = false }()
  28. buf := new(bytes.Buffer)
  29. if err := GenReST(rootCmd, buf); err != nil {
  30. t.Fatal(err)
  31. }
  32. output := buf.String()
  33. unexpected := "Auto generated"
  34. checkStringOmits(t, output, unexpected)
  35. }
  36. func TestGenRSTTree(t *testing.T) {
  37. c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}
  38. tmpdir, err := ioutil.TempDir("", "test-gen-rst-tree")
  39. if err != nil {
  40. t.Fatalf("Failed to create tmpdir: %s", err.Error())
  41. }
  42. defer os.RemoveAll(tmpdir)
  43. if err := GenReSTTree(c, tmpdir); err != nil {
  44. t.Fatalf("GenReSTTree failed: %s", err.Error())
  45. }
  46. if _, err := os.Stat(filepath.Join(tmpdir, "do.rst")); err != nil {
  47. t.Fatalf("Expected file 'do.rst' to exist")
  48. }
  49. }
  50. func BenchmarkGenReSTToFile(b *testing.B) {
  51. file, err := ioutil.TempFile("", "")
  52. if err != nil {
  53. b.Fatal(err)
  54. }
  55. defer os.Remove(file.Name())
  56. defer file.Close()
  57. b.ResetTimer()
  58. for i := 0; i < b.N; i++ {
  59. if err := GenReST(rootCmd, file); err != nil {
  60. b.Fatal(err)
  61. }
  62. }
  63. }