Merge pull request #1457 from dearchap/issue_1094

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

@ -304,7 +304,7 @@ func (a *App) RunContext(ctx context.Context, arguments []string) (err error) {
return err return err
} }
if a.After != nil { if a.After != nil && !cCtx.shellComplete {
defer func() { defer func() {
if afterErr := a.After(cCtx); afterErr != nil { if afterErr := a.After(cCtx); afterErr != nil {
if err != nil { if err != nil {
@ -332,7 +332,7 @@ func (a *App) RunContext(ctx context.Context, arguments []string) (err error) {
return cerr return cerr
} }
if a.Before != nil { if a.Before != nil && !cCtx.shellComplete {
beforeErr := a.Before(cCtx) beforeErr := a.Before(cCtx)
if beforeErr != nil { if beforeErr != nil {
a.handleExitCoder(cCtx, beforeErr) a.handleExitCoder(cCtx, beforeErr)
@ -499,7 +499,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
return cerr return cerr
} }
if a.After != nil { if a.After != nil && !cCtx.shellComplete {
defer func() { defer func() {
afterErr := a.After(cCtx) afterErr := a.After(cCtx)
if afterErr != nil { 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) beforeErr := a.Before(cCtx)
if beforeErr != nil { if beforeErr != nil {
a.handleExitCoder(cCtx, beforeErr) a.handleExitCoder(cCtx, beforeErr)

@ -1245,6 +1245,58 @@ func TestApp_BeforeFunc(t *testing.T) {
} }
} }
func TestApp_BeforeAfterFuncShellCompletion(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) { func TestApp_AfterFunc(t *testing.T) {
counts := &opCounts{} counts := &opCounts{}
afterError := fmt.Errorf("fail") afterError := fmt.Errorf("fail")

Loading…
Cancel
Save