Ensure MultiError returned when both Before and After funcs given

This commit is contained in:
Dan Buch
2016-04-28 17:15:16 -04:00
parent 61d4175525
commit 4cae17cfe1
3 changed files with 38 additions and 10 deletions

View File

@@ -522,6 +522,30 @@ func TestApp_BeforeFunc(t *testing.T) {
if counts.SubCommand != 0 {
t.Errorf("Subcommand executed when NOT expected")
}
// reset
counts = &opCounts{}
afterError := errors.New("fail again")
app.After = func(_ *Context) error {
return afterError
}
// run with the Before() func failing, wrapped by After()
err = app.Run([]string{"command", "--opt", "fail", "sub"})
// should be the same error produced by the Before func
if _, ok := err.(MultiError); !ok {
t.Errorf("MultiError expected, but not received")
}
if counts.Before != 1 {
t.Errorf("Before() not executed when expected")
}
if counts.SubCommand != 0 {
t.Errorf("Subcommand executed when NOT expected")
}
}
func TestApp_AfterFunc(t *testing.T) {
@@ -735,9 +759,10 @@ func TestApp_OrderOfOperations(t *testing.T) {
},
}
app.Action = func(c *Context) {
app.Action = func(c *Context) error {
counts.Total++
counts.Action = counts.Total
return nil
}
_ = app.Run([]string{"command", "--nope"})