Improve tests in doc/
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Albert Nigmatzianov · committed by Albert Nigmatzianov · parent d6a4305 · Browse files · 5 files·+163 added−410 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+163 added−410 removed
- other:+163 added−410 removed
Provenance
- Commit
2da4a54- Tree
d07b7fe- Author
Albert Nigmatzianov <albertnigma@gmail.com>- Committer
Albert Nigmatzianov <albertnigma@gmail.com>- Parent
d6a4305
5 files changed+163 added−410 removed
modified:doc/cmd_test.go+49 added-108 removed
1 package doc2 3 import (4- "bytes"5- "fmt"6- "runtime"4 "strings"5 "testing"6 7 "github.com/spf13/cobra"8 )9 13-var flagb1, flagb2, flagb3, flagbr, flagbp bool14-var flags1, flags2a, flags2b, flags3 string15-var flagi1, flagi2, flagi3, flagir int10+func emptyRun(*cobra.Command, []string) {}11 17-const strtwoParentHelp = "help message for parent flag strtwo"18-const strtwoChildHelp = "help message for child flag strtwo"12+func init() {13+ rootCmd.PersistentFlags().StringP("rootflag", "r", "two", "")14+ rootCmd.PersistentFlags().StringP("strtwo", "t", "two", "help message for parent flag strtwo")15 16+ echoCmd.PersistentFlags().StringP("strone", "s", "one", "help message for flag strone")17+ echoCmd.PersistentFlags().BoolP("persistentbool", "p", false, "help message for flag persistentbool")18+ echoCmd.Flags().IntP("intone", "i", 123, "help message for flag intone")19+ echoCmd.Flags().BoolP("boolone", "b", true, "help message for flag boolone")20+21+ timesCmd.PersistentFlags().StringP("strtwo", "t", "2", "help message for child flag strtwo")22+ timesCmd.Flags().IntP("inttwo", "j", 234, "help message for flag inttwo")23+ timesCmd.Flags().BoolP("booltwo", "c", false, "help message for flag booltwo")24+25+ printCmd.PersistentFlags().StringP("strthree", "s", "three", "help message for flag strthree")26+ printCmd.Flags().IntP("intthree", "i", 345, "help message for flag intthree")27+ printCmd.Flags().BoolP("boolthree", "b", true, "help message for flag boolthree")28+29+ echoCmd.AddCommand(timesCmd, echoSubCmd, deprecatedCmd)30+ rootCmd.AddCommand(printCmd, echoCmd)31+}32+20-var cmdEcho = &cobra.Command{33+var rootCmd = &cobra.Command{34+ Use: "root",35+ Short: "Root short description",36+ Long: "Root long description",37+ Run: emptyRun,38+}39+40+var echoCmd = &cobra.Command{41 Use: "echo [string to echo]",42 Aliases: []string{"say"},43 Short: "Echo anything to the screen",24- Long: `an utterly useless command for testing.`,44+ Long: "an utterly useless command for testing",45 Example: "Just run cobra-test echo",46 }47 28-var cmdEchoSub = &cobra.Command{48+var echoSubCmd = &cobra.Command{49 Use: "echosub [string to print]",50 Short: "second sub command for echo",31- Long: `an absolutely utterly useless command for testing gendocs!.`,32- Run: func(cmd *cobra.Command, args []string) {},51+ Long: "an absolutely utterly useless command for testing gendocs!.",52+ Run: emptyRun,53 }54 35-var cmdDeprecated = &cobra.Command{55+var timesCmd = &cobra.Command{56+ Use: "times [# times] [string to echo]",57+ SuggestFor: []string{"counts"},58+ Short: "Echo anything to the screen more times",59+ Long: `a slightly useless command for testing.`,60+ Run: emptyRun,61+}62+63+var deprecatedCmd = &cobra.Command{64 Use: "deprecated [can't do anything here]",65 Short: "A command which is deprecated",66 Long: `an absolutely utterly useless command for testing deprecation!.`,67 Deprecated: "Please use echo instead",68 }69 42-var cmdTimes = &cobra.Command{43- Use: "times [# times] [string to echo]",44- SuggestFor: []string{"counts"},45- Short: "Echo anything to the screen more times",46- Long: `a slightly useless command for testing.`,47- PersistentPreRun: func(cmd *cobra.Command, args []string) {},48- Run: func(cmd *cobra.Command, args []string) {},49-}50-51-var cmdPrint = &cobra.Command{70+var printCmd = &cobra.Command{71 Use: "print [string to print]",72 Short: "Print anything to the screen",73 Long: `an absolutely utterly useless command for testing.`,74 }75 57-var cmdRootNoRun = &cobra.Command{58- Use: "cobra-test",59- Short: "The root can run its own function",60- Long: "The root description for help",61-}62-63-var cmdRootSameName = &cobra.Command{64- Use: "print",65- Short: "Root with the same name as a subcommand",66- Long: "The root description for help",67-}68-69-var cmdRootWithRun = &cobra.Command{70- Use: "cobra-test",71- Short: "The root can run its own function",72- Long: "The root description for help",73-}74-75-var cmdSubNoRun = &cobra.Command{76- Use: "subnorun",77- Short: "A subcommand without a Run function",78- Long: "A long output about a subcommand without a Run function",79-}80-81-var cmdVersion1 = &cobra.Command{82- Use: "version",83- Short: "Print the version number",84- Long: `First version of the version command`,85-}86-87-var cmdVersion2 = &cobra.Command{88- Use: "version",89- Short: "Print the version number",90- Long: `Second version of the version command`,91-}92-93-func flagInit() {94- cmdEcho.ResetFlags()95- cmdPrint.ResetFlags()96- cmdTimes.ResetFlags()97- cmdRootNoRun.ResetFlags()98- cmdRootSameName.ResetFlags()99- cmdRootWithRun.ResetFlags()100- cmdSubNoRun.ResetFlags()101- cmdRootNoRun.PersistentFlags().StringVarP(&flags2a, "strtwo", "t", "two", strtwoParentHelp)102- cmdEcho.Flags().IntVarP(&flagi1, "intone", "i", 123, "help message for flag intone")103- cmdTimes.Flags().IntVarP(&flagi2, "inttwo", "j", 234, "help message for flag inttwo")104- cmdPrint.Flags().IntVarP(&flagi3, "intthree", "i", 345, "help message for flag intthree")105- cmdEcho.PersistentFlags().StringVarP(&flags1, "strone", "s", "one", "help message for flag strone")106- cmdEcho.PersistentFlags().BoolVarP(&flagbp, "persistentbool", "p", false, "help message for flag persistentbool")107- cmdTimes.PersistentFlags().StringVarP(&flags2b, "strtwo", "t", "2", strtwoChildHelp)108- cmdPrint.PersistentFlags().StringVarP(&flags3, "strthree", "s", "three", "help message for flag strthree")109- cmdEcho.Flags().BoolVarP(&flagb1, "boolone", "b", true, "help message for flag boolone")110- cmdTimes.Flags().BoolVarP(&flagb2, "booltwo", "c", false, "help message for flag booltwo")111- cmdPrint.Flags().BoolVarP(&flagb3, "boolthree", "b", true, "help message for flag boolthree")112- cmdVersion1.ResetFlags()113- cmdVersion2.ResetFlags()114-}115-116-func initializeWithRootCmd() *cobra.Command {117- cmdRootWithRun.ResetCommands()118- flagInit()119- cmdRootWithRun.Flags().BoolVarP(&flagbr, "boolroot", "b", false, "help message for flag boolroot")120- cmdRootWithRun.Flags().IntVarP(&flagir, "introot", "i", 321, "help message for flag introot")121- return cmdRootWithRun122-}123-124-func checkStringContains(t *testing.T, found, expected string) {125- if !strings.Contains(found, expected) {126- logErr(t, found, expected)76+func checkStringContains(t *testing.T, got, expected string) {77+ if !strings.Contains(got, expected) {78+ t.Errorf("Expected to contain: \n %v\nGot:\n %v\n", expected, got)79 }80 }81 130-func checkStringOmits(t *testing.T, found, expected string) {131- if strings.Contains(found, expected) {132- logErr(t, found, expected)82+func checkStringOmits(t *testing.T, got, expected string) {83+ if strings.Contains(got, expected) {84+ t.Errorf("Expected to not contain: \n %v\nGot: %v", expected, got)85 }86 }135-136-func logErr(t *testing.T, found, expected string) {137- out := new(bytes.Buffer)138-139- _, _, line, ok := runtime.Caller(2)140- if ok {141- fmt.Fprintf(out, "Line: %d ", line)142- }143- fmt.Fprintf(out, "Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)144- t.Errorf(out.String())145-}modified:doc/man_docs_test.go+53 added-92 removed
16 unmodified lines17 return strings.Replace(in, "-", "\\-", -1)18 }19 20 func TestGenManDoc(t *testing.T) {21- c := initializeWithRootCmd()22- // Need two commands to run the command alphabetical sort23- cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)24- c.AddCommand(cmdPrint, cmdEcho)25- cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)26-27- out := new(bytes.Buffer)28-21 header := &GenManHeader{22 Title: "Project",23 Section: "2",24 }25+26 // We generate on a subcommand so we have both subcommands and parents27+ buf := new(bytes.Buffer)34- if err := GenMan(cmdEcho, header, out); err != nil {28+ if err := GenMan(echoCmd, header, buf); err != nil {29 t.Fatal(err)30 }37- found := out.String()31+ output := buf.String()32 33 // Make sure parent has - in CommandPath() in SEE ALSO:40- parentPath := cmdEcho.Parent().CommandPath()34+ parentPath := echoCmd.Parent().CommandPath()35 dashParentPath := strings.Replace(parentPath, " ", "-", -1)36 expected := translate(dashParentPath)37 expected = expected + "(" + header.Section + ")"44- checkStringContains(t, found, expected)38+ checkStringContains(t, output, expected)39 46- // Our description47- expected = translate(cmdEcho.Name())48- checkStringContains(t, found, expected)49-50- // Better have our example51- expected = translate(cmdEcho.Name())52- checkStringContains(t, found, expected)53-54- // A local flag55- expected = "boolone"56- checkStringContains(t, found, expected)57-58- // persistent flag on parent59- expected = "rootflag"60- checkStringContains(t, found, expected)61-62- // We better output info about our parent63- expected = translate(cmdRootWithRun.Name())64- checkStringContains(t, found, expected)65-66- // And about subcommands67- expected = translate(cmdEchoSub.Name())68- checkStringContains(t, found, expected)69-70- unexpected := translate(cmdDeprecated.Name())71- checkStringOmits(t, found, unexpected)72-73- // auto generated74- expected = translate("Auto generated")75- checkStringContains(t, found, expected)40+ checkStringContains(t, output, translate(echoCmd.Name()))41+ checkStringContains(t, output, translate(echoCmd.Name()))42+ checkStringContains(t, output, "boolone")43+ checkStringContains(t, output, "rootflag")44+ checkStringContains(t, output, translate(rootCmd.Name()))45+ checkStringContains(t, output, translate(echoSubCmd.Name()))46+ checkStringOmits(t, output, translate(deprecatedCmd.Name()))47+ checkStringContains(t, output, translate("Auto generated"))48 }49 50 func TestGenManNoGenTag(t *testing.T) {79- c := initializeWithRootCmd()80- // Need two commands to run the command alphabetical sort81- cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)82- c.AddCommand(cmdPrint, cmdEcho)83- cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)84- cmdEcho.DisableAutoGenTag = true85- out := new(bytes.Buffer)51+ echoCmd.DisableAutoGenTag = true52+ defer func() { echoCmd.DisableAutoGenTag = false }()53 54 header := &GenManHeader{55 Title: "Project",56 Section: "2",57 }58+59 // We generate on a subcommand so we have both subcommands and parents60+ buf := new(bytes.Buffer)92- if err := GenMan(cmdEcho, header, out); err != nil {61+ if err := GenMan(echoCmd, header, buf); err != nil {62 t.Fatal(err)63 }95- found := out.String()64+ output := buf.String()65 66 unexpected := translate("#HISTORY")98- checkStringOmits(t, found, unexpected)67+ checkStringOmits(t, output, unexpected)68 }69 70 func TestGenManSeeAlso(t *testing.T) {102- noop := func(cmd *cobra.Command, args []string) {}71+ rootCmd := &cobra.Command{Use: "root", Run: emptyRun}72+ aCmd := &cobra.Command{Use: "aaa", Run: emptyRun, Hidden: true} // #22973+ bCmd := &cobra.Command{Use: "bbb", Run: emptyRun}74+ cCmd := &cobra.Command{Use: "ccc", Run: emptyRun}75+ rootCmd.AddCommand(aCmd, bCmd, cCmd)76 104- top := &cobra.Command{Use: "top", Run: noop}105- aaa := &cobra.Command{Use: "aaa", Run: noop, Hidden: true} // #229106- bbb := &cobra.Command{Use: "bbb", Run: noop}107- ccc := &cobra.Command{Use: "ccc", Run: noop}108- top.AddCommand(aaa, bbb, ccc)109-110- out := new(bytes.Buffer)77+ buf := new(bytes.Buffer)78 header := &GenManHeader{}112- if err := GenMan(top, header, out); err != nil {79+ if err := GenMan(rootCmd, header, buf); err != nil {80 t.Fatal(err)81 }82+ scanner := bufio.NewScanner(buf)83 116- scanner := bufio.NewScanner(out)117-118- if err := AssertLineFound(scanner, ".SH SEE ALSO"); err != nil {119- t.Fatal(fmt.Errorf("Couldn't find SEE ALSO section header: %s", err.Error()))84+ if err := assertLineFound(scanner, ".SH SEE ALSO"); err != nil {85+ t.Fatalf("Couldn't find SEE ALSO section header: %v", err)86 }121-122- if err := AssertNextLineEquals(scanner, ".PP"); err != nil {123- t.Fatal(fmt.Errorf("First line after SEE ALSO wasn't break-indent: %s", err.Error()))87+ if err := assertNextLineEquals(scanner, ".PP"); err != nil {88+ t.Fatalf("First line after SEE ALSO wasn't break-indent: %v", err)89 }125-126- if err := AssertNextLineEquals(scanner, `\fBtop\-bbb(1)\fP, \fBtop\-ccc(1)\fP`); err != nil {127- t.Fatal(fmt.Errorf("Second line after SEE ALSO wasn't correct: %s", err.Error()))90+ if err := assertNextLineEquals(scanner, `\fBroot\-bbb(1)\fP, \fBroot\-ccc(1)\fP`); err != nil {91+ t.Fatalf("Second line after SEE ALSO wasn't correct: %v", err)92 }93 }94 95 func TestManPrintFlagsHidesShortDeperecated(t *testing.T) {132- cmd := &cobra.Command{}133- flags := cmd.Flags()134- flags.StringP("foo", "f", "default", "Foo flag")135- flags.MarkShorthandDeprecated("foo", "don't use it no more")96+ c := &cobra.Command{}97+ c.Flags().StringP("foo", "f", "default", "Foo flag")98+ c.Flags().MarkShorthandDeprecated("foo", "don't use it no more")99 137- out := new(bytes.Buffer)138- manPrintFlags(out, flags)100+ buf := new(bytes.Buffer)101+ manPrintFlags(buf, c.Flags())102 103+ got := buf.String()104 expected := "**--foo**=\"default\"\n\tFoo flag\n\n"141- if out.String() != expected {142- t.Fatalf("Expected %s, but got %s", expected, out.String())105+ if got != expected {106+ t.Errorf("Expected %v, got %v", expected, got)107 }108 }109 110 func TestGenManTree(t *testing.T) {147- cmd := &cobra.Command{148- Use: "do [OPTIONS] arg1 arg2",149- }111+ c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}112 header := &GenManHeader{Section: "2"}113 tmpdir, err := ioutil.TempDir("", "test-gen-man-tree")114 if err != nil {115 t.Fatalf("Failed to create tmpdir: %s", err.Error())116 }117 defer os.RemoveAll(tmpdir)118 157- if err := GenManTree(cmd, header, tmpdir); err != nil {119+ if err := GenManTree(c, header, tmpdir); err != nil {120 t.Fatalf("GenManTree failed: %s", err.Error())121 }122 123 if _, err := os.Stat(filepath.Join(tmpdir, "do.2")); err != nil {4 unmodified lines128 t.Fatalf("Expected header.Title to be unmodified")129 }130 }131 170-func AssertLineFound(scanner *bufio.Scanner, expectedLine string) error {132+func assertLineFound(scanner *bufio.Scanner, expectedLine string) error {133 for scanner.Scan() {134 line := scanner.Text()135 if line == expectedLine {136 return nil137 }138 }139 140 if err := scanner.Err(); err != nil {179- return fmt.Errorf("AssertLineFound: scan failed: %s", err.Error())141+ return fmt.Errorf("scan failed: %s", err)142 }143 182- return fmt.Errorf("AssertLineFound: hit EOF before finding %#v", expectedLine)144+ return fmt.Errorf("hit EOF before finding %v", expectedLine)145 }146 185-func AssertNextLineEquals(scanner *bufio.Scanner, expectedLine string) error {147+func assertNextLineEquals(scanner *bufio.Scanner, expectedLine string) error {148 if scanner.Scan() {149 line := scanner.Text()150 if line == expectedLine {151 return nil152 }191- return fmt.Errorf("AssertNextLineEquals: got %#v, not %#v", line, expectedLine)153+ return fmt.Errorf("got %v, not %v", line, expectedLine)154 }155 156 if err := scanner.Err(); err != nil {195- return fmt.Errorf("AssertNextLineEquals: scan failed: %s", err.Error())157+ return fmt.Errorf("scan failed: %v", err)158 }159 198- return fmt.Errorf("AssertNextLineEquals: hit EOF before finding %#v", expectedLine)160+ return fmt.Errorf("hit EOF before finding %v", expectedLine)161 }162 163 func BenchmarkGenManToFile(b *testing.B) {202- c := initializeWithRootCmd()164 file, err := ioutil.TempFile("", "")165 if err != nil {166 b.Fatal(err)167 }1 unmodified line169 defer file.Close()170 171 b.ResetTimer()172 for i := 0; i < b.N; i++ {212- if err := GenMan(c, nil, file); err != nil {173+ if err := GenMan(rootCmd, nil, file); err != nil {174 b.Fatal(err)175 }176 }177 }modified:doc/md_docs_test.go+22 added-72 removed
3 unmodified lines4 "bytes"5 "io/ioutil"6 "os"7 "path/filepath"8- "strings"8 "testing"9 10 "github.com/spf13/cobra"11 )12 13 func TestGenMdDoc(t *testing.T) {15- c := initializeWithRootCmd()16- // Need two commands to run the command alphabetical sort17- cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)18- c.AddCommand(cmdPrint, cmdEcho)19- cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)20-21- out := new(bytes.Buffer)22-23- // We generate on s subcommand so we have both subcommands and parents24- if err := GenMarkdown(cmdEcho, out); err != nil {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 }27- found := out.String()19+ output := buf.String()20 29- // Our description30- expected := cmdEcho.Long31- if !strings.Contains(found, expected) {32- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)33- }34-35- // Better have our example36- expected = cmdEcho.Example37- if !strings.Contains(found, expected) {38- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)39- }40-41- // A local flag42- expected = "boolone"43- if !strings.Contains(found, expected) {44- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)45- }46-47- // persistent flag on parent48- expected = "rootflag"49- if !strings.Contains(found, expected) {50- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)51- }52-53- // We better output info about our parent54- expected = cmdRootWithRun.Short55- if !strings.Contains(found, expected) {56- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)57- }58-59- // And about subcommands60- expected = cmdEchoSub.Short61- if !strings.Contains(found, expected) {62- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)63- }64-65- unexpected := cmdDeprecated.Short66- if strings.Contains(found, unexpected) {67- t.Errorf("Unexpected response.\nFound: %v\nBut should not have!!\n", unexpected)68- }21+ 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 }29 30 func TestGenMdNoTag(t *testing.T) {72- c := initializeWithRootCmd()73- // Need two commands to run the command alphabetical sort74- cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)75- c.AddCommand(cmdPrint, cmdEcho)76- c.DisableAutoGenTag = true77- cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)78- out := new(bytes.Buffer)31+ rootCmd.DisableAutoGenTag = true32+ defer func() { rootCmd.DisableAutoGenTag = false }()33 34+ buf := new(bytes.Buffer)80- if err := GenMarkdown(c, out); err != nil {35+ if err := GenMarkdown(rootCmd, buf); err != nil {36 t.Fatal(err)37 }83- found := out.String()38+ output := buf.String()39 85- unexpected := "Auto generated"86- checkStringOmits(t, found, unexpected)87-40+ checkStringOmits(t, output, "Auto generated")41 }42 43 func TestGenMdTree(t *testing.T) {91- cmd := &cobra.Command{92- Use: "do [OPTIONS] arg1 arg2",93- }44+ c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}45 tmpdir, err := ioutil.TempDir("", "test-gen-md-tree")46 if err != nil {96- t.Fatalf("Failed to create tmpdir: %s", err.Error())47+ t.Fatalf("Failed to create tmpdir: %v", err)48 }49 defer os.RemoveAll(tmpdir)50 100- if err := GenMarkdownTree(cmd, tmpdir); err != nil {101- t.Fatalf("GenMarkdownTree failed: %s", err.Error())51+ if err := GenMarkdownTree(c, tmpdir); err != nil {52+ t.Fatalf("GenMarkdownTree failed: %v", err)53 }54 55 if _, err := os.Stat(filepath.Join(tmpdir, "do.md")); err != nil {56 t.Fatalf("Expected file 'do.md' to exist")57 }58 }59 60 func BenchmarkGenMarkdownToFile(b *testing.B) {110- c := initializeWithRootCmd()61 file, err := ioutil.TempFile("", "")62 if err != nil {63 b.Fatal(err)64 }1 unmodified line66 defer file.Close()67 68 b.ResetTimer()69 for i := 0; i < b.N; i++ {120- if err := GenMarkdown(c, file); err != nil {70+ if err := GenMarkdown(rootCmd, file); err != nil {71 b.Fatal(err)72 }73 }74 }modified:doc/rest_docs_test.go+21 added-69 removed
3 unmodified lines4 "bytes"5 "io/ioutil"6 "os"7 "path/filepath"8- "strings"8 "testing"9 10 "github.com/spf13/cobra"11 )12 13 func TestGenRSTDoc(t *testing.T) {15- c := initializeWithRootCmd()16- // Need two commands to run the command alphabetical sort17- cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)18- c.AddCommand(cmdPrint, cmdEcho)19- cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)20-21- out := new(bytes.Buffer)22-23- // We generate on s subcommand so we have both subcommands and parents24- if err := GenReST(cmdEcho, out); err != nil {14+ // We generate on a subcommand so we have both subcommands and parents15+ buf := new(bytes.Buffer)16+ if err := GenReST(echoCmd, buf); err != nil {17 t.Fatal(err)18 }27- found := out.String()19+ output := buf.String()20 29- // Our description30- expected := cmdEcho.Long31- if !strings.Contains(found, expected) {32- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)33- }34-35- // Better have our example36- expected = cmdEcho.Example37- if !strings.Contains(found, expected) {38- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)39- }40-41- // A local flag42- expected = "boolone"43- if !strings.Contains(found, expected) {44- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)45- }46-47- // persistent flag on parent48- expected = "rootflag"49- if !strings.Contains(found, expected) {50- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)51- }52-53- // We better output info about our parent54- expected = cmdRootWithRun.Short55- if !strings.Contains(found, expected) {56- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)57- }58-59- // And about subcommands60- expected = cmdEchoSub.Short61- if !strings.Contains(found, expected) {62- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)63- }64-65- unexpected := cmdDeprecated.Short66- if strings.Contains(found, unexpected) {67- t.Errorf("Unexpected response.\nFound: %v\nBut should not have!!\n", unexpected)68- }21+ 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 }29 30 func TestGenRSTNoTag(t *testing.T) {72- c := initializeWithRootCmd()73- // Need two commands to run the command alphabetical sort74- cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)75- c.AddCommand(cmdPrint, cmdEcho)76- c.DisableAutoGenTag = true77- cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)78- out := new(bytes.Buffer)31+ rootCmd.DisableAutoGenTag = true32+ defer func() { rootCmd.DisableAutoGenTag = false }()33 34+ buf := new(bytes.Buffer)80- if err := GenReST(c, out); err != nil {35+ if err := GenReST(rootCmd, buf); err != nil {36 t.Fatal(err)37 }83- found := out.String()38+ output := buf.String()39 40 unexpected := "Auto generated"86- checkStringOmits(t, found, unexpected)87-41+ checkStringOmits(t, output, unexpected)42 }43 44 func TestGenRSTTree(t *testing.T) {91- cmd := &cobra.Command{92- Use: "do [OPTIONS] arg1 arg2",93- }45+ c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}46+47 tmpdir, err := ioutil.TempDir("", "test-gen-rst-tree")48 if err != nil {49 t.Fatalf("Failed to create tmpdir: %s", err.Error())50 }51 defer os.RemoveAll(tmpdir)52 100- if err := GenReSTTree(cmd, tmpdir); err != nil {53+ if err := GenReSTTree(c, tmpdir); err != nil {54 t.Fatalf("GenReSTTree failed: %s", err.Error())55 }56 57 if _, err := os.Stat(filepath.Join(tmpdir, "do.rst")); err != nil {1 unmodified line59 }60 }61 62 func BenchmarkGenReSTToFile(b *testing.B) {110- c := initializeWithRootCmd()63 file, err := ioutil.TempFile("", "")64 if err != nil {65 b.Fatal(err)66 }1 unmodified line68 defer file.Close()69 70 b.ResetTimer()71 for i := 0; i < b.N; i++ {120- if err := GenReST(c, file); err != nil {72+ if err := GenReST(rootCmd, file); err != nil {73 b.Fatal(err)74 }75 }76 }modified:doc/yaml_docs_test.go+18 added-69 removed
3 unmodified lines4 "bytes"5 "io/ioutil"6 "os"7 "path/filepath"8- "strings"8 "testing"9 10 "github.com/spf13/cobra"11 )12 13 func TestGenYamlDoc(t *testing.T) {15- c := initializeWithRootCmd()16- // Need two commands to run the command alphabetical sort17- cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)18- c.AddCommand(cmdPrint, cmdEcho)19- cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)20-21- out := new(bytes.Buffer)22-14 // We generate on s subcommand so we have both subcommands and parents15+ buf := new(bytes.Buffer)24- if err := GenYaml(cmdEcho, out); err != nil {16+ if err := GenYaml(echoCmd, buf); err != nil {17 t.Fatal(err)18 }27- found := out.String()19+ output := buf.String()20 29- // Our description30- expected := cmdEcho.Long31- if !strings.Contains(found, expected) {32- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)33- }34-35- // Better have our example36- expected = cmdEcho.Example37- if !strings.Contains(found, expected) {38- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)39- }40-41- // A local flag42- expected = "boolone"43- if !strings.Contains(found, expected) {44- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)45- }46-47- // persistent flag on parent48- expected = "rootflag"49- if !strings.Contains(found, expected) {50- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)51- }52-53- // We better output info about our parent54- expected = cmdRootWithRun.Short55- if !strings.Contains(found, expected) {56- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)57- }58-59- // And about subcommands60- expected = cmdEchoSub.Short61- if !strings.Contains(found, expected) {62- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)63- }64-65- unexpected := cmdDeprecated.Short66- if strings.Contains(found, unexpected) {67- t.Errorf("Unexpected response.\nFound: %v\nBut should not have!!\n", unexpected)68- }21+ 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 }28 29 func TestGenYamlNoTag(t *testing.T) {72- c := initializeWithRootCmd()73- // Need two commands to run the command alphabetical sort74- cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)75- c.AddCommand(cmdPrint, cmdEcho)76- c.DisableAutoGenTag = true77- cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)78- out := new(bytes.Buffer)30+ rootCmd.DisableAutoGenTag = true31+ defer func() { rootCmd.DisableAutoGenTag = false }()32 33+ buf := new(bytes.Buffer)80- if err := GenYaml(c, out); err != nil {34+ if err := GenYaml(rootCmd, buf); err != nil {35 t.Fatal(err)36 }83- found := out.String()37+ output := buf.String()38 85- unexpected := "Auto generated"86- checkStringOmits(t, found, unexpected)87-39+ checkStringOmits(t, output, "Auto generated")40 }41 42 func TestGenYamlTree(t *testing.T) {91- cmd := &cobra.Command{92- Use: "do [OPTIONS] arg1 arg2",93- }43+ c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}44 45 tmpdir, err := ioutil.TempDir("", "test-gen-yaml-tree")46 if err != nil {47 t.Fatalf("Failed to create tmpdir: %s", err.Error())48 }49 defer os.RemoveAll(tmpdir)50 101- if err := GenYamlTree(cmd, tmpdir); err != nil {51+ if err := GenYamlTree(c, tmpdir); err != nil {52 t.Fatalf("GenYamlTree failed: %s", err.Error())53 }54 55 if _, err := os.Stat(filepath.Join(tmpdir, "do.yaml")); err != nil {1 unmodified line57 }58 }59 60 func BenchmarkGenYamlToFile(b *testing.B) {111- c := initializeWithRootCmd()61 file, err := ioutil.TempFile("", "")62 if err != nil {63 b.Fatal(err)64 }1 unmodified line66 defer file.Close()67 68 b.ResetTimer()69 for i := 0; i < b.N; i++ {121- if err := GenYaml(c, file); err != nil {70+ if err := GenYaml(rootCmd, file); err != nil {71 b.Fatal(err)72 }73 }74 }