From 446aad581be05ccbc0c6d8fe8edbf2239a7f6870 Mon Sep 17 00:00:00 2001 From: Dokiy Date: Thu, 28 Jul 2022 19:00:48 +0800 Subject: [PATCH] Fix After not run --- app.go | 24 ++++++++++++------------ app_test.go | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/app.go b/app.go index e9493d3877..6b743bf7eb 100644 --- a/app.go +++ b/app.go @@ -302,6 +302,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 @@ -318,18 +330,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 { diff --git a/app_test.go b/app_test.go index e7c759eea7..cbc1c8b995 100644 --- a/app_test.go +++ b/app_test.go @@ -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) {