Skip to content
Myr

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

5 files+163 added−410 removed

modified:doc/cmd_test.go+49 added-108 removed
1package doc23import (4 "bytes"5 "fmt"6 "runtime"4 "strings"5 "testing"67 "github.com/spf13/cobra"8)913var flagb1, flagb2, flagb3, flagbr, flagbp bool14var flags1, flags2a, flags2b, flags3 string15var flagi1, flagi2, flagi3, flagir int10func emptyRun(*cobra.Command, []string) {}1117const strtwoParentHelp = "help message for parent flag strtwo"18const strtwoChildHelp = "help message for child flag strtwo"12func init() {13 rootCmd.PersistentFlags().StringP("rootflag", "r", "two", "")14 rootCmd.PersistentFlags().StringP("strtwo", "t", "two", "help message for parent flag strtwo")1516 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")2021 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")2425 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")2829 echoCmd.AddCommand(timesCmd, echoSubCmd, deprecatedCmd)30 rootCmd.AddCommand(printCmd, echoCmd)31}3220var cmdEcho = &cobra.Command{33var rootCmd = &cobra.Command{34 Use: "root",35 Short: "Root short description",36 Long: "Root long description",37 Run: emptyRun,38}3940var 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}4728var cmdEchoSub = &cobra.Command{48var 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}5435var cmdDeprecated = &cobra.Command{55var 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}6263var 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}6942var 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}5051var cmdPrint = &cobra.Command{70var 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}7557var cmdRootNoRun = &cobra.Command{58 Use: "cobra-test",59 Short: "The root can run its own function",60 Long: "The root description for help",61}6263var cmdRootSameName = &cobra.Command{64 Use: "print",65 Short: "Root with the same name as a subcommand",66 Long: "The root description for help",67}6869var cmdRootWithRun = &cobra.Command{70 Use: "cobra-test",71 Short: "The root can run its own function",72 Long: "The root description for help",73}7475var 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}8081var cmdVersion1 = &cobra.Command{82 Use: "version",83 Short: "Print the version number",84 Long: `First version of the version command`,85}8687var cmdVersion2 = &cobra.Command{88 Use: "version",89 Short: "Print the version number",90 Long: `Second version of the version command`,91}9293func 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}115116func 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}123124func checkStringContains(t *testing.T, found, expected string) {125 if !strings.Contains(found, expected) {126 logErr(t, found, expected)76func 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}81130func checkStringOmits(t *testing.T, found, expected string) {131 if strings.Contains(found, expected) {132 logErr(t, found, expected)82func 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}135136func logErr(t *testing.T, found, expected string) {137 out := new(bytes.Buffer)138139 _, _, 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 lines
17 return strings.Replace(in, "-", "\\-", -1)18}1920func 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)2627 out := new(bytes.Buffer)2821 header := &GenManHeader{22 Title: "Project",23 Section: "2",24 }2526 // 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()3233 // 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)3946 // Our description47 expected = translate(cmdEcho.Name())48 checkStringContains(t, found, expected)4950 // Better have our example51 expected = translate(cmdEcho.Name())52 checkStringContains(t, found, expected)5354 // A local flag55 expected = "boolone"56 checkStringContains(t, found, expected)5758 // persistent flag on parent59 expected = "rootflag"60 checkStringContains(t, found, expected)6162 // We better output info about our parent63 expected = translate(cmdRootWithRun.Name())64 checkStringContains(t, found, expected)6566 // And about subcommands67 expected = translate(cmdEchoSub.Name())68 checkStringContains(t, found, expected)6970 unexpected := translate(cmdDeprecated.Name())71 checkStringOmits(t, found, unexpected)7273 // 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}4950func 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 }()5354 header := &GenManHeader{55 Title: "Project",56 Section: "2",57 }5859 // 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()6566 unexpected := translate("#HISTORY")98 checkStringOmits(t, found, unexpected)67 checkStringOmits(t, output, unexpected)68}6970func 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)76104 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)109110 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)83116 scanner := bufio.NewScanner(out)117118 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 }121122 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 }125126 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}9495func 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")99137 out := new(bytes.Buffer)138 manPrintFlags(out, flags)100 buf := new(bytes.Buffer)101 manPrintFlags(buf, c.Flags())102103 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}109110func 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)118157 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 }122123 if _, err := os.Stat(filepath.Join(tmpdir, "do.2")); err != nil {
4 unmodified lines
128 t.Fatalf("Expected header.Title to be unmodified")129 }130}131170func AssertLineFound(scanner *bufio.Scanner, expectedLine string) error {132func assertLineFound(scanner *bufio.Scanner, expectedLine string) error {133 for scanner.Scan() {134 line := scanner.Text()135 if line == expectedLine {136 return nil137 }138 }139140 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 }143182 return fmt.Errorf("AssertLineFound: hit EOF before finding %#v", expectedLine)144 return fmt.Errorf("hit EOF before finding %v", expectedLine)145}146185func AssertNextLineEquals(scanner *bufio.Scanner, expectedLine string) error {147func 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 }155156 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 }159198 return fmt.Errorf("AssertNextLineEquals: hit EOF before finding %#v", expectedLine)160 return fmt.Errorf("hit EOF before finding %v", expectedLine)161}162163func BenchmarkGenManToFile(b *testing.B) {202 c := initializeWithRootCmd()164 file, err := ioutil.TempFile("", "")165 if err != nil {166 b.Fatal(err)167 }
1 unmodified line
169 defer file.Close()170171 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 lines
4 "bytes"5 "io/ioutil"6 "os"7 "path/filepath"8 "strings"8 "testing"910 "github.com/spf13/cobra"11)1213func 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)2021 out := new(bytes.Buffer)2223 // 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()2029 // 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 }3435 // 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 }4041 // 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 }4647 // 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 }5253 // 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 }5859 // 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 }6465 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}2930func 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 }()3334 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()3985 unexpected := "Auto generated"86 checkStringOmits(t, found, unexpected)8740 checkStringOmits(t, output, "Auto generated")41}4243func 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)50100 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 }5455 if _, err := os.Stat(filepath.Join(tmpdir, "do.md")); err != nil {56 t.Fatalf("Expected file 'do.md' to exist")57 }58}5960func BenchmarkGenMarkdownToFile(b *testing.B) {110 c := initializeWithRootCmd()61 file, err := ioutil.TempFile("", "")62 if err != nil {63 b.Fatal(err)64 }
1 unmodified line
66 defer file.Close()6768 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 lines
4 "bytes"5 "io/ioutil"6 "os"7 "path/filepath"8 "strings"8 "testing"910 "github.com/spf13/cobra"11)1213func 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)2021 out := new(bytes.Buffer)2223 // 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()2029 // 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 }3435 // 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 }4041 // 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 }4647 // 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 }5253 // 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 }5859 // 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 }6465 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}2930func 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 }()3334 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()3940 unexpected := "Auto generated"86 checkStringOmits(t, found, unexpected)8741 checkStringOmits(t, output, unexpected)42}4344func TestGenRSTTree(t *testing.T) {91 cmd := &cobra.Command{92 Use: "do [OPTIONS] arg1 arg2",93 }45 c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}4647 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)52100 if err := GenReSTTree(cmd, tmpdir); err != nil {53 if err := GenReSTTree(c, tmpdir); err != nil {54 t.Fatalf("GenReSTTree failed: %s", err.Error())55 }5657 if _, err := os.Stat(filepath.Join(tmpdir, "do.rst")); err != nil {
1 unmodified line
59 }60}6162func BenchmarkGenReSTToFile(b *testing.B) {110 c := initializeWithRootCmd()63 file, err := ioutil.TempFile("", "")64 if err != nil {65 b.Fatal(err)66 }
1 unmodified line
68 defer file.Close()6970 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 lines
4 "bytes"5 "io/ioutil"6 "os"7 "path/filepath"8 "strings"8 "testing"910 "github.com/spf13/cobra"11)1213func 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)2021 out := new(bytes.Buffer)2214 // 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()2029 // 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 }3435 // 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 }4041 // 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 }4647 // 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 }5253 // 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 }5859 // 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 }6465 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}2829func 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 }()3233 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()3885 unexpected := "Auto generated"86 checkStringOmits(t, found, unexpected)8739 checkStringOmits(t, output, "Auto generated")40}4142func TestGenYamlTree(t *testing.T) {91 cmd := &cobra.Command{92 Use: "do [OPTIONS] arg1 arg2",93 }43 c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}4445 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)50101 if err := GenYamlTree(cmd, tmpdir); err != nil {51 if err := GenYamlTree(c, tmpdir); err != nil {52 t.Fatalf("GenYamlTree failed: %s", err.Error())53 }5455 if _, err := os.Stat(filepath.Join(tmpdir, "do.yaml")); err != nil {
1 unmodified line
57 }58}5960func BenchmarkGenYamlToFile(b *testing.B) {111 c := initializeWithRootCmd()61 file, err := ioutil.TempFile("", "")62 if err != nil {63 b.Fatal(err)64 }
1 unmodified line
66 defer file.Close()6768 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}