From 57a842342298b01caf7a14ec62e5e61e4a0d6af0 Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Wed, 17 Aug 2022 21:19:34 -0400 Subject: [PATCH 1/2] Fix:(issue_1094) Dont execute Before/After handlers during shell completion --- app.go | 8 ++++---- app_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/app.go b/app.go index 0b3f45c..b39c8b8 100644 --- a/app.go +++ b/app.go @@ -304,7 +304,7 @@ func (a *App) RunContext(ctx context.Context, arguments []string) (err error) { return err } - if a.After != nil { + if a.After != nil && !cCtx.shellComplete { defer func() { if afterErr := a.After(cCtx); afterErr != nil { if err != nil { @@ -332,7 +332,7 @@ func (a *App) RunContext(ctx context.Context, arguments []string) (err error) { return cerr } - if a.Before != nil { + if a.Before != nil && !cCtx.shellComplete { beforeErr := a.Before(cCtx) if beforeErr != nil { a.handleExitCoder(cCtx, beforeErr) @@ -499,7 +499,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) { return cerr } - if a.After != nil { + if a.After != nil && !cCtx.shellComplete { defer func() { afterErr := a.After(cCtx) if afterErr != nil { @@ -513,7 +513,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) { }() } - if a.Before != nil { + if a.Before != nil && !cCtx.shellComplete { beforeErr := a.Before(cCtx) if beforeErr != nil { a.handleExitCoder(cCtx, beforeErr) diff --git a/app_test.go b/app_test.go index cc77ce3..098cb92 100644 --- a/app_test.go +++ b/app_test.go @@ -1245,6 +1245,58 @@ func TestApp_BeforeFunc(t *testing.T) { } } +func TestApp_BeforeFuncShellCompletion(t *testing.T) { + counts := &opCounts{} + var err error + + app := &App{ + EnableBashCompletion: true, + Before: func(c *Context) error { + counts.Total++ + counts.Before = counts.Total + return nil + }, + After: func(c *Context) error { + counts.Total++ + counts.After = counts.Total + return nil + }, + Commands: []*Command{ + { + Name: "sub", + Action: func(c *Context) error { + counts.Total++ + counts.SubCommand = counts.Total + return nil + }, + }, + }, + Flags: []Flag{ + &StringFlag{Name: "opt"}, + }, + Writer: ioutil.Discard, + } + + // run with the Before() func succeeding + err = app.Run([]string{"command", "--opt", "succeed", "sub", "--generate-bash-completion"}) + + if err != nil { + t.Fatalf("Run error: %s", err) + } + + if counts.Before != 0 { + t.Errorf("Before() executed when not expected") + } + + if counts.After != 0 { + t.Errorf("After() executed when not expected") + } + + if counts.SubCommand != 0 { + t.Errorf("Subcommand executed more than expected") + } +} + func TestApp_AfterFunc(t *testing.T) { counts := &opCounts{} afterError := fmt.Errorf("fail") From 7b28ca54eaec2695d6adc4686fe00098200faf99 Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Wed, 17 Aug 2022 21:22:53 -0400 Subject: [PATCH 2/2] Rename function --- app_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app_test.go b/app_test.go index 098cb92..64316fc 100644 --- a/app_test.go +++ b/app_test.go @@ -1245,7 +1245,7 @@ func TestApp_BeforeFunc(t *testing.T) { } } -func TestApp_BeforeFuncShellCompletion(t *testing.T) { +func TestApp_BeforeAfterFuncShellCompletion(t *testing.T) { counts := &opCounts{} var err error