2015-07-20 19:18:25 +00:00
|
|
|
package cli
|
2013-07-20 22:50:13 +00:00
|
|
|
|
|
|
|
import (
|
2015-05-04 01:37:51 +00:00
|
|
|
"bytes"
|
2016-02-09 16:36:13 +00:00
|
|
|
"errors"
|
2014-12-02 04:20:21 +00:00
|
|
|
"flag"
|
2013-07-20 22:50:13 +00:00
|
|
|
"fmt"
|
2015-05-04 00:43:52 +00:00
|
|
|
"io"
|
2015-11-28 17:26:10 +00:00
|
|
|
"io/ioutil"
|
2013-07-20 22:50:13 +00:00
|
|
|
"os"
|
2015-08-21 11:25:37 +00:00
|
|
|
"reflect"
|
2015-05-04 01:37:51 +00:00
|
|
|
"strings"
|
2013-09-14 22:29:57 +00:00
|
|
|
"testing"
|
2013-07-20 22:50:13 +00:00
|
|
|
)
|
|
|
|
|
2016-07-24 15:57:51 +00:00
|
|
|
var (
|
|
|
|
lastExitCode = 0
|
|
|
|
fakeOsExiter = func(rc int) {
|
|
|
|
lastExitCode = rc
|
|
|
|
}
|
|
|
|
fakeErrWriter = &bytes.Buffer{}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
OsExiter = fakeOsExiter
|
|
|
|
ErrWriter = fakeErrWriter
|
|
|
|
}
|
|
|
|
|
2016-04-28 20:05:14 +00:00
|
|
|
type opCounts struct {
|
|
|
|
Total, BashComplete, OnUsageError, Before, CommandNotFound, Action, After, SubCommand int
|
|
|
|
}
|
|
|
|
|
2015-11-15 21:07:28 +00:00
|
|
|
func ExampleApp_Run() {
|
2013-07-20 22:50:13 +00:00
|
|
|
// set args for examples sake
|
|
|
|
os.Args = []string{"greet", "--name", "Jeremy"}
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
2013-07-20 22:50:13 +00:00
|
|
|
app.Name = "greet"
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Flags = []Flag{
|
|
|
|
StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
|
2013-07-20 22:50:13 +00:00
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
app.Action = func(c *Context) error {
|
2013-07-20 22:50:13 +00:00
|
|
|
fmt.Printf("Hello %v\n", c.String("name"))
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2013-07-20 22:50:13 +00:00
|
|
|
}
|
2015-10-25 05:51:06 +00:00
|
|
|
app.UsageText = "app [first_arg] [second_arg]"
|
2015-02-20 23:44:00 +00:00
|
|
|
app.Author = "Harrison"
|
|
|
|
app.Email = "harrison@lolwut.com"
|
2016-05-09 13:40:09 +00:00
|
|
|
app.Authors = []Author{{Name: "Oliver Allen", Email: "oliver@toyshop.com"}}
|
2013-07-20 22:50:13 +00:00
|
|
|
app.Run(os.Args)
|
|
|
|
// Output:
|
|
|
|
// Hello Jeremy
|
|
|
|
}
|
2013-09-14 22:29:57 +00:00
|
|
|
|
2015-11-15 21:07:28 +00:00
|
|
|
func ExampleApp_Run_subcommand() {
|
2015-03-17 01:04:57 +00:00
|
|
|
// set args for examples sake
|
|
|
|
os.Args = []string{"say", "hi", "english", "--name", "Jeremy"}
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
2015-03-17 01:04:57 +00:00
|
|
|
app.Name = "say"
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Commands = []Command{
|
2015-03-17 01:04:57 +00:00
|
|
|
{
|
|
|
|
Name: "hello",
|
|
|
|
Aliases: []string{"hi"},
|
|
|
|
Usage: "use it to see a description",
|
|
|
|
Description: "This is how we describe hello the function",
|
2015-07-20 19:18:25 +00:00
|
|
|
Subcommands: []Command{
|
2015-03-17 01:04:57 +00:00
|
|
|
{
|
|
|
|
Name: "english",
|
|
|
|
Aliases: []string{"en"},
|
|
|
|
Usage: "sends a greeting in english",
|
|
|
|
Description: "greets someone in english",
|
2015-07-20 19:18:25 +00:00
|
|
|
Flags: []Flag{
|
|
|
|
StringFlag{
|
2015-03-17 01:04:57 +00:00
|
|
|
Name: "name",
|
|
|
|
Value: "Bob",
|
|
|
|
Usage: "Name of the person to greet",
|
|
|
|
},
|
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(c *Context) error {
|
2015-03-17 01:04:57 +00:00
|
|
|
fmt.Println("Hello,", c.String("name"))
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2015-03-17 01:04:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Run(os.Args)
|
|
|
|
// Output:
|
|
|
|
// Hello, Jeremy
|
|
|
|
}
|
|
|
|
|
2016-10-22 23:02:21 +00:00
|
|
|
func ExampleApp_Run_appHelp() {
|
|
|
|
// set args for examples sake
|
|
|
|
os.Args = []string{"greet", "help"}
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
app.Name = "greet"
|
|
|
|
app.Version = "0.1.0"
|
|
|
|
app.Description = "This is how we describe greet the app"
|
|
|
|
app.Authors = []Author{
|
|
|
|
{Name: "Harrison", Email: "harrison@lolwut.com"},
|
|
|
|
{Name: "Oliver Allen", Email: "oliver@toyshop.com"},
|
|
|
|
}
|
|
|
|
app.Flags = []Flag{
|
|
|
|
StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
|
|
|
|
}
|
|
|
|
app.Commands = []Command{
|
|
|
|
{
|
|
|
|
Name: "describeit",
|
|
|
|
Aliases: []string{"d"},
|
|
|
|
Usage: "use it to see a description",
|
|
|
|
Description: "This is how we describe describeit the function",
|
|
|
|
Action: func(c *Context) error {
|
|
|
|
fmt.Printf("i like to describe things")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
app.Run(os.Args)
|
|
|
|
// Output:
|
|
|
|
// NAME:
|
|
|
|
// greet - A new cli application
|
|
|
|
//
|
|
|
|
// USAGE:
|
|
|
|
// greet [global options] command [command options] [arguments...]
|
|
|
|
//
|
|
|
|
// VERSION:
|
|
|
|
// 0.1.0
|
|
|
|
//
|
|
|
|
// DESCRIPTION:
|
|
|
|
// This is how we describe greet the app
|
|
|
|
//
|
|
|
|
// AUTHORS:
|
|
|
|
// Harrison <harrison@lolwut.com>
|
|
|
|
// Oliver Allen <oliver@toyshop.com>
|
|
|
|
//
|
|
|
|
// COMMANDS:
|
2019-03-06 14:51:22 +00:00
|
|
|
// describeit, d use it to see a description
|
|
|
|
// help, h Shows a list of commands or help for one command
|
2016-10-22 23:02:21 +00:00
|
|
|
//
|
|
|
|
// GLOBAL OPTIONS:
|
|
|
|
// --name value a name to say (default: "bob")
|
|
|
|
// --help, -h show help
|
|
|
|
// --version, -v print the version
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleApp_Run_commandHelp() {
|
2014-02-19 21:32:01 +00:00
|
|
|
// set args for examples sake
|
|
|
|
os.Args = []string{"greet", "h", "describeit"}
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
2014-02-19 21:32:01 +00:00
|
|
|
app.Name = "greet"
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Flags = []Flag{
|
|
|
|
StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
|
2014-02-19 21:32:01 +00:00
|
|
|
}
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Commands = []Command{
|
2014-02-19 21:37:20 +00:00
|
|
|
{
|
2014-04-12 22:13:47 +00:00
|
|
|
Name: "describeit",
|
2015-03-10 04:24:57 +00:00
|
|
|
Aliases: []string{"d"},
|
2014-04-12 22:13:47 +00:00
|
|
|
Usage: "use it to see a description",
|
2014-02-19 21:32:01 +00:00
|
|
|
Description: "This is how we describe describeit the function",
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(c *Context) error {
|
2014-02-19 21:37:20 +00:00
|
|
|
fmt.Printf("i like to describe things")
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-02-19 21:37:20 +00:00
|
|
|
},
|
2014-02-19 21:32:01 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
app.Run(os.Args)
|
|
|
|
// Output:
|
2014-02-19 21:37:20 +00:00
|
|
|
// NAME:
|
2015-08-13 04:43:14 +00:00
|
|
|
// greet describeit - use it to see a description
|
2014-02-19 21:32:01 +00:00
|
|
|
//
|
2014-02-19 21:37:20 +00:00
|
|
|
// USAGE:
|
2015-08-13 04:43:14 +00:00
|
|
|
// greet describeit [arguments...]
|
2014-02-19 21:32:01 +00:00
|
|
|
//
|
2014-02-19 21:37:20 +00:00
|
|
|
// DESCRIPTION:
|
|
|
|
// This is how we describe describeit the function
|
2014-02-19 21:32:01 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 21:37:07 +00:00
|
|
|
func ExampleApp_Run_noAction() {
|
|
|
|
app := App{}
|
|
|
|
app.Name = "greet"
|
|
|
|
app.Run([]string{"greet"})
|
|
|
|
// Output:
|
|
|
|
// NAME:
|
|
|
|
// greet
|
|
|
|
//
|
|
|
|
// USAGE:
|
|
|
|
// [global options] command [command options] [arguments...]
|
|
|
|
//
|
|
|
|
// COMMANDS:
|
2019-03-06 14:51:22 +00:00
|
|
|
// help, h Shows a list of commands or help for one command
|
2016-11-12 21:37:07 +00:00
|
|
|
//
|
|
|
|
// GLOBAL OPTIONS:
|
|
|
|
// --help, -h show help
|
|
|
|
// --version, -v print the version
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleApp_Run_subcommandNoAction() {
|
|
|
|
app := App{}
|
|
|
|
app.Name = "greet"
|
|
|
|
app.Commands = []Command{
|
|
|
|
{
|
|
|
|
Name: "describeit",
|
|
|
|
Aliases: []string{"d"},
|
|
|
|
Usage: "use it to see a description",
|
|
|
|
Description: "This is how we describe describeit the function",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
app.Run([]string{"greet", "describeit"})
|
|
|
|
// Output:
|
|
|
|
// NAME:
|
|
|
|
// describeit - use it to see a description
|
|
|
|
//
|
|
|
|
// USAGE:
|
|
|
|
// describeit [arguments...]
|
|
|
|
//
|
|
|
|
// DESCRIPTION:
|
|
|
|
// This is how we describe describeit the function
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-21 07:31:48 +00:00
|
|
|
func ExampleApp_Run_bashComplete_withShortFlag() {
|
|
|
|
os.Args = []string{"greet", "-", "--generate-bash-completion"}
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
app.Name = "greet"
|
|
|
|
app.EnableBashCompletion = true
|
|
|
|
app.Flags = []Flag{
|
|
|
|
IntFlag{
|
|
|
|
Name: "other,o",
|
|
|
|
},
|
|
|
|
StringFlag{
|
|
|
|
Name: "xyz,x",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Run(os.Args)
|
|
|
|
// Output:
|
|
|
|
// --other
|
|
|
|
// -o
|
|
|
|
// --xyz
|
|
|
|
// -x
|
|
|
|
// --help
|
|
|
|
// -h
|
|
|
|
// --version
|
|
|
|
// -v
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleApp_Run_bashComplete_withLongFlag() {
|
|
|
|
os.Args = []string{"greet", "--s", "--generate-bash-completion"}
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
app.Name = "greet"
|
|
|
|
app.EnableBashCompletion = true
|
|
|
|
app.Flags = []Flag{
|
|
|
|
IntFlag{
|
|
|
|
Name: "other,o",
|
|
|
|
},
|
|
|
|
StringFlag{
|
|
|
|
Name: "xyz,x",
|
|
|
|
},
|
|
|
|
StringFlag{
|
|
|
|
Name: "some-flag,s",
|
|
|
|
},
|
|
|
|
StringFlag{
|
|
|
|
Name: "similar-flag",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Run(os.Args)
|
|
|
|
// Output:
|
|
|
|
// --some-flag
|
|
|
|
// --similar-flag
|
|
|
|
}
|
2019-08-05 18:18:08 +00:00
|
|
|
func ExampleApp_Run_bashComplete_withMultipleLongFlag() {
|
|
|
|
os.Args = []string{"greet", "--st", "--generate-bash-completion"}
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
app.Name = "greet"
|
|
|
|
app.EnableBashCompletion = true
|
|
|
|
app.Flags = []Flag{
|
|
|
|
IntFlag{
|
|
|
|
Name: "int-flag,i",
|
|
|
|
},
|
|
|
|
StringFlag{
|
|
|
|
Name: "string,s",
|
|
|
|
},
|
|
|
|
StringFlag{
|
|
|
|
Name: "string-flag-2",
|
|
|
|
},
|
|
|
|
StringFlag{
|
|
|
|
Name: "similar-flag",
|
|
|
|
},
|
|
|
|
StringFlag{
|
|
|
|
Name: "some-flag",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Run(os.Args)
|
|
|
|
// Output:
|
|
|
|
// --string
|
|
|
|
// --string-flag-2
|
|
|
|
}
|
2019-03-21 07:31:48 +00:00
|
|
|
|
2015-11-15 21:07:28 +00:00
|
|
|
func ExampleApp_Run_bashComplete() {
|
2014-04-12 22:13:47 +00:00
|
|
|
// set args for examples sake
|
|
|
|
os.Args = []string{"greet", "--generate-bash-completion"}
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
2014-04-12 22:13:47 +00:00
|
|
|
app.Name = "greet"
|
|
|
|
app.EnableBashCompletion = true
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Commands = []Command{
|
2014-04-12 22:13:47 +00:00
|
|
|
{
|
|
|
|
Name: "describeit",
|
2015-03-10 04:24:57 +00:00
|
|
|
Aliases: []string{"d"},
|
2014-04-12 22:13:47 +00:00
|
|
|
Usage: "use it to see a description",
|
|
|
|
Description: "This is how we describe describeit the function",
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(c *Context) error {
|
2014-04-12 22:13:47 +00:00
|
|
|
fmt.Printf("i like to describe things")
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-04-12 22:13:47 +00:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
Name: "next",
|
|
|
|
Usage: "next example",
|
|
|
|
Description: "more stuff to see when generating bash completion",
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(c *Context) error {
|
2014-04-12 22:13:47 +00:00
|
|
|
fmt.Printf("the next example")
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-04-12 22:13:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Run(os.Args)
|
|
|
|
// Output:
|
|
|
|
// describeit
|
|
|
|
// d
|
|
|
|
// next
|
|
|
|
// help
|
|
|
|
// h
|
|
|
|
}
|
|
|
|
|
2016-07-24 21:29:13 +00:00
|
|
|
func ExampleApp_Run_zshComplete() {
|
|
|
|
// set args for examples sake
|
|
|
|
os.Args = []string{"greet", "--generate-bash-completion"}
|
|
|
|
os.Setenv("_CLI_ZSH_AUTOCOMPLETE_HACK", "1")
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
app.Name = "greet"
|
|
|
|
app.EnableBashCompletion = true
|
|
|
|
app.Commands = []Command{
|
|
|
|
{
|
|
|
|
Name: "describeit",
|
|
|
|
Aliases: []string{"d"},
|
|
|
|
Usage: "use it to see a description",
|
|
|
|
Description: "This is how we describe describeit the function",
|
|
|
|
Action: func(c *Context) error {
|
|
|
|
fmt.Printf("i like to describe things")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
Name: "next",
|
|
|
|
Usage: "next example",
|
|
|
|
Description: "more stuff to see when generating bash completion",
|
|
|
|
Action: func(c *Context) error {
|
|
|
|
fmt.Printf("the next example")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Run(os.Args)
|
|
|
|
// Output:
|
|
|
|
// describeit:use it to see a description
|
|
|
|
// d:use it to see a description
|
|
|
|
// next:next example
|
|
|
|
// help:Shows a list of commands or help for one command
|
|
|
|
// h:Shows a list of commands or help for one command
|
|
|
|
}
|
|
|
|
|
2013-09-14 22:29:57 +00:00
|
|
|
func TestApp_Run(t *testing.T) {
|
|
|
|
s := ""
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
2016-04-27 13:12:34 +00:00
|
|
|
app.Action = func(c *Context) error {
|
2013-11-24 13:40:21 +00:00
|
|
|
s = s + c.Args().First()
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2013-09-14 22:29:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 13:12:34 +00:00
|
|
|
err := app.Run([]string{"command", "foo"})
|
2013-11-01 13:23:19 +00:00
|
|
|
expect(t, err, nil)
|
2016-04-27 13:12:34 +00:00
|
|
|
err = app.Run([]string{"command", "bar"})
|
2013-11-01 13:23:19 +00:00
|
|
|
expect(t, err, nil)
|
2013-09-14 22:29:57 +00:00
|
|
|
expect(t, s, "foobar")
|
|
|
|
}
|
|
|
|
|
|
|
|
var commandAppTests = []struct {
|
|
|
|
name string
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{"foobar", true},
|
|
|
|
{"batbaz", true},
|
|
|
|
{"b", true},
|
|
|
|
{"f", true},
|
|
|
|
{"bat", false},
|
|
|
|
{"nothing", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApp_Command(t *testing.T) {
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
|
|
|
fooCommand := Command{Name: "foobar", Aliases: []string{"f"}}
|
|
|
|
batCommand := Command{Name: "batbaz", Aliases: []string{"b"}}
|
|
|
|
app.Commands = []Command{
|
2013-09-14 22:29:57 +00:00
|
|
|
fooCommand,
|
|
|
|
batCommand,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range commandAppTests {
|
|
|
|
expect(t, app.Command(test.name) != nil, test.expected)
|
|
|
|
}
|
|
|
|
}
|
2013-09-18 16:24:20 +00:00
|
|
|
|
2016-10-19 03:56:31 +00:00
|
|
|
func TestApp_Setup_defaultsWriter(t *testing.T) {
|
|
|
|
app := &App{}
|
|
|
|
app.Setup()
|
|
|
|
expect(t, app.Writer, os.Stdout)
|
|
|
|
}
|
|
|
|
|
2013-09-18 16:24:20 +00:00
|
|
|
func TestApp_CommandWithArgBeforeFlags(t *testing.T) {
|
|
|
|
var parsedOption, firstArg string
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
|
|
|
command := Command{
|
2013-09-18 16:24:20 +00:00
|
|
|
Name: "cmd",
|
2015-07-20 19:18:25 +00:00
|
|
|
Flags: []Flag{
|
|
|
|
StringFlag{Name: "option", Value: "", Usage: "some option"},
|
2013-09-18 16:24:20 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(c *Context) error {
|
2013-09-18 16:24:20 +00:00
|
|
|
parsedOption = c.String("option")
|
2013-11-24 13:40:21 +00:00
|
|
|
firstArg = c.Args().First()
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2013-09-18 16:24:20 +00:00
|
|
|
},
|
|
|
|
}
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Commands = []Command{command}
|
2013-09-18 16:24:20 +00:00
|
|
|
|
|
|
|
app.Run([]string{"", "cmd", "my-arg", "--option", "my-option"})
|
|
|
|
|
|
|
|
expect(t, parsedOption, "my-option")
|
|
|
|
expect(t, firstArg, "my-arg")
|
|
|
|
}
|
2013-09-24 19:36:01 +00:00
|
|
|
|
2018-02-10 12:35:23 +00:00
|
|
|
func TestApp_CommandWithArgBeforeBoolFlags(t *testing.T) {
|
|
|
|
var parsedOption, parsedSecondOption, firstArg string
|
|
|
|
var parsedBool, parsedSecondBool bool
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
command := Command{
|
|
|
|
Name: "cmd",
|
|
|
|
Flags: []Flag{
|
|
|
|
StringFlag{Name: "option", Value: "", Usage: "some option"},
|
|
|
|
StringFlag{Name: "secondOption", Value: "", Usage: "another option"},
|
|
|
|
BoolFlag{Name: "boolflag", Usage: "some bool"},
|
|
|
|
BoolFlag{Name: "b", Usage: "another bool"},
|
|
|
|
},
|
|
|
|
Action: func(c *Context) error {
|
|
|
|
parsedOption = c.String("option")
|
|
|
|
parsedSecondOption = c.String("secondOption")
|
|
|
|
parsedBool = c.Bool("boolflag")
|
|
|
|
parsedSecondBool = c.Bool("b")
|
|
|
|
firstArg = c.Args().First()
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
app.Commands = []Command{command}
|
|
|
|
|
|
|
|
app.Run([]string{"", "cmd", "my-arg", "--boolflag", "--option", "my-option", "-b", "--secondOption", "fancy-option"})
|
|
|
|
|
|
|
|
expect(t, parsedOption, "my-option")
|
|
|
|
expect(t, parsedSecondOption, "fancy-option")
|
|
|
|
expect(t, parsedBool, true)
|
|
|
|
expect(t, parsedSecondBool, true)
|
|
|
|
expect(t, firstArg, "my-arg")
|
|
|
|
}
|
|
|
|
|
2015-01-09 19:46:29 +00:00
|
|
|
func TestApp_RunAsSubcommandParseFlags(t *testing.T) {
|
2015-07-20 19:18:25 +00:00
|
|
|
var context *Context
|
2015-01-09 19:46:29 +00:00
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
a := NewApp()
|
|
|
|
a.Commands = []Command{
|
2015-01-09 19:46:29 +00:00
|
|
|
{
|
|
|
|
Name: "foo",
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(c *Context) error {
|
2015-01-09 19:46:29 +00:00
|
|
|
context = c
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2015-01-09 19:46:29 +00:00
|
|
|
},
|
2015-07-20 19:18:25 +00:00
|
|
|
Flags: []Flag{
|
|
|
|
StringFlag{
|
2015-01-09 19:46:29 +00:00
|
|
|
Name: "lang",
|
|
|
|
Value: "english",
|
|
|
|
Usage: "language for the greeting",
|
|
|
|
},
|
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Before: func(_ *Context) error { return nil },
|
2015-01-09 19:46:29 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
a.Run([]string{"", "foo", "--lang", "spanish", "abcd"})
|
|
|
|
|
|
|
|
expect(t, context.Args().Get(0), "abcd")
|
|
|
|
expect(t, context.String("lang"), "spanish")
|
|
|
|
}
|
|
|
|
|
2016-10-07 14:38:36 +00:00
|
|
|
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"))
|
|
|
|
}
|
|
|
|
|
2014-12-08 17:42:04 +00:00
|
|
|
func TestApp_CommandWithFlagBeforeTerminator(t *testing.T) {
|
|
|
|
var parsedOption string
|
|
|
|
var args []string
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
|
|
|
command := Command{
|
2014-12-08 17:42:04 +00:00
|
|
|
Name: "cmd",
|
2015-07-20 19:18:25 +00:00
|
|
|
Flags: []Flag{
|
|
|
|
StringFlag{Name: "option", Value: "", Usage: "some option"},
|
2014-12-08 17:42:04 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(c *Context) error {
|
2014-12-08 17:42:04 +00:00
|
|
|
parsedOption = c.String("option")
|
|
|
|
args = c.Args()
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-12-08 17:42:04 +00:00
|
|
|
},
|
|
|
|
}
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Commands = []Command{command}
|
2014-12-08 17:42:04 +00:00
|
|
|
|
|
|
|
app.Run([]string{"", "cmd", "my-arg", "--option", "my-option", "--", "--notARealFlag"})
|
|
|
|
|
|
|
|
expect(t, parsedOption, "my-option")
|
|
|
|
expect(t, args[0], "my-arg")
|
|
|
|
expect(t, args[1], "--")
|
|
|
|
expect(t, args[2], "--notARealFlag")
|
|
|
|
}
|
|
|
|
|
2016-01-26 23:34:53 +00:00
|
|
|
func TestApp_CommandWithDash(t *testing.T) {
|
|
|
|
var args []string
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
command := Command{
|
|
|
|
Name: "cmd",
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(c *Context) error {
|
2016-01-26 23:34:53 +00:00
|
|
|
args = c.Args()
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2016-01-26 23:34:53 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
app.Commands = []Command{command}
|
|
|
|
|
|
|
|
app.Run([]string{"", "cmd", "my-arg", "-"})
|
|
|
|
|
|
|
|
expect(t, args[0], "my-arg")
|
|
|
|
expect(t, args[1], "-")
|
|
|
|
}
|
|
|
|
|
2014-12-08 17:42:04 +00:00
|
|
|
func TestApp_CommandWithNoFlagBeforeTerminator(t *testing.T) {
|
|
|
|
var args []string
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
|
|
|
command := Command{
|
2014-12-08 17:42:04 +00:00
|
|
|
Name: "cmd",
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(c *Context) error {
|
2014-12-08 17:42:04 +00:00
|
|
|
args = c.Args()
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-12-08 17:42:04 +00:00
|
|
|
},
|
|
|
|
}
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Commands = []Command{command}
|
2014-12-08 17:42:04 +00:00
|
|
|
|
|
|
|
app.Run([]string{"", "cmd", "my-arg", "--", "notAFlagAtAll"})
|
|
|
|
|
|
|
|
expect(t, args[0], "my-arg")
|
|
|
|
expect(t, args[1], "--")
|
|
|
|
expect(t, args[2], "notAFlagAtAll")
|
|
|
|
}
|
|
|
|
|
2016-05-09 03:54:12 +00:00
|
|
|
func TestApp_VisibleCommands(t *testing.T) {
|
|
|
|
app := NewApp()
|
|
|
|
app.Commands = []Command{
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2016-05-09 03:54:12 +00:00
|
|
|
Name: "frob",
|
|
|
|
HelpName: "foo frob",
|
|
|
|
Action: func(_ *Context) error { return nil },
|
|
|
|
},
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2016-05-09 03:54:12 +00:00
|
|
|
Name: "frib",
|
|
|
|
HelpName: "foo frib",
|
|
|
|
Hidden: true,
|
|
|
|
Action: func(_ *Context) error { return nil },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Setup()
|
|
|
|
expected := []Command{
|
|
|
|
app.Commands[0],
|
|
|
|
app.Commands[2], // help
|
|
|
|
}
|
|
|
|
actual := app.VisibleCommands()
|
|
|
|
expect(t, len(expected), len(actual))
|
|
|
|
for i, actualCommand := range actual {
|
|
|
|
expectedCommand := expected[i]
|
|
|
|
|
|
|
|
if expectedCommand.Action != nil {
|
|
|
|
// comparing func addresses is OK!
|
|
|
|
expect(t, fmt.Sprintf("%p", expectedCommand.Action), fmt.Sprintf("%p", actualCommand.Action))
|
|
|
|
}
|
|
|
|
|
|
|
|
// nil out funcs, as they cannot be compared
|
|
|
|
// (https://github.com/golang/go/issues/8554)
|
|
|
|
expectedCommand.Action = nil
|
|
|
|
actualCommand.Action = nil
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expectedCommand, actualCommand) {
|
|
|
|
t.Errorf("expected\n%#v\n!=\n%#v", expectedCommand, actualCommand)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 10:16:30 +00:00
|
|
|
func TestApp_UseShortOptionHandling(t *testing.T) {
|
|
|
|
var one, two bool
|
|
|
|
var name string
|
|
|
|
expected := "expectedName"
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
app.UseShortOptionHandling = true
|
|
|
|
app.Flags = []Flag{
|
|
|
|
BoolFlag{Name: "one, o"},
|
|
|
|
BoolFlag{Name: "two, t"},
|
|
|
|
StringFlag{Name: "name, n"},
|
|
|
|
}
|
|
|
|
app.Action = func(c *Context) error {
|
|
|
|
one = c.Bool("one")
|
|
|
|
two = c.Bool("two")
|
|
|
|
name = c.String("name")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Run([]string{"", "-on", expected})
|
|
|
|
expect(t, one, true)
|
|
|
|
expect(t, two, false)
|
|
|
|
expect(t, name, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApp_UseShortOptionHandlingCommand(t *testing.T) {
|
|
|
|
var one, two bool
|
|
|
|
var name string
|
|
|
|
expected := "expectedName"
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
app.UseShortOptionHandling = true
|
|
|
|
command := Command{
|
|
|
|
Name: "cmd",
|
|
|
|
Flags: []Flag{
|
|
|
|
BoolFlag{Name: "one, o"},
|
|
|
|
BoolFlag{Name: "two, t"},
|
|
|
|
StringFlag{Name: "name, n"},
|
|
|
|
},
|
|
|
|
Action: func(c *Context) error {
|
|
|
|
one = c.Bool("one")
|
|
|
|
two = c.Bool("two")
|
|
|
|
name = c.String("name")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
app.Commands = []Command{command}
|
|
|
|
|
|
|
|
app.Run([]string{"", "cmd", "-on", expected})
|
|
|
|
expect(t, one, true)
|
|
|
|
expect(t, two, false)
|
|
|
|
expect(t, name, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApp_UseShortOptionHandlingSubCommand(t *testing.T) {
|
|
|
|
var one, two bool
|
|
|
|
var name string
|
|
|
|
expected := "expectedName"
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
app.UseShortOptionHandling = true
|
|
|
|
command := Command{
|
|
|
|
Name: "cmd",
|
|
|
|
}
|
|
|
|
subCommand := Command{
|
|
|
|
Name: "sub",
|
|
|
|
Flags: []Flag{
|
|
|
|
BoolFlag{Name: "one, o"},
|
|
|
|
BoolFlag{Name: "two, t"},
|
|
|
|
StringFlag{Name: "name, n"},
|
|
|
|
},
|
|
|
|
Action: func(c *Context) error {
|
|
|
|
one = c.Bool("one")
|
|
|
|
two = c.Bool("two")
|
|
|
|
name = c.String("name")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
command.Subcommands = []Command{subCommand}
|
|
|
|
app.Commands = []Command{command}
|
|
|
|
|
|
|
|
err := app.Run([]string{"", "cmd", "sub", "-on", expected})
|
|
|
|
expect(t, err, nil)
|
|
|
|
expect(t, one, true)
|
|
|
|
expect(t, two, false)
|
|
|
|
expect(t, name, expected)
|
|
|
|
}
|
|
|
|
|
2013-12-03 13:42:09 +00:00
|
|
|
func TestApp_Float64Flag(t *testing.T) {
|
|
|
|
var meters float64
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
|
|
|
app.Flags = []Flag{
|
|
|
|
Float64Flag{Name: "height", Value: 1.5, Usage: "Set the height, in meters"},
|
2013-12-03 13:42:09 +00:00
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
app.Action = func(c *Context) error {
|
2013-12-03 13:42:09 +00:00
|
|
|
meters = c.Float64("height")
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2013-12-03 13:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
app.Run([]string{"", "--height", "1.93"})
|
|
|
|
expect(t, meters, 1.93)
|
|
|
|
}
|
|
|
|
|
2013-09-24 19:36:01 +00:00
|
|
|
func TestApp_ParseSliceFlags(t *testing.T) {
|
|
|
|
var parsedIntSlice []int
|
|
|
|
var parsedStringSlice []string
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
|
|
|
command := Command{
|
2013-09-24 19:36:01 +00:00
|
|
|
Name: "cmd",
|
2015-07-20 19:18:25 +00:00
|
|
|
Flags: []Flag{
|
|
|
|
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"},
|
2013-09-24 19:36:01 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(c *Context) error {
|
2013-09-24 19:36:01 +00:00
|
|
|
parsedIntSlice = c.IntSlice("p")
|
|
|
|
parsedStringSlice = c.StringSlice("ip")
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2013-09-24 19:36:01 +00:00
|
|
|
},
|
|
|
|
}
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Commands = []Command{command}
|
2013-09-24 19:36:01 +00:00
|
|
|
|
|
|
|
app.Run([]string{"", "cmd", "my-arg", "-p", "22", "-p", "80", "-ip", "8.8.8.8", "-ip", "8.8.4.4"})
|
|
|
|
|
|
|
|
IntsEquals := func(a, b []int) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, v := range a {
|
|
|
|
if v != b[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
StrsEquals := func(a, b []string) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, v := range a {
|
|
|
|
if v != b[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
var expectedIntSlice = []int{22, 80}
|
|
|
|
var expectedStringSlice = []string{"8.8.8.8", "8.8.4.4"}
|
|
|
|
|
|
|
|
if !IntsEquals(parsedIntSlice, expectedIntSlice) {
|
2014-07-11 22:16:19 +00:00
|
|
|
t.Errorf("%v does not match %v", parsedIntSlice, expectedIntSlice)
|
2013-09-24 19:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !StrsEquals(parsedStringSlice, expectedStringSlice) {
|
2014-07-11 22:16:19 +00:00
|
|
|
t.Errorf("%v does not match %v", parsedStringSlice, expectedStringSlice)
|
2013-09-24 19:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-04 16:14:33 +00:00
|
|
|
|
2015-05-31 20:50:23 +00:00
|
|
|
func TestApp_ParseSliceFlagsWithMissingValue(t *testing.T) {
|
|
|
|
var parsedIntSlice []int
|
|
|
|
var parsedStringSlice []string
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
|
|
|
command := Command{
|
2015-05-31 20:50:23 +00:00
|
|
|
Name: "cmd",
|
2015-07-20 19:18:25 +00:00
|
|
|
Flags: []Flag{
|
|
|
|
IntSliceFlag{Name: "a", Usage: "set numbers"},
|
|
|
|
StringSliceFlag{Name: "str", Usage: "set strings"},
|
2015-05-31 20:50:23 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(c *Context) error {
|
2015-05-31 20:50:23 +00:00
|
|
|
parsedIntSlice = c.IntSlice("a")
|
|
|
|
parsedStringSlice = c.StringSlice("str")
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2015-05-31 20:50:23 +00:00
|
|
|
},
|
|
|
|
}
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Commands = []Command{command}
|
2015-05-31 20:50:23 +00:00
|
|
|
|
|
|
|
app.Run([]string{"", "cmd", "my-arg", "-a", "2", "-str", "A"})
|
|
|
|
|
|
|
|
var expectedIntSlice = []int{2}
|
|
|
|
var expectedStringSlice = []string{"A"}
|
|
|
|
|
|
|
|
if parsedIntSlice[0] != expectedIntSlice[0] {
|
|
|
|
t.Errorf("%v does not match %v", parsedIntSlice[0], expectedIntSlice[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
if parsedStringSlice[0] != expectedStringSlice[0] {
|
|
|
|
t.Errorf("%v does not match %v", parsedIntSlice[0], expectedIntSlice[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-12 07:39:13 +00:00
|
|
|
func TestApp_DefaultStdout(t *testing.T) {
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
2014-06-12 07:39:13 +00:00
|
|
|
|
2014-12-02 04:50:04 +00:00
|
|
|
if app.Writer != os.Stdout {
|
2014-06-12 07:39:13 +00:00
|
|
|
t.Error("Default output writer not set.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-10 15:31:53 +00:00
|
|
|
type mockWriter struct {
|
2014-06-12 07:39:13 +00:00
|
|
|
written []byte
|
|
|
|
}
|
|
|
|
|
2014-12-10 15:31:53 +00:00
|
|
|
func (fw *mockWriter) Write(p []byte) (n int, err error) {
|
2014-06-12 07:39:13 +00:00
|
|
|
if fw.written == nil {
|
|
|
|
fw.written = p
|
|
|
|
} else {
|
|
|
|
fw.written = append(fw.written, p...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
2014-12-10 15:31:53 +00:00
|
|
|
func (fw *mockWriter) GetWritten() (b []byte) {
|
2014-06-12 07:39:13 +00:00
|
|
|
return fw.written
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApp_SetStdout(t *testing.T) {
|
2014-12-10 15:31:53 +00:00
|
|
|
w := &mockWriter{}
|
2014-06-12 07:39:13 +00:00
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
2014-06-12 07:39:13 +00:00
|
|
|
app.Name = "test"
|
2014-12-10 15:31:53 +00:00
|
|
|
app.Writer = w
|
2014-06-12 07:39:13 +00:00
|
|
|
|
2016-04-27 13:12:34 +00:00
|
|
|
err := app.Run([]string{"help"})
|
2014-06-12 07:39:13 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Run error: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-12-10 15:31:53 +00:00
|
|
|
if len(w.written) == 0 {
|
2014-06-12 07:39:13 +00:00
|
|
|
t.Error("App did not write output to desired writer.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-04 16:14:33 +00:00
|
|
|
func TestApp_BeforeFunc(t *testing.T) {
|
2016-04-28 20:05:14 +00:00
|
|
|
counts := &opCounts{}
|
2014-01-04 16:14:33 +00:00
|
|
|
beforeError := fmt.Errorf("fail")
|
|
|
|
var err error
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
2014-01-04 16:14:33 +00:00
|
|
|
|
2016-04-27 13:12:34 +00:00
|
|
|
app.Before = func(c *Context) error {
|
2016-04-28 20:05:14 +00:00
|
|
|
counts.Total++
|
|
|
|
counts.Before = counts.Total
|
2014-01-04 16:14:33 +00:00
|
|
|
s := c.String("opt")
|
|
|
|
if s == "fail" {
|
2016-04-27 13:12:34 +00:00
|
|
|
return beforeError
|
2014-01-04 16:14:33 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-01-04 16:14:33 +00:00
|
|
|
}
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Commands = []Command{
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2014-01-04 16:14:33 +00:00
|
|
|
Name: "sub",
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(c *Context) error {
|
2016-04-28 20:05:14 +00:00
|
|
|
counts.Total++
|
|
|
|
counts.SubCommand = counts.Total
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-01-04 16:14:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Flags = []Flag{
|
|
|
|
StringFlag{Name: "opt"},
|
2014-01-04 16:14:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// run with the Before() func succeeding
|
2016-04-27 13:12:34 +00:00
|
|
|
err = app.Run([]string{"command", "--opt", "succeed", "sub"})
|
2014-01-04 16:14:33 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Run error: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-04-28 20:05:14 +00:00
|
|
|
if counts.Before != 1 {
|
2014-01-04 16:14:33 +00:00
|
|
|
t.Errorf("Before() not executed when expected")
|
|
|
|
}
|
|
|
|
|
2016-04-28 20:05:14 +00:00
|
|
|
if counts.SubCommand != 2 {
|
2014-01-04 16:14:33 +00:00
|
|
|
t.Errorf("Subcommand not executed when expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset
|
2016-04-28 20:05:14 +00:00
|
|
|
counts = &opCounts{}
|
2014-01-04 16:14:33 +00:00
|
|
|
|
|
|
|
// run with the Before() func failing
|
2016-04-27 13:12:34 +00:00
|
|
|
err = app.Run([]string{"command", "--opt", "fail", "sub"})
|
2014-01-04 16:14:33 +00:00
|
|
|
|
|
|
|
// should be the same error produced by the Before func
|
|
|
|
if err != beforeError {
|
|
|
|
t.Errorf("Run error expected, but not received")
|
|
|
|
}
|
|
|
|
|
2016-04-28 20:05:14 +00:00
|
|
|
if counts.Before != 1 {
|
2014-01-04 16:14:33 +00:00
|
|
|
t.Errorf("Before() not executed when expected")
|
|
|
|
}
|
|
|
|
|
2016-04-28 20:05:14 +00:00
|
|
|
if counts.SubCommand != 0 {
|
2014-01-04 16:14:33 +00:00
|
|
|
t.Errorf("Subcommand executed when NOT expected")
|
|
|
|
}
|
2016-04-28 21:15:16 +00:00
|
|
|
|
|
|
|
// reset
|
|
|
|
counts = &opCounts{}
|
|
|
|
|
|
|
|
afterError := errors.New("fail again")
|
|
|
|
app.After = func(_ *Context) error {
|
|
|
|
return afterError
|
|
|
|
}
|
|
|
|
|
|
|
|
// run with the Before() func failing, wrapped by After()
|
|
|
|
err = app.Run([]string{"command", "--opt", "fail", "sub"})
|
|
|
|
|
|
|
|
// should be the same error produced by the Before func
|
|
|
|
if _, ok := err.(MultiError); !ok {
|
|
|
|
t.Errorf("MultiError expected, but not received")
|
|
|
|
}
|
|
|
|
|
|
|
|
if counts.Before != 1 {
|
|
|
|
t.Errorf("Before() not executed when expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
if counts.SubCommand != 0 {
|
|
|
|
t.Errorf("Subcommand executed when NOT expected")
|
|
|
|
}
|
2014-01-04 16:14:33 +00:00
|
|
|
}
|
2014-02-04 16:40:06 +00:00
|
|
|
|
2014-11-18 22:44:21 +00:00
|
|
|
func TestApp_AfterFunc(t *testing.T) {
|
2016-04-28 20:05:14 +00:00
|
|
|
counts := &opCounts{}
|
2014-11-18 22:44:21 +00:00
|
|
|
afterError := fmt.Errorf("fail")
|
|
|
|
var err error
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
2014-11-18 22:44:21 +00:00
|
|
|
|
2016-04-27 13:12:34 +00:00
|
|
|
app.After = func(c *Context) error {
|
2016-04-28 20:05:14 +00:00
|
|
|
counts.Total++
|
|
|
|
counts.After = counts.Total
|
2014-11-18 22:44:21 +00:00
|
|
|
s := c.String("opt")
|
|
|
|
if s == "fail" {
|
2016-04-27 13:12:34 +00:00
|
|
|
return afterError
|
2014-11-18 22:44:21 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-11-18 22:44:21 +00:00
|
|
|
}
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Commands = []Command{
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2014-11-18 22:44:21 +00:00
|
|
|
Name: "sub",
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(c *Context) error {
|
2016-04-28 20:05:14 +00:00
|
|
|
counts.Total++
|
|
|
|
counts.SubCommand = counts.Total
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-11-18 22:44:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Flags = []Flag{
|
|
|
|
StringFlag{Name: "opt"},
|
2014-11-18 22:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// run with the After() func succeeding
|
2016-04-27 13:12:34 +00:00
|
|
|
err = app.Run([]string{"command", "--opt", "succeed", "sub"})
|
2014-11-18 22:44:21 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Run error: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-04-28 20:05:14 +00:00
|
|
|
if counts.After != 2 {
|
2014-11-18 22:44:21 +00:00
|
|
|
t.Errorf("After() not executed when expected")
|
|
|
|
}
|
|
|
|
|
2016-04-28 20:05:14 +00:00
|
|
|
if counts.SubCommand != 1 {
|
2014-11-18 22:44:21 +00:00
|
|
|
t.Errorf("Subcommand not executed when expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset
|
2016-04-28 20:05:14 +00:00
|
|
|
counts = &opCounts{}
|
2014-11-18 22:44:21 +00:00
|
|
|
|
|
|
|
// run with the Before() func failing
|
2016-04-27 13:12:34 +00:00
|
|
|
err = app.Run([]string{"command", "--opt", "fail", "sub"})
|
2014-11-18 22:44:21 +00:00
|
|
|
|
|
|
|
// should be the same error produced by the Before func
|
|
|
|
if err != afterError {
|
|
|
|
t.Errorf("Run error expected, but not received")
|
|
|
|
}
|
|
|
|
|
2016-04-28 20:05:14 +00:00
|
|
|
if counts.After != 2 {
|
2014-11-18 22:44:21 +00:00
|
|
|
t.Errorf("After() not executed when expected")
|
|
|
|
}
|
|
|
|
|
2016-04-28 20:05:14 +00:00
|
|
|
if counts.SubCommand != 1 {
|
2014-11-18 22:44:21 +00:00
|
|
|
t.Errorf("Subcommand not executed when expected")
|
|
|
|
}
|
|
|
|
}
|
2014-02-04 16:40:06 +00:00
|
|
|
|
2014-12-02 04:20:21 +00:00
|
|
|
func TestAppNoHelpFlag(t *testing.T) {
|
2015-07-20 19:18:25 +00:00
|
|
|
oldFlag := HelpFlag
|
2014-12-02 04:20:21 +00:00
|
|
|
defer func() {
|
2015-07-20 19:18:25 +00:00
|
|
|
HelpFlag = oldFlag
|
2014-12-02 04:20:21 +00:00
|
|
|
}()
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
HelpFlag = BoolFlag{}
|
2014-12-02 04:20:21 +00:00
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
2015-11-28 17:26:10 +00:00
|
|
|
app.Writer = ioutil.Discard
|
2016-04-27 13:12:34 +00:00
|
|
|
err := app.Run([]string{"test", "-h"})
|
2014-12-02 04:20:21 +00:00
|
|
|
|
|
|
|
if err != flag.ErrHelp {
|
|
|
|
t.Errorf("expected error about missing help flag, but got: %s (%T)", err, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-13 20:57:06 +00:00
|
|
|
func TestRequiredFlagAppRunBehavior(t *testing.T) {
|
2019-07-13 10:44:39 +00:00
|
|
|
tdata := []struct {
|
2019-07-29 05:19:35 +00:00
|
|
|
testCase string
|
2019-08-02 03:27:51 +00:00
|
|
|
appFlags []Flag
|
2019-07-29 05:19:35 +00:00
|
|
|
appRunInput []string
|
2019-08-02 03:30:43 +00:00
|
|
|
appCommands []Command
|
2019-07-29 05:19:35 +00:00
|
|
|
expectedAnError bool
|
2019-07-13 10:44:39 +00:00
|
|
|
}{
|
2019-08-02 04:48:52 +00:00
|
|
|
// assertion: empty input, when a required flag is present, errors
|
2019-07-13 10:44:39 +00:00
|
|
|
{
|
2019-08-02 04:46:56 +00:00
|
|
|
testCase: "error_case_empty_input_with_required_flag_on_app",
|
2019-08-02 03:27:51 +00:00
|
|
|
appRunInput: []string{"myCLI"},
|
|
|
|
appFlags: []Flag{StringFlag{Name: "requiredFlag", Required: true}},
|
2019-07-29 05:19:35 +00:00
|
|
|
expectedAnError: true,
|
2019-07-13 10:51:26 +00:00
|
|
|
},
|
2019-08-02 03:58:08 +00:00
|
|
|
{
|
2019-08-02 04:46:56 +00:00
|
|
|
testCase: "error_case_empty_input_with_required_flag_on_command",
|
2019-08-02 03:58:08 +00:00
|
|
|
appRunInput: []string{"myCLI", "myCommand"},
|
|
|
|
appCommands: []Command{Command{
|
|
|
|
Name: "myCommand",
|
|
|
|
Flags: []Flag{StringFlag{Name: "requiredFlag", Required: true}},
|
|
|
|
}},
|
|
|
|
expectedAnError: true,
|
|
|
|
},
|
|
|
|
{
|
2019-08-02 04:46:56 +00:00
|
|
|
testCase: "error_case_empty_input_with_required_flag_on_subcommand",
|
2019-08-02 03:58:08 +00:00
|
|
|
appRunInput: []string{"myCLI", "myCommand", "mySubCommand"},
|
|
|
|
appCommands: []Command{Command{
|
|
|
|
Name: "myCommand",
|
|
|
|
Subcommands: []Command{Command{
|
|
|
|
Name: "mySubCommand",
|
|
|
|
Flags: []Flag{StringFlag{Name: "requiredFlag", Required: true}},
|
|
|
|
}},
|
|
|
|
}},
|
|
|
|
expectedAnError: true,
|
|
|
|
},
|
2019-08-02 04:48:52 +00:00
|
|
|
// assertion: inputing --help, when a required flag is present, does not error
|
2019-07-13 10:51:26 +00:00
|
|
|
{
|
2019-08-02 04:46:56 +00:00
|
|
|
testCase: "valid_case_help_input_with_required_flag_on_app",
|
2019-08-02 03:27:51 +00:00
|
|
|
appRunInput: []string{"myCLI", "--help"},
|
|
|
|
appFlags: []Flag{StringFlag{Name: "requiredFlag", Required: true}},
|
2019-07-13 10:51:26 +00:00
|
|
|
},
|
|
|
|
{
|
2019-08-02 04:46:56 +00:00
|
|
|
testCase: "valid_case_help_input_with_required_flag_on_command",
|
2019-08-02 03:39:37 +00:00
|
|
|
appRunInput: []string{"myCLI", "myCommand", "--help"},
|
|
|
|
appCommands: []Command{Command{
|
|
|
|
Name: "myCommand",
|
|
|
|
Flags: []Flag{StringFlag{Name: "requiredFlag", Required: true}},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
2019-08-02 04:46:56 +00:00
|
|
|
testCase: "valid_case_help_input_with_required_flag_on_subcommand",
|
2019-08-02 03:39:37 +00:00
|
|
|
appRunInput: []string{"myCLI", "myCommand", "mySubCommand", "--help"},
|
|
|
|
appCommands: []Command{Command{
|
|
|
|
Name: "myCommand",
|
|
|
|
Subcommands: []Command{Command{
|
|
|
|
Name: "mySubCommand",
|
|
|
|
Flags: []Flag{StringFlag{Name: "requiredFlag", Required: true}},
|
|
|
|
}},
|
|
|
|
}},
|
|
|
|
},
|
2019-08-02 04:48:52 +00:00
|
|
|
// assertion: giving optional input, when a required flag is present, errors
|
2019-08-02 03:39:37 +00:00
|
|
|
{
|
2019-08-02 04:46:56 +00:00
|
|
|
testCase: "error_case_optional_input_with_required_flag_on_app",
|
2019-08-02 03:27:51 +00:00
|
|
|
appRunInput: []string{"myCLI", "--optional", "cats"},
|
|
|
|
appFlags: []Flag{StringFlag{Name: "requiredFlag", Required: true}, StringFlag{Name: "optional"}},
|
2019-07-29 05:19:35 +00:00
|
|
|
expectedAnError: true,
|
2019-07-13 10:44:39 +00:00
|
|
|
},
|
2019-08-02 04:45:11 +00:00
|
|
|
{
|
2019-08-02 04:46:56 +00:00
|
|
|
testCase: "error_case_optional_input_with_required_flag_on_command",
|
2019-08-02 04:45:11 +00:00
|
|
|
appRunInput: []string{"myCLI", "myCommand", "--optional", "cats"},
|
|
|
|
appCommands: []Command{Command{
|
|
|
|
Name: "myCommand",
|
|
|
|
Flags: []Flag{StringFlag{Name: "requiredFlag", Required: true}, StringFlag{Name: "optional"}},
|
|
|
|
}},
|
|
|
|
expectedAnError: true,
|
|
|
|
},
|
|
|
|
{
|
2019-08-02 04:46:56 +00:00
|
|
|
testCase: "error_case_optional_input_with_required_flag_on_subcommand",
|
2019-08-02 04:45:11 +00:00
|
|
|
appRunInput: []string{"myCLI", "myCommand", "mySubCommand", "--optional", "cats"},
|
|
|
|
appCommands: []Command{Command{
|
|
|
|
Name: "myCommand",
|
|
|
|
Subcommands: []Command{Command{
|
|
|
|
Name: "mySubCommand",
|
|
|
|
Flags: []Flag{StringFlag{Name: "requiredFlag", Required: true}, StringFlag{Name: "optional"}},
|
|
|
|
}},
|
|
|
|
}},
|
|
|
|
expectedAnError: true,
|
|
|
|
},
|
2019-08-02 04:48:52 +00:00
|
|
|
// assertion: when a required flag is present, inputting that required flag does not error
|
2019-08-02 03:27:51 +00:00
|
|
|
{
|
2019-08-02 04:46:56 +00:00
|
|
|
testCase: "valid_case_required_flag_input_on_app",
|
2019-08-02 03:27:51 +00:00
|
|
|
appRunInput: []string{"myCLI", "--requiredFlag", "cats"},
|
|
|
|
appFlags: []Flag{StringFlag{Name: "requiredFlag", Required: true}},
|
|
|
|
},
|
2019-08-02 03:35:23 +00:00
|
|
|
{
|
2019-08-02 04:46:56 +00:00
|
|
|
testCase: "valid_case_required_flag_input_on_command",
|
2019-08-02 03:35:23 +00:00
|
|
|
appRunInput: []string{"myCLI", "myCommand", "--requiredFlag", "cats"},
|
|
|
|
appCommands: []Command{Command{
|
|
|
|
Name: "myCommand",
|
|
|
|
Flags: []Flag{StringFlag{Name: "requiredFlag", Required: true}},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
2019-08-02 04:46:56 +00:00
|
|
|
testCase: "valid_case_required_flag_input_on_subcommand",
|
2019-08-02 03:35:23 +00:00
|
|
|
appRunInput: []string{"myCLI", "myCommand", "mySubCommand", "--requiredFlag", "cats"},
|
|
|
|
appCommands: []Command{Command{
|
|
|
|
Name: "myCommand",
|
|
|
|
Subcommands: []Command{Command{
|
|
|
|
Name: "mySubCommand",
|
|
|
|
Flags: []Flag{StringFlag{Name: "requiredFlag", Required: true}},
|
|
|
|
}},
|
|
|
|
}},
|
|
|
|
},
|
2014-02-04 16:40:06 +00:00
|
|
|
}
|
2019-07-13 10:44:39 +00:00
|
|
|
for _, test := range tdata {
|
|
|
|
t.Run(test.testCase, func(t *testing.T) {
|
2019-08-02 06:27:34 +00:00
|
|
|
// setup
|
2019-07-13 10:44:39 +00:00
|
|
|
app := NewApp()
|
2019-08-02 03:27:51 +00:00
|
|
|
app.Flags = test.appFlags
|
2019-08-02 03:30:43 +00:00
|
|
|
app.Commands = test.appCommands
|
2019-07-13 20:57:06 +00:00
|
|
|
|
|
|
|
// logic under test
|
2019-07-13 10:51:26 +00:00
|
|
|
err := app.Run(test.appRunInput)
|
2019-07-13 10:44:39 +00:00
|
|
|
|
2019-07-13 20:57:06 +00:00
|
|
|
// assertions
|
|
|
|
if test.expectedAnError && err == nil {
|
|
|
|
t.Errorf("expected an error, but there was none")
|
|
|
|
}
|
2019-08-02 04:35:15 +00:00
|
|
|
if _, ok := err.(requiredFlagsErr); test.expectedAnError && !ok {
|
|
|
|
t.Errorf("expected a requiredFlagsErr, but got: %s", err)
|
|
|
|
}
|
2019-07-13 20:57:06 +00:00
|
|
|
if !test.expectedAnError && err != nil {
|
2019-07-13 10:44:39 +00:00
|
|
|
t.Errorf("did not expected an error, but there was one: %s", err)
|
|
|
|
}
|
|
|
|
})
|
2014-02-04 16:40:06 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-31 03:40:46 +00:00
|
|
|
|
2014-02-04 16:40:06 +00:00
|
|
|
func TestAppHelpPrinter(t *testing.T) {
|
2015-07-20 19:18:25 +00:00
|
|
|
oldPrinter := HelpPrinter
|
2014-02-04 16:40:06 +00:00
|
|
|
defer func() {
|
2015-07-20 19:18:25 +00:00
|
|
|
HelpPrinter = oldPrinter
|
2014-02-04 16:40:06 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
var wasCalled = false
|
2015-07-20 19:18:25 +00:00
|
|
|
HelpPrinter = func(w io.Writer, template string, data interface{}) {
|
2014-02-04 16:40:06 +00:00
|
|
|
wasCalled = true
|
|
|
|
}
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
2014-02-04 16:40:06 +00:00
|
|
|
app.Run([]string{"-h"})
|
|
|
|
|
|
|
|
if wasCalled == false {
|
|
|
|
t.Errorf("Help printer expected to be called, but was not")
|
|
|
|
}
|
|
|
|
}
|
2014-03-31 03:40:46 +00:00
|
|
|
|
2016-04-28 20:05:14 +00:00
|
|
|
func TestApp_VersionPrinter(t *testing.T) {
|
2015-07-20 19:18:25 +00:00
|
|
|
oldPrinter := VersionPrinter
|
2014-06-18 04:37:55 +00:00
|
|
|
defer func() {
|
2015-07-20 19:18:25 +00:00
|
|
|
VersionPrinter = oldPrinter
|
2014-06-18 04:37:55 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
var wasCalled = false
|
2015-07-20 19:18:25 +00:00
|
|
|
VersionPrinter = func(c *Context) {
|
2014-06-18 04:37:55 +00:00
|
|
|
wasCalled = true
|
|
|
|
}
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
|
|
|
ctx := NewContext(app, nil, nil)
|
|
|
|
ShowVersion(ctx)
|
2014-06-18 04:37:55 +00:00
|
|
|
|
|
|
|
if wasCalled == false {
|
|
|
|
t.Errorf("Version printer expected to be called, but was not")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 20:05:14 +00:00
|
|
|
func TestApp_CommandNotFound(t *testing.T) {
|
|
|
|
counts := &opCounts{}
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
2014-03-31 03:40:46 +00:00
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app.CommandNotFound = func(c *Context, command string) {
|
2016-04-28 20:05:14 +00:00
|
|
|
counts.Total++
|
|
|
|
counts.CommandNotFound = counts.Total
|
2014-03-31 03:40:46 +00:00
|
|
|
}
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Commands = []Command{
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2014-03-31 03:40:46 +00:00
|
|
|
Name: "bar",
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(c *Context) error {
|
2016-04-28 20:05:14 +00:00
|
|
|
counts.Total++
|
|
|
|
counts.SubCommand = counts.Total
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-03-31 03:40:46 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Run([]string{"command", "foo"})
|
|
|
|
|
2016-04-28 20:05:14 +00:00
|
|
|
expect(t, counts.CommandNotFound, 1)
|
|
|
|
expect(t, counts.SubCommand, 0)
|
|
|
|
expect(t, counts.Total, 1)
|
2014-03-31 03:40:46 +00:00
|
|
|
}
|
2014-07-06 09:04:48 +00:00
|
|
|
|
2016-04-28 20:05:14 +00:00
|
|
|
func TestApp_OrderOfOperations(t *testing.T) {
|
|
|
|
counts := &opCounts{}
|
|
|
|
|
|
|
|
resetCounts := func() { counts = &opCounts{} }
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
2016-04-28 20:05:14 +00:00
|
|
|
app.EnableBashCompletion = true
|
|
|
|
app.BashComplete = func(c *Context) {
|
|
|
|
counts.Total++
|
|
|
|
counts.BashComplete = counts.Total
|
2015-06-29 21:20:27 +00:00
|
|
|
}
|
2016-04-28 20:20:02 +00:00
|
|
|
|
2016-04-28 20:05:14 +00:00
|
|
|
app.OnUsageError = func(c *Context, err error, isSubcommand bool) error {
|
|
|
|
counts.Total++
|
|
|
|
counts.OnUsageError = counts.Total
|
|
|
|
return errors.New("hay OnUsageError")
|
2015-06-29 21:20:27 +00:00
|
|
|
}
|
|
|
|
|
2016-04-28 20:05:14 +00:00
|
|
|
beforeNoError := func(c *Context) error {
|
|
|
|
counts.Total++
|
|
|
|
counts.Before = counts.Total
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2015-06-29 21:20:27 +00:00
|
|
|
}
|
|
|
|
|
2016-04-28 20:05:14 +00:00
|
|
|
beforeError := func(c *Context) error {
|
|
|
|
counts.Total++
|
|
|
|
counts.Before = counts.Total
|
|
|
|
return errors.New("hay Before")
|
|
|
|
}
|
2015-06-29 21:20:27 +00:00
|
|
|
|
2016-04-28 20:05:14 +00:00
|
|
|
app.Before = beforeNoError
|
|
|
|
app.CommandNotFound = func(c *Context, command string) {
|
|
|
|
counts.Total++
|
|
|
|
counts.CommandNotFound = counts.Total
|
2014-07-06 09:04:48 +00:00
|
|
|
}
|
|
|
|
|
2016-04-28 20:05:14 +00:00
|
|
|
afterNoError := func(c *Context) error {
|
|
|
|
counts.Total++
|
|
|
|
counts.After = counts.Total
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
afterError := func(c *Context) error {
|
|
|
|
counts.Total++
|
|
|
|
counts.After = counts.Total
|
|
|
|
return errors.New("hay After")
|
2014-07-06 09:04:48 +00:00
|
|
|
}
|
|
|
|
|
2016-04-28 20:05:14 +00:00
|
|
|
app.After = afterNoError
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Commands = []Command{
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2016-04-28 20:05:14 +00:00
|
|
|
Name: "bar",
|
2016-04-28 20:20:02 +00:00
|
|
|
Action: func(c *Context) error {
|
2016-04-28 20:05:14 +00:00
|
|
|
counts.Total++
|
|
|
|
counts.SubCommand = counts.Total
|
2016-04-28 20:20:02 +00:00
|
|
|
return nil
|
2014-07-06 09:04:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-04-28 21:15:16 +00:00
|
|
|
app.Action = func(c *Context) error {
|
2016-04-28 20:05:14 +00:00
|
|
|
counts.Total++
|
|
|
|
counts.Action = counts.Total
|
2016-04-28 21:15:16 +00:00
|
|
|
return nil
|
2016-04-28 20:05:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_ = app.Run([]string{"command", "--nope"})
|
|
|
|
expect(t, counts.OnUsageError, 1)
|
|
|
|
expect(t, counts.Total, 1)
|
|
|
|
|
|
|
|
resetCounts()
|
|
|
|
|
|
|
|
_ = app.Run([]string{"command", "--generate-bash-completion"})
|
|
|
|
expect(t, counts.BashComplete, 1)
|
|
|
|
expect(t, counts.Total, 1)
|
|
|
|
|
|
|
|
resetCounts()
|
|
|
|
|
|
|
|
oldOnUsageError := app.OnUsageError
|
|
|
|
app.OnUsageError = nil
|
|
|
|
_ = app.Run([]string{"command", "--nope"})
|
|
|
|
expect(t, counts.Total, 0)
|
|
|
|
app.OnUsageError = oldOnUsageError
|
|
|
|
|
|
|
|
resetCounts()
|
|
|
|
|
|
|
|
_ = app.Run([]string{"command", "foo"})
|
|
|
|
expect(t, counts.OnUsageError, 0)
|
|
|
|
expect(t, counts.Before, 1)
|
|
|
|
expect(t, counts.CommandNotFound, 0)
|
|
|
|
expect(t, counts.Action, 2)
|
|
|
|
expect(t, counts.After, 3)
|
|
|
|
expect(t, counts.Total, 3)
|
|
|
|
|
|
|
|
resetCounts()
|
|
|
|
|
|
|
|
app.Before = beforeError
|
|
|
|
_ = app.Run([]string{"command", "bar"})
|
|
|
|
expect(t, counts.OnUsageError, 0)
|
|
|
|
expect(t, counts.Before, 1)
|
|
|
|
expect(t, counts.After, 2)
|
|
|
|
expect(t, counts.Total, 2)
|
|
|
|
app.Before = beforeNoError
|
|
|
|
|
|
|
|
resetCounts()
|
|
|
|
|
|
|
|
app.After = nil
|
|
|
|
_ = app.Run([]string{"command", "bar"})
|
|
|
|
expect(t, counts.OnUsageError, 0)
|
|
|
|
expect(t, counts.Before, 1)
|
|
|
|
expect(t, counts.SubCommand, 2)
|
|
|
|
expect(t, counts.Total, 2)
|
|
|
|
app.After = afterNoError
|
|
|
|
|
|
|
|
resetCounts()
|
|
|
|
|
|
|
|
app.After = afterError
|
|
|
|
err := app.Run([]string{"command", "bar"})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected a non-nil error")
|
|
|
|
}
|
|
|
|
expect(t, counts.OnUsageError, 0)
|
|
|
|
expect(t, counts.Before, 1)
|
|
|
|
expect(t, counts.SubCommand, 2)
|
|
|
|
expect(t, counts.After, 3)
|
|
|
|
expect(t, counts.Total, 3)
|
|
|
|
app.After = afterNoError
|
|
|
|
|
|
|
|
resetCounts()
|
|
|
|
|
|
|
|
oldCommands := app.Commands
|
|
|
|
app.Commands = nil
|
|
|
|
_ = app.Run([]string{"command"})
|
|
|
|
expect(t, counts.OnUsageError, 0)
|
|
|
|
expect(t, counts.Before, 1)
|
|
|
|
expect(t, counts.Action, 2)
|
|
|
|
expect(t, counts.After, 3)
|
|
|
|
expect(t, counts.Total, 3)
|
|
|
|
app.Commands = oldCommands
|
2014-07-06 09:04:48 +00:00
|
|
|
}
|
2015-05-04 01:37:51 +00:00
|
|
|
|
|
|
|
func TestApp_Run_CommandWithSubcommandHasHelpTopic(t *testing.T) {
|
|
|
|
var subcommandHelpTopics = [][]string{
|
|
|
|
{"command", "foo", "--help"},
|
|
|
|
{"command", "foo", "-h"},
|
|
|
|
{"command", "foo", "help"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, flagSet := range subcommandHelpTopics {
|
|
|
|
t.Logf("==> checking with flags %v", flagSet)
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
2015-05-04 01:37:51 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
app.Writer = buf
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
subCmdBar := Command{
|
2015-05-04 01:37:51 +00:00
|
|
|
Name: "bar",
|
|
|
|
Usage: "does bar things",
|
|
|
|
}
|
2015-07-20 19:18:25 +00:00
|
|
|
subCmdBaz := Command{
|
2015-05-04 01:37:51 +00:00
|
|
|
Name: "baz",
|
|
|
|
Usage: "does baz things",
|
|
|
|
}
|
2015-07-20 19:18:25 +00:00
|
|
|
cmd := Command{
|
2015-05-04 01:37:51 +00:00
|
|
|
Name: "foo",
|
|
|
|
Description: "descriptive wall of text about how it does foo things",
|
2015-07-20 19:18:25 +00:00
|
|
|
Subcommands: []Command{subCmdBar, subCmdBaz},
|
2016-07-13 21:44:13 +00:00
|
|
|
Action: func(c *Context) error { return nil },
|
2015-05-04 01:37:51 +00:00
|
|
|
}
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Commands = []Command{cmd}
|
2016-04-27 13:12:34 +00:00
|
|
|
err := app.Run(flagSet)
|
2015-05-04 01:37:51 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
output := buf.String()
|
|
|
|
t.Logf("output: %q\n", buf.Bytes())
|
|
|
|
|
|
|
|
if strings.Contains(output, "No help topic for") {
|
|
|
|
t.Errorf("expect a help topic, got none: \n%q", output)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, shouldContain := range []string{
|
|
|
|
cmd.Name, cmd.Description,
|
|
|
|
subCmdBar.Name, subCmdBar.Usage,
|
|
|
|
subCmdBaz.Name, subCmdBaz.Usage,
|
|
|
|
} {
|
|
|
|
if !strings.Contains(output, shouldContain) {
|
|
|
|
t.Errorf("want help to contain %q, did not: \n%q", shouldContain, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-03 03:16:44 +00:00
|
|
|
|
2015-06-20 23:59:54 +00:00
|
|
|
func TestApp_Run_SubcommandFullPath(t *testing.T) {
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
2015-06-20 23:59:54 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
app.Writer = buf
|
2015-08-13 04:43:14 +00:00
|
|
|
app.Name = "command"
|
2015-07-20 19:18:25 +00:00
|
|
|
subCmd := Command{
|
2015-06-20 23:59:54 +00:00
|
|
|
Name: "bar",
|
|
|
|
Usage: "does bar things",
|
|
|
|
}
|
2015-07-20 19:18:25 +00:00
|
|
|
cmd := Command{
|
2015-06-20 23:59:54 +00:00
|
|
|
Name: "foo",
|
|
|
|
Description: "foo commands",
|
2015-07-20 19:18:25 +00:00
|
|
|
Subcommands: []Command{subCmd},
|
2015-06-20 23:59:54 +00:00
|
|
|
}
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Commands = []Command{cmd}
|
2015-06-20 23:59:54 +00:00
|
|
|
|
2016-04-27 13:12:34 +00:00
|
|
|
err := app.Run([]string{"command", "foo", "bar", "--help"})
|
2015-06-20 23:59:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
output := buf.String()
|
2015-08-13 04:43:14 +00:00
|
|
|
if !strings.Contains(output, "command foo bar - does bar things") {
|
2015-06-20 23:59:54 +00:00
|
|
|
t.Errorf("expected full path to subcommand: %s", output)
|
|
|
|
}
|
|
|
|
if !strings.Contains(output, "command foo bar [arguments...]") {
|
|
|
|
t.Errorf("expected full path to subcommand: %s", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-13 05:14:26 +00:00
|
|
|
func TestApp_Run_SubcommandHelpName(t *testing.T) {
|
|
|
|
app := NewApp()
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
app.Writer = buf
|
|
|
|
app.Name = "command"
|
|
|
|
subCmd := Command{
|
|
|
|
Name: "bar",
|
|
|
|
HelpName: "custom",
|
|
|
|
Usage: "does bar things",
|
|
|
|
}
|
|
|
|
cmd := Command{
|
|
|
|
Name: "foo",
|
|
|
|
Description: "foo commands",
|
|
|
|
Subcommands: []Command{subCmd},
|
|
|
|
}
|
|
|
|
app.Commands = []Command{cmd}
|
|
|
|
|
2016-04-27 13:12:34 +00:00
|
|
|
err := app.Run([]string{"command", "foo", "bar", "--help"})
|
2015-08-13 05:14:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
output := buf.String()
|
|
|
|
if !strings.Contains(output, "custom - does bar things") {
|
|
|
|
t.Errorf("expected HelpName for subcommand: %s", output)
|
|
|
|
}
|
|
|
|
if !strings.Contains(output, "custom [arguments...]") {
|
|
|
|
t.Errorf("expected HelpName to subcommand: %s", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApp_Run_CommandHelpName(t *testing.T) {
|
|
|
|
app := NewApp()
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
app.Writer = buf
|
|
|
|
app.Name = "command"
|
|
|
|
subCmd := Command{
|
|
|
|
Name: "bar",
|
|
|
|
Usage: "does bar things",
|
|
|
|
}
|
|
|
|
cmd := Command{
|
|
|
|
Name: "foo",
|
|
|
|
HelpName: "custom",
|
|
|
|
Description: "foo commands",
|
|
|
|
Subcommands: []Command{subCmd},
|
|
|
|
}
|
|
|
|
app.Commands = []Command{cmd}
|
|
|
|
|
2016-04-27 13:12:34 +00:00
|
|
|
err := app.Run([]string{"command", "foo", "bar", "--help"})
|
2015-08-13 05:14:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
output := buf.String()
|
|
|
|
if !strings.Contains(output, "command foo bar - does bar things") {
|
|
|
|
t.Errorf("expected full path to subcommand: %s", output)
|
|
|
|
}
|
|
|
|
if !strings.Contains(output, "command foo bar [arguments...]") {
|
|
|
|
t.Errorf("expected full path to subcommand: %s", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApp_Run_CommandSubcommandHelpName(t *testing.T) {
|
|
|
|
app := NewApp()
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
app.Writer = buf
|
|
|
|
app.Name = "base"
|
|
|
|
subCmd := Command{
|
|
|
|
Name: "bar",
|
|
|
|
HelpName: "custom",
|
|
|
|
Usage: "does bar things",
|
|
|
|
}
|
|
|
|
cmd := Command{
|
|
|
|
Name: "foo",
|
|
|
|
Description: "foo commands",
|
|
|
|
Subcommands: []Command{subCmd},
|
|
|
|
}
|
|
|
|
app.Commands = []Command{cmd}
|
|
|
|
|
2016-04-27 13:12:34 +00:00
|
|
|
err := app.Run([]string{"command", "foo", "--help"})
|
2015-08-13 05:14:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
output := buf.String()
|
|
|
|
if !strings.Contains(output, "base foo - foo commands") {
|
|
|
|
t.Errorf("expected full path to subcommand: %s", output)
|
|
|
|
}
|
|
|
|
if !strings.Contains(output, "base foo command [command options] [arguments...]") {
|
|
|
|
t.Errorf("expected full path to subcommand: %s", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-03 03:16:44 +00:00
|
|
|
func TestApp_Run_Help(t *testing.T) {
|
|
|
|
var helpArguments = [][]string{{"boom", "--help"}, {"boom", "-h"}, {"boom", "help"}}
|
|
|
|
|
|
|
|
for _, args := range helpArguments {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
t.Logf("==> checking with arguments %v", args)
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
2015-06-03 03:16:44 +00:00
|
|
|
app.Name = "boom"
|
|
|
|
app.Usage = "make an explosive entrance"
|
|
|
|
app.Writer = buf
|
2016-04-27 13:12:34 +00:00
|
|
|
app.Action = func(c *Context) error {
|
2015-06-03 03:16:44 +00:00
|
|
|
buf.WriteString("boom I say!")
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2015-06-03 03:16:44 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 13:12:34 +00:00
|
|
|
err := app.Run(args)
|
2015-06-03 03:16:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
output := buf.String()
|
|
|
|
t.Logf("output: %q\n", buf.Bytes())
|
|
|
|
|
|
|
|
if !strings.Contains(output, "boom - make an explosive entrance") {
|
|
|
|
t.Errorf("want help to contain %q, did not: \n%q", "boom - make an explosive entrance", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApp_Run_Version(t *testing.T) {
|
|
|
|
var versionArguments = [][]string{{"boom", "--version"}, {"boom", "-v"}}
|
|
|
|
|
|
|
|
for _, args := range versionArguments {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
t.Logf("==> checking with arguments %v", args)
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
2015-06-03 03:16:44 +00:00
|
|
|
app.Name = "boom"
|
|
|
|
app.Usage = "make an explosive entrance"
|
|
|
|
app.Version = "0.1.0"
|
|
|
|
app.Writer = buf
|
2016-04-27 13:12:34 +00:00
|
|
|
app.Action = func(c *Context) error {
|
2015-06-03 03:16:44 +00:00
|
|
|
buf.WriteString("boom I say!")
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2015-06-03 03:16:44 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 13:12:34 +00:00
|
|
|
err := app.Run(args)
|
2015-06-03 03:16:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
output := buf.String()
|
|
|
|
t.Logf("output: %q\n", buf.Bytes())
|
|
|
|
|
|
|
|
if !strings.Contains(output, "0.1.0") {
|
|
|
|
t.Errorf("want version to contain %q, did not: \n%q", "0.1.0", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-11 05:23:20 +00:00
|
|
|
|
2015-08-21 11:25:37 +00:00
|
|
|
func TestApp_Run_Categories(t *testing.T) {
|
|
|
|
app := NewApp()
|
|
|
|
app.Name = "categories"
|
2016-06-22 13:48:39 +00:00
|
|
|
app.HideHelp = true
|
2015-08-21 11:25:37 +00:00
|
|
|
app.Commands = []Command{
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2015-08-21 11:25:37 +00:00
|
|
|
Name: "command1",
|
|
|
|
Category: "1",
|
|
|
|
},
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2015-08-21 11:25:37 +00:00
|
|
|
Name: "command2",
|
|
|
|
Category: "1",
|
|
|
|
},
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2015-08-21 11:25:37 +00:00
|
|
|
Name: "command3",
|
|
|
|
Category: "2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
app.Writer = buf
|
|
|
|
|
|
|
|
app.Run([]string{"categories"})
|
|
|
|
|
|
|
|
expect := CommandCategories{
|
|
|
|
&CommandCategory{
|
|
|
|
Name: "1",
|
|
|
|
Commands: []Command{
|
|
|
|
app.Commands[0],
|
|
|
|
app.Commands[1],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&CommandCategory{
|
|
|
|
Name: "2",
|
|
|
|
Commands: []Command{
|
|
|
|
app.Commands[2],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(app.Categories(), expect) {
|
|
|
|
t.Fatalf("expected categories %#v, to equal %#v", app.Categories(), expect)
|
|
|
|
}
|
2016-03-20 19:17:13 +00:00
|
|
|
|
|
|
|
output := buf.String()
|
|
|
|
t.Logf("output: %q\n", buf.Bytes())
|
|
|
|
|
2016-05-03 10:54:05 +00:00
|
|
|
if !strings.Contains(output, "1:\n command1") {
|
|
|
|
t.Errorf("want buffer to include category %q, did not: \n%q", "1:\n command1", output)
|
2016-03-20 19:17:13 +00:00
|
|
|
}
|
2015-08-21 11:25:37 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 03:54:12 +00:00
|
|
|
func TestApp_VisibleCategories(t *testing.T) {
|
|
|
|
app := NewApp()
|
|
|
|
app.Name = "visible-categories"
|
2016-06-22 13:48:39 +00:00
|
|
|
app.HideHelp = true
|
2016-05-09 03:54:12 +00:00
|
|
|
app.Commands = []Command{
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2016-05-09 03:54:12 +00:00
|
|
|
Name: "command1",
|
|
|
|
Category: "1",
|
|
|
|
HelpName: "foo command1",
|
|
|
|
Hidden: true,
|
|
|
|
},
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2016-05-09 03:54:12 +00:00
|
|
|
Name: "command2",
|
|
|
|
Category: "2",
|
|
|
|
HelpName: "foo command2",
|
|
|
|
},
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2016-05-09 03:54:12 +00:00
|
|
|
Name: "command3",
|
|
|
|
Category: "3",
|
|
|
|
HelpName: "foo command3",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []*CommandCategory{
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2016-05-09 03:54:12 +00:00
|
|
|
Name: "2",
|
|
|
|
Commands: []Command{
|
|
|
|
app.Commands[1],
|
|
|
|
},
|
|
|
|
},
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2016-05-09 03:54:12 +00:00
|
|
|
Name: "3",
|
|
|
|
Commands: []Command{
|
|
|
|
app.Commands[2],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Setup()
|
|
|
|
expect(t, expected, app.VisibleCategories())
|
|
|
|
|
|
|
|
app = NewApp()
|
|
|
|
app.Name = "visible-categories"
|
2016-06-22 13:48:39 +00:00
|
|
|
app.HideHelp = true
|
2016-05-09 03:54:12 +00:00
|
|
|
app.Commands = []Command{
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2016-05-09 03:54:12 +00:00
|
|
|
Name: "command1",
|
|
|
|
Category: "1",
|
|
|
|
HelpName: "foo command1",
|
|
|
|
Hidden: true,
|
|
|
|
},
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2016-05-09 03:54:12 +00:00
|
|
|
Name: "command2",
|
|
|
|
Category: "2",
|
|
|
|
HelpName: "foo command2",
|
|
|
|
Hidden: true,
|
|
|
|
},
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2016-05-09 03:54:12 +00:00
|
|
|
Name: "command3",
|
|
|
|
Category: "3",
|
|
|
|
HelpName: "foo command3",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expected = []*CommandCategory{
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2016-05-09 03:54:12 +00:00
|
|
|
Name: "3",
|
|
|
|
Commands: []Command{
|
|
|
|
app.Commands[2],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Setup()
|
|
|
|
expect(t, expected, app.VisibleCategories())
|
|
|
|
|
|
|
|
app = NewApp()
|
|
|
|
app.Name = "visible-categories"
|
2016-06-22 13:48:39 +00:00
|
|
|
app.HideHelp = true
|
2016-05-09 03:54:12 +00:00
|
|
|
app.Commands = []Command{
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2016-05-09 03:54:12 +00:00
|
|
|
Name: "command1",
|
|
|
|
Category: "1",
|
|
|
|
HelpName: "foo command1",
|
|
|
|
Hidden: true,
|
|
|
|
},
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2016-05-09 03:54:12 +00:00
|
|
|
Name: "command2",
|
|
|
|
Category: "2",
|
|
|
|
HelpName: "foo command2",
|
|
|
|
Hidden: true,
|
|
|
|
},
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2016-05-09 03:54:12 +00:00
|
|
|
Name: "command3",
|
|
|
|
Category: "3",
|
|
|
|
HelpName: "foo command3",
|
|
|
|
Hidden: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expected = []*CommandCategory{}
|
|
|
|
|
|
|
|
app.Setup()
|
|
|
|
expect(t, expected, app.VisibleCategories())
|
|
|
|
}
|
|
|
|
|
2015-06-02 04:11:20 +00:00
|
|
|
func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
2016-04-27 13:12:34 +00:00
|
|
|
app.Action = func(c *Context) error { return nil }
|
|
|
|
app.Before = func(c *Context) error { return fmt.Errorf("before error") }
|
|
|
|
app.After = func(c *Context) error { return fmt.Errorf("after error") }
|
2015-06-02 04:11:20 +00:00
|
|
|
|
2016-04-27 13:12:34 +00:00
|
|
|
err := app.Run([]string{"foo"})
|
2015-06-02 04:11:20 +00:00
|
|
|
if err == nil {
|
2016-02-09 16:36:13 +00:00
|
|
|
t.Fatalf("expected to receive error from Run, got none")
|
2015-06-02 04:11:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(err.Error(), "before error") {
|
|
|
|
t.Errorf("expected text of error from Before method, but got none in \"%v\"", err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "after error") {
|
|
|
|
t.Errorf("expected text of error from After method, but got none in \"%v\"", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApp_Run_SubcommandDoesNotOverwriteErrorFromBefore(t *testing.T) {
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
|
|
|
app.Commands = []Command{
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2015-12-25 20:45:58 +00:00
|
|
|
Subcommands: []Command{
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2015-12-25 20:45:58 +00:00
|
|
|
Name: "sub",
|
|
|
|
},
|
|
|
|
},
|
2015-06-02 04:11:20 +00:00
|
|
|
Name: "bar",
|
2016-04-27 13:12:34 +00:00
|
|
|
Before: func(c *Context) error { return fmt.Errorf("before error") },
|
|
|
|
After: func(c *Context) error { return fmt.Errorf("after error") },
|
2015-06-02 04:11:20 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-04-27 13:12:34 +00:00
|
|
|
err := app.Run([]string{"foo", "bar"})
|
2015-06-02 04:11:20 +00:00
|
|
|
if err == nil {
|
2016-02-09 16:36:13 +00:00
|
|
|
t.Fatalf("expected to receive error from Run, got none")
|
2015-06-02 04:11:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(err.Error(), "before error") {
|
|
|
|
t.Errorf("expected text of error from Before method, but got none in \"%v\"", err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "after error") {
|
|
|
|
t.Errorf("expected text of error from After method, but got none in \"%v\"", err)
|
|
|
|
}
|
|
|
|
}
|
2016-01-23 11:47:24 +00:00
|
|
|
|
|
|
|
func TestApp_OnUsageError_WithWrongFlagValue(t *testing.T) {
|
|
|
|
app := NewApp()
|
|
|
|
app.Flags = []Flag{
|
|
|
|
IntFlag{Name: "flag"},
|
|
|
|
}
|
|
|
|
app.OnUsageError = func(c *Context, err error, isSubcommand bool) error {
|
|
|
|
if isSubcommand {
|
|
|
|
t.Errorf("Expect no subcommand")
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(err.Error(), "invalid value \"wrong\"") {
|
|
|
|
t.Errorf("Expect an invalid value error, but got \"%v\"", err)
|
|
|
|
}
|
|
|
|
return errors.New("intercepted: " + err.Error())
|
|
|
|
}
|
|
|
|
app.Commands = []Command{
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2016-01-23 11:47:24 +00:00
|
|
|
Name: "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-04-27 13:12:34 +00:00
|
|
|
err := app.Run([]string{"foo", "--flag=wrong"})
|
2016-01-23 11:47:24 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected to receive error from Run, got none")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(err.Error(), "intercepted: invalid value") {
|
|
|
|
t.Errorf("Expect an intercepted error, but got \"%v\"", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApp_OnUsageError_WithWrongFlagValue_ForSubcommand(t *testing.T) {
|
|
|
|
app := NewApp()
|
|
|
|
app.Flags = []Flag{
|
|
|
|
IntFlag{Name: "flag"},
|
|
|
|
}
|
|
|
|
app.OnUsageError = func(c *Context, err error, isSubcommand bool) error {
|
|
|
|
if isSubcommand {
|
|
|
|
t.Errorf("Expect subcommand")
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(err.Error(), "invalid value \"wrong\"") {
|
|
|
|
t.Errorf("Expect an invalid value error, but got \"%v\"", err)
|
|
|
|
}
|
|
|
|
return errors.New("intercepted: " + err.Error())
|
|
|
|
}
|
|
|
|
app.Commands = []Command{
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2016-01-23 11:47:24 +00:00
|
|
|
Name: "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-04-27 13:12:34 +00:00
|
|
|
err := app.Run([]string{"foo", "--flag=wrong", "bar"})
|
2016-01-23 11:47:24 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected to receive error from Run, got none")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(err.Error(), "intercepted: invalid value") {
|
|
|
|
t.Errorf("Expect an intercepted error, but got \"%v\"", err)
|
|
|
|
}
|
|
|
|
}
|
2016-04-28 15:03:10 +00:00
|
|
|
|
2017-05-06 03:07:18 +00:00
|
|
|
// A custom flag that conforms to the relevant interfaces, but has none of the
|
|
|
|
// fields that the other flag types do.
|
|
|
|
type customBoolFlag struct {
|
|
|
|
Nombre string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't use the normal FlagStringer
|
|
|
|
func (c *customBoolFlag) String() string {
|
|
|
|
return "***" + c.Nombre + "***"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *customBoolFlag) GetName() string {
|
|
|
|
return c.Nombre
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *customBoolFlag) Apply(set *flag.FlagSet) {
|
|
|
|
set.String(c.Nombre, c.Nombre, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCustomFlagsUnused(t *testing.T) {
|
|
|
|
app := NewApp()
|
|
|
|
app.Flags = []Flag{&customBoolFlag{"custom"}}
|
|
|
|
|
|
|
|
err := app.Run([]string{"foo"})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Run returned unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCustomFlagsUsed(t *testing.T) {
|
|
|
|
app := NewApp()
|
|
|
|
app.Flags = []Flag{&customBoolFlag{"custom"}}
|
|
|
|
|
|
|
|
err := app.Run([]string{"foo", "--custom=bar"})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Run returned unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCustomHelpVersionFlags(t *testing.T) {
|
|
|
|
app := NewApp()
|
|
|
|
|
|
|
|
// Be sure to reset the global flags
|
|
|
|
defer func(helpFlag Flag, versionFlag Flag) {
|
|
|
|
HelpFlag = helpFlag
|
|
|
|
VersionFlag = versionFlag
|
|
|
|
}(HelpFlag, VersionFlag)
|
|
|
|
|
|
|
|
HelpFlag = &customBoolFlag{"help-custom"}
|
|
|
|
VersionFlag = &customBoolFlag{"version-custom"}
|
|
|
|
|
|
|
|
err := app.Run([]string{"foo", "--help-custom=bar"})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Run returned unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 15:03:10 +00:00
|
|
|
func TestHandleAction_WithNonFuncAction(t *testing.T) {
|
|
|
|
app := NewApp()
|
|
|
|
app.Action = 42
|
2016-09-17 23:54:29 +00:00
|
|
|
fs, err := flagSet(app.Name, app.Flags)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error creating FlagSet: %s", err)
|
|
|
|
}
|
|
|
|
err = HandleAction(app.Action, NewContext(app, fs, nil))
|
2016-04-28 15:03:10 +00:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected to receive error from Run, got none")
|
|
|
|
}
|
|
|
|
|
|
|
|
exitErr, ok := err.(*ExitError)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected to receive a *ExitError")
|
|
|
|
}
|
|
|
|
|
2016-11-02 03:33:12 +00:00
|
|
|
if !strings.HasPrefix(exitErr.Error(), "ERROR invalid Action type.") {
|
2016-04-28 15:03:10 +00:00
|
|
|
t.Fatalf("expected an unknown Action error, but got: %v", exitErr.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if exitErr.ExitCode() != 2 {
|
|
|
|
t.Fatalf("expected error exit code to be 2, but got: %v", exitErr.ExitCode())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleAction_WithInvalidFuncSignature(t *testing.T) {
|
|
|
|
app := NewApp()
|
|
|
|
app.Action = func() string { return "" }
|
2016-09-17 23:54:29 +00:00
|
|
|
fs, err := flagSet(app.Name, app.Flags)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error creating FlagSet: %s", err)
|
|
|
|
}
|
|
|
|
err = HandleAction(app.Action, NewContext(app, fs, nil))
|
2016-04-28 15:03:10 +00:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected to receive error from Run, got none")
|
|
|
|
}
|
|
|
|
|
|
|
|
exitErr, ok := err.(*ExitError)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected to receive a *ExitError")
|
|
|
|
}
|
|
|
|
|
2016-11-02 03:33:12 +00:00
|
|
|
if !strings.HasPrefix(exitErr.Error(), "ERROR invalid Action type") {
|
2016-04-28 15:03:10 +00:00
|
|
|
t.Fatalf("expected an unknown Action error, but got: %v", exitErr.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if exitErr.ExitCode() != 2 {
|
|
|
|
t.Fatalf("expected error exit code to be 2, but got: %v", exitErr.ExitCode())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleAction_WithInvalidFuncReturnSignature(t *testing.T) {
|
|
|
|
app := NewApp()
|
|
|
|
app.Action = func(_ *Context) (int, error) { return 0, nil }
|
2016-09-17 23:54:29 +00:00
|
|
|
fs, err := flagSet(app.Name, app.Flags)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error creating FlagSet: %s", err)
|
|
|
|
}
|
|
|
|
err = HandleAction(app.Action, NewContext(app, fs, nil))
|
2016-04-28 15:03:10 +00:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected to receive error from Run, got none")
|
|
|
|
}
|
|
|
|
|
|
|
|
exitErr, ok := err.(*ExitError)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected to receive a *ExitError")
|
|
|
|
}
|
|
|
|
|
2016-11-02 03:33:12 +00:00
|
|
|
if !strings.HasPrefix(exitErr.Error(), "ERROR invalid Action type") {
|
2016-04-28 15:03:10 +00:00
|
|
|
t.Fatalf("expected an invalid Action signature error, but got: %v", exitErr.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if exitErr.ExitCode() != 2 {
|
|
|
|
t.Fatalf("expected error exit code to be 2, but got: %v", exitErr.ExitCode())
|
|
|
|
}
|
|
|
|
}
|
2016-06-09 21:32:00 +00:00
|
|
|
|
2017-06-28 19:52:50 +00:00
|
|
|
func TestHandleExitCoder_Default(t *testing.T) {
|
|
|
|
app := NewApp()
|
|
|
|
fs, err := flagSet(app.Name, app.Flags)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error creating FlagSet: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := NewContext(app, fs, nil)
|
2017-06-28 20:04:09 +00:00
|
|
|
app.handleExitCoder(ctx, NewExitError("Default Behavior Error", 42))
|
2017-06-28 19:52:50 +00:00
|
|
|
|
|
|
|
output := fakeErrWriter.String()
|
|
|
|
if !strings.Contains(output, "Default") {
|
|
|
|
t.Fatalf("Expected Default Behavior from Error Handler but got: %s", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleExitCoder_Custom(t *testing.T) {
|
|
|
|
app := NewApp()
|
|
|
|
fs, err := flagSet(app.Name, app.Flags)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error creating FlagSet: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
app.ExitErrHandler = func(_ *Context, _ error) {
|
|
|
|
fmt.Fprintln(ErrWriter, "I'm a Custom error handler, I print what I want!")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := NewContext(app, fs, nil)
|
2017-06-28 20:04:09 +00:00
|
|
|
app.handleExitCoder(ctx, NewExitError("Default Behavior Error", 42))
|
2017-06-28 19:52:50 +00:00
|
|
|
|
|
|
|
output := fakeErrWriter.String()
|
|
|
|
if !strings.Contains(output, "Custom") {
|
|
|
|
t.Fatalf("Expected Custom Behavior from Error Handler but got: %s", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-09 21:32:00 +00:00
|
|
|
func TestHandleAction_WithUnknownPanic(t *testing.T) {
|
|
|
|
defer func() { refute(t, recover(), nil) }()
|
|
|
|
|
|
|
|
var fn ActionFunc
|
|
|
|
|
|
|
|
app := NewApp()
|
|
|
|
app.Action = func(ctx *Context) error {
|
|
|
|
fn(ctx)
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-17 23:54:29 +00:00
|
|
|
fs, err := flagSet(app.Name, app.Flags)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error creating FlagSet: %s", err)
|
|
|
|
}
|
|
|
|
HandleAction(app.Action, NewContext(app, fs, nil))
|
2016-06-09 21:32:00 +00:00
|
|
|
}
|
2016-11-14 17:10:51 +00:00
|
|
|
|
|
|
|
func TestShellCompletionForIncompleteFlags(t *testing.T) {
|
|
|
|
app := NewApp()
|
|
|
|
app.Flags = []Flag{
|
|
|
|
IntFlag{
|
|
|
|
Name: "test-completion",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
app.EnableBashCompletion = true
|
|
|
|
app.BashComplete = func(ctx *Context) {
|
|
|
|
for _, command := range ctx.App.Commands {
|
|
|
|
if command.Hidden {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range command.Names() {
|
|
|
|
fmt.Fprintln(ctx.App.Writer, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, flag := range ctx.App.Flags {
|
|
|
|
for _, name := range strings.Split(flag.GetName(), ",") {
|
2017-05-06 03:07:18 +00:00
|
|
|
if name == BashCompletionFlag.GetName() {
|
2016-11-14 17:10:51 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch name = strings.TrimSpace(name); len(name) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
fmt.Fprintln(ctx.App.Writer, "-"+name)
|
|
|
|
default:
|
|
|
|
fmt.Fprintln(ctx.App.Writer, "--"+name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
app.Action = func(ctx *Context) error {
|
|
|
|
return fmt.Errorf("should not get here")
|
|
|
|
}
|
2017-05-06 03:07:18 +00:00
|
|
|
err := app.Run([]string{"", "--test-completion", "--" + BashCompletionFlag.GetName()})
|
2016-11-14 17:10:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("app should not return an error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2016-11-21 15:47:23 +00:00
|
|
|
|
|
|
|
func TestHandleActionActuallyWorksWithActions(t *testing.T) {
|
|
|
|
var f ActionFunc
|
|
|
|
called := false
|
|
|
|
f = func(c *Context) error {
|
|
|
|
called = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := HandleAction(f, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Should not have errored: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !called {
|
|
|
|
t.Errorf("Function was not called")
|
|
|
|
}
|
2016-06-09 21:32:00 +00:00
|
|
|
}
|