From f481377b9a72dc1811410142f7fb3930e4b7593e Mon Sep 17 00:00:00 2001 From: chenyunda218 Date: Thu, 10 Sep 2026 10:12:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=AC=E8=A9=A6=E6=94=B9=E6=96=B7=E8=A8=80?= =?UTF-8?q?=E5=85=B7=E9=AB=94=E7=B5=90=E6=9D=9F=E7=A2=BC=E6=95=B8=E5=80=BC?= =?UTF-8?q?=EF=BC=8C=E5=B0=8D=E7=85=A7=20README=20=E8=A6=8F=E6=A0=BC?= =?UTF-8?q?=EF=BC=88#10=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit cli_test、dailyops_commands_test、login_test、exitcode_test 原以 int(ExitUsage)/int(ExitAPI)/int(ExitInternal) 常數互比,常數若再 與 README 脫鉤測試仍會綠。改為直接斷言 0/2/3(內部錯誤 1)。 常數本體已隨 #5(6951817)改為明確數值,未知命令實測回 2。 --- internal/cli/cli_test.go | 6 ++++-- internal/cli/dailyops_commands_test.go | 13 ++++++++----- internal/cli/exitcode_test.go | 18 ++++++++++-------- internal/cli/login_test.go | 20 ++++++++++++-------- 4 files changed, 34 insertions(+), 23 deletions(-) diff --git a/internal/cli/cli_test.go b/internal/cli/cli_test.go index 200aded..4b3b60e 100644 --- a/internal/cli/cli_test.go +++ b/internal/cli/cli_test.go @@ -51,8 +51,10 @@ func TestRunVersion(t *testing.T) { func TestRunUnknownCommand(t *testing.T) { _, stderr, code := run("no-such-command") - if code != int(ExitUsage) { - t.Errorf("未知命令應回 ExitUsage(%d),得到 %d", int(ExitUsage), code) + // 直接對照 README「輸出與結束碼」的數值(用法錯誤=2), + // 不以常數互比,避免常數與規格再次脫鉤(#10)。 + if code != 2 { + t.Errorf("未知命令應回 2(README 用法錯誤),得到 %d", code) } if !strings.Contains(stderr, "unknown command") { t.Errorf("未知命令應在 stderr 說明,得到:%q", stderr) diff --git a/internal/cli/dailyops_commands_test.go b/internal/cli/dailyops_commands_test.go index b59e175..187270d 100644 --- a/internal/cli/dailyops_commands_test.go +++ b/internal/cli/dailyops_commands_test.go @@ -53,8 +53,9 @@ func TestIssuesListRequiresRepo(t *testing.T) { t.Errorf("不應發出任何請求") }) _, stderr, code := run("issues", "list") - if code != int(ExitUsage) { - t.Errorf("缺 --repo 應回 ExitUsage,得到 %d", code) + // 對照 README「輸出與結束碼」:用法錯誤=2(#10)。 + if code != 2 { + t.Errorf("缺 --repo 應回 2(README 用法錯誤),得到 %d", code) } if !strings.Contains(stderr, "--repo") { t.Errorf("錯誤訊息應提及 --repo,得到:%s", stderr) @@ -110,8 +111,9 @@ func TestWriteCommandsRequireYes(t *testing.T) { t.Errorf("%s:未帶 --yes 不得發出請求(%s %s)", tc.name, r.Method, r.URL.Path) }) _, stderr, code := run(tc.args...) - if code != int(ExitUsage) { - t.Errorf("%s 未帶 --yes 應回 ExitUsage(2),得到 %d", tc.name, code) + // 對照 README「輸出與結束碼」:用法錯誤=2(#10)。 + if code != 2 { + t.Errorf("%s 未帶 --yes 應回 2(README 用法錯誤),得到 %d", tc.name, code) } if !strings.Contains(stderr, "--yes") { t.Errorf("%s 錯誤訊息應提及 --yes,得到:%s", tc.name, stderr) @@ -235,7 +237,8 @@ func TestAPIDirectPassthrough(t *testing.T) { } _, stderr, code := run("api", "--method", "POST", "/version") - if code != int(ExitUsage) || !strings.Contains(stderr, "--yes") { + // 對照 README「輸出與結束碼」:用法錯誤=2(#10)。 + if code != 2 || !strings.Contains(stderr, "--yes") { t.Errorf("POST 未帶 --yes 應回 2 並提及 --yes,得到 %d:%s", code, stderr) } } diff --git a/internal/cli/exitcode_test.go b/internal/cli/exitcode_test.go index 1e449d0..59a77c5 100644 --- a/internal/cli/exitcode_test.go +++ b/internal/cli/exitcode_test.go @@ -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) } } diff --git a/internal/cli/login_test.go b/internal/cli/login_test.go index aea20d9..f8e632f 100644 --- a/internal/cli/login_test.go +++ b/internal/cli/login_test.go @@ -145,8 +145,9 @@ func TestLoginAddRejected401NoWrite(t *testing.T) { `) _, stderr, code := run(append(prefix, "--url", srv.URL, "login", "add", "--token", "bad-tok")...) - if code != int(ExitAPI) { - t.Fatalf("401 should exit 3, got %d (stderr %s)", code, stderr) + // 401 是 API 錯誤;對照 README「輸出與結束碼」=3(#10)。 + if code != 3 { + t.Fatalf("401 should exit 3 (README api error), got %d (stderr %s)", code, stderr) } if !strings.Contains(stderr, "401") { t.Fatalf("stderr should mention 401: %s", stderr) @@ -250,8 +251,9 @@ func TestLoginRemoveAsksConfirmation(t *testing.T) { func TestLoginUnknownSubcommandUsage(t *testing.T) { prefix, _ := loginTestEnv(t, "") _, stderr, code := run(append(prefix, "login", "bogus")...) - if code != int(ExitUsage) { - t.Fatalf("want exit 2, got %d", code) + // 對照 README「輸出與結束碼」:用法錯誤=2(#10)。 + if code != 2 { + t.Fatalf("want exit 2 (README usage), got %d", code) } if !strings.Contains(stderr, "unknown login subcommand") { t.Fatalf("stderr: %s", stderr) @@ -261,8 +263,9 @@ func TestLoginUnknownSubcommandUsage(t *testing.T) { func TestLoginNoSubcommandUsage(t *testing.T) { prefix, _ := loginTestEnv(t, "") _, stderr, code := run(append(prefix, "login")...) - if code != int(ExitUsage) { - t.Fatalf("want exit 2, got %d", code) + // 對照 README「輸出與結束碼」:用法錯誤=2(#10)。 + if code != 2 { + t.Fatalf("want exit 2 (README usage), got %d", code) } if !strings.Contains(stderr, "subcommand") { t.Fatalf("stderr: %s", stderr) @@ -272,8 +275,9 @@ func TestLoginNoSubcommandUsage(t *testing.T) { func TestLoginRemoveNotFound(t *testing.T) { prefix, _ := loginTestEnv(t, "logins: []\n") _, stderr, code := run(append(prefix, "login", "remove", "--yes", "ghost")...) - if code != int(ExitInternal) { - t.Fatalf("want exit 1, got %d", code) + // ExitInternal 不在 README 保證範圍;此處固定為 1(內部錯誤,#10)。 + if code != 1 { + t.Fatalf("want exit 1 (internal), got %d", code) } if !strings.Contains(stderr, `login "ghost" not found`) { t.Fatalf("stderr: %s", stderr)