Add test for persistent bool flag on subcommand

This commit is contained in:
Jeff Lowdermilk 2015-03-18 14:42:38 -07:00 committed by Brendan Burns
parent 384c059f4b
commit 79bd93d369
2 changed files with 13 additions and 9 deletions

View File

@ -10,7 +10,7 @@ import (
var _ = fmt.Println var _ = fmt.Println
var tp, te, tt, t1 []string var tp, te, tt, t1 []string
var flagb1, flagb2, flagb3, flagbr bool var flagb1, flagb2, flagb3, flagbr, flagbp bool
var flags1, flags2a, flags2b, flags3 string var flags1, flags2a, flags2b, flags3 string
var flagi1, flagi2, flagi3, flagir int var flagi1, flagi2, flagi3, flagir int
var globalFlag1 bool var globalFlag1 bool
@ -80,6 +80,7 @@ func flagInit() {
cmdTimes.Flags().IntVarP(&flagi2, "inttwo", "j", 234, "help message for flag inttwo") cmdTimes.Flags().IntVarP(&flagi2, "inttwo", "j", 234, "help message for flag inttwo")
cmdPrint.Flags().IntVarP(&flagi3, "intthree", "i", 345, "help message for flag intthree") cmdPrint.Flags().IntVarP(&flagi3, "intthree", "i", 345, "help message for flag intthree")
cmdEcho.PersistentFlags().StringVarP(&flags1, "strone", "s", "one", "help message for flag strone") cmdEcho.PersistentFlags().StringVarP(&flags1, "strone", "s", "one", "help message for flag strone")
cmdEcho.PersistentFlags().BoolVarP(&flagbp, "persistentbool", "p", false, "help message for flag persistentbool")
cmdTimes.PersistentFlags().StringVarP(&flags2b, "strtwo", "t", "2", strtwoChildHelp) cmdTimes.PersistentFlags().StringVarP(&flags2b, "strtwo", "t", "2", strtwoChildHelp)
cmdPrint.PersistentFlags().StringVarP(&flags3, "strthree", "s", "three", "help message for flag strthree") cmdPrint.PersistentFlags().StringVarP(&flags3, "strthree", "s", "three", "help message for flag strthree")
cmdEcho.Flags().BoolVarP(&flagb1, "boolone", "b", true, "help message for flag boolone") cmdEcho.Flags().BoolVarP(&flagb1, "boolone", "b", true, "help message for flag boolone")
@ -417,19 +418,21 @@ func TestTrailingCommandFlags(t *testing.T) {
} }
func TestPersistentFlags(t *testing.T) { func TestPersistentFlags(t *testing.T) {
fullSetupTest("echo -s something more here") fullSetupTest("echo -s something -p more here")
// persistentFlag should act like normal flag on it's own command // persistentFlag should act like normal flag on it's own command
if strings.Join(te, " ") != "more here" { if strings.Join(te, " ") != "more here" {
t.Errorf("flags didn't leave proper args remaining..%s given", te) t.Errorf("flags didn't leave proper args remaining..%s given", te)
} }
// persistentFlag should act like normal flag on it's own command
if flags1 != "something" { if flags1 != "something" {
t.Errorf("string flag didn't get correct value, had %v", flags1) t.Errorf("string flag didn't get correct value, had %v", flags1)
} }
if !flagbp {
t.Errorf("persistent bool flag not parsed correctly. Expected true, had %v", flagbp)
}
fullSetupTest("echo times -s again -c test here") // persistentFlag should act like normal flag on it's own command
fullSetupTest("echo times -s again -c -p test here")
if strings.Join(tt, " ") != "test here" { if strings.Join(tt, " ") != "test here" {
t.Errorf("flags didn't leave proper args remaining..%s given", tt) t.Errorf("flags didn't leave proper args remaining..%s given", tt)
@ -439,8 +442,11 @@ func TestPersistentFlags(t *testing.T) {
t.Errorf("string flag didn't get correct value, had %v", flags1) t.Errorf("string flag didn't get correct value, had %v", flags1)
} }
if flagb2 != true { if !flagb2 {
t.Errorf("local flag not parsed correctly. Expected false, had %v", flagb2) t.Errorf("local flag not parsed correctly. Expected true, had %v", flagb2)
}
if !flagbp {
t.Errorf("persistent bool flag not parsed correctly. Expected true, had %v", flagbp)
} }
} }

View File

@ -5,8 +5,6 @@ import (
"testing" "testing"
) )
// TODO Test persistent flags on subcommands. This requires testing the full execute function.
func TestStripFlags(t *testing.T) { func TestStripFlags(t *testing.T) {
tests := []struct { tests := []struct {
input []string input []string