1package doc 2 3import ( 4 "bytes" 5 "fmt" 6 "runtime" 7 "strings" 8 "testing" 9 10 "github.com/spf13/cobra" 11) 12 13var flagb1, flagb2, flagb3, flagbr, flagbp bool 14var flags1, flags2a, flags2b, flags3 string 15var flagi1, flagi2, flagi3, flagir int 16 17const strtwoParentHelp = "help message for parent flag strtwo" 18const strtwoChildHelp = "help message for child flag strtwo" 19 20var cmdEcho = &cobra.Command{ 21 Use: "echo [string to echo]", 22 Aliases: []string{"say"}, 23 Short: "Echo anything to the screen", 24 Long: `an utterly useless command for testing.`, 25 Example: "Just run cobra-test echo", 26} 27 28var cmdEchoSub = &cobra.Command{ 29 Use: "echosub [string to print]", 30 Short: "second sub command for echo", 31 Long: `an absolutely utterly useless command for testing gendocs!.`, 32 Run: func(cmd *cobra.Command, args []string) {}, 33} 34 35var cmdDeprecated = &cobra.Command{ 36 Use: "deprecated [can't do anything here]", 37 Short: "A command which is deprecated", 38 Long: `an absolutely utterly useless command for testing deprecation!.`, 39 Deprecated: "Please use echo instead", 40} 41 42var 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 51var cmdPrint = &cobra.Command{ 52 Use: "print [string to print]", 53 Short: "Print anything to the screen", 54 Long: `an absolutely utterly useless command for testing.`, 55} 56 57var 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 63var 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 69var 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 75var 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 81var cmdVersion1 = &cobra.Command{ 82 Use: "version", 83 Short: "Print the version number", 84 Long: `First version of the version command`, 85} 86 87var cmdVersion2 = &cobra.Command{ 88 Use: "version", 89 Short: "Print the version number", 90 Long: `Second version of the version command`, 91} 92 93func 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 116func 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 cmdRootWithRun 122} 123 124func checkStringContains(t *testing.T, found, expected string) { 125 if !strings.Contains(found, expected) { 126 logErr(t, found, expected) 127 } 128} 129 130func checkStringOmits(t *testing.T, found, expected string) { 131 if strings.Contains(found, expected) { 132 logErr(t, found, expected) 133 } 134} 135 136func 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} 146