Added tests for generating additional help topics

This commit is contained in:
Mark Scannell 2019-10-05 16:41:14 +01:00
parent ace5fe16e9
commit d703b98665
2 changed files with 43 additions and 0 deletions

View file

@ -28,6 +28,7 @@ func init() {
echoCmd.AddCommand(timesCmd, echoSubCmd, deprecatedCmd) echoCmd.AddCommand(timesCmd, echoSubCmd, deprecatedCmd)
rootCmd.AddCommand(printCmd, echoCmd) rootCmd.AddCommand(printCmd, echoCmd)
rootCmd.AddCommand(helpDoc)
} }
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
@ -73,6 +74,19 @@ var printCmd = &cobra.Command{
Long: `an absolutely utterly useless command for testing.`, Long: `an absolutely utterly useless command for testing.`,
} }
var helpText = `Long text describing the subject.
And more and more text.
And more and more text.
`
var helpDoc = &cobra.Command{
Use: "subject",
Short: "Help on subject",
Long: helpText,
}
func checkStringContains(t *testing.T, got, expected string) { func checkStringContains(t *testing.T, got, expected string) {
if !strings.Contains(got, expected) { if !strings.Contains(got, expected) {
t.Errorf("Expected to contain: \n %v\nGot:\n %v\n", expected, got) t.Errorf("Expected to contain: \n %v\nGot:\n %v\n", expected, got)

View file

@ -81,6 +81,35 @@ func TestGenMdTree(t *testing.T) {
} }
} }
func TestGenTreeWithAdditionalHelpTopics(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "test-gen-md-tree")
if err != nil {
t.Fatalf("Failed to create tmpdir: %v", err)
}
defer os.RemoveAll(tmpdir)
if err := GenMarkdownTree(rootCmd, tmpdir); err != nil {
t.Fatal(err)
}
filesToTest := []string{"root.md", "root_print.md", "root_subject.md"}
for _, f := range filesToTest {
if _, err := os.Stat(filepath.Join(tmpdir, f)); err != nil {
t.Fatalf("Expected file '%s' to exist", f)
}
}
}
func TestGenMdWithAdditionalHelpTopics(t *testing.T) {
buf := new(bytes.Buffer)
if err := GenMarkdown(rootCmd, buf); err != nil {
t.Fatal(err)
}
output := buf.String()
checkStringContains(t, output, "Help on subject")
}
func BenchmarkGenMarkdownToFile(b *testing.B) { func BenchmarkGenMarkdownToFile(b *testing.B) {
file, err := ioutil.TempFile("", "") file, err := ioutil.TempFile("", "")
if err != nil { if err != nil {