Skip to content
Myr

md_docs_test.go

This file

Path

doc/md_docs_test.go

112 lines · 3.0 KiB · Go

Last changed by

Revert "Add the ability to specify a filePostpender in GenMarkdownTreeCustom (#1270)" (#1317)John McBridebefore adoptionff416ad

History of this file

Take it

1package doc23import (4	"bytes"5	"io/ioutil"6	"os"7	"path/filepath"8	"testing"910	"github.com/spf13/cobra"11)1213func TestGenMdDoc(t *testing.T) {14	// We generate on subcommand so we have both subcommands and parents.15	buf := new(bytes.Buffer)16	if err := GenMarkdown(echoCmd, buf); err != nil {17		t.Fatal(err)18	}19	output := buf.String()2021	checkStringContains(t, output, echoCmd.Long)22	checkStringContains(t, output, echoCmd.Example)23	checkStringContains(t, output, "boolone")24	checkStringContains(t, output, "rootflag")25	checkStringContains(t, output, rootCmd.Short)26	checkStringContains(t, output, echoSubCmd.Short)27	checkStringOmits(t, output, deprecatedCmd.Short)28	checkStringContains(t, output, "Options inherited from parent commands")29}3031func TestGenMdDocWithNoLongOrSynopsis(t *testing.T) {32	// We generate on subcommand so we have both subcommands and parents.33	buf := new(bytes.Buffer)34	if err := GenMarkdown(dummyCmd, buf); err != nil {35		t.Fatal(err)36	}37	output := buf.String()3839	checkStringContains(t, output, dummyCmd.Example)40	checkStringContains(t, output, dummyCmd.Short)41	checkStringContains(t, output, "Options inherited from parent commands")42	checkStringOmits(t, output, "### Synopsis")43}4445func TestGenMdNoHiddenParents(t *testing.T) {46	// We generate on subcommand so we have both subcommands and parents.47	for _, name := range []string{"rootflag", "strtwo"} {48		f := rootCmd.PersistentFlags().Lookup(name)49		f.Hidden = true50		defer func() { f.Hidden = false }()51	}52	buf := new(bytes.Buffer)53	if err := GenMarkdown(echoCmd, buf); err != nil {54		t.Fatal(err)55	}56	output := buf.String()5758	checkStringContains(t, output, echoCmd.Long)59	checkStringContains(t, output, echoCmd.Example)60	checkStringContains(t, output, "boolone")61	checkStringOmits(t, output, "rootflag")62	checkStringContains(t, output, rootCmd.Short)63	checkStringContains(t, output, echoSubCmd.Short)64	checkStringOmits(t, output, deprecatedCmd.Short)65	checkStringOmits(t, output, "Options inherited from parent commands")66}6768func TestGenMdNoTag(t *testing.T) {69	rootCmd.DisableAutoGenTag = true70	defer func() { rootCmd.DisableAutoGenTag = false }()7172	buf := new(bytes.Buffer)73	if err := GenMarkdown(rootCmd, buf); err != nil {74		t.Fatal(err)75	}76	output := buf.String()7778	checkStringOmits(t, output, "Auto generated")79}8081func TestGenMdTree(t *testing.T) {82	c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}83	tmpdir, err := ioutil.TempDir("", "test-gen-md-tree")84	if err != nil {85		t.Fatalf("Failed to create tmpdir: %v", err)86	}87	defer os.RemoveAll(tmpdir)8889	if err := GenMarkdownTree(c, tmpdir); err != nil {90		t.Fatalf("GenMarkdownTree failed: %v", err)91	}9293	if _, err := os.Stat(filepath.Join(tmpdir, "do.md")); err != nil {94		t.Fatalf("Expected file 'do.md' to exist")95	}96}9798func BenchmarkGenMarkdownToFile(b *testing.B) {99	file, err := ioutil.TempFile("", "")100	if err != nil {101		b.Fatal(err)102	}103	defer os.Remove(file.Name())104	defer file.Close()105106	b.ResetTimer()107	for i := 0; i < b.N; i++ {108		if err := GenMarkdown(rootCmd, file); err != nil {109			b.Fatal(err)110		}111	}112}