@@ -558,6 +558,86 @@ func TestApp_RunDefaultCommandWithSubCommand(t *testing.T) {
558
558
}
559
559
}
560
560
561
+ var defaultCommandFlagAppTests = []struct {
562
+ cmdName string
563
+ flag string
564
+ defaultCmd string
565
+ expected bool
566
+ }{
567
+ {"foobar" , "" , "foobar" , true },
568
+ {"foobar" , "-c derp" , "foobar" , true },
569
+ {"batbaz" , "" , "foobar" , true },
570
+ {"b" , "" , "" , true },
571
+ {"f" , "" , "" , true },
572
+ {"" , "" , "foobar" , true },
573
+ {"" , "" , "" , true },
574
+ {"" , "-j" , "foobar" , true },
575
+ {"" , "-j" , "foobar" , true },
576
+ {"" , "-c derp" , "foobar" , true },
577
+ {"" , "--carly=derp" , "foobar" , true },
578
+ {"" , "-j" , "foobar" , true },
579
+ {"" , "-j" , "" , true },
580
+ {" " , "-j" , "foobar" , false },
581
+ {"" , "" , "" , true },
582
+ {" " , "" , "" , false },
583
+ {" " , "-j" , "" , false },
584
+ {"bat" , "" , "batbaz" , false },
585
+ {"nothing" , "" , "batbaz" , false },
586
+ {"nothing" , "" , "" , false },
587
+ {"nothing" , "--jimbob" , "batbaz" , false },
588
+ {"nothing" , "--carly" , "" , false },
589
+ }
590
+
591
+ func TestApp_RunDefaultCommandWithFlags (t * testing.T ) {
592
+ for _ , test := range defaultCommandFlagAppTests {
593
+ testTitle := fmt .Sprintf ("command=%[1]s-flag=%[2]s-default=%[3]s" , test .cmdName , test .flag , test .defaultCmd )
594
+ t .Run (testTitle , func (t * testing.T ) {
595
+ app := & App {
596
+ DefaultCommand : test .defaultCmd ,
597
+ Flags : []Flag {
598
+ & StringFlag {
599
+ Name : "carly" ,
600
+ Aliases : []string {"c" },
601
+ Required : false ,
602
+ },
603
+ & BoolFlag {
604
+ Name : "jimbob" ,
605
+ Aliases : []string {"j" },
606
+ Required : false ,
607
+ Value : true ,
608
+ },
609
+ },
610
+ Commands : []* Command {
611
+ {
612
+ Name : "foobar" ,
613
+ Aliases : []string {"f" },
614
+ },
615
+ {Name : "batbaz" , Aliases : []string {"b" }},
616
+ },
617
+ }
618
+
619
+ appArgs := []string {"c" }
620
+
621
+ if test .flag != "" {
622
+ flags := strings .Split (test .flag , " " )
623
+ if len (flags ) > 1 {
624
+ appArgs = append (appArgs , flags ... )
625
+ }
626
+
627
+ flags = strings .Split (test .flag , "=" )
628
+ if len (flags ) > 1 {
629
+ appArgs = append (appArgs , flags ... )
630
+ }
631
+ }
632
+
633
+ appArgs = append (appArgs , test .cmdName )
634
+
635
+ err := app .Run (appArgs )
636
+ expect (t , err == nil , test .expected )
637
+ })
638
+ }
639
+ }
640
+
561
641
func TestApp_Setup_defaultsReader (t * testing.T ) {
562
642
app := & App {}
563
643
app .Setup ()
0 commit comments