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