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 var name string
expected := "expectedName" expected := "expectedName"
app := NewApp() app := newTestApp()
app.UseShortOptionHandling = true app.UseShortOptionHandling = true
app.Flags = []Flag{ app.Flags = []Flag{
&BoolFlag{Name: "one", Aliases: []string{"o"}}, &BoolFlag{Name: "one", Aliases: []string{"o"}},
@ -633,7 +633,7 @@ func TestApp_UseShortOptionHandling(t *testing.T) {
} }
func TestApp_UseShortOptionHandling_missing_value(t *testing.T) { func TestApp_UseShortOptionHandling_missing_value(t *testing.T) {
app := NewApp() app := newTestApp()
app.UseShortOptionHandling = true app.UseShortOptionHandling = true
app.Flags = []Flag{ app.Flags = []Flag{
&StringFlag{Name: "name", Aliases: []string{"n"}}, &StringFlag{Name: "name", Aliases: []string{"n"}},
@ -648,7 +648,7 @@ func TestApp_UseShortOptionHandlingCommand(t *testing.T) {
var name string var name string
expected := "expectedName" expected := "expectedName"
app := NewApp() app := newTestApp()
app.UseShortOptionHandling = true app.UseShortOptionHandling = true
command := &Command{ command := &Command{
Name: "cmd", Name: "cmd",
@ -673,7 +673,7 @@ func TestApp_UseShortOptionHandlingCommand(t *testing.T) {
} }
func TestApp_UseShortOptionHandlingCommand_missing_value(t *testing.T) { func TestApp_UseShortOptionHandlingCommand_missing_value(t *testing.T) {
app := NewApp() app := newTestApp()
app.UseShortOptionHandling = true app.UseShortOptionHandling = true
command := &Command{ command := &Command{
Name: "cmd", Name: "cmd",
@ -692,7 +692,7 @@ func TestApp_UseShortOptionHandlingSubCommand(t *testing.T) {
var name string var name string
expected := "expectedName" expected := "expectedName"
app := NewApp() app := newTestApp()
app.UseShortOptionHandling = true app.UseShortOptionHandling = true
command := &Command{ command := &Command{
Name: "cmd", Name: "cmd",
@ -722,7 +722,7 @@ func TestApp_UseShortOptionHandlingSubCommand(t *testing.T) {
} }
func TestApp_UseShortOptionHandlingSubCommand_missing_value(t *testing.T) { func TestApp_UseShortOptionHandlingSubCommand_missing_value(t *testing.T) {
app := NewApp() app := newTestApp()
app.UseShortOptionHandling = true app.UseShortOptionHandling = true
command := &Command{ command := &Command{
Name: "cmd", Name: "cmd",
@ -925,6 +925,7 @@ func TestApp_BeforeFunc(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
&StringFlag{Name: "opt"}, &StringFlag{Name: "opt"},
}, },
Writer: ioutil.Discard,
} }
// run with the Before() func succeeding // run with the Before() func succeeding
@ -1186,7 +1187,7 @@ func TestRequiredFlagAppRunBehavior(t *testing.T) {
for _, test := range tdata { for _, test := range tdata {
t.Run(test.testCase, func(t *testing.T) { t.Run(test.testCase, func(t *testing.T) {
// setup // setup
app := NewApp() app := newTestApp()
app.Flags = test.appFlags app.Flags = test.appFlags
app.Commands = test.appCommands app.Commands = test.appCommands
@ -1280,7 +1281,6 @@ func TestApp_OrderOfOperations(t *testing.T) {
app := &App{ app := &App{
EnableBashCompletion: true, EnableBashCompletion: true,
BashComplete: func(c *Context) { BashComplete: func(c *Context) {
_, _ = fmt.Fprintf(os.Stderr, "---> BashComplete(%#v)\n", c)
counts.Total++ counts.Total++
counts.ShellComplete = counts.Total counts.ShellComplete = counts.Total
}, },
@ -1289,6 +1289,7 @@ func TestApp_OrderOfOperations(t *testing.T) {
counts.OnUsageError = counts.Total counts.OnUsageError = counts.Total
return errors.New("hay OnUsageError") return errors.New("hay OnUsageError")
}, },
Writer: ioutil.Discard,
} }
beforeNoError := func(c *Context) error { beforeNoError := func(c *Context) error {
@ -1422,49 +1423,50 @@ func TestApp_Run_CommandWithSubcommandHasHelpTopic(t *testing.T) {
} }
for _, flagSet := range subcommandHelpTopics { 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{} app := &App{}
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
app.Writer = buf app.Writer = buf
subCmdBar := &Command{ subCmdBar := &Command{
Name: "bar", Name: "bar",
Usage: "does bar things", Usage: "does bar things",
}
subCmdBaz := &Command{
Name: "baz",
Usage: "does baz things",
}
cmd := &Command{
Name: "foo",
Description: "descriptive wall of text about how it does foo things",
Subcommands: []*Command{subCmdBar, subCmdBaz},
Action: func(c *Context) error { return nil },
}
app.Commands = []*Command{cmd}
err := app.Run(flagSet)
if err != nil {
t.Error(err)
}
output := buf.String()
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)
} }
} subCmdBaz := &Command{
Name: "baz",
Usage: "does baz things",
}
cmd := &Command{
Name: "foo",
Description: "descriptive wall of text about how it does foo things",
Subcommands: []*Command{subCmdBar, subCmdBaz},
Action: func(c *Context) error { return nil },
}
app.Commands = []*Command{cmd}
err := app.Run(flagSet)
if err != nil {
t.Error(err)
}
output := buf.String()
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)
}
}
})
} }
} }
@ -1610,31 +1612,31 @@ func TestApp_Run_Help(t *testing.T) {
var helpArguments = [][]string{{"boom", "--help"}, {"boom", "-h"}, {"boom", "help"}} var helpArguments = [][]string{{"boom", "--help"}, {"boom", "-h"}, {"boom", "help"}}
for _, args := range helpArguments { 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{ app := &App{
Name: "boom", Name: "boom",
Usage: "make an explosive entrance", Usage: "make an explosive entrance",
Writer: buf, Writer: buf,
Action: func(c *Context) error { Action: func(c *Context) error {
buf.WriteString("boom I say!") buf.WriteString("boom I say!")
return nil return nil
}, },
} }
err := app.Run(args) err := app.Run(args)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
output := buf.String() output := buf.String()
t.Logf("output: %q\n", buf.Bytes())
if !strings.Contains(output, "boom - make an explosive entrance") { 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) t.Errorf("want help to contain %q, did not: \n%q", "boom - make an explosive entrance", output)
} }
})
} }
} }
@ -1642,32 +1644,32 @@ func TestApp_Run_Version(t *testing.T) {
var versionArguments = [][]string{{"boom", "--version"}, {"boom", "-v"}} var versionArguments = [][]string{{"boom", "--version"}, {"boom", "-v"}}
for _, args := range versionArguments { 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{ app := &App{
Name: "boom", Name: "boom",
Usage: "make an explosive entrance", Usage: "make an explosive entrance",
Version: "0.1.0", Version: "0.1.0",
Writer: buf, Writer: buf,
Action: func(c *Context) error { Action: func(c *Context) error {
buf.WriteString("boom I say!") buf.WriteString("boom I say!")
return nil return nil
}, },
} }
err := app.Run(args) err := app.Run(args)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
output := buf.String() output := buf.String()
t.Logf("output: %q\n", buf.Bytes())
if !strings.Contains(output, "0.1.0") { if !strings.Contains(output, "0.1.0") {
t.Errorf("want version to contain %q, did not: \n%q", "0.1.0", output) 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 }, Action: func(c *Context) error { return nil },
Before: func(c *Context) error { return fmt.Errorf("before error") }, Before: func(c *Context) error { return fmt.Errorf("before error") },
After: func(c *Context) error { return fmt.Errorf("after error") }, After: func(c *Context) error { return fmt.Errorf("after error") },
Writer: ioutil.Discard,
} }
err := app.Run([]string{"foo"}) err := app.Run([]string{"foo"})
@ -1979,7 +1982,8 @@ func (c *customBoolFlag) IsSet() bool {
func TestCustomFlagsUnused(t *testing.T) { func TestCustomFlagsUnused(t *testing.T) {
app := &App{ app := &App{
Flags: []Flag{&customBoolFlag{"custom"}}, Flags: []Flag{&customBoolFlag{"custom"}},
Writer: ioutil.Discard,
} }
err := app.Run([]string{"foo"}) err := app.Run([]string{"foo"})
@ -1990,7 +1994,8 @@ func TestCustomFlagsUnused(t *testing.T) {
func TestCustomFlagsUsed(t *testing.T) { func TestCustomFlagsUsed(t *testing.T) {
app := &App{ app := &App{
Flags: []Flag{&customBoolFlag{"custom"}}, Flags: []Flag{&customBoolFlag{"custom"}},
Writer: ioutil.Discard,
} }
err := app.Run([]string{"foo", "--custom=bar"}) err := app.Run([]string{"foo", "--custom=bar"})
@ -2000,7 +2005,9 @@ func TestCustomFlagsUsed(t *testing.T) {
} }
func TestCustomHelpVersionFlags(t *testing.T) { func TestCustomHelpVersionFlags(t *testing.T) {
app := &App{} app := &App{
Writer: ioutil.Discard,
}
// Be sure to reset the global flags // Be sure to reset the global flags
defer func(helpFlag Flag, versionFlag Flag) { defer func(helpFlag Flag, versionFlag Flag) {
@ -2018,7 +2025,7 @@ func TestCustomHelpVersionFlags(t *testing.T) {
} }
func TestHandleExitCoder_Default(t *testing.T) { func TestHandleExitCoder_Default(t *testing.T) {
app := NewApp() app := newTestApp()
fs, err := flagSet(app.Name, app.Flags) fs, err := flagSet(app.Name, app.Flags)
if err != nil { if err != nil {
t.Errorf("error creating FlagSet: %s", err) t.Errorf("error creating FlagSet: %s", err)
@ -2034,7 +2041,7 @@ func TestHandleExitCoder_Default(t *testing.T) {
} }
func TestHandleExitCoder_Custom(t *testing.T) { func TestHandleExitCoder_Custom(t *testing.T) {
app := NewApp() app := newTestApp()
fs, err := flagSet(app.Name, app.Flags) fs, err := flagSet(app.Name, app.Flags)
if err != nil { if err != nil {
t.Errorf("error creating FlagSet: %s", err) t.Errorf("error creating FlagSet: %s", err)
@ -2091,6 +2098,7 @@ func TestShellCompletionForIncompleteFlags(t *testing.T) {
Action: func(ctx *Context) error { Action: func(ctx *Context) error {
return fmt.Errorf("should not get here") return fmt.Errorf("should not get here")
}, },
Writer: ioutil.Discard,
} }
err := app.Run([]string{"", "--test-completion", "--" + "generate-bash-completion"}) err := app.Run([]string{"", "--test-completion", "--" + "generate-bash-completion"})
if err != nil { if err != nil {
@ -2101,7 +2109,7 @@ func TestShellCompletionForIncompleteFlags(t *testing.T) {
func TestWhenExitSubCommandWithCodeThenAppQuitUnexpectedly(t *testing.T) { func TestWhenExitSubCommandWithCodeThenAppQuitUnexpectedly(t *testing.T) {
testCode := 104 testCode := 104
app := NewApp() app := newTestApp()
app.Commands = []*Command{ app.Commands = []*Command{
{ {
Name: "cmd", Name: "cmd",
@ -2120,7 +2128,6 @@ func TestWhenExitSubCommandWithCodeThenAppQuitUnexpectedly(t *testing.T) {
var exitCodeFromExitErrHandler int var exitCodeFromExitErrHandler int
app.ExitErrHandler = func(c *Context, err error) { app.ExitErrHandler = func(c *Context, err error) {
if exitErr, ok := err.(ExitCoder); ok { if exitErr, ok := err.(ExitCoder); ok {
t.Log(exitErr)
exitCodeFromExitErrHandler = exitErr.ExitCode() 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) 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} app.Commands = []*Command{cmd}
err := app.Run(c.testArgs) 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"}) err := app.Run([]string{"foo", "bar"})
@ -317,12 +318,12 @@ func TestCommandSkipFlagParsing(t *testing.T) {
&StringFlag{Name: "flag"}, &StringFlag{Name: "flag"},
}, },
Action: func(c *Context) error { Action: func(c *Context) error {
fmt.Printf("%+v\n", c.String("flag"))
args = c.Args() args = c.Args()
return nil return nil
}, },
}, },
}, },
Writer: ioutil.Discard,
} }
err := app.Run(c.testArgs) err := app.Run(c.testArgs)

View File

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

View File

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