Use a test table and add --help test

Signed-off-by: Nathan LeClaire <nathan.leclaire@gmail.com>
main
Nathan LeClaire 9 years ago committed by Jesse Szwedko
parent 8cd49b108c
commit 3323ab4460

@ -1,37 +1,29 @@
package cli package cli
import ( import (
"errors"
"flag" "flag"
"testing" "testing"
) )
func TestCommandDoNotIgnoreFlags(t *testing.T) { func TestCommandFlagParsing(t *testing.T) {
app := NewApp() cases := []struct {
set := flag.NewFlagSet("test", 0) testArgs []string
test := []string{"blah", "blah", "-break"} skipFlagParsing bool
set.Parse(test) expectedErr error
}{
c := NewContext(app, set, nil) {[]string{"blah", "blah", "-break"}, false, errors.New("flag provided but not defined: -break")}, // Test normal "not ignoring flags" flow
{[]string{"blah", "blah"}, true, nil}, // Test SkipFlagParsing without any args that look like flags
command := Command{ {[]string{"blah", "-break"}, true, nil}, // Test SkipFlagParsing with random flag arg
Name: "test-cmd", {[]string{"blah", "-help"}, true, nil}, // Test SkipFlagParsing with "special" help flag arg
Aliases: []string{"tc"},
Usage: "this is for testing",
Description: "testing",
Action: func(_ *Context) {},
} }
err := command.Run(c)
expect(t, err.Error(), "flag provided but not defined: -break")
}
func TestCommandIgnoreFlags(t *testing.T) { for _, c := range cases {
app := NewApp() app := NewApp()
set := flag.NewFlagSet("test", 0) set := flag.NewFlagSet("test", 0)
test := []string{"blah", "blah", "-break"} set.Parse(c.testArgs)
set.Parse(test)
c := NewContext(app, set, nil) context := NewContext(app, set, nil)
command := Command{ command := Command{
Name: "test-cmd", Name: "test-cmd",
@ -39,32 +31,13 @@ func TestCommandIgnoreFlags(t *testing.T) {
Usage: "this is for testing", Usage: "this is for testing",
Description: "testing", Description: "testing",
Action: func(_ *Context) {}, Action: func(_ *Context) {},
SkipFlagParsing: true,
} }
err := command.Run(c)
expect(t, err, nil) command.SkipFlagParsing = c.skipFlagParsing
}
// Fix bug with ignoring flag parsing that would still parse the first flag
func TestCommandIgnoreFlagsIncludingFirstArgument(t *testing.T) {
app := NewApp()
set := flag.NewFlagSet("test", 0)
test := []string{"blah", "-break"}
set.Parse(test)
c := NewContext(app, set, nil) err := command.Run(context)
command := Command{ expect(t, err, c.expectedErr)
Name: "test-cmd", expect(t, []string(context.Args()), c.testArgs)
Aliases: []string{"tc"},
Usage: "this is for testing",
Description: "testing",
Action: func(_ *Context) {},
SkipFlagParsing: true,
} }
err := command.Run(c)
expect(t, err, nil)
expect(t, []string(c.Args()), []string{"blah", "-break"})
} }

Loading…
Cancel
Save