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 `-`
This commit is contained in:
Eric Paris 2015-09-08 12:32:15 -04:00
parent 68f5a81a72
commit cfecf1379c
3 changed files with 35 additions and 32 deletions

View file

@ -255,16 +255,24 @@ func logErr(t *testing.T, found, expected string) {
t.Errorf(out.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) { func checkResultContains(t *testing.T, x resulter, check string) {
if !strings.Contains(x.Output, check) { checkStringContains(t, x.Output, check)
logErr(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) { func checkResultOmits(t *testing.T, x resulter, check string) {
if strings.Contains(x.Output, check) { checkStringOmits(t, x.Output, check)
logErr(t, x.Output, check)
}
} }
func checkOutputContains(t *testing.T, c *Command, check string) { func checkOutputContains(t *testing.T, c *Command, check string) {
@ -976,15 +984,15 @@ func TestFlagOnPflagCommandLine(t *testing.T) {
func TestAddTemplateFunctions(t *testing.T) { func TestAddTemplateFunctions(t *testing.T) {
AddTemplateFunc("t", func() bool { return true }) AddTemplateFunc("t", func() bool { return true })
AddTemplateFuncs(template.FuncMap{ AddTemplateFuncs(template.FuncMap{
"f": func() bool { return false }, "f": func() bool { return false },
"h": func() string { return "Hello," }, "h": func() string { return "Hello," },
"w": func() string { return "world." }}) "w": func() string { return "world." }})
const usage = "Hello, world." const usage = "Hello, world."
c := &Command{} c := &Command{}
c.SetUsageTemplate(`{{if t}}{{h}}{{end}}{{if f}}{{h}}{{end}} {{w}}`) c.SetUsageTemplate(`{{if t}}{{h}}{{end}}{{if f}}{{h}}{{end}} {{w}}`)
if us := c.UsageString(); us != usage { if us := c.UsageString(); us != usage {
t.Errorf("c.UsageString() != \"%s\", is \"%s\"", usage, us) t.Errorf("c.UsageString() != \"%s\", is \"%s\"", usage, us)
} }

View file

@ -145,7 +145,9 @@ func genMarkdown(cmd *Command, projectName string) []byte {
if cmd.hasSeeAlso() { if cmd.hasSeeAlso() {
fmt.Fprintf(buf, "# SEE ALSO\n") fmt.Fprintf(buf, "# SEE ALSO\n")
if cmd.HasParent() { 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() children := cmd.Commands()

View file

@ -28,44 +28,37 @@ func TestGenManDoc(t *testing.T) {
cmdEcho.GenMan("PROJECT", out) cmdEcho.GenMan("PROJECT", out)
found := out.String() 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 // Our description
expected := translate(cmdEcho.Name()) expected = translate(cmdEcho.Name())
if !strings.Contains(found, expected) { checkStringContains(t, found, expected)
t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)
}
// Better have our example // Better have our example
expected = translate(cmdEcho.Name()) expected = translate(cmdEcho.Name())
if !strings.Contains(found, expected) { checkStringContains(t, found, expected)
t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)
}
// A local flag // A local flag
expected = "boolone" expected = "boolone"
if !strings.Contains(found, expected) { checkStringContains(t, found, expected)
t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)
}
// persistent flag on parent // persistent flag on parent
expected = "rootflag" expected = "rootflag"
if !strings.Contains(found, expected) { checkStringContains(t, found, expected)
t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)
}
// We better output info about our parent // We better output info about our parent
expected = translate(cmdRootWithRun.Name()) expected = translate(cmdRootWithRun.Name())
if !strings.Contains(found, expected) { checkStringContains(t, found, expected)
t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)
}
// And about subcommands // And about subcommands
expected = translate(cmdEchoSub.Name()) expected = translate(cmdEchoSub.Name())
if !strings.Contains(found, expected) { checkStringContains(t, found, expected)
t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)
}
unexpected := translate(cmdDeprecated.Name()) unexpected := translate(cmdDeprecated.Name())
if strings.Contains(found, unexpected) { checkStringOmits(t, found, unexpected)
t.Errorf("Unexpected response.\nFound: %v\nBut should not have!!\n", unexpected)
}
} }