6
6
"fmt"
7
7
"io"
8
8
"os"
9
+ "reflect"
9
10
"runtime"
10
11
"strings"
11
12
"testing"
@@ -1349,7 +1350,6 @@ func TestWrap(t *testing.T) {
1349
1350
}
1350
1351
1351
1352
func TestWrappedHelp (t * testing.T ) {
1352
-
1353
1353
// Reset HelpPrinter after this test.
1354
1354
defer func (old helpPrinter ) {
1355
1355
HelpPrinter = old
@@ -1359,7 +1359,8 @@ func TestWrappedHelp(t *testing.T) {
1359
1359
app := & App {
1360
1360
Writer : output ,
1361
1361
Flags : []Flag {
1362
- & BoolFlag {Name : "foo" ,
1362
+ & BoolFlag {
1363
+ Name : "foo" ,
1363
1364
Aliases : []string {"h" },
1364
1365
Usage : "here's a really long help text line, let's see where it wraps. blah blah blah and so on." ,
1365
1366
},
@@ -1443,7 +1444,6 @@ COPYRIGHT:
1443
1444
}
1444
1445
1445
1446
func TestWrappedCommandHelp (t * testing.T ) {
1446
-
1447
1447
// Reset HelpPrinter after this test.
1448
1448
defer func (old helpPrinter ) {
1449
1449
HelpPrinter = old
@@ -1504,7 +1504,6 @@ OPTIONS:
1504
1504
}
1505
1505
1506
1506
func TestWrappedSubcommandHelp (t * testing.T ) {
1507
-
1508
1507
// Reset HelpPrinter after this test.
1509
1508
defer func (old helpPrinter ) {
1510
1509
HelpPrinter = old
@@ -1573,7 +1572,6 @@ OPTIONS:
1573
1572
}
1574
1573
1575
1574
func TestWrappedHelpSubcommand (t * testing.T ) {
1576
-
1577
1575
// Reset HelpPrinter after this test.
1578
1576
defer func (old helpPrinter ) {
1579
1577
HelpPrinter = old
@@ -1714,3 +1712,65 @@ GLOBAL OPTIONS:
1714
1712
output .String (), expected )
1715
1713
}
1716
1714
}
1715
+
1716
+ func Test_checkShellCompleteFlag (t * testing.T ) {
1717
+ t .Parallel ()
1718
+ tests := []struct {
1719
+ name string
1720
+ app * App
1721
+ arguments []string
1722
+ wantShellCompletion bool
1723
+ wantArgs []string
1724
+ }{
1725
+ {
1726
+ name : "disable bash completion" ,
1727
+ arguments : []string {"--generate-bash-completion" },
1728
+ app : & App {},
1729
+ wantShellCompletion : false ,
1730
+ wantArgs : []string {"--generate-bash-completion" },
1731
+ },
1732
+ {
1733
+ name : "--generate-bash-completion isn't used" ,
1734
+ arguments : []string {"foo" },
1735
+ app : & App {
1736
+ EnableBashCompletion : true ,
1737
+ },
1738
+ wantShellCompletion : false ,
1739
+ wantArgs : []string {"foo" },
1740
+ },
1741
+ {
1742
+ name : "arguments include double dash" ,
1743
+ arguments : []string {"--" , "foo" , "--generate-bash-completion" },
1744
+ app : & App {
1745
+ EnableBashCompletion : true ,
1746
+ },
1747
+ wantShellCompletion : false ,
1748
+ wantArgs : []string {"--" , "foo" , "--generate-bash-completion" },
1749
+ },
1750
+ {
1751
+ name : "--generate-bash-completion" ,
1752
+ arguments : []string {"foo" , "--generate-bash-completion" },
1753
+ app : & App {
1754
+ EnableBashCompletion : true ,
1755
+ },
1756
+ wantShellCompletion : true ,
1757
+ wantArgs : []string {"foo" },
1758
+ },
1759
+ }
1760
+
1761
+ for _ , tt := range tests {
1762
+ tt := tt
1763
+ t .Run (tt .name , func (t * testing.T ) {
1764
+ t .Parallel ()
1765
+ shellCompletion , args := checkShellCompleteFlag (tt .app , tt .arguments )
1766
+ if tt .wantShellCompletion != shellCompletion {
1767
+ t .Errorf ("Unexpected shell completion, got:\n %v\n expected: %v" ,
1768
+ shellCompletion , tt .wantShellCompletion )
1769
+ }
1770
+ if ! reflect .DeepEqual (tt .wantArgs , args ) {
1771
+ t .Errorf ("Unexpected arguments, got:\n %v\n expected: %v" ,
1772
+ args , tt .wantArgs )
1773
+ }
1774
+ })
1775
+ }
1776
+ }
0 commit comments