Skip to content

Commit 7ff69f0

Browse files
committed
Improve test coverage
1 parent 987fbca commit 7ff69f0

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

args_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ import (
88
"github.com/stretchr/testify/require"
99
)
1010

11+
func TestArgNotSet(t *testing.T) {
12+
arg := &StringArg{
13+
Name: "sa",
14+
Value: "foo",
15+
}
16+
17+
require.Equal(t, "foo", arg.Get())
18+
}
19+
1120
func TestArgsFloatTypes(t *testing.T) {
1221
cmd := buildMinimalTestCommand()
1322
var fval float64

command.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -334,14 +334,10 @@ func (cmd *Command) handleExitCoder(ctx context.Context, err error) error {
334334
}
335335

336336
func (cmd *Command) argsWithDefaultCommand(oldArgs Args) Args {
337-
if cmd.DefaultCommand != "" {
338-
rawArgs := append([]string{cmd.DefaultCommand}, oldArgs.Slice()...)
339-
newArgs := &stringSliceArgs{v: rawArgs}
337+
rawArgs := append([]string{cmd.DefaultCommand}, oldArgs.Slice()...)
338+
newArgs := &stringSliceArgs{v: rawArgs}
340339

341-
return newArgs
342-
}
343-
344-
return oldArgs
340+
return newArgs
345341
}
346342

347343
// Root returns the Command at the root of the graph

flag.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,6 @@ func (f FlagsByName) Len() int {
7777
}
7878

7979
func (f FlagsByName) Less(i, j int) bool {
80-
if len(f[j].Names()) == 0 {
81-
return false
82-
} else if len(f[i].Names()) == 0 {
83-
return true
84-
}
8580
return lexicographicLess(f[i].Names()[0], f[j].Names()[0])
8681
}
8782

flag_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,6 +1749,29 @@ func TestFlagActionFromEnv(t *testing.T) {
17491749
assert.Equal(t, x, 42)
17501750
}
17511751

1752+
func TestParseShortOptionBoolError(t *testing.T) {
1753+
cmd := buildMinimalTestCommand()
1754+
cmd.UseShortOptionHandling = true
1755+
cmd.Flags = []Flag{
1756+
&BoolFlag{Name: "debug", Aliases: []string{"d"}},
1757+
&BoolFlag{Name: "verbose", Aliases: []string{"v"}},
1758+
}
1759+
1760+
err := cmd.Run(buildTestContext(t), []string{"run", "-vd=notabool"})
1761+
assert.Error(t, err, "expected error parsing invalid bool")
1762+
}
1763+
1764+
func TestParseShortOptionIntError(t *testing.T) {
1765+
cmd := buildMinimalTestCommand()
1766+
cmd.Flags = []Flag{
1767+
&IntFlag{Name: "port", Aliases: []string{"p"}},
1768+
&BoolFlag{Name: "debug", Aliases: []string{"d"}},
1769+
}
1770+
1771+
err := cmd.Run(buildTestContext(t), []string{"run", "-dp=notanint"})
1772+
assert.Error(t, err, "expected error parsing invalid int")
1773+
}
1774+
17521775
func TestParseMultiString(t *testing.T) {
17531776
_ = (&Command{
17541777
Flags: []Flag{
@@ -3294,6 +3317,17 @@ func TestFileHint(t *testing.T) {
32943317
assert.Equal(t, "bar [/tmp/foo.txt]", withFileHint("/tmp/foo.txt", "bar"))
32953318
}
32963319

3320+
func TestHasFlags(t *testing.T) {
3321+
flagToCheck := &StringFlag{Name: "foo"}
3322+
flags := []Flag{
3323+
&StringFlag{Name: "bar"},
3324+
&Int64Flag{Name: "baz"},
3325+
flagToCheck,
3326+
}
3327+
3328+
assert.True(t, hasFlag(flags, flagToCheck))
3329+
}
3330+
32973331
func TestFlagsByName(t *testing.T) {
32983332
flags := []Flag{
32993333
&StringFlag{

0 commit comments

Comments
 (0)