cli_test、dailyops_commands_test、login_test、exitcode_test 原以 int(ExitUsage)/int(ExitAPI)/int(ExitInternal) 常數互比,常數若再 與 README 脫鉤測試仍會綠。改為直接斷言 0/2/3(內部錯誤 1)。 常數本體已隨 #5(6951817)改為明確數值,未知命令實測回 2。
63 lines
1.8 KiB
Go
63 lines
1.8 KiB
Go
// cli_test.go 驗證命令列架構的基本行為:用法輸出、子命令分派、
|
|
// 版本輸出與未知命令的錯誤處理。
|
|
package cli
|
|
|
|
import (
|
|
"bytes"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// run 是測試輔助:以 buffer 收集輸出並執行 Run。
|
|
func run(args ...string) (stdout, stderr string, code int) {
|
|
var out, errb bytes.Buffer
|
|
code = Run(&out, &errb, args)
|
|
return out.String(), errb.String(), code
|
|
}
|
|
|
|
func TestRunNoArgsShowsUsage(t *testing.T) {
|
|
stdout, _, code := run()
|
|
if code != 0 {
|
|
t.Errorf("無引數應回 0,得到 %d", code)
|
|
}
|
|
if !strings.Contains(stdout, "teai") || !strings.Contains(stdout, "命令") {
|
|
t.Errorf("無引數應印出用法說明,得到:\n%s", stdout)
|
|
}
|
|
}
|
|
|
|
func TestRunHelpFlags(t *testing.T) {
|
|
for _, flagArg := range []string{"-h", "--help", "help"} {
|
|
stdout, _, code := run(flagArg)
|
|
if code != 0 {
|
|
t.Errorf("%s 應回 0,得到 %d", flagArg, code)
|
|
}
|
|
if !strings.Contains(stdout, "用法") {
|
|
t.Errorf("%s 應印出用法,得到:\n%s", flagArg, stdout)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRunVersion(t *testing.T) {
|
|
for _, arg := range []string{"version", "-v", "--version"} {
|
|
stdout, _, code := run(arg)
|
|
if code != 0 {
|
|
t.Errorf("%s 應回 0,得到 %d", arg, code)
|
|
}
|
|
if !strings.Contains(stdout, "teai version ") {
|
|
t.Errorf("%s 應印出版本,得到:%q", arg, stdout)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRunUnknownCommand(t *testing.T) {
|
|
_, stderr, code := run("no-such-command")
|
|
// 直接對照 README「輸出與結束碼」的數值(用法錯誤=2),
|
|
// 不以常數互比,避免常數與規格再次脫鉤(#10)。
|
|
if code != 2 {
|
|
t.Errorf("未知命令應回 2(README 用法錯誤),得到 %d", code)
|
|
}
|
|
if !strings.Contains(stderr, "unknown command") {
|
|
t.Errorf("未知命令應在 stderr 說明,得到:%q", stderr)
|
|
}
|
|
}
|