Skip to content

Commit

Permalink
Use a test table and add --help test
Browse files Browse the repository at this point in the history
Signed-off-by: Nathan LeClaire <[email protected]>
  • Loading branch information
nathanleclaire authored and jszwedko committed Oct 30, 2015
1 parent 8cd49b1 commit 3323ab4
Showing 1 changed file with 27 additions and 54 deletions.
81 changes: 27 additions & 54 deletions command_test.go
Original file line number Diff line number Diff line change
@@ -1,70 +1,43 @@
package cli

import (
"errors"
"flag"
"testing"
)

func TestCommandDoNotIgnoreFlags(t *testing.T) {
app := NewApp()
set := flag.NewFlagSet("test", 0)
test := []string{"blah", "blah", "-break"}
set.Parse(test)

c := NewContext(app, set, nil)

command := Command{
Name: "test-cmd",
Aliases: []string{"tc"},
Usage: "this is for testing",
Description: "testing",
Action: func(_ *Context) {},
func TestCommandFlagParsing(t *testing.T) {
cases := []struct {
testArgs []string
skipFlagParsing bool
expectedErr error
}{
{[]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
{[]string{"blah", "-break"}, true, nil}, // Test SkipFlagParsing with random flag arg
{[]string{"blah", "-help"}, true, nil}, // Test SkipFlagParsing with "special" help flag arg
}
err := command.Run(c)

expect(t, err.Error(), "flag provided but not defined: -break")
}

func TestCommandIgnoreFlags(t *testing.T) {
app := NewApp()
set := flag.NewFlagSet("test", 0)
test := []string{"blah", "blah", "-break"}
set.Parse(test)
for _, c := range cases {
app := NewApp()
set := flag.NewFlagSet("test", 0)
set.Parse(c.testArgs)

c := NewContext(app, set, nil)
context := NewContext(app, set, nil)

command := Command{
Name: "test-cmd",
Aliases: []string{"tc"},
Usage: "this is for testing",
Description: "testing",
Action: func(_ *Context) {},
SkipFlagParsing: true,
}
err := command.Run(c)

expect(t, err, nil)
}
command := Command{
Name: "test-cmd",
Aliases: []string{"tc"},
Usage: "this is for testing",
Description: "testing",
Action: func(_ *Context) {},
}

// 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)
command.SkipFlagParsing = c.skipFlagParsing

c := NewContext(app, set, nil)
err := command.Run(context)

command := Command{
Name: "test-cmd",
Aliases: []string{"tc"},
Usage: "this is for testing",
Description: "testing",
Action: func(_ *Context) {},
SkipFlagParsing: true,
expect(t, err, c.expectedErr)
expect(t, []string(context.Args()), c.testArgs)
}
err := command.Run(c)
expect(t, err, nil)

expect(t, []string(c.Args()), []string{"blah", "-break"})
}

0 comments on commit 3323ab4

Please sign in to comment.