66 "fmt"
77 "io"
88 "os"
9+ "reflect"
910 "runtime"
1011 "strings"
1112 "testing"
@@ -1349,7 +1350,6 @@ func TestWrap(t *testing.T) {
13491350}
13501351
13511352func TestWrappedHelp (t * testing.T ) {
1352-
13531353 // Reset HelpPrinter after this test.
13541354 defer func (old helpPrinter ) {
13551355 HelpPrinter = old
@@ -1359,7 +1359,8 @@ func TestWrappedHelp(t *testing.T) {
13591359 app := & App {
13601360 Writer : output ,
13611361 Flags : []Flag {
1362- & BoolFlag {Name : "foo" ,
1362+ & BoolFlag {
1363+ Name : "foo" ,
13631364 Aliases : []string {"h" },
13641365 Usage : "here's a really long help text line, let's see where it wraps. blah blah blah and so on." ,
13651366 },
@@ -1443,7 +1444,6 @@ COPYRIGHT:
14431444}
14441445
14451446func TestWrappedCommandHelp (t * testing.T ) {
1446-
14471447 // Reset HelpPrinter after this test.
14481448 defer func (old helpPrinter ) {
14491449 HelpPrinter = old
@@ -1504,7 +1504,6 @@ OPTIONS:
15041504}
15051505
15061506func TestWrappedSubcommandHelp (t * testing.T ) {
1507-
15081507 // Reset HelpPrinter after this test.
15091508 defer func (old helpPrinter ) {
15101509 HelpPrinter = old
@@ -1573,7 +1572,6 @@ OPTIONS:
15731572}
15741573
15751574func TestWrappedHelpSubcommand (t * testing.T ) {
1576-
15771575 // Reset HelpPrinter after this test.
15781576 defer func (old helpPrinter ) {
15791577 HelpPrinter = old
@@ -1714,3 +1712,65 @@ GLOBAL OPTIONS:
17141712 output .String (), expected )
17151713 }
17161714}
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