From cfecf1379c6d9e380520c2d1f2a2dd759dcc80bf Mon Sep 17 00:00:00 2001 From: Eric Paris Date: Tue, 8 Sep 2015 12:32:15 -0400 Subject: [PATCH] manpage generation: Make sure parent in SEE ALSO has dashes The CommandPath() for a parent might have a space (if .Parent() != .Root()) so we need to replace those spaces with `-` --- cobra_test.go | 26 +++++++++++++++++--------- man_docs.go | 4 +++- man_docs_test.go | 37 +++++++++++++++---------------------- 3 files changed, 35 insertions(+), 32 deletions(-) diff --git a/cobra_test.go b/cobra_test.go index 3aed7dd6..01632691 100644 --- a/cobra_test.go +++ b/cobra_test.go @@ -255,16 +255,24 @@ func logErr(t *testing.T, found, expected string) { t.Errorf(out.String()) } +func checkStringContains(t *testing.T, found, expected string) { + if !strings.Contains(found, expected) { + logErr(t, found, expected) + } +} + func checkResultContains(t *testing.T, x resulter, check string) { - if !strings.Contains(x.Output, check) { - logErr(t, x.Output, check) + checkStringContains(t, x.Output, check) +} + +func checkStringOmits(t *testing.T, found, expected string) { + if strings.Contains(found, expected) { + logErr(t, found, expected) } } func checkResultOmits(t *testing.T, x resulter, check string) { - if strings.Contains(x.Output, check) { - logErr(t, x.Output, check) - } + checkStringOmits(t, x.Output, check) } func checkOutputContains(t *testing.T, c *Command, check string) { @@ -976,15 +984,15 @@ func TestFlagOnPflagCommandLine(t *testing.T) { func TestAddTemplateFunctions(t *testing.T) { AddTemplateFunc("t", func() bool { return true }) AddTemplateFuncs(template.FuncMap{ - "f": func() bool { return false }, - "h": func() string { return "Hello," }, + "f": func() bool { return false }, + "h": func() string { return "Hello," }, "w": func() string { return "world." }}) const usage = "Hello, world." - + c := &Command{} c.SetUsageTemplate(`{{if t}}{{h}}{{end}}{{if f}}{{h}}{{end}} {{w}}`) - + if us := c.UsageString(); us != usage { t.Errorf("c.UsageString() != \"%s\", is \"%s\"", usage, us) } diff --git a/man_docs.go b/man_docs.go index f260990e..894e5ecf 100644 --- a/man_docs.go +++ b/man_docs.go @@ -145,7 +145,9 @@ func genMarkdown(cmd *Command, projectName string) []byte { if cmd.hasSeeAlso() { fmt.Fprintf(buf, "# SEE ALSO\n") if cmd.HasParent() { - fmt.Fprintf(buf, "**%s(1)**, ", cmd.Parent().CommandPath()) + parentPath := cmd.Parent().CommandPath() + dashParentPath := strings.Replace(parentPath, " ", "-", -1) + fmt.Fprintf(buf, "**%s(1)**, ", dashParentPath) } children := cmd.Commands() diff --git a/man_docs_test.go b/man_docs_test.go index 6e31ab3b..cb86edb9 100644 --- a/man_docs_test.go +++ b/man_docs_test.go @@ -28,44 +28,37 @@ func TestGenManDoc(t *testing.T) { cmdEcho.GenMan("PROJECT", out) found := out.String() + // Make sure parent has - in CommandPath() in SEE ALSO: + parentPath := cmdEcho.Parent().CommandPath() + dashParentPath := strings.Replace(parentPath, " ", "-", -1) + expected := translate(dashParentPath) + expected = expected + "(1)" + checkStringContains(t, found, expected) + // Our description - expected := translate(cmdEcho.Name()) - if !strings.Contains(found, expected) { - t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found) - } + expected = translate(cmdEcho.Name()) + checkStringContains(t, found, expected) // Better have our example expected = translate(cmdEcho.Name()) - if !strings.Contains(found, expected) { - t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found) - } + checkStringContains(t, found, expected) // A local flag expected = "boolone" - if !strings.Contains(found, expected) { - t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found) - } + checkStringContains(t, found, expected) // persistent flag on parent expected = "rootflag" - if !strings.Contains(found, expected) { - t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found) - } + checkStringContains(t, found, expected) // We better output info about our parent expected = translate(cmdRootWithRun.Name()) - if !strings.Contains(found, expected) { - t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found) - } + checkStringContains(t, found, expected) // And about subcommands expected = translate(cmdEchoSub.Name()) - if !strings.Contains(found, expected) { - t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found) - } + checkStringContains(t, found, expected) unexpected := translate(cmdDeprecated.Name()) - if strings.Contains(found, unexpected) { - t.Errorf("Unexpected response.\nFound: %v\nBut should not have!!\n", unexpected) - } + checkStringOmits(t, found, unexpected) }