From 50ba2caeb57ce85e2a5d09a2fd59c0477d3a87eb Mon Sep 17 00:00:00 2001 From: Dan Ramich Date: Wed, 20 Sep 2023 11:57:39 -0600 Subject: [PATCH 1/2] Move newline to inside the DisableAutoGenTag block Fixes https://github.com/spf13/cobra/issues/2029 --- doc/md_docs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/md_docs.go b/doc/md_docs.go index c4a27c009..f243d47f9 100644 --- a/doc/md_docs.go +++ b/doc/md_docs.go @@ -105,9 +105,9 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) link = strings.ReplaceAll(link, " ", "_") buf.WriteString(fmt.Sprintf("* [%s](%s)\t - %s\n", cname, linkHandler(link), child.Short)) } - buf.WriteString("\n") } if !cmd.DisableAutoGenTag { + buf.WriteString("\n") buf.WriteString("###### Auto generated by spf13/cobra on " + time.Now().Format("2-Jan-2006") + "\n") } _, err := buf.WriteTo(w) From 83d142ca7680d5b1977366c27a27af92551523fb Mon Sep 17 00:00:00 2001 From: Dan Ramich Date: Sat, 18 May 2024 14:57:18 -0600 Subject: [PATCH 2/2] Add tests for double line break --- doc/cmd_test.go | 7 +++++++ doc/md_docs_test.go | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/doc/cmd_test.go b/doc/cmd_test.go index 0d022c77d..0ce3f9610 100644 --- a/doc/cmd_test.go +++ b/doc/cmd_test.go @@ -103,3 +103,10 @@ func checkStringOmits(t *testing.T, got, expected string) { t.Errorf("Expected to not contain: \n %v\nGot: %v", expected, got) } } + +func checkStringOmitsSuffix(t *testing.T, got, expected string) { + if strings.HasSuffix(got, expected) { + // Note this uses %q so things like line breaks can be seen in the printed output + t.Errorf("Expected to not have suffix: \n %q\nGot: %q", expected, got) + } +} diff --git a/doc/md_docs_test.go b/doc/md_docs_test.go index e70cad822..5dfa1b9ab 100644 --- a/doc/md_docs_test.go +++ b/doc/md_docs_test.go @@ -24,6 +24,8 @@ import ( "github.com/spf13/cobra" ) +const doubleLineBreak = "\n\n" + func TestGenMdDoc(t *testing.T) { // We generate on subcommand so we have both subcommands and parents. buf := new(bytes.Buffer) @@ -40,6 +42,7 @@ func TestGenMdDoc(t *testing.T) { checkStringContains(t, output, echoSubCmd.Short) checkStringOmits(t, output, deprecatedCmd.Short) checkStringContains(t, output, "Options inherited from parent commands") + checkStringOmitsSuffix(t, output, doubleLineBreak) } func TestGenMdDocWithNoLongOrSynopsis(t *testing.T) { @@ -54,6 +57,7 @@ func TestGenMdDocWithNoLongOrSynopsis(t *testing.T) { checkStringContains(t, output, dummyCmd.Short) checkStringContains(t, output, "Options inherited from parent commands") checkStringOmits(t, output, "### Synopsis") + checkStringOmitsSuffix(t, output, doubleLineBreak) } func TestGenMdNoHiddenParents(t *testing.T) { @@ -77,6 +81,7 @@ func TestGenMdNoHiddenParents(t *testing.T) { checkStringContains(t, output, echoSubCmd.Short) checkStringOmits(t, output, deprecatedCmd.Short) checkStringOmits(t, output, "Options inherited from parent commands") + checkStringOmitsSuffix(t, output, doubleLineBreak) } func TestGenMdNoTag(t *testing.T) { @@ -90,6 +95,7 @@ func TestGenMdNoTag(t *testing.T) { output := buf.String() checkStringOmits(t, output, "Auto generated") + checkStringOmitsSuffix(t, output, doubleLineBreak) } func TestGenMdTree(t *testing.T) {