測試改斷言具體結束碼數值,對照 README 規格(#10)

cli_test、dailyops_commands_test、login_test、exitcode_test 原以
int(ExitUsage)/int(ExitAPI)/int(ExitInternal) 常數互比,常數若再
與 README 脫鉤測試仍會綠。改為直接斷言 0/2/3(內部錯誤 1)。

常數本體已隨 #5(6951817)改為明確數值,未知命令實測回 2。
This commit is contained in:
2026-09-10 10:12:35 +08:00
parent c5d5c37eae
commit f481377b9a
4 changed files with 34 additions and 23 deletions
+10 -8
View File
@@ -10,20 +10,22 @@ import (
)
func TestExitCodeMapping(t *testing.T) {
// want 直接寫 README「輸出與結束碼」的數值(0/2/3;內部錯誤 1),
// 不以常數互比,避免常數與規格再次脫鉤(#10)。
cases := []struct {
name string
err error
want ExitCode
want int
}{
{"usage error", &ErrUsage{Msg: "bad flag"}, ExitUsage},
{"api error 401", &gitea.ErrAPI{StatusCode: 401, Op: "GET /api/v1/user"}, ExitAPI},
{"api error transport", &gitea.ErrAPI{Op: "GET /api/v1/user", Err: errString("dial tcp")}, ExitAPI},
{"wrapped api error", wrapped(&gitea.ErrAPI{StatusCode: 500}), ExitAPI},
{"other error", errString("boom"), ExitInternal},
{"nil maps to OK", nil, ExitOK},
{"usage error", &ErrUsage{Msg: "bad flag"}, 2},
{"api error 401", &gitea.ErrAPI{StatusCode: 401, Op: "GET /api/v1/user"}, 3},
{"api error transport", &gitea.ErrAPI{Op: "GET /api/v1/user", Err: errString("dial tcp")}, 3},
{"wrapped api error", wrapped(&gitea.ErrAPI{StatusCode: 500}), 3},
{"other error", errString("boom"), 1},
{"nil maps to OK", nil, 0},
}
for _, tc := range cases {
if got := exitCodeFor(tc.err); got != tc.want {
if got := int(exitCodeFor(tc.err)); got != tc.want {
t.Errorf("%s: exitCodeFor = %d, want %d", tc.name, got, tc.want)
}
}