cleanup tests, check required flags in more places
This commit is contained in:
parent
7ce0af189e
commit
23f09ac1e8
14
app.go
14
app.go
@ -339,13 +339,6 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
|||||||
return nerr
|
return nerr
|
||||||
}
|
}
|
||||||
|
|
||||||
cerr := checkRequiredFlags(a.Flags, set)
|
|
||||||
if cerr != nil {
|
|
||||||
showFlagError(a.Writer, cerr)
|
|
||||||
ShowSubcommandHelp(context)
|
|
||||||
return cerr
|
|
||||||
}
|
|
||||||
|
|
||||||
if checkCompletions(context) {
|
if checkCompletions(context) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -371,6 +364,13 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cerr := checkRequiredFlags(a.Flags, set)
|
||||||
|
if cerr != nil {
|
||||||
|
showFlagError(a.Writer, cerr)
|
||||||
|
ShowSubcommandHelp(context)
|
||||||
|
return cerr
|
||||||
|
}
|
||||||
|
|
||||||
if a.After != nil {
|
if a.After != nil {
|
||||||
defer func() {
|
defer func() {
|
||||||
afterErr := a.After(context)
|
afterErr := a.After(context)
|
||||||
|
70
app_test.go
70
app_test.go
@ -877,54 +877,38 @@ func TestAppNoHelpFlag(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestRequiredFlagAppRunBehavior tests the app-wide behavior of required flags
|
|
||||||
// and how they interact with the error response and help messages.
|
|
||||||
// A different test (`TestCheckRequiredFlags`) exists for the more fine grain
|
|
||||||
// behavior of required flags.
|
|
||||||
func TestRequiredFlagAppRunBehavior(t *testing.T) {
|
func TestRequiredFlagAppRunBehavior(t *testing.T) {
|
||||||
tdata := []struct {
|
tdata := []struct {
|
||||||
testCase string
|
testCase string
|
||||||
flags []Flag
|
flags []Flag
|
||||||
appRunInput []string
|
appRunInput []string
|
||||||
expectedHelpPrinter bool
|
expectedAnError bool
|
||||||
expectedAnError bool
|
|
||||||
expectedFlagErrorPrinter bool
|
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
// expectations:
|
// expectations:
|
||||||
// - empty input, when a required flag is present, shows the help message
|
// - empty input, when a required flag is present, shows the help message
|
||||||
// - empty input, when a required flag is present, errors
|
// - empty input, when a required flag is present, errors and shows the flag error message
|
||||||
// - empty input, when a required flag is present, show the flag error message
|
testCase: "empty_input_with_required_flag",
|
||||||
testCase: "empty_input_with_required_flag",
|
appRunInput: []string{"command"},
|
||||||
appRunInput: []string{"command"},
|
flags: []Flag{StringFlag{Name: "requiredFlag", Required: true}},
|
||||||
flags: []Flag{StringFlag{Name: "requiredFlag", Required: true}},
|
expectedAnError: true,
|
||||||
expectedHelpPrinter: true,
|
|
||||||
expectedAnError: true,
|
|
||||||
expectedFlagErrorPrinter: true,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
// expectations:
|
// expectations:
|
||||||
// - inputing --help, when a required flag is present, shows the help message
|
// - inputing --help, when a required flag is present, shows the help message
|
||||||
// - inputing --help, when a required flag is present, explicitly does not error
|
// - inputing --help, when a required flag is present, does not error
|
||||||
// - inputing --help, when a required flag is present, explicitly does not show the flag error message
|
testCase: "help_input_with_required_flag",
|
||||||
testCase: "help_input_with_required_flag",
|
appRunInput: []string{"command", "--help"},
|
||||||
appRunInput: []string{"command", "--help"},
|
flags: []Flag{StringFlag{Name: "requiredFlag", Required: true}},
|
||||||
flags: []Flag{StringFlag{Name: "requiredFlag", Required: true}},
|
|
||||||
expectedHelpPrinter: true,
|
|
||||||
expectedAnError: false, // explicit false for readability (this is the default value)
|
|
||||||
expectedFlagErrorPrinter: false, // explicit false for readability (this is the default value)
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
// expectations:
|
// expectations:
|
||||||
// - giving optional input, when a required flag is present, shows the help message
|
// - giving optional input, when a required flag is present, shows the help message
|
||||||
// - giving optional input, when a required flag is present, errors
|
// - giving optional input, when a required flag is present, errors and shows the flag error message
|
||||||
// - giving optional input, when a required flag is present, shows the flag error message
|
testCase: "optional_input_with_required_flag",
|
||||||
testCase: "optional_input_with_required_flag",
|
appRunInput: []string{"command", "--optional", "cats"},
|
||||||
appRunInput: []string{"command", "--optional", "cats"},
|
flags: []Flag{StringFlag{Name: "requiredFlag", Required: true}, StringFlag{Name: "optional"}},
|
||||||
flags: []Flag{StringFlag{Name: "requiredFlag", Required: true}, StringFlag{Name: "optional"}},
|
expectedAnError: true,
|
||||||
expectedHelpPrinter: true,
|
|
||||||
expectedAnError: true,
|
|
||||||
expectedFlagErrorPrinter: true,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range tdata {
|
for _, test := range tdata {
|
||||||
@ -935,9 +919,9 @@ func TestRequiredFlagAppRunBehavior(t *testing.T) {
|
|||||||
showFlagError = oldShowError
|
showFlagError = oldShowError
|
||||||
}()
|
}()
|
||||||
// setup - mock showFlagError
|
// setup - mock showFlagError
|
||||||
var showErrorWasCalled = false
|
var showFlagErrorWasCalled = false
|
||||||
showFlagError = func(writer io.Writer, err ...interface{}) (int, error) {
|
showFlagError = func(writer io.Writer, err ...interface{}) (int, error) {
|
||||||
showErrorWasCalled = true
|
showFlagErrorWasCalled = true
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
// setup - undo HelpPrinter mock when finished
|
// setup - undo HelpPrinter mock when finished
|
||||||
@ -953,26 +937,20 @@ func TestRequiredFlagAppRunBehavior(t *testing.T) {
|
|||||||
// setup - app
|
// setup - app
|
||||||
app := NewApp()
|
app := NewApp()
|
||||||
app.Flags = test.flags
|
app.Flags = test.flags
|
||||||
app.Commands = []Command{
|
|
||||||
Command{
|
|
||||||
Name: "command",
|
|
||||||
Flags: test.flags,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// logic under test
|
// logic under test
|
||||||
err := app.Run(test.appRunInput)
|
err := app.Run(test.appRunInput)
|
||||||
|
|
||||||
// assertions
|
// assertions
|
||||||
if test.expectedHelpPrinter && helpPrinterWasCalled == false {
|
if helpPrinterWasCalled == false {
|
||||||
t.Errorf("HelpPrinter expected to be called, but was not")
|
t.Errorf("HelpPrinter expected to be called, but was not")
|
||||||
}
|
}
|
||||||
if test.expectedFlagErrorPrinter && showErrorWasCalled == false {
|
|
||||||
t.Errorf("showFlagError expected to be called, but was not")
|
|
||||||
}
|
|
||||||
if test.expectedAnError && err == nil {
|
if test.expectedAnError && err == nil {
|
||||||
t.Errorf("expected an error, but there was none")
|
t.Errorf("expected an error, but there was none")
|
||||||
}
|
}
|
||||||
|
if test.expectedAnError && showFlagErrorWasCalled == false {
|
||||||
|
t.Errorf("showFlagError expected to be called, but was not")
|
||||||
|
}
|
||||||
if !test.expectedAnError && err != nil {
|
if !test.expectedAnError && err != nil {
|
||||||
t.Errorf("did not expected an error, but there was one: %s", err)
|
t.Errorf("did not expected an error, but there was one: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -135,6 +135,13 @@ func (c Command) Run(ctx *Context) (err error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cerr := checkRequiredFlags(c.Flags, set)
|
||||||
|
if cerr != nil {
|
||||||
|
showFlagError(context.App.Writer, cerr)
|
||||||
|
ShowCommandHelp(context, c.Name)
|
||||||
|
return cerr
|
||||||
|
}
|
||||||
|
|
||||||
if c.After != nil {
|
if c.After != nil {
|
||||||
defer func() {
|
defer func() {
|
||||||
afterErr := c.After(context)
|
afterErr := c.After(context)
|
||||||
|
Loading…
Reference in New Issue
Block a user