2015-11-24 02:19:16 +03:00
|
|
|
package doc
|
2015-08-19 01:33:41 +03:00
|
|
|
|
|
|
|
import (
|
2016-01-21 04:06:00 +03:00
|
|
|
"bufio"
|
2015-08-19 01:33:41 +03:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2016-01-21 04:06:00 +03:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2015-08-19 01:33:41 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = fmt.Println
|
|
|
|
var _ = os.Stderr
|
|
|
|
|
|
|
|
func translate(in string) string {
|
|
|
|
return strings.Replace(in, "-", "\\-", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenManDoc(t *testing.T) {
|
|
|
|
c := initializeWithRootCmd()
|
|
|
|
// Need two commands to run the command alphabetical sort
|
|
|
|
cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)
|
|
|
|
c.AddCommand(cmdPrint, cmdEcho)
|
|
|
|
cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)
|
|
|
|
|
|
|
|
out := new(bytes.Buffer)
|
|
|
|
|
2015-09-08 23:02:02 +03:00
|
|
|
header := &GenManHeader{
|
|
|
|
Title: "Project",
|
|
|
|
Section: "2",
|
|
|
|
}
|
2015-08-19 01:33:41 +03:00
|
|
|
// We generate on a subcommand so we have both subcommands and parents
|
2016-01-06 14:21:23 +03:00
|
|
|
if err := GenMan(cmdEcho, header, out); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-08-19 01:33:41 +03:00
|
|
|
found := out.String()
|
|
|
|
|
2015-09-08 19:32:15 +03:00
|
|
|
// Make sure parent has - in CommandPath() in SEE ALSO:
|
|
|
|
parentPath := cmdEcho.Parent().CommandPath()
|
|
|
|
dashParentPath := strings.Replace(parentPath, " ", "-", -1)
|
|
|
|
expected := translate(dashParentPath)
|
2015-09-08 23:02:02 +03:00
|
|
|
expected = expected + "(" + header.Section + ")"
|
2015-09-08 19:32:15 +03:00
|
|
|
checkStringContains(t, found, expected)
|
|
|
|
|
2015-08-19 01:33:41 +03:00
|
|
|
// Our description
|
2015-09-08 19:32:15 +03:00
|
|
|
expected = translate(cmdEcho.Name())
|
|
|
|
checkStringContains(t, found, expected)
|
2015-08-19 01:33:41 +03:00
|
|
|
|
|
|
|
// Better have our example
|
|
|
|
expected = translate(cmdEcho.Name())
|
2015-09-08 19:32:15 +03:00
|
|
|
checkStringContains(t, found, expected)
|
2015-08-19 01:33:41 +03:00
|
|
|
|
|
|
|
// A local flag
|
|
|
|
expected = "boolone"
|
2015-09-08 19:32:15 +03:00
|
|
|
checkStringContains(t, found, expected)
|
2015-08-19 01:33:41 +03:00
|
|
|
|
|
|
|
// persistent flag on parent
|
|
|
|
expected = "rootflag"
|
2015-09-08 19:32:15 +03:00
|
|
|
checkStringContains(t, found, expected)
|
2015-08-19 01:33:41 +03:00
|
|
|
|
|
|
|
// We better output info about our parent
|
|
|
|
expected = translate(cmdRootWithRun.Name())
|
2015-09-08 19:32:15 +03:00
|
|
|
checkStringContains(t, found, expected)
|
2015-08-19 01:33:41 +03:00
|
|
|
|
|
|
|
// And about subcommands
|
|
|
|
expected = translate(cmdEchoSub.Name())
|
2015-09-08 19:32:15 +03:00
|
|
|
checkStringContains(t, found, expected)
|
2015-08-19 01:33:41 +03:00
|
|
|
|
|
|
|
unexpected := translate(cmdDeprecated.Name())
|
2015-09-08 19:32:15 +03:00
|
|
|
checkStringOmits(t, found, unexpected)
|
2015-11-08 04:21:25 +03:00
|
|
|
|
|
|
|
// auto generated
|
|
|
|
expected = translate("Auto generated")
|
|
|
|
checkStringContains(t, found, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenManNoGenTag(t *testing.T) {
|
|
|
|
c := initializeWithRootCmd()
|
|
|
|
// Need two commands to run the command alphabetical sort
|
|
|
|
cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)
|
|
|
|
c.AddCommand(cmdPrint, cmdEcho)
|
|
|
|
cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)
|
|
|
|
cmdEcho.DisableAutoGenTag = true
|
|
|
|
out := new(bytes.Buffer)
|
|
|
|
|
|
|
|
header := &GenManHeader{
|
|
|
|
Title: "Project",
|
|
|
|
Section: "2",
|
|
|
|
}
|
|
|
|
// We generate on a subcommand so we have both subcommands and parents
|
2016-01-06 14:21:23 +03:00
|
|
|
if err := GenMan(cmdEcho, header, out); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-11-08 04:21:25 +03:00
|
|
|
found := out.String()
|
|
|
|
|
|
|
|
unexpected := translate("#HISTORY")
|
|
|
|
checkStringOmits(t, found, unexpected)
|
2015-08-19 01:33:41 +03:00
|
|
|
}
|
2016-01-21 04:06:00 +03:00
|
|
|
|
|
|
|
func TestGenManSeeAlso(t *testing.T) {
|
|
|
|
noop := func(cmd *cobra.Command, args []string) {}
|
|
|
|
|
|
|
|
top := &cobra.Command{Use: "top", Run: noop}
|
|
|
|
aaa := &cobra.Command{Use: "aaa", Run: noop, Hidden: true} // #229
|
|
|
|
bbb := &cobra.Command{Use: "bbb", Run: noop}
|
|
|
|
ccc := &cobra.Command{Use: "ccc", Run: noop}
|
|
|
|
top.AddCommand(aaa, bbb, ccc)
|
|
|
|
|
|
|
|
out := new(bytes.Buffer)
|
|
|
|
header := &GenManHeader{}
|
|
|
|
if err := GenMan(top, header, out); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(out)
|
|
|
|
|
|
|
|
if err := AssertLineFound(scanner, ".SH SEE ALSO"); err != nil {
|
|
|
|
t.Fatal(fmt.Errorf("Couldn't find SEE ALSO section header: %s", err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := AssertNextLineEquals(scanner, ".PP"); err != nil {
|
|
|
|
t.Fatal(fmt.Errorf("First line after SEE ALSO wasn't break-indent: %s", err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := AssertNextLineEquals(scanner, `\fBtop\-bbb(1)\fP, \fBtop\-ccc(1)\fP`); err != nil {
|
|
|
|
t.Fatal(fmt.Errorf("Second line after SEE ALSO wasn't correct: %s", err.Error()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func AssertLineFound(scanner *bufio.Scanner, expectedLine string) error {
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if line == expectedLine {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return fmt.Errorf("AssertLineFound: scan failed: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("AssertLineFound: hit EOF before finding %#v", expectedLine)
|
|
|
|
}
|
|
|
|
|
|
|
|
func AssertNextLineEquals(scanner *bufio.Scanner, expectedLine string) error {
|
|
|
|
if scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if line == expectedLine {
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-31 16:53:34 +03:00
|
|
|
return fmt.Errorf("AssertNextLineEquals: got %#v, not %#v", line, expectedLine)
|
2016-01-21 04:06:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return fmt.Errorf("AssertNextLineEquals: scan failed: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("AssertNextLineEquals: hit EOF before finding %#v", expectedLine)
|
|
|
|
}
|