alecthomas / alecthomas/kingpin

Unexpected "doubling" behavior when trying to test output

Abierto
#297 1 comentario 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
Go
Estrellas
3.6k
Forks
279
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

I am trying to write some unit tests for my kingpin.Application, to test things like help output. To do that, I build the application the same as I would in my real program, and redirect output to a buffer. I also apparently have to tell the app not to terminate the program (the test) whenever it encounters an error. But when I do all of these things, I notice that the Terminate callback is called multiple times (though only when I have a subcommand) and the expected output is duplicated.

Here is a small reproducible test case.

```go
package main

import (
"bytes"
"fmt"

"gopkg.in/alecthomas/kingpin.v2"
)

func main() {
app := kingpin.New("test", "Test application.")
_ = app.Command("subcommand", "A subcommand.")
var buf bytes.Buffer
app.Writer(&buf)
app.Terminate(func(exitcode int) { fmt.Printf("terminate(%d)\n", exitcode) })
_, err := app.Parse([]string{"--help"})
fmt.Printf("Parse error: %v\n", err)
fmt.Printf("%s\n", buf.String())
}
```

Expected output:

```
terminate(0)
Parse error: command not specified
usage: test [] [ ...]

Test application.

Flags:
--help Show context-sensitive help (also try --help-long and --help-man).

Commands:
help [...]
Show help.

subcommand
A subcommand.
```

Actual output:

```
terminate(0)
terminate(0)
Parse error: command not specified
usage: test [] [ ...]

Test application.

Flags:
--help Show context-sensitive help (also try --help-long and --help-man).

Commands:
help [...]
Show help.

subcommand
A subcommand.

usage: test [] [ ...]

Test application.

Flags:
--help Show context-sensitive help (also try --help-long and --help-man).

Commands:
help [...]
Show help.

subcommand
A subcommand.
```

If there is a better way to do what I want to do, I'm all ears. But for now, this looks like a bug.

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.