Skip to content
Myr

Add tests for RemoveCommand

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Étienne Vallette d'Osia · committed by Étienne Vallette d'Osia · parent 0e7e122 · Browse files · 1 file+52 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+52 added−0 removed

  • other:+52 added−0 removed

1 file+52 added−0 removed

modified:cobra_test.go+52 added
13 unmodified lines
14var flags1, flags2, flags3 string15var flagi1, flagi2, flagi3, flagir int16var globalFlag1 bool17var flagEcho, rootcalled bool18var versionUsed int1920var cmdPrint = &Command{21 Use: "print [string to print]",22 Short: "Print anything to the screen",
42 unmodified lines
65 rootcalled = true66 },67}6869var cmdVersion1 = &Command{70 Use: "version",71 Short: "Print the version number",72 Long: `First version of the version command`,73 Run: func(cmd *Command, args []string) {74 versionUsed = 175 },76}7778var cmdVersion2 = &Command{79 Use: "version",80 Short: "Print the version number",81 Long: `Second version of the version command`,82 Run: func(cmd *Command, args []string) {83 versionUsed = 284 },85}8687func flagInit() {88 cmdEcho.ResetFlags()89 cmdPrint.ResetFlags()90 cmdTimes.ResetFlags()
8 unmodified lines
99 cmdPrint.PersistentFlags().StringVarP(&flags3, "strthree", "s", "three", "help message for flag strthree")100 cmdEcho.Flags().BoolVarP(&flagb1, "boolone", "b", true, "help message for flag boolone")101 cmdTimes.Flags().BoolVarP(&flagb2, "booltwo", "c", false, "help message for flag booltwo")102 cmdPrint.Flags().BoolVarP(&flagb3, "boolthree", "b", true, "help message for flag boolthree")103 cmdVersion1.ResetFlags()104 cmdVersion2.ResetFlags()105}106107func commandInit() {108 cmdEcho.ResetCommands()
470 unmodified lines
579 t.Errorf("Valid Input shouldn't have errors, got:\n %q", x.Error)580 }581582}583584func TestRemoveCommand(t *testing.T) {585 versionUsed = 0586 c := initializeWithRootCmd()587 c.AddCommand(cmdVersion1)588 c.RemoveCommand(cmdVersion1)589 x := fullTester(c, "version")590 if x.Error == nil {591 t.Errorf("Removed command should not have been called\n")592 return593 }594}595596func TestReplaceCommandWithRemove(t *testing.T) {597 versionUsed = 0598 c := initializeWithRootCmd()599 c.AddCommand(cmdVersion1)600 c.RemoveCommand(cmdVersion1)601 c.AddCommand(cmdVersion2)602 x := fullTester(c, "version")603 if x.Error != nil {604 t.Errorf("Valid Input shouldn't have errors, got:\n %q", x.Error)605 return606 }607 if versionUsed == 1 {608 t.Errorf("Removed command shouldn't be called\n")609 }610 if versionUsed != 2 {611 t.Errorf("Replacing command should have been called but didn't\n")612 }613}