Merge branch 'master' into refactor-flag-names

This commit is contained in:
lynn [they] 2019-12-29 16:50:52 -08:00 committed by GitHub
commit 8d51211b11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 115 additions and 99 deletions

View File

@ -612,7 +612,7 @@ func TestApp_UseShortOptionHandling(t *testing.T) {
var name string
expected := "expectedName"
app := NewApp()
app := newTestApp()
app.UseShortOptionHandling = true
app.Flags = []Flag{
&BoolFlag{Name: "one", Aliases: []string{"o"}},
@ -633,7 +633,7 @@ func TestApp_UseShortOptionHandling(t *testing.T) {
}
func TestApp_UseShortOptionHandling_missing_value(t *testing.T) {
app := NewApp()
app := newTestApp()
app.UseShortOptionHandling = true
app.Flags = []Flag{
&StringFlag{Name: "name", Aliases: []string{"n"}},
@ -648,7 +648,7 @@ func TestApp_UseShortOptionHandlingCommand(t *testing.T) {
var name string
expected := "expectedName"
app := NewApp()
app := newTestApp()
app.UseShortOptionHandling = true
command := &Command{
Name: "cmd",
@ -673,7 +673,7 @@ func TestApp_UseShortOptionHandlingCommand(t *testing.T) {
}
func TestApp_UseShortOptionHandlingCommand_missing_value(t *testing.T) {
app := NewApp()
app := newTestApp()
app.UseShortOptionHandling = true
command := &Command{
Name: "cmd",
@ -692,7 +692,7 @@ func TestApp_UseShortOptionHandlingSubCommand(t *testing.T) {
var name string
expected := "expectedName"
app := NewApp()
app := newTestApp()
app.UseShortOptionHandling = true
command := &Command{
Name: "cmd",
@ -722,7 +722,7 @@ func TestApp_UseShortOptionHandlingSubCommand(t *testing.T) {
}
func TestApp_UseShortOptionHandlingSubCommand_missing_value(t *testing.T) {
app := NewApp()
app := newTestApp()
app.UseShortOptionHandling = true
command := &Command{
Name: "cmd",
@ -925,6 +925,7 @@ func TestApp_BeforeFunc(t *testing.T) {
Flags: []Flag{
&StringFlag{Name: "opt"},
},
Writer: ioutil.Discard,
}
// run with the Before() func succeeding
@ -1186,7 +1187,7 @@ func TestRequiredFlagAppRunBehavior(t *testing.T) {
for _, test := range tdata {
t.Run(test.testCase, func(t *testing.T) {
// setup
app := NewApp()
app := newTestApp()
app.Flags = test.appFlags
app.Commands = test.appCommands
@ -1280,7 +1281,6 @@ func TestApp_OrderOfOperations(t *testing.T) {
app := &App{
EnableBashCompletion: true,
BashComplete: func(c *Context) {
_, _ = fmt.Fprintf(os.Stderr, "---> BashComplete(%#v)\n", c)
counts.Total++
counts.ShellComplete = counts.Total
},
@ -1289,6 +1289,7 @@ func TestApp_OrderOfOperations(t *testing.T) {
counts.OnUsageError = counts.Total
return errors.New("hay OnUsageError")
},
Writer: ioutil.Discard,
}
beforeNoError := func(c *Context) error {
@ -1422,7 +1423,7 @@ func TestApp_Run_CommandWithSubcommandHasHelpTopic(t *testing.T) {
}
for _, flagSet := range subcommandHelpTopics {
t.Logf("==> checking with flags %v", flagSet)
t.Run(fmt.Sprintf("checking with flags %v", flagSet), func(t *testing.T) {
app := &App{}
buf := new(bytes.Buffer)
@ -1465,6 +1466,7 @@ func TestApp_Run_CommandWithSubcommandHasHelpTopic(t *testing.T) {
t.Errorf("want help to contain %q, did not: \n%q", shouldContain, output)
}
}
})
}
}
@ -1610,9 +1612,9 @@ 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.Run(fmt.Sprintf("checking with arguments %v", args), func(t *testing.T) {
t.Logf("==> checking with arguments %v", args)
buf := new(bytes.Buffer)
app := &App{
Name: "boom",
@ -1630,11 +1632,11 @@ func TestApp_Run_Help(t *testing.T) {
}
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)
}
})
}
}
@ -1642,9 +1644,9 @@ func TestApp_Run_Version(t *testing.T) {
var versionArguments = [][]string{{"boom", "--version"}, {"boom", "-v"}}
for _, args := range versionArguments {
buf := new(bytes.Buffer)
t.Run(fmt.Sprintf("checking with arguments %v", args), func(t *testing.T) {
t.Logf("==> checking with arguments %v", args)
buf := new(bytes.Buffer)
app := &App{
Name: "boom",
@ -1663,11 +1665,11 @@ func TestApp_Run_Version(t *testing.T) {
}
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)
}
})
}
}
@ -1835,6 +1837,7 @@ func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
Action: func(c *Context) error { return nil },
Before: func(c *Context) error { return fmt.Errorf("before error") },
After: func(c *Context) error { return fmt.Errorf("after error") },
Writer: ioutil.Discard,
}
err := app.Run([]string{"foo"})
@ -1980,6 +1983,7 @@ func (c *customBoolFlag) IsSet() bool {
func TestCustomFlagsUnused(t *testing.T) {
app := &App{
Flags: []Flag{&customBoolFlag{"custom"}},
Writer: ioutil.Discard,
}
err := app.Run([]string{"foo"})
@ -1991,6 +1995,7 @@ func TestCustomFlagsUnused(t *testing.T) {
func TestCustomFlagsUsed(t *testing.T) {
app := &App{
Flags: []Flag{&customBoolFlag{"custom"}},
Writer: ioutil.Discard,
}
err := app.Run([]string{"foo", "--custom=bar"})
@ -2000,7 +2005,9 @@ func TestCustomFlagsUsed(t *testing.T) {
}
func TestCustomHelpVersionFlags(t *testing.T) {
app := &App{}
app := &App{
Writer: ioutil.Discard,
}
// Be sure to reset the global flags
defer func(helpFlag Flag, versionFlag Flag) {
@ -2018,7 +2025,7 @@ func TestCustomHelpVersionFlags(t *testing.T) {
}
func TestHandleExitCoder_Default(t *testing.T) {
app := NewApp()
app := newTestApp()
fs, err := flagSet(app.Name, app.Flags)
if err != nil {
t.Errorf("error creating FlagSet: %s", err)
@ -2034,7 +2041,7 @@ func TestHandleExitCoder_Default(t *testing.T) {
}
func TestHandleExitCoder_Custom(t *testing.T) {
app := NewApp()
app := newTestApp()
fs, err := flagSet(app.Name, app.Flags)
if err != nil {
t.Errorf("error creating FlagSet: %s", err)
@ -2091,6 +2098,7 @@ func TestShellCompletionForIncompleteFlags(t *testing.T) {
Action: func(ctx *Context) error {
return fmt.Errorf("should not get here")
},
Writer: ioutil.Discard,
}
err := app.Run([]string{"", "--test-completion", "--" + "generate-bash-completion"})
if err != nil {
@ -2101,7 +2109,7 @@ func TestShellCompletionForIncompleteFlags(t *testing.T) {
func TestWhenExitSubCommandWithCodeThenAppQuitUnexpectedly(t *testing.T) {
testCode := 104
app := NewApp()
app := newTestApp()
app.Commands = []*Command{
{
Name: "cmd",
@ -2120,7 +2128,6 @@ func TestWhenExitSubCommandWithCodeThenAppQuitUnexpectedly(t *testing.T) {
var exitCodeFromExitErrHandler int
app.ExitErrHandler = func(c *Context, err error) {
if exitErr, ok := err.(ExitCoder); ok {
t.Log(exitErr)
exitCodeFromExitErrHandler = exitErr.ExitCode()
}
}
@ -2151,3 +2158,9 @@ func TestWhenExitSubCommandWithCodeThenAppQuitUnexpectedly(t *testing.T) {
t.Errorf("exitCodeFromOsExiter valeu should be %v, but its value is %v", testCode, exitCodeFromExitErrHandler)
}
}
func newTestApp() *App {
a := NewApp()
a.Writer = ioutil.Discard
return a
}

View File

@ -93,7 +93,7 @@ func TestParseAndRunShortOpts(t *testing.T) {
},
}
app := NewApp()
app := newTestApp()
app.Commands = []*Command{cmd}
err := app.Run(c.testArgs)
@ -116,6 +116,7 @@ func TestCommand_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
},
},
},
Writer: ioutil.Discard,
}
err := app.Run([]string{"foo", "bar"})
@ -317,12 +318,12 @@ func TestCommandSkipFlagParsing(t *testing.T) {
&StringFlag{Name: "flag"},
},
Action: func(c *Context) error {
fmt.Printf("%+v\n", c.String("flag"))
args = c.Args()
return nil
},
},
},
Writer: ioutil.Discard,
}
err := app.Run(c.testArgs)

View File

@ -6,7 +6,7 @@ import (
)
func testApp() *App {
app := NewApp()
app := newTestApp()
app.Name = "greet"
app.Flags = []Flag{
&StringFlag{

View File

@ -1719,6 +1719,7 @@ func TestTimestampFlagApply(t *testing.T) {
func TestTimestampFlagApply_Fail_Parse_Wrong_Layout(t *testing.T) {
fl := TimestampFlag{Name: "time", Aliases: []string{"t"}, Layout: "randomlayout"}
set := flag.NewFlagSet("test", 0)
set.SetOutput(ioutil.Discard)
_ = fl.Apply(set)
err := set.Parse([]string{"--time", "2006-01-02T15:04:05Z"})
@ -1728,6 +1729,7 @@ func TestTimestampFlagApply_Fail_Parse_Wrong_Layout(t *testing.T) {
func TestTimestampFlagApply_Fail_Parse_Wrong_Time(t *testing.T) {
fl := TimestampFlag{Name: "time", Aliases: []string{"t"}, Layout: "Jan 2, 2006 at 3:04pm (MST)"}
set := flag.NewFlagSet("test", 0)
set.SetOutput(ioutil.Discard)
_ = fl.Apply(set)
err := set.Parse([]string{"--time", "2006-01-02T15:04:05Z"})