-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Really fix TestApp_RunAsSubCommandIncorrectUsage
An earlier commit 6564c0f made an attempt at fixing this test, but apparently it is not working correctly with Go <= 1.16, because the panic is expected, and it only happens with Go 1.17+. Rather than adding an ugly kludge checking the runtime go version, let's have two versions of the test. Once Go 1.16 is no longer supported, we can remove the go116_test.go file. Signed-off-by: Kir Kolyshkin <[email protected]>
- Loading branch information
Showing
3 changed files
with
61 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//go:build !go1.17 | ||
// +build !go1.17 | ||
|
||
package cli | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"flag" | ||
"testing" | ||
) | ||
|
||
func TestApp_RunAsSubCommandIncorrectUsage(t *testing.T) { | ||
a := App{ | ||
Flags: []Flag{ | ||
StringFlag{Name: "--foo"}, | ||
}, | ||
Writer: bytes.NewBufferString(""), | ||
} | ||
|
||
set := flag.NewFlagSet("", flag.ContinueOnError) | ||
_ = set.Parse([]string{"", "---foo"}) | ||
c := &Context{flagSet: set} | ||
|
||
err := a.RunAsSubcommand(c) | ||
|
||
expect(t, err, errors.New("bad flag syntax: ---foo")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//go:build go1.17 | ||
// +build go1.17 | ||
|
||
package cli | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"testing" | ||
) | ||
|
||
func TestApp_RunAsSubCommandIncorrectUsage(t *testing.T) { | ||
a := App{ | ||
Flags: []Flag{ | ||
StringFlag{Name: "--foo"}, | ||
}, | ||
Writer: bytes.NewBufferString(""), | ||
} | ||
|
||
set := flag.NewFlagSet("", flag.ContinueOnError) | ||
_ = set.Parse([]string{"", "---foo"}) | ||
c := &Context{flagSet: set} | ||
|
||
// Go 1.17+ panics when invalid flag is given. | ||
// Catch it here and consider the test passed. | ||
defer func() { | ||
if err := recover(); err == nil { | ||
t.Fatal("expected error, got nothing") | ||
} | ||
}() | ||
|
||
_ = a.RunAsSubcommand(c) | ||
} |