Improve vendoring options by removing self-referential imports in tests.

This commit is contained in:
Edward Sheffler III
2015-07-20 12:18:25 -07:00
committed by Edward Sheffler III
parent bca61c476e
commit 8ea1232ede
7 changed files with 319 additions and 331 deletions

View File

@@ -1,4 +1,4 @@
package cli_test
package cli
import (
"bytes"
@@ -8,25 +8,23 @@ import (
"os"
"strings"
"testing"
"github.com/codegangsta/cli"
)
func ExampleApp() {
// set args for examples sake
os.Args = []string{"greet", "--name", "Jeremy"}
app := cli.NewApp()
app := NewApp()
app.Name = "greet"
app.Flags = []cli.Flag{
cli.StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
app.Flags = []Flag{
StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
}
app.Action = func(c *cli.Context) {
app.Action = func(c *Context) {
fmt.Printf("Hello %v\n", c.String("name"))
}
app.Author = "Harrison"
app.Email = "harrison@lolwut.com"
app.Authors = []cli.Author{cli.Author{Name: "Oliver Allen", Email: "oliver@toyshop.com"}}
app.Authors = []Author{Author{Name: "Oliver Allen", Email: "oliver@toyshop.com"}}
app.Run(os.Args)
// Output:
// Hello Jeremy
@@ -35,28 +33,28 @@ func ExampleApp() {
func ExampleAppSubcommand() {
// set args for examples sake
os.Args = []string{"say", "hi", "english", "--name", "Jeremy"}
app := cli.NewApp()
app := NewApp()
app.Name = "say"
app.Commands = []cli.Command{
app.Commands = []Command{
{
Name: "hello",
Aliases: []string{"hi"},
Usage: "use it to see a description",
Description: "This is how we describe hello the function",
Subcommands: []cli.Command{
Subcommands: []Command{
{
Name: "english",
Aliases: []string{"en"},
Usage: "sends a greeting in english",
Description: "greets someone in english",
Flags: []cli.Flag{
cli.StringFlag{
Flags: []Flag{
StringFlag{
Name: "name",
Value: "Bob",
Usage: "Name of the person to greet",
},
},
Action: func(c *cli.Context) {
Action: func(c *Context) {
fmt.Println("Hello,", c.String("name"))
},
},
@@ -73,18 +71,18 @@ func ExampleAppHelp() {
// set args for examples sake
os.Args = []string{"greet", "h", "describeit"}
app := cli.NewApp()
app := NewApp()
app.Name = "greet"
app.Flags = []cli.Flag{
cli.StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
app.Flags = []Flag{
StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
}
app.Commands = []cli.Command{
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 *cli.Context) {
Action: func(c *Context) {
fmt.Printf("i like to describe things")
},
},
@@ -105,23 +103,23 @@ func ExampleAppBashComplete() {
// set args for examples sake
os.Args = []string{"greet", "--generate-bash-completion"}
app := cli.NewApp()
app := NewApp()
app.Name = "greet"
app.EnableBashCompletion = true
app.Commands = []cli.Command{
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 *cli.Context) {
Action: func(c *Context) {
fmt.Printf("i like to describe things")
},
}, {
Name: "next",
Usage: "next example",
Description: "more stuff to see when generating bash completion",
Action: func(c *cli.Context) {
Action: func(c *Context) {
fmt.Printf("the next example")
},
},
@@ -139,8 +137,8 @@ func ExampleAppBashComplete() {
func TestApp_Run(t *testing.T) {
s := ""
app := cli.NewApp()
app.Action = func(c *cli.Context) {
app := NewApp()
app.Action = func(c *Context) {
s = s + c.Args().First()
}
@@ -164,10 +162,10 @@ var commandAppTests = []struct {
}
func TestApp_Command(t *testing.T) {
app := cli.NewApp()
fooCommand := cli.Command{Name: "foobar", Aliases: []string{"f"}}
batCommand := cli.Command{Name: "batbaz", Aliases: []string{"b"}}
app.Commands = []cli.Command{
app := NewApp()
fooCommand := Command{Name: "foobar", Aliases: []string{"f"}}
batCommand := Command{Name: "batbaz", Aliases: []string{"b"}}
app.Commands = []Command{
fooCommand,
batCommand,
}
@@ -180,18 +178,18 @@ func TestApp_Command(t *testing.T) {
func TestApp_CommandWithArgBeforeFlags(t *testing.T) {
var parsedOption, firstArg string
app := cli.NewApp()
command := cli.Command{
app := NewApp()
command := Command{
Name: "cmd",
Flags: []cli.Flag{
cli.StringFlag{Name: "option", Value: "", Usage: "some option"},
Flags: []Flag{
StringFlag{Name: "option", Value: "", Usage: "some option"},
},
Action: func(c *cli.Context) {
Action: func(c *Context) {
parsedOption = c.String("option")
firstArg = c.Args().First()
},
}
app.Commands = []cli.Command{command}
app.Commands = []Command{command}
app.Run([]string{"", "cmd", "my-arg", "--option", "my-option"})
@@ -200,23 +198,23 @@ func TestApp_CommandWithArgBeforeFlags(t *testing.T) {
}
func TestApp_RunAsSubcommandParseFlags(t *testing.T) {
var context *cli.Context
var context *Context
a := cli.NewApp()
a.Commands = []cli.Command{
a := NewApp()
a.Commands = []Command{
{
Name: "foo",
Action: func(c *cli.Context) {
Action: func(c *Context) {
context = c
},
Flags: []cli.Flag{
cli.StringFlag{
Flags: []Flag{
StringFlag{
Name: "lang",
Value: "english",
Usage: "language for the greeting",
},
},
Before: func(_ *cli.Context) error { return nil },
Before: func(_ *Context) error { return nil },
},
}
a.Run([]string{"", "foo", "--lang", "spanish", "abcd"})
@@ -229,18 +227,18 @@ func TestApp_CommandWithFlagBeforeTerminator(t *testing.T) {
var parsedOption string
var args []string
app := cli.NewApp()
command := cli.Command{
app := NewApp()
command := Command{
Name: "cmd",
Flags: []cli.Flag{
cli.StringFlag{Name: "option", Value: "", Usage: "some option"},
Flags: []Flag{
StringFlag{Name: "option", Value: "", Usage: "some option"},
},
Action: func(c *cli.Context) {
Action: func(c *Context) {
parsedOption = c.String("option")
args = c.Args()
},
}
app.Commands = []cli.Command{command}
app.Commands = []Command{command}
app.Run([]string{"", "cmd", "my-arg", "--option", "my-option", "--", "--notARealFlag"})
@@ -253,14 +251,14 @@ func TestApp_CommandWithFlagBeforeTerminator(t *testing.T) {
func TestApp_CommandWithNoFlagBeforeTerminator(t *testing.T) {
var args []string
app := cli.NewApp()
command := cli.Command{
app := NewApp()
command := Command{
Name: "cmd",
Action: func(c *cli.Context) {
Action: func(c *Context) {
args = c.Args()
},
}
app.Commands = []cli.Command{command}
app.Commands = []Command{command}
app.Run([]string{"", "cmd", "my-arg", "--", "notAFlagAtAll"})
@@ -272,11 +270,11 @@ func TestApp_CommandWithNoFlagBeforeTerminator(t *testing.T) {
func TestApp_Float64Flag(t *testing.T) {
var meters float64
app := cli.NewApp()
app.Flags = []cli.Flag{
cli.Float64Flag{Name: "height", Value: 1.5, Usage: "Set the height, in meters"},
app := NewApp()
app.Flags = []Flag{
Float64Flag{Name: "height", Value: 1.5, Usage: "Set the height, in meters"},
}
app.Action = func(c *cli.Context) {
app.Action = func(c *Context) {
meters = c.Float64("height")
}
@@ -289,21 +287,21 @@ func TestApp_ParseSliceFlags(t *testing.T) {
var parsedIntSlice []int
var parsedStringSlice []string
app := cli.NewApp()
command := cli.Command{
app := NewApp()
command := Command{
Name: "cmd",
Flags: []cli.Flag{
cli.IntSliceFlag{Name: "p", Value: &cli.IntSlice{}, Usage: "set one or more ip addr"},
cli.StringSliceFlag{Name: "ip", Value: &cli.StringSlice{}, Usage: "set one or more ports to open"},
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"},
},
Action: func(c *cli.Context) {
Action: func(c *Context) {
parsedIntSlice = c.IntSlice("p")
parsedStringSlice = c.StringSlice("ip")
parsedOption = c.String("option")
firstArg = c.Args().First()
},
}
app.Commands = []cli.Command{command}
app.Commands = []Command{command}
app.Run([]string{"", "cmd", "my-arg", "-p", "22", "-p", "80", "-ip", "8.8.8.8", "-ip", "8.8.4.4"})
@@ -346,19 +344,19 @@ func TestApp_ParseSliceFlagsWithMissingValue(t *testing.T) {
var parsedIntSlice []int
var parsedStringSlice []string
app := cli.NewApp()
command := cli.Command{
app := NewApp()
command := Command{
Name: "cmd",
Flags: []cli.Flag{
cli.IntSliceFlag{Name: "a", Usage: "set numbers"},
cli.StringSliceFlag{Name: "str", Usage: "set strings"},
Flags: []Flag{
IntSliceFlag{Name: "a", Usage: "set numbers"},
StringSliceFlag{Name: "str", Usage: "set strings"},
},
Action: func(c *cli.Context) {
Action: func(c *Context) {
parsedIntSlice = c.IntSlice("a")
parsedStringSlice = c.StringSlice("str")
},
}
app.Commands = []cli.Command{command}
app.Commands = []Command{command}
app.Run([]string{"", "cmd", "my-arg", "-a", "2", "-str", "A"})
@@ -375,7 +373,7 @@ func TestApp_ParseSliceFlagsWithMissingValue(t *testing.T) {
}
func TestApp_DefaultStdout(t *testing.T) {
app := cli.NewApp()
app := NewApp()
if app.Writer != os.Stdout {
t.Error("Default output writer not set.")
@@ -403,7 +401,7 @@ func (fw *mockWriter) GetWritten() (b []byte) {
func TestApp_SetStdout(t *testing.T) {
w := &mockWriter{}
app := cli.NewApp()
app := NewApp()
app.Name = "test"
app.Writer = w
@@ -423,9 +421,9 @@ func TestApp_BeforeFunc(t *testing.T) {
beforeError := fmt.Errorf("fail")
var err error
app := cli.NewApp()
app := NewApp()
app.Before = func(c *cli.Context) error {
app.Before = func(c *Context) error {
beforeRun = true
s := c.String("opt")
if s == "fail" {
@@ -435,17 +433,17 @@ func TestApp_BeforeFunc(t *testing.T) {
return nil
}
app.Commands = []cli.Command{
cli.Command{
app.Commands = []Command{
Command{
Name: "sub",
Action: func(c *cli.Context) {
Action: func(c *Context) {
subcommandRun = true
},
},
}
app.Flags = []cli.Flag{
cli.StringFlag{Name: "opt"},
app.Flags = []Flag{
StringFlag{Name: "opt"},
}
// run with the Before() func succeeding
@@ -489,9 +487,9 @@ func TestApp_AfterFunc(t *testing.T) {
afterError := fmt.Errorf("fail")
var err error
app := cli.NewApp()
app := NewApp()
app.After = func(c *cli.Context) error {
app.After = func(c *Context) error {
afterRun = true
s := c.String("opt")
if s == "fail" {
@@ -501,17 +499,17 @@ func TestApp_AfterFunc(t *testing.T) {
return nil
}
app.Commands = []cli.Command{
cli.Command{
app.Commands = []Command{
Command{
Name: "sub",
Action: func(c *cli.Context) {
Action: func(c *Context) {
subcommandRun = true
},
},
}
app.Flags = []cli.Flag{
cli.StringFlag{Name: "opt"},
app.Flags = []Flag{
StringFlag{Name: "opt"},
}
// run with the After() func succeeding
@@ -550,14 +548,14 @@ func TestApp_AfterFunc(t *testing.T) {
}
func TestAppNoHelpFlag(t *testing.T) {
oldFlag := cli.HelpFlag
oldFlag := HelpFlag
defer func() {
cli.HelpFlag = oldFlag
HelpFlag = oldFlag
}()
cli.HelpFlag = cli.BoolFlag{}
HelpFlag = BoolFlag{}
app := cli.NewApp()
app := NewApp()
err := app.Run([]string{"test", "-h"})
if err != flag.ErrHelp {
@@ -566,17 +564,17 @@ func TestAppNoHelpFlag(t *testing.T) {
}
func TestAppHelpPrinter(t *testing.T) {
oldPrinter := cli.HelpPrinter
oldPrinter := HelpPrinter
defer func() {
cli.HelpPrinter = oldPrinter
HelpPrinter = oldPrinter
}()
var wasCalled = false
cli.HelpPrinter = func(w io.Writer, template string, data interface{}) {
HelpPrinter = func(w io.Writer, template string, data interface{}) {
wasCalled = true
}
app := cli.NewApp()
app := NewApp()
app.Run([]string{"-h"})
if wasCalled == false {
@@ -585,19 +583,19 @@ func TestAppHelpPrinter(t *testing.T) {
}
func TestAppVersionPrinter(t *testing.T) {
oldPrinter := cli.VersionPrinter
oldPrinter := VersionPrinter
defer func() {
cli.VersionPrinter = oldPrinter
VersionPrinter = oldPrinter
}()
var wasCalled = false
cli.VersionPrinter = func(c *cli.Context) {
VersionPrinter = func(c *Context) {
wasCalled = true
}
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
cli.ShowVersion(ctx)
app := NewApp()
ctx := NewContext(app, nil, nil)
ShowVersion(ctx)
if wasCalled == false {
t.Errorf("Version printer expected to be called, but was not")
@@ -606,16 +604,16 @@ func TestAppVersionPrinter(t *testing.T) {
func TestAppCommandNotFound(t *testing.T) {
beforeRun, subcommandRun := false, false
app := cli.NewApp()
app := NewApp()
app.CommandNotFound = func(c *cli.Context, command string) {
app.CommandNotFound = func(c *Context, command string) {
beforeRun = true
}
app.Commands = []cli.Command{
cli.Command{
app.Commands = []Command{
Command{
Name: "bar",
Action: func(c *cli.Context) {
Action: func(c *Context) {
subcommandRun = true
},
},
@@ -630,11 +628,11 @@ func TestAppCommandNotFound(t *testing.T) {
func TestGlobalFlag(t *testing.T) {
var globalFlag string
var globalFlagSet bool
app := cli.NewApp()
app.Flags = []cli.Flag{
cli.StringFlag{Name: "global, g", Usage: "global"},
app := NewApp()
app.Flags = []Flag{
StringFlag{Name: "global, g", Usage: "global"},
}
app.Action = func(c *cli.Context) {
app.Action = func(c *Context) {
globalFlag = c.GlobalString("global")
globalFlagSet = c.GlobalIsSet("global")
}
@@ -647,22 +645,22 @@ func TestGlobalFlag(t *testing.T) {
func TestGlobalFlagsInSubcommands(t *testing.T) {
subcommandRun := false
parentFlag := false
app := cli.NewApp()
app := NewApp()
app.Flags = []cli.Flag{
cli.BoolFlag{Name: "debug, d", Usage: "Enable debugging"},
app.Flags = []Flag{
BoolFlag{Name: "debug, d", Usage: "Enable debugging"},
}
app.Commands = []cli.Command{
cli.Command{
app.Commands = []Command{
Command{
Name: "foo",
Flags: []cli.Flag{
cli.BoolFlag{Name: "parent, p", Usage: "Parent flag"},
Flags: []Flag{
BoolFlag{Name: "parent, p", Usage: "Parent flag"},
},
Subcommands: []cli.Command{
Subcommands: []Command{
{
Name: "bar",
Action: func(c *cli.Context) {
Action: func(c *Context) {
if c.GlobalBool("debug") {
subcommandRun = true
}
@@ -691,25 +689,25 @@ func TestApp_Run_CommandWithSubcommandHasHelpTopic(t *testing.T) {
for _, flagSet := range subcommandHelpTopics {
t.Logf("==> checking with flags %v", flagSet)
app := cli.NewApp()
app := NewApp()
buf := new(bytes.Buffer)
app.Writer = buf
subCmdBar := cli.Command{
subCmdBar := Command{
Name: "bar",
Usage: "does bar things",
}
subCmdBaz := cli.Command{
subCmdBaz := Command{
Name: "baz",
Usage: "does baz things",
}
cmd := cli.Command{
cmd := Command{
Name: "foo",
Description: "descriptive wall of text about how it does foo things",
Subcommands: []cli.Command{subCmdBar, subCmdBaz},
Subcommands: []Command{subCmdBar, subCmdBaz},
}
app.Commands = []cli.Command{cmd}
app.Commands = []Command{cmd}
err := app.Run(flagSet)
if err != nil {
@@ -736,20 +734,20 @@ func TestApp_Run_CommandWithSubcommandHasHelpTopic(t *testing.T) {
}
func TestApp_Run_SubcommandFullPath(t *testing.T) {
app := cli.NewApp()
app := NewApp()
buf := new(bytes.Buffer)
app.Writer = buf
subCmd := cli.Command{
subCmd := Command{
Name: "bar",
Usage: "does bar things",
}
cmd := cli.Command{
cmd := Command{
Name: "foo",
Description: "foo commands",
Subcommands: []cli.Command{subCmd},
Subcommands: []Command{subCmd},
}
app.Commands = []cli.Command{cmd}
app.Commands = []Command{cmd}
err := app.Run([]string{"command", "foo", "bar", "--help"})
if err != nil {
@@ -773,11 +771,11 @@ func TestApp_Run_Help(t *testing.T) {
t.Logf("==> checking with arguments %v", args)
app := cli.NewApp()
app := NewApp()
app.Name = "boom"
app.Usage = "make an explosive entrance"
app.Writer = buf
app.Action = func(c *cli.Context) {
app.Action = func(c *Context) {
buf.WriteString("boom I say!")
}
@@ -803,12 +801,12 @@ func TestApp_Run_Version(t *testing.T) {
t.Logf("==> checking with arguments %v", args)
app := cli.NewApp()
app := NewApp()
app.Name = "boom"
app.Usage = "make an explosive entrance"
app.Version = "0.1.0"
app.Writer = buf
app.Action = func(c *cli.Context) {
app.Action = func(c *Context) {
buf.WriteString("boom I say!")
}
@@ -827,10 +825,10 @@ func TestApp_Run_Version(t *testing.T) {
}
func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
app := cli.NewApp()
app.Action = func(c *cli.Context) {}
app.Before = func(c *cli.Context) error { return fmt.Errorf("before error") }
app.After = func(c *cli.Context) error { return fmt.Errorf("after error") }
app := NewApp()
app.Action = func(c *Context) {}
app.Before = func(c *Context) error { return fmt.Errorf("before error") }
app.After = func(c *Context) error { return fmt.Errorf("after error") }
err := app.Run([]string{"foo"})
if err == nil {
@@ -846,12 +844,12 @@ func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
}
func TestApp_Run_SubcommandDoesNotOverwriteErrorFromBefore(t *testing.T) {
app := cli.NewApp()
app.Commands = []cli.Command{
cli.Command{
app := NewApp()
app.Commands = []Command{
Command{
Name: "bar",
Before: func(c *cli.Context) error { return fmt.Errorf("before error") },
After: func(c *cli.Context) error { return fmt.Errorf("after error") },
Before: func(c *Context) error { return fmt.Errorf("before error") },
After: func(c *Context) error { return fmt.Errorf("after error") },
},
}