Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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) | ||
} |