Skip to content
Myr

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

2 files+31 added−48 removed

modified:command_test.go+29 added-46 removed
30 unmodified lines
31func resetCommandLineFlagSet() {32 pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)33}3435func 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}4041func 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}4647func TestSingleCommand(t *testing.T) {48 var rootCmdArgs []string49 rootCmd := &Command{50 Use: "root",
360 unmodified lines
411 if err == nil {412 t.Errorf("Invalid flag should generate error")413 }414403 if !strings.Contains(err.Error(), "unknown shorthand") {404 t.Errorf("Wrong error message: %q", err.Error())405 }415 checkStringContains(t, err.Error(), "unknown shorthand")416417 if intFlagValue != 7 {418 t.Errorf("Expected flag value: %v, got %v", 7, intFlagValue)419 }
7 unmodified lines
427 if err == nil {428 t.Errorf("Invalid flag value should generate error")429 }430421 if !strings.Contains(err.Error(), "invalid syntax") {422 t.Errorf("Wrong error message: %q", err)423 }431 checkStringContains(t, err.Error(), "invalid syntax")432}433434func TestFlagBeforeCommand(t *testing.T) {435 rootCmd := &Command{Use: "root", Run: emptyRun}
302 unmodified lines
738 if err != nil {739 t.Errorf("Unexpected error: %v", err)740 }741734 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}744745func TestHelpCommandExecutedOnChild(t *testing.T) {746 rootCmd := &Command{Use: "root", Run: emptyRun}
4 unmodified lines
751 if err != nil {752 t.Errorf("Unexpected error: %v", err)753 }754749 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}757758func TestSetHelpCommand(t *testing.T) {759 c := &Command{Use: "c", Run: emptyRun}
25 unmodified lines
785 if err != nil {786 t.Errorf("Unexpected error: %v", err)787 }788785 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}791792func TestHelpFlagExecutedOnChild(t *testing.T) {793 rootCmd := &Command{Use: "root", Run: emptyRun}
4 unmodified lines
798 if err != nil {799 t.Errorf("Unexpected error: %v", err)800 }801800 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}804805// 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.809func 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)811814 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)814817 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 }819823 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}822823func TestFlagsInUsage(t *testing.T) {824 rootCmd := &Command{Use: "root", Args: NoArgs, Run: func(*Command, []string) {}}
1 unmodified line
826 if err != nil {827 t.Errorf("Unexpected error: %v", err)828 }829835 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}832833func TestHelpExecutedOnNonRunnableChild(t *testing.T) {834 rootCmd := &Command{Use: "root", Run: emptyRun}
4 unmodified lines
839 if err != nil {840 t.Errorf("Unexpected error: %v", err)841 }842851 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}845846func TestUsageIsNotPrintedTwice(t *testing.T) {847 var cmd = &Command{Use: "root"}
138 unmodified lines
986 if err != nil {987 t.Errorf("Unexpected error: %v", err)988 }9891000 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}992993func TestHooks(t *testing.T) {994 var (
239 unmodified lines
1234 c := &Command{Use: "c", Run: emptyRun}1235 c.AddCommand(&Command{Use: "child", Run: emptyRun})12361237 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)12391240 resetCommandLineFlagSet()1241}1242
147 unmodified lines
1390 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}13961397func TestTraverseWithParentFlags(t *testing.T) {1398 rootCmd := &Command{Use: "root", TraverseChildren: true}
110 unmodified lines
modified:command.go+2 added-2 removed
873 unmodified lines
874 c.RemoveCommand(c.helpCommand)875 c.AddCommand(c.helpCommand)876}877878// ResetCommands used for testing.878// ResetCommands delete parent, subcommand and help command from c.879func (c *Command) ResetCommands() {880 c.parent = nil881 c.commands = nil882 c.helpCommand = nil
387 unmodified lines
1270 }1271 return c.pflags1272}12731274// ResetFlags is used in testing.1274// ResetFlags deletes all flags from command.1275func (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