Skip to content
Myr

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

2 files+13 added−9 removed

modified:cobra_test.go+13 added-7 removed
8 unmodified lines
910var _ = fmt.Println1112var tp, te, tt, t1 []string13var flagb1, flagb2, flagb3, flagbr bool13var flagb1, flagb2, flagb3, flagbr, flagbp bool14var flags1, flags2a, flags2b, flags3 string15var flagi1, flagi2, flagi3, flagir int16var globalFlag1 bool17var flagEcho, rootcalled bool
61 unmodified lines
79 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 lines
417 }418}419420func TestPersistentFlags(t *testing.T) {420 fullSetupTest("echo -s something more here")421 fullSetupTest("echo -s something -p more here")422423 // 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 }426427 // 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 }433434 // 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")436437 if strings.Join(tt, " ") != "test here" {438 t.Errorf("flags didn't leave proper args remaining..%s given", tt)439 }
1 unmodified line
441 if flags1 != "again" {442 t.Errorf("string flag didn't get correct value, had %v", flags1)443 }444442 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}452453func TestHelpCommand(t *testing.T) {454 c := fullSetupTest("help echo")
155 unmodified lines
modified:command_test.go-2 removed
3 unmodified lines
4 "reflect"5 "testing"6)78// TODO Test persistent flags on subcommands. This requires testing the full execute function.98func TestStripFlags(t *testing.T) {9 tests := []struct {10 input []string11 output []string
79 unmodified lines