Add the ability to specify a filePostpender in GenMarkdownTreeCustom (#1270)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Joon-Ho Son · committed by GitHub · parent 4384b91 · Browse files · 2 files·+46 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+46 added−4 removed
- other:+46 added−4 removed
Provenance
- Commit
23a6174- Tree
0abebd4- Author
Joon-Ho Son <20669173+sonjoonho@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
4384b91
2 files changed+46 added−4 removed
modified:doc/md_docs_test.go+39 added
93 unmodified lines94 t.Fatalf("Expected file 'do.md' to exist")95 }96 }97 98+func TestGenMdTreeCustom(t *testing.T) {99+ tmpdir, err := ioutil.TempDir("", "test-gen-md-tree")100+ if err != nil {101+ t.Fatalf("Failed to create tmpdir: %v", err)102+ }103+ defer os.RemoveAll(tmpdir)104+105+ prepender := func(s string) string { return "Prepended" }106+ postpender := func(s string) string { return "Postpended" }107+ identity := func(s string) string { return s }108+109+ if err := GenMarkdownTreeCustom(rootCmd, tmpdir, prepender, postpender, identity); err != nil {110+ t.Fatalf("GenMarkdownTree failed: %v", err)111+ }112+113+ gotRoot := fileContents(t, tmpdir, "root.md")114+ checkStringContains(t, gotRoot, "Prepended")115+ checkStringContains(t, gotRoot, rootCmd.Long)116+ checkStringContains(t, gotRoot, "Postpended")117+118+ gotEcho := fileContents(t, tmpdir, "root_echo.md")119+ checkStringContains(t, gotEcho, "Prepended")120+ checkStringContains(t, gotEcho, echoCmd.Long)121+ checkStringContains(t, gotEcho, "Postpended")122+123+ gotEchoSub := fileContents(t, tmpdir, "root_echo_echosub.md")124+ checkStringContains(t, gotEchoSub, "Prepended")125+ checkStringContains(t, gotEchoSub, echoSubCmd.Long)126+ checkStringContains(t, gotEchoSub, "Postpended")127+}128+129 func BenchmarkGenMarkdownToFile(b *testing.B) {130 file, err := ioutil.TempFile("", "")131 if err != nil {132 b.Fatal(err)7 unmodified lines140 b.Fatal(err)141 }142 }143 }144+145+func fileContents(t *testing.T, dir, filename string) string {146+ contents, err := ioutil.ReadFile(filepath.Join(dir, filename))147+ if err != nil {148+ t.Fatalf("Error loading file %q: %v ", filename, err)149+ }150+ return string(contents)151+}modified:doc/md_docs.go+7 added-4 removed
120 unmodified lines121 // help output will be in the file `cmd-sub-third.1`.122 func GenMarkdownTree(cmd *cobra.Command, dir string) error {123 identity := func(s string) string { return s }124 emptyStr := func(s string) string { return "" }125- return GenMarkdownTreeCustom(cmd, dir, emptyStr, identity)125+ return GenMarkdownTreeCustom(cmd, dir, emptyStr, emptyStr, identity)126 }127 128 // GenMarkdownTreeCustom is the the same as GenMarkdownTree, but129-// with custom filePrepender and linkHandler.130-func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHandler func(string) string) error {129+// with custom filePrepender, filePostpender, and linkHandler.130+func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender, filePostpender, linkHandler func(string) string) error {131 for _, c := range cmd.Commands() {132 if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {133 continue134 }135- if err := GenMarkdownTreeCustom(c, dir, filePrepender, linkHandler); err != nil {135+ if err := GenMarkdownTreeCustom(c, dir, filePrepender, filePostpender, linkHandler); err != nil {136 return err137 }138 }139 10 unmodified lines150 }151 if err := GenMarkdownCustom(cmd, f, linkHandler); err != nil {152 return err153 }154+ if _, err := io.WriteString(f, filePostpender(filename)); err != nil {155+ return err156+ }157 return nil158 }