Edit ResetFlags and ResetCommands descriptions
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 65c8acb · Browse files · 2 files·+31 added−48 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+31 added−48 removed
- other:+31 added−48 removed
Provenance
- Commit
d6a4305- Tree
31c69a7- Author
Albert Nigmatzianov <albertnigma@gmail.com>- Committer
Albert Nigmatzianov <albertnigma@gmail.com>- Parent
65c8acb
2 files changed+31 added−48 removed
modified:command_test.go+29 added-46 removed
30 unmodified lines31 func resetCommandLineFlagSet() {32 pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)33 }34 35+func checkStringContains(t *testing.T, got, expected string) {36+ if !strings.Contains(got, expected) {37+ t.Errorf("Expected to contain: \n %v\nGot:\n %v\n", expected, got)38+ }39+}40+41+func checkStringOmits(t *testing.T, got, expected string) {42+ if strings.Contains(got, expected) {43+ t.Errorf("Expected to not contain: \n %v\nGot: %v", expected, got)44+ }45+}46+47 func TestSingleCommand(t *testing.T) {48 var rootCmdArgs []string49 rootCmd := &Command{50 Use: "root",360 unmodified lines411 if err == nil {412 t.Errorf("Invalid flag should generate error")413 }414 403- if !strings.Contains(err.Error(), "unknown shorthand") {404- t.Errorf("Wrong error message: %q", err.Error())405- }415+ checkStringContains(t, err.Error(), "unknown shorthand")416 417 if intFlagValue != 7 {418 t.Errorf("Expected flag value: %v, got %v", 7, intFlagValue)419 }7 unmodified lines427 if err == nil {428 t.Errorf("Invalid flag value should generate error")429 }430 421- if !strings.Contains(err.Error(), "invalid syntax") {422- t.Errorf("Wrong error message: %q", err)423- }431+ checkStringContains(t, err.Error(), "invalid syntax")432 }433 434 func TestFlagBeforeCommand(t *testing.T) {435 rootCmd := &Command{Use: "root", Run: emptyRun}302 unmodified lines738 if err != nil {739 t.Errorf("Unexpected error: %v", err)740 }741 734- if !strings.Contains(output, rootCmd.Long) {735- t.Errorf("Expected to contain: %q, got: %q", rootCmd.Long, output)736- }742+ checkStringContains(t, output, rootCmd.Long)743 }744 745 func TestHelpCommandExecutedOnChild(t *testing.T) {746 rootCmd := &Command{Use: "root", Run: emptyRun}4 unmodified lines751 if err != nil {752 t.Errorf("Unexpected error: %v", err)753 }754 749- if !strings.Contains(output, childCmd.Long) {750- t.Errorf("Expected to contain: %q, got: %q", childCmd.Long, output)751- }755+ checkStringContains(t, output, childCmd.Long)756 }757 758 func TestSetHelpCommand(t *testing.T) {759 c := &Command{Use: "c", Run: emptyRun}25 unmodified lines785 if err != nil {786 t.Errorf("Unexpected error: %v", err)787 }788 785- if !strings.Contains(output, rootCmd.Long) {786- t.Errorf("Expected to contain: %q, got: %q", rootCmd.Long, output)787- }789+ checkStringContains(t, output, rootCmd.Long)790 }791 792 func TestHelpFlagExecutedOnChild(t *testing.T) {793 rootCmd := &Command{Use: "root", Run: emptyRun}4 unmodified lines798 if err != nil {799 t.Errorf("Unexpected error: %v", err)800 }801 800- if !strings.Contains(output, childCmd.Long) {801- t.Errorf("Expected to contain: %q, got: %q", childCmd.Long, output)802- }802+ checkStringContains(t, output, childCmd.Long)803 }804 805 // TestHelpFlagInHelp checks,806 // if '--help' flag is shown in help for child (executing `parent help child`),807 // that has no other flags.808 // Related to https://github.com/spf13/cobra/issues/302.809 func TestHelpFlagInHelp(t *testing.T) {810- output := new(bytes.Buffer)811- parent := &Command{Use: "parent", Run: func(*Command, []string) {}}810+ parentCmd := &Command{Use: "parent", Run: func(*Command, []string) {}}812- parent.SetOutput(output)811 814- child := &Command{Use: "child", Run: func(*Command, []string) {}}815- parent.AddCommand(child)812+ childCmd := &Command{Use: "child", Run: func(*Command, []string) {}}813+ parentCmd.AddCommand(childCmd)814 817- parent.SetArgs([]string{"help", "child"})818- err := parent.Execute()815+ output, err := executeCommand(parentCmd, "help", "child")816 if err != nil {820- t.Fatal(err)817+ t.Errorf("Unexpected error: %v", err)818 }819 823- if !strings.Contains(output.String(), "[flags]") {824- t.Errorf("\nExpecting to contain: %v\nGot: %v", "[flags]", output.String())825- }820+ checkStringContains(t, output, "[flags]")821 }822 823 func TestFlagsInUsage(t *testing.T) {824 rootCmd := &Command{Use: "root", Args: NoArgs, Run: func(*Command, []string) {}}1 unmodified line826 if err != nil {827 t.Errorf("Unexpected error: %v", err)828 }829 835- expected := "[flags]"836- if !strings.Contains(output, expected) {837- t.Errorf("Expected to contain %q\ngot: %v", expected, output)838- }830+ checkStringContains(t, output, "[flags]")831 }832 833 func TestHelpExecutedOnNonRunnableChild(t *testing.T) {834 rootCmd := &Command{Use: "root", Run: emptyRun}4 unmodified lines839 if err != nil {840 t.Errorf("Unexpected error: %v", err)841 }842 851- if !strings.Contains(output, childCmd.Long) {852- t.Errorf("Expected to contain: %q, got: %q", childCmd.Long, output)853- }843+ checkStringContains(t, output, childCmd.Long)844 }845 846 func TestUsageIsNotPrintedTwice(t *testing.T) {847 var cmd = &Command{Use: "root"}138 unmodified lines986 if err != nil {987 t.Errorf("Unexpected error: %v", err)988 }989 1000- expected := deprecatedCmd.Deprecated1001- if !strings.Contains(output, expected) {1002- t.Errorf("Expected to contain: %q\nGot:%q", expected, output)1003- }990+ checkStringContains(t, output, deprecatedCmd.Deprecated)991 }992 993 func TestHooks(t *testing.T) {994 var (239 unmodified lines1234 c := &Command{Use: "c", Run: emptyRun}1235 c.AddCommand(&Command{Use: "child", Run: emptyRun})1236 1237 output, _ := executeCommand(c, "--help")1251- if !strings.Contains(output, flagName) {1252- t.Errorf("Expected to contain: %q\nGot: %v", flagName, output)1253- }1238+ checkStringContains(t, output, flagName)1239 1240 resetCommandLineFlagSet()1241 }1242 147 unmodified lines1390 output, err := executeCommand(c, "c", "-d")1391 if err != nil {1392 t.Error("Unexpected error:", err)1393 }1409- if !strings.Contains(output, "This flag is deprecated") {1410- t.Errorf("Expected to contain deprecated message, but got %q", output)1411- }1394+ checkStringContains(t, output, "This flag is deprecated")1395 }1396 1397 func TestTraverseWithParentFlags(t *testing.T) {1398 rootCmd := &Command{Use: "root", TraverseChildren: true}110 unmodified linesmodified:command.go+2 added-2 removed
873 unmodified lines874 c.RemoveCommand(c.helpCommand)875 c.AddCommand(c.helpCommand)876 }877 878-// ResetCommands used for testing.878+// ResetCommands delete parent, subcommand and help command from c.879 func (c *Command) ResetCommands() {880 c.parent = nil881 c.commands = nil882 c.helpCommand = nil387 unmodified lines1270 }1271 return c.pflags1272 }1273 1274-// ResetFlags is used in testing.1274+// ResetFlags deletes all flags from command.1275 func (c *Command) ResetFlags() {1276 c.flagErrorBuf = new(bytes.Buffer)1277 c.flagErrorBuf.Reset()1278 c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)129 unmodified lines