Files
teai/internal/cli/cli_test.go
ceoandchenyunda218 bc46620d04 初始化專案:Go 模組骨架、CLI 進入點、README、測試
- go.mod(module gitea.alterminal.com/alterminal/teai,Go 1.26)
- cmd/teai/main.go:程式進入點,結束碼交由 internal/cli.Run 處理
- internal/cli:命令列架構(引數剖析、子命令分派、version 命令)
- internal/cli/cli_test.go:用法輸出、version、未知命令等基本測試
- README.md:專案說明、建置與開發慣例(繁體中文)
- .gitignore:建置產物、測試輸出、環境檔
- 僅使用標準庫,可離線建置;授權待維護者選定

Refs #1
2026-09-09 23:06:39 +08:00

61 lines
1.6 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")
if code != int(ExitUsage) {
t.Errorf("未知命令應回 ExitUsage(%d),得到 %d", int(ExitUsage), code)
}
if !strings.Contains(stderr, "unknown command") {
t.Errorf("未知命令應在 stderr 說明,得到:%q", stderr)
}
}