Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move newline to inside the DisableAutoGenTag block #2030

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion doc/md_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
dramich marked this conversation as resolved.
Show resolved Hide resolved
}
_, err := buf.WriteTo(w)
Expand Down
6 changes: 6 additions & 0 deletions doc/md_docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nice, but don't we want the more general check, that the output always ends with a single newline?

}

func TestGenMdDocWithNoLongOrSynopsis(t *testing.T) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down