1package doc 2 3import ( 4 "bytes" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "strings" 9 "testing" 10 11 "github.com/spf13/cobra" 12) 13 14func TestGenMdDoc(t *testing.T) { 15 c := initializeWithRootCmd() 16 // Need two commands to run the command alphabetical sort 17 cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated) 18 c.AddCommand(cmdPrint, cmdEcho) 19 cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp) 20 21 out := new(bytes.Buffer) 22 23 // We generate on s subcommand so we have both subcommands and parents 24 if err := GenMarkdown(cmdEcho, out); err != nil { 25 t.Fatal(err) 26 } 27 found := out.String() 28 29 // Our description 30 expected := cmdEcho.Long 31 if !strings.Contains(found, expected) { 32 t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found) 33 } 34 35 // Better have our example 36 expected = cmdEcho.Example 37 if !strings.Contains(found, expected) { 38 t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found) 39 } 40 41 // A local flag 42 expected = "boolone" 43 if !strings.Contains(found, expected) { 44 t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found) 45 } 46 47 // persistent flag on parent 48 expected = "rootflag" 49 if !strings.Contains(found, expected) { 50 t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found) 51 } 52 53 // We better output info about our parent 54 expected = cmdRootWithRun.Short 55 if !strings.Contains(found, expected) { 56 t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found) 57 } 58 59 // And about subcommands 60 expected = cmdEchoSub.Short 61 if !strings.Contains(found, expected) { 62 t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found) 63 } 64 65 unexpected := cmdDeprecated.Short 66 if strings.Contains(found, unexpected) { 67 t.Errorf("Unexpected response.\nFound: %v\nBut should not have!!\n", unexpected) 68 } 69} 70 71func TestGenMdNoTag(t *testing.T) { 72 c := initializeWithRootCmd() 73 // Need two commands to run the command alphabetical sort 74 cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated) 75 c.AddCommand(cmdPrint, cmdEcho) 76 c.DisableAutoGenTag = true 77 cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp) 78 out := new(bytes.Buffer) 79 80 if err := GenMarkdown(c, out); err != nil { 81 t.Fatal(err) 82 } 83 found := out.String() 84 85 unexpected := "Auto generated" 86 checkStringOmits(t, found, unexpected) 87 88} 89 90func TestGenMdTree(t *testing.T) { 91 cmd := &cobra.Command{ 92 Use: "do [OPTIONS] arg1 arg2", 93 } 94 tmpdir, err := ioutil.TempDir("", "test-gen-md-tree") 95 if err != nil { 96 t.Fatalf("Failed to create tmpdir: %s", err.Error()) 97 } 98 defer os.RemoveAll(tmpdir) 99 100 if err := GenMarkdownTree(cmd, tmpdir); err != nil { 101 t.Fatalf("GenMarkdownTree failed: %s", err.Error()) 102 } 103 104 if _, err := os.Stat(filepath.Join(tmpdir, "do.md")); err != nil { 105 t.Fatalf("Expected file 'do.md' to exist") 106 } 107} 108 109func BenchmarkGenMarkdownToFile(b *testing.B) { 110 c := initializeWithRootCmd() 111 file, err := ioutil.TempFile("", "") 112 if err != nil { 113 b.Fatal(err) 114 } 115 defer os.Remove(file.Name()) 116 defer file.Close() 117 118 b.ResetTimer() 119 for i := 0; i < b.N; i++ { 120 if err := GenMarkdown(c, file); err != nil { 121 b.Fatal(err) 122 } 123 } 124} 125