Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix After not run #1444

Merged
merged 1 commit into from Aug 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 12 additions & 12 deletions app.go
Expand Up @@ -300,6 +300,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
Expand All @@ -316,18 +328,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 {
Expand Down
21 changes: 21 additions & 0 deletions app_test.go
Expand Up @@ -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) {
Expand Down