alecthomas / alecthomas/kingpin

Unexpected "doubling" behavior when trying to test output

未關閉
#297 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
Go
星號
3.6k
分支
279
PR 合併指標
30 天內沒有已合併 PR

描述

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.

貢獻指南

這個儲存庫沒有索引到貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。