Add test for persistent bool flag on subcommand
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Jeff Lowdermilk · committed by Brendan Burns · parent 384c059 · Browse files · 2 files·+13 added−9 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+13 added−9 removed
- other:+13 added−9 removed
Provenance
- Commit
79bd93d- Tree
87ca59a- Author
Jeff Lowdermilk <jeffml@google.com>- Committer
Brendan Burns <bburns@google.com>- Parent
384c059
2 files changed+13 added−9 removed
modified:cobra_test.go+13 added-7 removed
8 unmodified lines9 10 var _ = fmt.Println11 12 var tp, te, tt, t1 []string13-var flagb1, flagb2, flagb3, flagbr bool13+var flagb1, flagb2, flagb3, flagbr, flagbp bool14 var flags1, flags2a, flags2b, flags3 string15 var flagi1, flagi2, flagi3, flagir int16 var globalFlag1 bool17 var flagEcho, rootcalled bool61 unmodified lines79 cmdEcho.Flags().IntVarP(&flagi1, "intone", "i", 123, "help message for flag intone")80 cmdTimes.Flags().IntVarP(&flagi2, "inttwo", "j", 234, "help message for flag inttwo")81 cmdPrint.Flags().IntVarP(&flagi3, "intthree", "i", 345, "help message for flag intthree")82 cmdEcho.PersistentFlags().StringVarP(&flags1, "strone", "s", "one", "help message for flag strone")83+ cmdEcho.PersistentFlags().BoolVarP(&flagbp, "persistentbool", "p", false, "help message for flag persistentbool")84 cmdTimes.PersistentFlags().StringVarP(&flags2b, "strtwo", "t", "2", strtwoChildHelp)85 cmdPrint.PersistentFlags().StringVarP(&flags3, "strthree", "s", "three", "help message for flag strthree")86 cmdEcho.Flags().BoolVarP(&flagb1, "boolone", "b", true, "help message for flag boolone")87 cmdTimes.Flags().BoolVarP(&flagb2, "booltwo", "c", false, "help message for flag booltwo")329 unmodified lines417 }418 }419 420 func TestPersistentFlags(t *testing.T) {420- fullSetupTest("echo -s something more here")421+ fullSetupTest("echo -s something -p more here")422 423 // persistentFlag should act like normal flag on it's own command424 if strings.Join(te, " ") != "more here" {425 t.Errorf("flags didn't leave proper args remaining..%s given", te)426 }426-427- // persistentFlag should act like normal flag on it's own command427 if flags1 != "something" {428 t.Errorf("string flag didn't get correct value, had %v", flags1)429 }430+ if !flagbp {431+ t.Errorf("persistent bool flag not parsed correctly. Expected true, had %v", flagbp)432+ }433 434+ // persistentFlag should act like normal flag on it's own command432- fullSetupTest("echo times -s again -c test here")435+ fullSetupTest("echo times -s again -c -p test here")436 437 if strings.Join(tt, " ") != "test here" {438 t.Errorf("flags didn't leave proper args remaining..%s given", tt)439 }1 unmodified line441 if flags1 != "again" {442 t.Errorf("string flag didn't get correct value, had %v", flags1)443 }444 442- if flagb2 != true {443- t.Errorf("local flag not parsed correctly. Expected false, had %v", flagb2)445+ if !flagb2 {446+ t.Errorf("local flag not parsed correctly. Expected true, had %v", flagb2)447 }448+ if !flagbp {449+ t.Errorf("persistent bool flag not parsed correctly. Expected true, had %v", flagbp)450+ }451 }452 453 func TestHelpCommand(t *testing.T) {454 c := fullSetupTest("help echo")155 unmodified linesmodified:command_test.go-2 removed
3 unmodified lines4 "reflect"5 "testing"6 )7 8-// TODO Test persistent flags on subcommands. This requires testing the full execute function.9-8 func TestStripFlags(t *testing.T) {9 tests := []struct {10 input []string11 output []string79 unmodified lines