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
|
|
|
)
|
|
|
|
|
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-25 22:10:10 +00:00
|
|
|
app.Action = func(c *Context) int {
|
2013-07-20 22:50:13 +00:00
|
|
|
fmt.Printf("Hello %v\n", c.String("name"))
|
2015-07-28 18:28:37 +00:00
|
|
|
return 0
|
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"
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Authors = []Author{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-25 22:10:10 +00:00
|
|
|
Action: func(c *Context) int {
|
2015-03-17 01:04:57 +00:00
|
|
|
fmt.Println("Hello,", c.String("name"))
|
2015-07-28 18:28:37 +00:00
|
|
|
return 0
|
2015-03-17 01:04:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Run(os.Args)
|
|
|
|
// Output:
|
|
|
|
// Hello, Jeremy
|
|
|
|
}
|
|
|
|
|
2015-11-15 21:07:28 +00:00
|
|
|
func ExampleApp_Run_help() {
|
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-25 22:10:10 +00:00
|
|
|
Action: func(c *Context) int {
|
2014-02-19 21:37:20 +00:00
|
|
|
fmt.Printf("i like to describe things")
|
2015-07-28 18:28:37 +00:00
|
|
|
return 0
|
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
|
|
|
}
|
|
|
|
|
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-25 22:10:10 +00:00
|
|
|
Action: func(c *Context) int {
|
2014-04-12 22:13:47 +00:00
|
|
|
fmt.Printf("i like to describe things")
|
2015-07-28 18:28:37 +00:00
|
|
|
return 0
|
2014-04-12 22:13:47 +00:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
Name: "next",
|
|
|
|
Usage: "next example",
|
|
|
|
Description: "more stuff to see when generating bash completion",
|
2016-04-25 22:10:10 +00:00
|
|
|
Action: func(c *Context) int {
|
2014-04-12 22:13:47 +00:00
|
|
|
fmt.Printf("the next example")
|
2015-07-28 18:28:37 +00:00
|
|
|
return 0
|
2014-04-12 22:13:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Run(os.Args)
|
|
|
|
// Output:
|
|
|
|
// describeit
|
|
|
|
// d
|
|
|
|
// next
|
|
|
|
// help
|
|
|
|
// h
|
|
|
|
}
|
|
|
|
|
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-25 22:10:10 +00:00
|
|
|
app.Action = func(c *Context) int {
|
2013-11-24 13:40:21 +00:00
|
|
|
s = s + c.Args().First()
|
2015-07-28 18:28:37 +00:00
|
|
|
return 0
|
2013-09-14 22:29:57 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 18:28:37 +00:00
|
|
|
ec, err := app.Run([]string{"command", "foo"})
|
2013-11-01 13:23:19 +00:00
|
|
|
expect(t, err, nil)
|
2015-07-28 18:28:37 +00:00
|
|
|
expect(t, ec, 0)
|
|
|
|
ec, err = app.Run([]string{"command", "bar"})
|
2013-11-01 13:23:19 +00:00
|
|
|
expect(t, err, nil)
|
2015-07-28 18:28:37 +00:00
|
|
|
expect(t, ec, 0)
|
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
|
|
|
|
|
|
|
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-25 22:10:10 +00:00
|
|
|
Action: func(c *Context) int {
|
2013-09-18 16:24:20 +00:00
|
|
|
parsedOption = c.String("option")
|
2013-11-24 13:40:21 +00:00
|
|
|
firstArg = c.Args().First()
|
2015-07-28 18:28:37 +00:00
|
|
|
return 0
|
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
|
|
|
|
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-25 22:10:10 +00:00
|
|
|
Action: func(c *Context) int {
|
2015-01-09 19:46:29 +00:00
|
|
|
context = c
|
2015-07-28 18:28:37 +00:00
|
|
|
return 0
|
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-25 22:10:10 +00:00
|
|
|
Before: func(_ *Context) (int, error) { return 0, 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")
|
|
|
|
}
|
|
|
|
|
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-25 22:10:10 +00:00
|
|
|
Action: func(c *Context) int {
|
2014-12-08 17:42:04 +00:00
|
|
|
parsedOption = c.String("option")
|
|
|
|
args = c.Args()
|
2015-07-28 18:28:37 +00:00
|
|
|
return 0
|
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-25 22:10:10 +00:00
|
|
|
Action: func(c *Context) int {
|
2016-01-26 23:34:53 +00:00
|
|
|
args = c.Args()
|
2016-04-25 22:10:10 +00:00
|
|
|
return 0
|
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-25 22:10:10 +00:00
|
|
|
Action: func(c *Context) int {
|
2014-12-08 17:42:04 +00:00
|
|
|
args = c.Args()
|
2015-07-28 18:28:37 +00:00
|
|
|
return 0
|
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")
|
|
|
|
}
|
|
|
|
|
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-25 22:10:10 +00:00
|
|
|
app.Action = func(c *Context) int {
|
2013-12-03 13:42:09 +00:00
|
|
|
meters = c.Float64("height")
|
2015-07-28 18:28:37 +00:00
|
|
|
return 0
|
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 parsedOption, firstArg string
|
|
|
|
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-25 22:10:10 +00:00
|
|
|
Action: func(c *Context) int {
|
2013-09-24 19:36:01 +00:00
|
|
|
parsedIntSlice = c.IntSlice("p")
|
|
|
|
parsedStringSlice = c.StringSlice("ip")
|
|
|
|
parsedOption = c.String("option")
|
2013-11-24 13:40:21 +00:00
|
|
|
firstArg = c.Args().First()
|
2015-07-28 18:28:37 +00:00
|
|
|
return 0
|
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-25 22:10:10 +00:00
|
|
|
Action: func(c *Context) int {
|
2015-05-31 20:50:23 +00:00
|
|
|
parsedIntSlice = c.IntSlice("a")
|
|
|
|
parsedStringSlice = c.StringSlice("str")
|
2015-07-28 18:28:37 +00:00
|
|
|
return 0
|
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
|
|
|
|
2015-07-28 18:28:37 +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) {
|
|
|
|
beforeRun, subcommandRun := false, false
|
|
|
|
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-25 22:10:10 +00:00
|
|
|
app.Before = func(c *Context) (int, error) {
|
2014-01-04 16:14:33 +00:00
|
|
|
beforeRun = true
|
|
|
|
s := c.String("opt")
|
|
|
|
if s == "fail" {
|
2016-04-25 22:10:10 +00:00
|
|
|
return DefaultErrorExitCode, beforeError
|
2014-01-04 16:14:33 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 22:10:10 +00:00
|
|
|
return DefaultSuccessExitCode, nil
|
2014-01-04 16:14:33 +00:00
|
|
|
}
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Commands = []Command{
|
|
|
|
Command{
|
2014-01-04 16:14:33 +00:00
|
|
|
Name: "sub",
|
2016-04-25 22:10:10 +00:00
|
|
|
Action: func(c *Context) int {
|
2014-01-04 16:14:33 +00:00
|
|
|
subcommandRun = true
|
2016-04-25 22:10:10 +00:00
|
|
|
return DefaultSuccessExitCode
|
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
|
2015-07-28 18:28:37 +00:00
|
|
|
ec, 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if beforeRun == false {
|
|
|
|
t.Errorf("Before() not executed when expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
if subcommandRun == false {
|
|
|
|
t.Errorf("Subcommand not executed when expected")
|
|
|
|
}
|
|
|
|
|
2016-04-25 22:10:10 +00:00
|
|
|
if ec != DefaultSuccessExitCode {
|
|
|
|
t.Errorf("Expected exit code to be %d but got %d", DefaultSuccessExitCode, ec)
|
2015-07-28 18:28:37 +00:00
|
|
|
}
|
|
|
|
|
2014-01-04 16:14:33 +00:00
|
|
|
// reset
|
|
|
|
beforeRun, subcommandRun = false, false
|
|
|
|
|
|
|
|
// run with the Before() func failing
|
2015-07-28 18:28:37 +00:00
|
|
|
ec, 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
if beforeRun == false {
|
|
|
|
t.Errorf("Before() not executed when expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
if subcommandRun == true {
|
|
|
|
t.Errorf("Subcommand executed when NOT expected")
|
|
|
|
}
|
|
|
|
|
2016-04-25 22:10:10 +00:00
|
|
|
if ec != DefaultErrorExitCode {
|
|
|
|
t.Errorf("Expected exit code to be %d but got %d", DefaultErrorExitCode, ec)
|
2015-07-28 18:28:37 +00:00
|
|
|
}
|
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) {
|
|
|
|
afterRun, subcommandRun := false, false
|
|
|
|
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-25 22:10:10 +00:00
|
|
|
app.After = func(c *Context) (int, error) {
|
2014-11-18 22:44:21 +00:00
|
|
|
afterRun = true
|
|
|
|
s := c.String("opt")
|
|
|
|
if s == "fail" {
|
2016-04-25 22:10:10 +00:00
|
|
|
return DefaultErrorExitCode, afterError
|
2014-11-18 22:44:21 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 22:10:10 +00:00
|
|
|
return DefaultSuccessExitCode, nil
|
2014-11-18 22:44:21 +00:00
|
|
|
}
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Commands = []Command{
|
|
|
|
Command{
|
2014-11-18 22:44:21 +00:00
|
|
|
Name: "sub",
|
2016-04-25 22:10:10 +00:00
|
|
|
Action: func(c *Context) int {
|
2014-11-18 22:44:21 +00:00
|
|
|
subcommandRun = true
|
2016-04-25 22:10:10 +00:00
|
|
|
return DefaultSuccessExitCode
|
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
|
2015-07-28 18:28:37 +00:00
|
|
|
ec, 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if afterRun == false {
|
|
|
|
t.Errorf("After() not executed when expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
if subcommandRun == false {
|
|
|
|
t.Errorf("Subcommand not executed when expected")
|
|
|
|
}
|
|
|
|
|
2016-04-25 22:10:10 +00:00
|
|
|
if ec != DefaultSuccessExitCode {
|
|
|
|
t.Errorf("Expected exit code to be %d but got %d", DefaultSuccessExitCode, ec)
|
2015-07-28 18:28:37 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 22:44:21 +00:00
|
|
|
// reset
|
|
|
|
afterRun, subcommandRun = false, false
|
|
|
|
|
|
|
|
// run with the Before() func failing
|
2015-07-28 18:28:37 +00:00
|
|
|
ec, 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
if afterRun == false {
|
|
|
|
t.Errorf("After() not executed when expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
if subcommandRun == false {
|
|
|
|
t.Errorf("Subcommand not executed when expected")
|
|
|
|
}
|
2015-07-28 18:28:37 +00:00
|
|
|
|
2016-04-25 22:10:10 +00:00
|
|
|
if ec != DefaultErrorExitCode {
|
|
|
|
t.Errorf("Expected exit code to be %d but got %d", DefaultErrorExitCode, ec)
|
2015-07-28 18:28:37 +00:00
|
|
|
}
|
2014-11-18 22:44:21 +00:00
|
|
|
}
|
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
|
2015-07-28 18:28:37 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-06-18 04:37:55 +00:00
|
|
|
func TestAppVersionPrinter(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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-31 03:40:46 +00:00
|
|
|
func TestAppCommandNotFound(t *testing.T) {
|
|
|
|
beforeRun, subcommandRun := false, false
|
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) {
|
2014-03-31 03:40:46 +00:00
|
|
|
beforeRun = true
|
|
|
|
}
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Commands = []Command{
|
|
|
|
Command{
|
2014-03-31 03:40:46 +00:00
|
|
|
Name: "bar",
|
2016-04-25 22:10:10 +00:00
|
|
|
Action: func(c *Context) int {
|
2014-03-31 03:40:46 +00:00
|
|
|
subcommandRun = true
|
2015-07-28 18:28:37 +00:00
|
|
|
return 0
|
2014-03-31 03:40:46 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Run([]string{"command", "foo"})
|
|
|
|
|
|
|
|
expect(t, beforeRun, true)
|
|
|
|
expect(t, subcommandRun, false)
|
|
|
|
}
|
2014-07-06 09:04:48 +00:00
|
|
|
|
2015-06-29 21:20:27 +00:00
|
|
|
func TestGlobalFlag(t *testing.T) {
|
|
|
|
var globalFlag string
|
|
|
|
var globalFlagSet bool
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
|
|
|
app.Flags = []Flag{
|
|
|
|
StringFlag{Name: "global, g", Usage: "global"},
|
2015-06-29 21:20:27 +00:00
|
|
|
}
|
2016-04-25 22:10:10 +00:00
|
|
|
app.Action = func(c *Context) int {
|
2015-06-29 21:20:27 +00:00
|
|
|
globalFlag = c.GlobalString("global")
|
|
|
|
globalFlagSet = c.GlobalIsSet("global")
|
2015-07-28 18:28:37 +00:00
|
|
|
return 0
|
2015-06-29 21:20:27 +00:00
|
|
|
}
|
|
|
|
app.Run([]string{"command", "-g", "foo"})
|
|
|
|
expect(t, globalFlag, "foo")
|
|
|
|
expect(t, globalFlagSet, true)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-06 09:04:48 +00:00
|
|
|
func TestGlobalFlagsInSubcommands(t *testing.T) {
|
|
|
|
subcommandRun := false
|
2015-05-18 15:39:48 +00:00
|
|
|
parentFlag := false
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
2014-07-06 09:04:48 +00:00
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Flags = []Flag{
|
|
|
|
BoolFlag{Name: "debug, d", Usage: "Enable debugging"},
|
2014-07-06 09:04:48 +00:00
|
|
|
}
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Commands = []Command{
|
|
|
|
Command{
|
2014-07-06 09:04:48 +00:00
|
|
|
Name: "foo",
|
2015-07-20 19:18:25 +00:00
|
|
|
Flags: []Flag{
|
|
|
|
BoolFlag{Name: "parent, p", Usage: "Parent flag"},
|
2015-05-18 15:39:48 +00:00
|
|
|
},
|
2015-07-20 19:18:25 +00:00
|
|
|
Subcommands: []Command{
|
2014-07-06 09:04:48 +00:00
|
|
|
{
|
|
|
|
Name: "bar",
|
2016-04-25 22:10:10 +00:00
|
|
|
Action: func(c *Context) int {
|
2014-07-06 09:04:48 +00:00
|
|
|
if c.GlobalBool("debug") {
|
|
|
|
subcommandRun = true
|
|
|
|
}
|
2015-05-18 15:39:48 +00:00
|
|
|
if c.GlobalBool("parent") {
|
|
|
|
parentFlag = true
|
|
|
|
}
|
2015-07-28 18:28:37 +00:00
|
|
|
return 0
|
2014-07-06 09:04:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-05-18 15:39:48 +00:00
|
|
|
app.Run([]string{"command", "-d", "foo", "-p", "bar"})
|
2014-07-06 09:04:48 +00:00
|
|
|
|
|
|
|
expect(t, subcommandRun, true)
|
2015-05-18 15:39:48 +00:00
|
|
|
expect(t, parentFlag, true)
|
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},
|
2015-05-04 01:37:51 +00:00
|
|
|
}
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
app.Commands = []Command{cmd}
|
2015-07-28 18:28:37 +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-25 22:10:10 +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-25 22:10:10 +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}
|
|
|
|
|
2015-07-28 18:28:37 +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-25 22:10:10 +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-25 22:10:10 +00:00
|
|
|
app.Action = func(c *Context) int {
|
2015-06-03 03:16:44 +00:00
|
|
|
buf.WriteString("boom I say!")
|
2015-07-28 18:28:37 +00:00
|
|
|
return 0
|
2015-06-03 03:16:44 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 18:28:37 +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-25 22:10:10 +00:00
|
|
|
app.Action = func(c *Context) int {
|
2015-06-03 03:16:44 +00:00
|
|
|
buf.WriteString("boom I say!")
|
2015-07-28 18:28:37 +00:00
|
|
|
return 0
|
2015-06-03 03:16:44 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 18:28:37 +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"
|
|
|
|
app.Commands = []Command{
|
|
|
|
Command{
|
|
|
|
Name: "command1",
|
|
|
|
Category: "1",
|
|
|
|
},
|
|
|
|
Command{
|
|
|
|
Name: "command2",
|
|
|
|
Category: "1",
|
|
|
|
},
|
|
|
|
Command{
|
|
|
|
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())
|
|
|
|
|
|
|
|
if !strings.Contains(output, "1:\n command1") {
|
|
|
|
t.Errorf("want buffer to include category %q, did not: \n%q", "1:\n command1", output)
|
|
|
|
}
|
2015-08-21 11:25:37 +00:00
|
|
|
}
|
|
|
|
|
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-25 22:10:10 +00:00
|
|
|
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") }
|
2015-06-02 04:11:20 +00:00
|
|
|
|
2015-07-28 18:28:37 +00:00
|
|
|
ec, 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)
|
|
|
|
}
|
2015-07-28 18:28:37 +00:00
|
|
|
|
|
|
|
if ec != 2 {
|
|
|
|
t.Errorf("Expected exit code to be %d but got %d", 2, ec)
|
|
|
|
}
|
2015-06-02 04:11:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestApp_Run_SubcommandDoesNotOverwriteErrorFromBefore(t *testing.T) {
|
2015-07-20 19:18:25 +00:00
|
|
|
app := NewApp()
|
|
|
|
app.Commands = []Command{
|
|
|
|
Command{
|
2015-12-25 20:45:58 +00:00
|
|
|
Subcommands: []Command{
|
|
|
|
Command{
|
|
|
|
Name: "sub",
|
|
|
|
},
|
|
|
|
},
|
2015-06-02 04:11:20 +00:00
|
|
|
Name: "bar",
|
2016-04-25 22:10:10 +00:00
|
|
|
Before: func(c *Context) (int, error) { return 1, fmt.Errorf("before error") },
|
|
|
|
After: func(c *Context) (int, error) { return 2, fmt.Errorf("after error") },
|
2015-06-02 04:11:20 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-28 18:28:37 +00:00
|
|
|
ec, 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)
|
|
|
|
}
|
2015-07-28 18:28:37 +00:00
|
|
|
|
|
|
|
if ec != 2 {
|
|
|
|
t.Errorf("Expected exit code to be %d but got %d", 2, ec)
|
|
|
|
}
|
2015-06-02 04:11:20 +00:00
|
|
|
}
|
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{
|
|
|
|
Command{
|
|
|
|
Name: "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-04-25 22:10:10 +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{
|
|
|
|
Command{
|
|
|
|
Name: "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-04-25 22:10:10 +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)
|
|
|
|
}
|
|
|
|
}
|