Merge branch 'develop' of https://github.com/txgruppi/cli into txgruppi-develop
This commit is contained in:
150
app_test.go
150
app_test.go
@@ -22,8 +22,9 @@ func ExampleApp_Run() {
|
||||
app.Flags = []Flag{
|
||||
StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
|
||||
}
|
||||
app.Action = func(c *Context) {
|
||||
app.Action = func(c *Context) int {
|
||||
fmt.Printf("Hello %v\n", c.String("name"))
|
||||
return 0
|
||||
}
|
||||
app.UsageText = "app [first_arg] [second_arg]"
|
||||
app.Author = "Harrison"
|
||||
@@ -58,8 +59,9 @@ func ExampleApp_Run_subcommand() {
|
||||
Usage: "Name of the person to greet",
|
||||
},
|
||||
},
|
||||
Action: func(c *Context) {
|
||||
Action: func(c *Context) int {
|
||||
fmt.Println("Hello,", c.String("name"))
|
||||
return 0
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -86,8 +88,9 @@ func ExampleApp_Run_help() {
|
||||
Aliases: []string{"d"},
|
||||
Usage: "use it to see a description",
|
||||
Description: "This is how we describe describeit the function",
|
||||
Action: func(c *Context) {
|
||||
Action: func(c *Context) int {
|
||||
fmt.Printf("i like to describe things")
|
||||
return 0
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -116,15 +119,17 @@ func ExampleApp_Run_bashComplete() {
|
||||
Aliases: []string{"d"},
|
||||
Usage: "use it to see a description",
|
||||
Description: "This is how we describe describeit the function",
|
||||
Action: func(c *Context) {
|
||||
Action: func(c *Context) int {
|
||||
fmt.Printf("i like to describe things")
|
||||
return 0
|
||||
},
|
||||
}, {
|
||||
Name: "next",
|
||||
Usage: "next example",
|
||||
Description: "more stuff to see when generating bash completion",
|
||||
Action: func(c *Context) {
|
||||
Action: func(c *Context) int {
|
||||
fmt.Printf("the next example")
|
||||
return 0
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -142,14 +147,17 @@ func TestApp_Run(t *testing.T) {
|
||||
s := ""
|
||||
|
||||
app := NewApp()
|
||||
app.Action = func(c *Context) {
|
||||
app.Action = func(c *Context) int {
|
||||
s = s + c.Args().First()
|
||||
return 0
|
||||
}
|
||||
|
||||
err := app.Run([]string{"command", "foo"})
|
||||
ec, err := app.Run([]string{"command", "foo"})
|
||||
expect(t, err, nil)
|
||||
err = app.Run([]string{"command", "bar"})
|
||||
expect(t, ec, 0)
|
||||
ec, err = app.Run([]string{"command", "bar"})
|
||||
expect(t, err, nil)
|
||||
expect(t, ec, 0)
|
||||
expect(t, s, "foobar")
|
||||
}
|
||||
|
||||
@@ -188,9 +196,10 @@ func TestApp_CommandWithArgBeforeFlags(t *testing.T) {
|
||||
Flags: []Flag{
|
||||
StringFlag{Name: "option", Value: "", Usage: "some option"},
|
||||
},
|
||||
Action: func(c *Context) {
|
||||
Action: func(c *Context) int {
|
||||
parsedOption = c.String("option")
|
||||
firstArg = c.Args().First()
|
||||
return 0
|
||||
},
|
||||
}
|
||||
app.Commands = []Command{command}
|
||||
@@ -208,8 +217,9 @@ func TestApp_RunAsSubcommandParseFlags(t *testing.T) {
|
||||
a.Commands = []Command{
|
||||
{
|
||||
Name: "foo",
|
||||
Action: func(c *Context) {
|
||||
Action: func(c *Context) int {
|
||||
context = c
|
||||
return 0
|
||||
},
|
||||
Flags: []Flag{
|
||||
StringFlag{
|
||||
@@ -218,7 +228,7 @@ func TestApp_RunAsSubcommandParseFlags(t *testing.T) {
|
||||
Usage: "language for the greeting",
|
||||
},
|
||||
},
|
||||
Before: func(_ *Context) error { return nil },
|
||||
Before: func(_ *Context) (int, error) { return 0, nil },
|
||||
},
|
||||
}
|
||||
a.Run([]string{"", "foo", "--lang", "spanish", "abcd"})
|
||||
@@ -237,9 +247,10 @@ func TestApp_CommandWithFlagBeforeTerminator(t *testing.T) {
|
||||
Flags: []Flag{
|
||||
StringFlag{Name: "option", Value: "", Usage: "some option"},
|
||||
},
|
||||
Action: func(c *Context) {
|
||||
Action: func(c *Context) int {
|
||||
parsedOption = c.String("option")
|
||||
args = c.Args()
|
||||
return 0
|
||||
},
|
||||
}
|
||||
app.Commands = []Command{command}
|
||||
@@ -258,8 +269,9 @@ func TestApp_CommandWithDash(t *testing.T) {
|
||||
app := NewApp()
|
||||
command := Command{
|
||||
Name: "cmd",
|
||||
Action: func(c *Context) {
|
||||
Action: func(c *Context) int {
|
||||
args = c.Args()
|
||||
return 0
|
||||
},
|
||||
}
|
||||
app.Commands = []Command{command}
|
||||
@@ -276,8 +288,9 @@ func TestApp_CommandWithNoFlagBeforeTerminator(t *testing.T) {
|
||||
app := NewApp()
|
||||
command := Command{
|
||||
Name: "cmd",
|
||||
Action: func(c *Context) {
|
||||
Action: func(c *Context) int {
|
||||
args = c.Args()
|
||||
return 0
|
||||
},
|
||||
}
|
||||
app.Commands = []Command{command}
|
||||
@@ -296,8 +309,9 @@ func TestApp_Float64Flag(t *testing.T) {
|
||||
app.Flags = []Flag{
|
||||
Float64Flag{Name: "height", Value: 1.5, Usage: "Set the height, in meters"},
|
||||
}
|
||||
app.Action = func(c *Context) {
|
||||
app.Action = func(c *Context) int {
|
||||
meters = c.Float64("height")
|
||||
return 0
|
||||
}
|
||||
|
||||
app.Run([]string{"", "--height", "1.93"})
|
||||
@@ -316,11 +330,12 @@ func TestApp_ParseSliceFlags(t *testing.T) {
|
||||
IntSliceFlag{Name: "p", Value: &IntSlice{}, Usage: "set one or more ip addr"},
|
||||
StringSliceFlag{Name: "ip", Value: &StringSlice{}, Usage: "set one or more ports to open"},
|
||||
},
|
||||
Action: func(c *Context) {
|
||||
Action: func(c *Context) int {
|
||||
parsedIntSlice = c.IntSlice("p")
|
||||
parsedStringSlice = c.StringSlice("ip")
|
||||
parsedOption = c.String("option")
|
||||
firstArg = c.Args().First()
|
||||
return 0
|
||||
},
|
||||
}
|
||||
app.Commands = []Command{command}
|
||||
@@ -373,9 +388,10 @@ func TestApp_ParseSliceFlagsWithMissingValue(t *testing.T) {
|
||||
IntSliceFlag{Name: "a", Usage: "set numbers"},
|
||||
StringSliceFlag{Name: "str", Usage: "set strings"},
|
||||
},
|
||||
Action: func(c *Context) {
|
||||
Action: func(c *Context) int {
|
||||
parsedIntSlice = c.IntSlice("a")
|
||||
parsedStringSlice = c.StringSlice("str")
|
||||
return 0
|
||||
},
|
||||
}
|
||||
app.Commands = []Command{command}
|
||||
@@ -427,7 +443,7 @@ func TestApp_SetStdout(t *testing.T) {
|
||||
app.Name = "test"
|
||||
app.Writer = w
|
||||
|
||||
err := app.Run([]string{"help"})
|
||||
_, err := app.Run([]string{"help"})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Run error: %s", err)
|
||||
@@ -445,21 +461,22 @@ func TestApp_BeforeFunc(t *testing.T) {
|
||||
|
||||
app := NewApp()
|
||||
|
||||
app.Before = func(c *Context) error {
|
||||
app.Before = func(c *Context) (int, error) {
|
||||
beforeRun = true
|
||||
s := c.String("opt")
|
||||
if s == "fail" {
|
||||
return beforeError
|
||||
return DefaultErrorExitCode, beforeError
|
||||
}
|
||||
|
||||
return nil
|
||||
return DefaultSuccessExitCode, nil
|
||||
}
|
||||
|
||||
app.Commands = []Command{
|
||||
Command{
|
||||
Name: "sub",
|
||||
Action: func(c *Context) {
|
||||
Action: func(c *Context) int {
|
||||
subcommandRun = true
|
||||
return DefaultSuccessExitCode
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -469,7 +486,7 @@ func TestApp_BeforeFunc(t *testing.T) {
|
||||
}
|
||||
|
||||
// run with the Before() func succeeding
|
||||
err = app.Run([]string{"command", "--opt", "succeed", "sub"})
|
||||
ec, err := app.Run([]string{"command", "--opt", "succeed", "sub"})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Run error: %s", err)
|
||||
@@ -483,11 +500,15 @@ func TestApp_BeforeFunc(t *testing.T) {
|
||||
t.Errorf("Subcommand not executed when expected")
|
||||
}
|
||||
|
||||
if ec != DefaultSuccessExitCode {
|
||||
t.Errorf("Expected exit code to be %d but got %d", DefaultSuccessExitCode, ec)
|
||||
}
|
||||
|
||||
// reset
|
||||
beforeRun, subcommandRun = false, false
|
||||
|
||||
// run with the Before() func failing
|
||||
err = app.Run([]string{"command", "--opt", "fail", "sub"})
|
||||
ec, err = app.Run([]string{"command", "--opt", "fail", "sub"})
|
||||
|
||||
// should be the same error produced by the Before func
|
||||
if err != beforeError {
|
||||
@@ -502,6 +523,9 @@ func TestApp_BeforeFunc(t *testing.T) {
|
||||
t.Errorf("Subcommand executed when NOT expected")
|
||||
}
|
||||
|
||||
if ec != DefaultErrorExitCode {
|
||||
t.Errorf("Expected exit code to be %d but got %d", DefaultErrorExitCode, ec)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApp_AfterFunc(t *testing.T) {
|
||||
@@ -511,21 +535,22 @@ func TestApp_AfterFunc(t *testing.T) {
|
||||
|
||||
app := NewApp()
|
||||
|
||||
app.After = func(c *Context) error {
|
||||
app.After = func(c *Context) (int, error) {
|
||||
afterRun = true
|
||||
s := c.String("opt")
|
||||
if s == "fail" {
|
||||
return afterError
|
||||
return DefaultErrorExitCode, afterError
|
||||
}
|
||||
|
||||
return nil
|
||||
return DefaultSuccessExitCode, nil
|
||||
}
|
||||
|
||||
app.Commands = []Command{
|
||||
Command{
|
||||
Name: "sub",
|
||||
Action: func(c *Context) {
|
||||
Action: func(c *Context) int {
|
||||
subcommandRun = true
|
||||
return DefaultSuccessExitCode
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -535,7 +560,7 @@ func TestApp_AfterFunc(t *testing.T) {
|
||||
}
|
||||
|
||||
// run with the After() func succeeding
|
||||
err = app.Run([]string{"command", "--opt", "succeed", "sub"})
|
||||
ec, err := app.Run([]string{"command", "--opt", "succeed", "sub"})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Run error: %s", err)
|
||||
@@ -549,11 +574,15 @@ func TestApp_AfterFunc(t *testing.T) {
|
||||
t.Errorf("Subcommand not executed when expected")
|
||||
}
|
||||
|
||||
if ec != DefaultSuccessExitCode {
|
||||
t.Errorf("Expected exit code to be %d but got %d", DefaultSuccessExitCode, ec)
|
||||
}
|
||||
|
||||
// reset
|
||||
afterRun, subcommandRun = false, false
|
||||
|
||||
// run with the Before() func failing
|
||||
err = app.Run([]string{"command", "--opt", "fail", "sub"})
|
||||
ec, err = app.Run([]string{"command", "--opt", "fail", "sub"})
|
||||
|
||||
// should be the same error produced by the Before func
|
||||
if err != afterError {
|
||||
@@ -567,6 +596,10 @@ func TestApp_AfterFunc(t *testing.T) {
|
||||
if subcommandRun == false {
|
||||
t.Errorf("Subcommand not executed when expected")
|
||||
}
|
||||
|
||||
if ec != DefaultErrorExitCode {
|
||||
t.Errorf("Expected exit code to be %d but got %d", DefaultErrorExitCode, ec)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppNoHelpFlag(t *testing.T) {
|
||||
@@ -579,7 +612,7 @@ func TestAppNoHelpFlag(t *testing.T) {
|
||||
|
||||
app := NewApp()
|
||||
app.Writer = ioutil.Discard
|
||||
err := app.Run([]string{"test", "-h"})
|
||||
_, err := app.Run([]string{"test", "-h"})
|
||||
|
||||
if err != flag.ErrHelp {
|
||||
t.Errorf("expected error about missing help flag, but got: %s (%T)", err, err)
|
||||
@@ -636,8 +669,9 @@ func TestAppCommandNotFound(t *testing.T) {
|
||||
app.Commands = []Command{
|
||||
Command{
|
||||
Name: "bar",
|
||||
Action: func(c *Context) {
|
||||
Action: func(c *Context) int {
|
||||
subcommandRun = true
|
||||
return 0
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -655,9 +689,10 @@ func TestGlobalFlag(t *testing.T) {
|
||||
app.Flags = []Flag{
|
||||
StringFlag{Name: "global, g", Usage: "global"},
|
||||
}
|
||||
app.Action = func(c *Context) {
|
||||
app.Action = func(c *Context) int {
|
||||
globalFlag = c.GlobalString("global")
|
||||
globalFlagSet = c.GlobalIsSet("global")
|
||||
return 0
|
||||
}
|
||||
app.Run([]string{"command", "-g", "foo"})
|
||||
expect(t, globalFlag, "foo")
|
||||
@@ -683,13 +718,14 @@ func TestGlobalFlagsInSubcommands(t *testing.T) {
|
||||
Subcommands: []Command{
|
||||
{
|
||||
Name: "bar",
|
||||
Action: func(c *Context) {
|
||||
Action: func(c *Context) int {
|
||||
if c.GlobalBool("debug") {
|
||||
subcommandRun = true
|
||||
}
|
||||
if c.GlobalBool("parent") {
|
||||
parentFlag = true
|
||||
}
|
||||
return 0
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -731,7 +767,7 @@ func TestApp_Run_CommandWithSubcommandHasHelpTopic(t *testing.T) {
|
||||
}
|
||||
|
||||
app.Commands = []Command{cmd}
|
||||
err := app.Run(flagSet)
|
||||
_, err := app.Run(flagSet)
|
||||
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
@@ -772,7 +808,7 @@ func TestApp_Run_SubcommandFullPath(t *testing.T) {
|
||||
}
|
||||
app.Commands = []Command{cmd}
|
||||
|
||||
err := app.Run([]string{"command", "foo", "bar", "--help"})
|
||||
_, err := app.Run([]string{"command", "foo", "bar", "--help"})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -803,7 +839,7 @@ func TestApp_Run_SubcommandHelpName(t *testing.T) {
|
||||
}
|
||||
app.Commands = []Command{cmd}
|
||||
|
||||
err := app.Run([]string{"command", "foo", "bar", "--help"})
|
||||
_, err := app.Run([]string{"command", "foo", "bar", "--help"})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -834,7 +870,7 @@ func TestApp_Run_CommandHelpName(t *testing.T) {
|
||||
}
|
||||
app.Commands = []Command{cmd}
|
||||
|
||||
err := app.Run([]string{"command", "foo", "bar", "--help"})
|
||||
_, err := app.Run([]string{"command", "foo", "bar", "--help"})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -865,7 +901,7 @@ func TestApp_Run_CommandSubcommandHelpName(t *testing.T) {
|
||||
}
|
||||
app.Commands = []Command{cmd}
|
||||
|
||||
err := app.Run([]string{"command", "foo", "--help"})
|
||||
_, err := app.Run([]string{"command", "foo", "--help"})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -891,11 +927,12 @@ func TestApp_Run_Help(t *testing.T) {
|
||||
app.Name = "boom"
|
||||
app.Usage = "make an explosive entrance"
|
||||
app.Writer = buf
|
||||
app.Action = func(c *Context) {
|
||||
app.Action = func(c *Context) int {
|
||||
buf.WriteString("boom I say!")
|
||||
return 0
|
||||
}
|
||||
|
||||
err := app.Run(args)
|
||||
_, err := app.Run(args)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -922,11 +959,12 @@ func TestApp_Run_Version(t *testing.T) {
|
||||
app.Usage = "make an explosive entrance"
|
||||
app.Version = "0.1.0"
|
||||
app.Writer = buf
|
||||
app.Action = func(c *Context) {
|
||||
app.Action = func(c *Context) int {
|
||||
buf.WriteString("boom I say!")
|
||||
return 0
|
||||
}
|
||||
|
||||
err := app.Run(args)
|
||||
_, err := app.Run(args)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -991,11 +1029,11 @@ func TestApp_Run_Categories(t *testing.T) {
|
||||
|
||||
func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
|
||||
app := NewApp()
|
||||
app.Action = func(c *Context) {}
|
||||
app.Before = func(c *Context) error { return fmt.Errorf("before error") }
|
||||
app.After = func(c *Context) error { return fmt.Errorf("after error") }
|
||||
app.Action = func(c *Context) int { return 0 }
|
||||
app.Before = func(c *Context) (int, error) { return 1, fmt.Errorf("before error") }
|
||||
app.After = func(c *Context) (int, error) { return 2, fmt.Errorf("after error") }
|
||||
|
||||
err := app.Run([]string{"foo"})
|
||||
ec, err := app.Run([]string{"foo"})
|
||||
if err == nil {
|
||||
t.Fatalf("expected to receive error from Run, got none")
|
||||
}
|
||||
@@ -1006,6 +1044,10 @@ func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
|
||||
if !strings.Contains(err.Error(), "after error") {
|
||||
t.Errorf("expected text of error from After method, but got none in \"%v\"", err)
|
||||
}
|
||||
|
||||
if ec != 2 {
|
||||
t.Errorf("Expected exit code to be %d but got %d", 2, ec)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApp_Run_SubcommandDoesNotOverwriteErrorFromBefore(t *testing.T) {
|
||||
@@ -1018,12 +1060,12 @@ func TestApp_Run_SubcommandDoesNotOverwriteErrorFromBefore(t *testing.T) {
|
||||
},
|
||||
},
|
||||
Name: "bar",
|
||||
Before: func(c *Context) error { return fmt.Errorf("before error") },
|
||||
After: func(c *Context) error { return fmt.Errorf("after error") },
|
||||
Before: func(c *Context) (int, error) { return 1, fmt.Errorf("before error") },
|
||||
After: func(c *Context) (int, error) { return 2, fmt.Errorf("after error") },
|
||||
},
|
||||
}
|
||||
|
||||
err := app.Run([]string{"foo", "bar"})
|
||||
ec, err := app.Run([]string{"foo", "bar"})
|
||||
if err == nil {
|
||||
t.Fatalf("expected to receive error from Run, got none")
|
||||
}
|
||||
@@ -1034,6 +1076,10 @@ func TestApp_Run_SubcommandDoesNotOverwriteErrorFromBefore(t *testing.T) {
|
||||
if !strings.Contains(err.Error(), "after error") {
|
||||
t.Errorf("expected text of error from After method, but got none in \"%v\"", err)
|
||||
}
|
||||
|
||||
if ec != 2 {
|
||||
t.Errorf("Expected exit code to be %d but got %d", 2, ec)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApp_OnUsageError_WithWrongFlagValue(t *testing.T) {
|
||||
@@ -1056,7 +1102,7 @@ func TestApp_OnUsageError_WithWrongFlagValue(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
err := app.Run([]string{"foo", "--flag=wrong"})
|
||||
_, err := app.Run([]string{"foo", "--flag=wrong"})
|
||||
if err == nil {
|
||||
t.Fatalf("expected to receive error from Run, got none")
|
||||
}
|
||||
@@ -1086,7 +1132,7 @@ func TestApp_OnUsageError_WithWrongFlagValue_ForSubcommand(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
err := app.Run([]string{"foo", "--flag=wrong", "bar"})
|
||||
_, err := app.Run([]string{"foo", "--flag=wrong", "bar"})
|
||||
if err == nil {
|
||||
t.Fatalf("expected to receive error from Run, got none")
|
||||
}
|
||||
|
Reference in New Issue
Block a user