Merge pull request #1444 from Dokiys/fix/add_after_before_help_check

Fix After not run
main
dearchap 2 years ago committed by GitHub
commit e3ee4fb3ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -304,6 +304,18 @@ func (a *App) RunContext(ctx context.Context, arguments []string) (err error) {
return err
}
if a.After != nil {
defer func() {
if afterErr := a.After(cCtx); afterErr != nil {
if err != nil {
err = newMultiError(err, afterErr)
} else {
err = afterErr
}
}
}()
}
if !a.HideHelp && checkHelp(cCtx) {
_ = ShowAppHelp(cCtx)
return nil
@ -320,18 +332,6 @@ func (a *App) RunContext(ctx context.Context, arguments []string) (err error) {
return cerr
}
if a.After != nil {
defer func() {
if afterErr := a.After(cCtx); afterErr != nil {
if err != nil {
err = newMultiError(err, afterErr)
} else {
err = afterErr
}
}
}()
}
if a.Before != nil {
beforeErr := a.Before(cCtx)
if beforeErr != nil {

@ -1309,6 +1309,27 @@ func TestApp_AfterFunc(t *testing.T) {
if counts.SubCommand != 1 {
t.Errorf("Subcommand not executed when expected")
}
/*
reset
*/
counts = &opCounts{}
// run with none args
err = app.Run([]string{"command"})
// should be the same error produced by the Before func
if err != nil {
t.Fatalf("Run error: %s", err)
}
if counts.After != 1 {
t.Errorf("After() not executed when expected")
}
if counts.SubCommand != 0 {
t.Errorf("Subcommand not executed when expected")
}
}
func TestAppNoHelpFlag(t *testing.T) {

Loading…
Cancel
Save