Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0ed6f262c8 | ||
|
|
fb2990dc04 | ||
|
|
4a34def1f5 | ||
|
|
c4fb641094 | ||
|
|
f7d63d6e8f | ||
|
|
f481377b9a |
+36
-3
@@ -150,10 +150,31 @@ var globalFlags = map[string]bool{
|
|||||||
"--timeout": true,
|
"--timeout": true,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// reservedFlags 回傳 dispatch 不得搶走的旗標名稱(含 -- 前綴):這些
|
||||||
|
// 旗標屬於目前命令(或其子命令),即使與全域選項同名也必須原樣留給
|
||||||
|
// 命令剖析。目前唯一與全域同名的是 login add 的 --url/--token
|
||||||
|
// (README「teai login add --url … --token …」)。其餘命令的旗標
|
||||||
|
// (--repo、--has-work、--mine……)本來就不在 globalFlags 內,不受影響。
|
||||||
|
func reservedFlags(name string) map[string]bool {
|
||||||
|
reserved := make(map[string]bool)
|
||||||
|
for _, f := range commandReservedFlags[name] {
|
||||||
|
reserved[f] = true
|
||||||
|
}
|
||||||
|
return reserved
|
||||||
|
}
|
||||||
|
|
||||||
|
// commandReservedFlags 登記每個命令「與全域選項同名」的自有旗標。
|
||||||
|
// 命令旗標優先留給命令;新命令有同姓旗標時在這裡登記(#18)。
|
||||||
|
var commandReservedFlags = map[string][]string{
|
||||||
|
"login": {"--url", "--token"},
|
||||||
|
}
|
||||||
|
|
||||||
// extractGlobals 從 args 中「抽取」全域選項(可出現在命令之後、與命令
|
// extractGlobals 從 args 中「抽取」全域選項(可出現在命令之後、與命令
|
||||||
// 旗標交錯),其餘引數依原順序保留給子命令剖析。未知的選項一律留給
|
// 旗標交錯),其餘引數依原順序保留給子命令剖析。未知的選項一律留給
|
||||||
// 子命令處理(可能是命令旗標,如 --has-work),不在這裡報錯。
|
// 子命令處理(可能是命令旗標,如 --has-work),不在這裡報錯。
|
||||||
func extractGlobals(g *Globals, args []string) ([]string, error) {
|
// reserved 是目前命令保留的旗標名稱(含 -- 前綴,見 reservedFlags):
|
||||||
|
// 列於其中的選項即使與全域同名也原樣留給命令,命令旗標優先(#18)。
|
||||||
|
func extractGlobals(g *Globals, args []string, reserved map[string]bool) ([]string, error) {
|
||||||
rest := make([]string, 0, len(args))
|
rest := make([]string, 0, len(args))
|
||||||
applyGlobal := func(name, value string) error {
|
applyGlobal := func(name, value string) error {
|
||||||
switch name {
|
switch name {
|
||||||
@@ -197,6 +218,17 @@ func extractGlobals(g *Globals, args []string) ([]string, error) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
name, inline, hasInline := strings.Cut(arg, "=")
|
name, inline, hasInline := strings.Cut(arg, "=")
|
||||||
|
if reserved[name] {
|
||||||
|
// 命令保留旗標:即使與全域選項同名也留給命令剖析(#18)。
|
||||||
|
rest = append(rest, arg)
|
||||||
|
if !hasInline && i+1 < len(args) {
|
||||||
|
// 旗標值跟在下一個引數;一併保留,不當成全域值。
|
||||||
|
rest = append(rest, args[i+1])
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
continue
|
||||||
|
}
|
||||||
_, ok := globalFlags[name]
|
_, ok := globalFlags[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
// 不是全域選項:留給子命令(可能是 --has-work 這類命令旗標)。
|
// 不是全域選項:留給子命令(可能是 --has-work 這類命令旗標)。
|
||||||
@@ -293,11 +325,12 @@ func parseGlobals(g *Globals, args []string) ([]string, error) {
|
|||||||
// 旗標剖析交由各命令自行處理(不同命令有不同旗標,如 pulls --mine);
|
// 旗標剖析交由各命令自行處理(不同命令有不同旗標,如 pulls --mine);
|
||||||
// 全域選項允許出現在命令之後(README 範例:teai next --output table),
|
// 全域選項允許出現在命令之後(README 範例:teai next --output table),
|
||||||
// dispatch 先從 args 剝離全域選項併入 Globals,其餘(含命令自己的旗標,
|
// dispatch 先從 args 剝離全域選項併入 Globals,其餘(含命令自己的旗標,
|
||||||
// 如 --has-work)原樣交給命令,不會誤判為未知全域選項。
|
// 如 --has-work)原樣交給命令,不會誤判為未知全域選項。與全域選項
|
||||||
|
// 同名的命令旗標(登記於 commandReservedFlags)優先留給命令(#18)。
|
||||||
func dispatch(env *Env, name string, args []string) int {
|
func dispatch(env *Env, name string, args []string) int {
|
||||||
cmd := commands[name]
|
cmd := commands[name]
|
||||||
g := env.Globals
|
g := env.Globals
|
||||||
rest, err := extractGlobals(&g, args)
|
rest, err := extractGlobals(&g, args, reservedFlags(name))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(env.Err, "teai: %v\n\n", err)
|
fmt.Fprintf(env.Err, "teai: %v\n\n", err)
|
||||||
printUsage(env.Err)
|
printUsage(env.Err)
|
||||||
|
|||||||
@@ -51,8 +51,10 @@ func TestRunVersion(t *testing.T) {
|
|||||||
|
|
||||||
func TestRunUnknownCommand(t *testing.T) {
|
func TestRunUnknownCommand(t *testing.T) {
|
||||||
_, stderr, code := run("no-such-command")
|
_, stderr, code := run("no-such-command")
|
||||||
if code != int(ExitUsage) {
|
// 直接對照 README「輸出與結束碼」的數值(用法錯誤=2),
|
||||||
t.Errorf("未知命令應回 ExitUsage(%d),得到 %d", int(ExitUsage), code)
|
// 不以常數互比,避免常數與規格再次脫鉤(#10)。
|
||||||
|
if code != 2 {
|
||||||
|
t.Errorf("未知命令應回 2(README 用法錯誤),得到 %d", code)
|
||||||
}
|
}
|
||||||
if !strings.Contains(stderr, "unknown command") {
|
if !strings.Contains(stderr, "unknown command") {
|
||||||
t.Errorf("未知命令應在 stderr 說明,得到:%q", stderr)
|
t.Errorf("未知命令應在 stderr 說明,得到:%q", stderr)
|
||||||
|
|||||||
@@ -53,8 +53,9 @@ func TestIssuesListRequiresRepo(t *testing.T) {
|
|||||||
t.Errorf("不應發出任何請求")
|
t.Errorf("不應發出任何請求")
|
||||||
})
|
})
|
||||||
_, stderr, code := run("issues", "list")
|
_, stderr, code := run("issues", "list")
|
||||||
if code != int(ExitUsage) {
|
// 對照 README「輸出與結束碼」:用法錯誤=2(#10)。
|
||||||
t.Errorf("缺 --repo 應回 ExitUsage,得到 %d", code)
|
if code != 2 {
|
||||||
|
t.Errorf("缺 --repo 應回 2(README 用法錯誤),得到 %d", code)
|
||||||
}
|
}
|
||||||
if !strings.Contains(stderr, "--repo") {
|
if !strings.Contains(stderr, "--repo") {
|
||||||
t.Errorf("錯誤訊息應提及 --repo,得到:%s", stderr)
|
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)
|
t.Errorf("%s:未帶 --yes 不得發出請求(%s %s)", tc.name, r.Method, r.URL.Path)
|
||||||
})
|
})
|
||||||
_, stderr, code := run(tc.args...)
|
_, stderr, code := run(tc.args...)
|
||||||
if code != int(ExitUsage) {
|
// 對照 README「輸出與結束碼」:用法錯誤=2(#10)。
|
||||||
t.Errorf("%s 未帶 --yes 應回 ExitUsage(2),得到 %d", tc.name, code)
|
if code != 2 {
|
||||||
|
t.Errorf("%s 未帶 --yes 應回 2(README 用法錯誤),得到 %d", tc.name, code)
|
||||||
}
|
}
|
||||||
if !strings.Contains(stderr, "--yes") {
|
if !strings.Contains(stderr, "--yes") {
|
||||||
t.Errorf("%s 錯誤訊息應提及 --yes,得到:%s", tc.name, stderr)
|
t.Errorf("%s 錯誤訊息應提及 --yes,得到:%s", tc.name, stderr)
|
||||||
@@ -235,7 +237,8 @@ func TestAPIDirectPassthrough(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_, stderr, code := run("api", "--method", "POST", "/version")
|
_, 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)
|
t.Errorf("POST 未帶 --yes 應回 2 並提及 --yes,得到 %d:%s", code, stderr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,20 +10,22 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestExitCodeMapping(t *testing.T) {
|
func TestExitCodeMapping(t *testing.T) {
|
||||||
|
// want 直接寫 README「輸出與結束碼」的數值(0/2/3;內部錯誤 1),
|
||||||
|
// 不以常數互比,避免常數與規格再次脫鉤(#10)。
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
name string
|
name string
|
||||||
err error
|
err error
|
||||||
want ExitCode
|
want int
|
||||||
}{
|
}{
|
||||||
{"usage error", &ErrUsage{Msg: "bad flag"}, ExitUsage},
|
{"usage error", &ErrUsage{Msg: "bad flag"}, 2},
|
||||||
{"api error 401", &gitea.ErrAPI{StatusCode: 401, Op: "GET /api/v1/user"}, ExitAPI},
|
{"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")}, ExitAPI},
|
{"api error transport", &gitea.ErrAPI{Op: "GET /api/v1/user", Err: errString("dial tcp")}, 3},
|
||||||
{"wrapped api error", wrapped(&gitea.ErrAPI{StatusCode: 500}), ExitAPI},
|
{"wrapped api error", wrapped(&gitea.ErrAPI{StatusCode: 500}), 3},
|
||||||
{"other error", errString("boom"), ExitInternal},
|
{"other error", errString("boom"), 1},
|
||||||
{"nil maps to OK", nil, ExitOK},
|
{"nil maps to OK", nil, 0},
|
||||||
}
|
}
|
||||||
for _, tc := range cases {
|
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)
|
t.Errorf("%s: exitCodeFor = %d, want %d", tc.name, got, tc.want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -77,7 +79,7 @@ func TestExtractGlobals(t *testing.T) {
|
|||||||
"--output", "table",
|
"--output", "table",
|
||||||
"--timeout", "45s",
|
"--timeout", "45s",
|
||||||
"next",
|
"next",
|
||||||
})
|
}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -98,7 +100,7 @@ func TestExtractGlobals(t *testing.T) {
|
|||||||
func TestExtractGlobalsAfterCommand(t *testing.T) {
|
func TestExtractGlobalsAfterCommand(t *testing.T) {
|
||||||
// 全域選項出現在命令之後也應抽出(README:teai next --output table)。
|
// 全域選項出現在命令之後也應抽出(README:teai next --output table)。
|
||||||
g := defaultGlobals()
|
g := defaultGlobals()
|
||||||
rest, err := extractGlobals(&g, []string{"version", "--output", "table"})
|
rest, err := extractGlobals(&g, []string{"version", "--output", "table"}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -122,7 +124,7 @@ func TestExtractGlobalsKeepsCommandFlags(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, args := range cases {
|
for _, args := range cases {
|
||||||
g := defaultGlobals()
|
g := defaultGlobals()
|
||||||
rest, err := extractGlobals(&g, args)
|
rest, err := extractGlobals(&g, args, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("extractGlobals(%v) 不應報錯:%v", args, err)
|
t.Fatalf("extractGlobals(%v) 不應報錯:%v", args, err)
|
||||||
}
|
}
|
||||||
@@ -143,7 +145,7 @@ func TestExtractGlobalsKeepsCommandFlags(t *testing.T) {
|
|||||||
|
|
||||||
func TestExtractGlobalsDoubleDashStops(t *testing.T) {
|
func TestExtractGlobalsDoubleDashStops(t *testing.T) {
|
||||||
g := defaultGlobals()
|
g := defaultGlobals()
|
||||||
rest, err := extractGlobals(&g, []string{"--output", "table", "--", "--url", "x"})
|
rest, err := extractGlobals(&g, []string{"--output", "table", "--", "--url", "x"}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -156,6 +158,56 @@ func TestExtractGlobalsDoubleDashStops(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestExtractGlobalsReservedFlags 驗證 #18:與全域同名的命令旗標
|
||||||
|
// (登記於 commandReservedFlags)必須留給命令,其餘全域選項照常抽取。
|
||||||
|
func TestExtractGlobalsReservedFlags(t *testing.T) {
|
||||||
|
g := defaultGlobals()
|
||||||
|
rest, err := extractGlobals(&g, []string{
|
||||||
|
"add",
|
||||||
|
"--url", "https://srv.example.com", // login add 自有旗標:保留
|
||||||
|
"--token=tok", // 同上(inline 形式):保留
|
||||||
|
"--name", "test-site",
|
||||||
|
"--output", "table", // 非保留:仍是全域
|
||||||
|
}, reservedFlags("login"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
want := []string{"add", "--url", "https://srv.example.com", "--token=tok", "--name", "test-site"}
|
||||||
|
if len(rest) != len(want) {
|
||||||
|
t.Fatalf("rest = %v, want %v", rest, want)
|
||||||
|
}
|
||||||
|
for i := range want {
|
||||||
|
if rest[i] != want[i] {
|
||||||
|
t.Fatalf("rest = %v, want %v", rest, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if g.Output != gitea.FormatTable {
|
||||||
|
t.Fatalf("output = %v, want table", g.Output)
|
||||||
|
}
|
||||||
|
if g.URL != defaultGlobals().URL || g.Token != "" {
|
||||||
|
t.Fatalf("reserved flags must not leak into globals: %+v", g)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestExtractGlobalsNonReservedCommand 驗證 #13 行為不回歸:未登記保留
|
||||||
|
// 旗標的命令(如 next/members/pulls),命令後的全域選項照常抽取、
|
||||||
|
// 命令自己的旗標(--has-work 等)原樣保留。
|
||||||
|
func TestExtractGlobalsNonReservedCommand(t *testing.T) {
|
||||||
|
g := defaultGlobals()
|
||||||
|
rest, err := extractGlobals(&g, []string{
|
||||||
|
"members", "--has-work", "--output", "table",
|
||||||
|
}, reservedFlags("members"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(rest) != 2 || rest[0] != "members" || rest[1] != "--has-work" {
|
||||||
|
t.Fatalf("rest = %v, want [members --has-work]", rest)
|
||||||
|
}
|
||||||
|
if g.Output != gitea.FormatTable {
|
||||||
|
t.Fatalf("output = %v, want table", g.Output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// errString 把字串轉成 error(測試輔助)。
|
// errString 把字串轉成 error(測試輔助)。
|
||||||
type errStr string
|
type errStr string
|
||||||
|
|
||||||
|
|||||||
@@ -145,8 +145,9 @@ func TestLoginAddRejected401NoWrite(t *testing.T) {
|
|||||||
`)
|
`)
|
||||||
_, stderr, code := run(append(prefix,
|
_, stderr, code := run(append(prefix,
|
||||||
"--url", srv.URL, "login", "add", "--token", "bad-tok")...)
|
"--url", srv.URL, "login", "add", "--token", "bad-tok")...)
|
||||||
if code != int(ExitAPI) {
|
// 401 是 API 錯誤;對照 README「輸出與結束碼」=3(#10)。
|
||||||
t.Fatalf("401 should exit 3, got %d (stderr %s)", code, stderr)
|
if code != 3 {
|
||||||
|
t.Fatalf("401 should exit 3 (README api error), got %d (stderr %s)", code, stderr)
|
||||||
}
|
}
|
||||||
if !strings.Contains(stderr, "401") {
|
if !strings.Contains(stderr, "401") {
|
||||||
t.Fatalf("stderr should mention 401: %s", stderr)
|
t.Fatalf("stderr should mention 401: %s", stderr)
|
||||||
@@ -250,8 +251,9 @@ func TestLoginRemoveAsksConfirmation(t *testing.T) {
|
|||||||
func TestLoginUnknownSubcommandUsage(t *testing.T) {
|
func TestLoginUnknownSubcommandUsage(t *testing.T) {
|
||||||
prefix, _ := loginTestEnv(t, "")
|
prefix, _ := loginTestEnv(t, "")
|
||||||
_, stderr, code := run(append(prefix, "login", "bogus")...)
|
_, stderr, code := run(append(prefix, "login", "bogus")...)
|
||||||
if code != int(ExitUsage) {
|
// 對照 README「輸出與結束碼」:用法錯誤=2(#10)。
|
||||||
t.Fatalf("want exit 2, got %d", code)
|
if code != 2 {
|
||||||
|
t.Fatalf("want exit 2 (README usage), got %d", code)
|
||||||
}
|
}
|
||||||
if !strings.Contains(stderr, "unknown login subcommand") {
|
if !strings.Contains(stderr, "unknown login subcommand") {
|
||||||
t.Fatalf("stderr: %s", stderr)
|
t.Fatalf("stderr: %s", stderr)
|
||||||
@@ -261,8 +263,9 @@ func TestLoginUnknownSubcommandUsage(t *testing.T) {
|
|||||||
func TestLoginNoSubcommandUsage(t *testing.T) {
|
func TestLoginNoSubcommandUsage(t *testing.T) {
|
||||||
prefix, _ := loginTestEnv(t, "")
|
prefix, _ := loginTestEnv(t, "")
|
||||||
_, stderr, code := run(append(prefix, "login")...)
|
_, stderr, code := run(append(prefix, "login")...)
|
||||||
if code != int(ExitUsage) {
|
// 對照 README「輸出與結束碼」:用法錯誤=2(#10)。
|
||||||
t.Fatalf("want exit 2, got %d", code)
|
if code != 2 {
|
||||||
|
t.Fatalf("want exit 2 (README usage), got %d", code)
|
||||||
}
|
}
|
||||||
if !strings.Contains(stderr, "subcommand") {
|
if !strings.Contains(stderr, "subcommand") {
|
||||||
t.Fatalf("stderr: %s", stderr)
|
t.Fatalf("stderr: %s", stderr)
|
||||||
@@ -272,8 +275,9 @@ func TestLoginNoSubcommandUsage(t *testing.T) {
|
|||||||
func TestLoginRemoveNotFound(t *testing.T) {
|
func TestLoginRemoveNotFound(t *testing.T) {
|
||||||
prefix, _ := loginTestEnv(t, "logins: []\n")
|
prefix, _ := loginTestEnv(t, "logins: []\n")
|
||||||
_, stderr, code := run(append(prefix, "login", "remove", "--yes", "ghost")...)
|
_, stderr, code := run(append(prefix, "login", "remove", "--yes", "ghost")...)
|
||||||
if code != int(ExitInternal) {
|
// ExitInternal 不在 README 保證範圍;此處固定為 1(內部錯誤,#10)。
|
||||||
t.Fatalf("want exit 1, got %d", code)
|
if code != 1 {
|
||||||
|
t.Fatalf("want exit 1 (internal), got %d", code)
|
||||||
}
|
}
|
||||||
if !strings.Contains(stderr, `login "ghost" not found`) {
|
if !strings.Contains(stderr, `login "ghost" not found`) {
|
||||||
t.Fatalf("stderr: %s", stderr)
|
t.Fatalf("stderr: %s", stderr)
|
||||||
|
|||||||
Reference in New Issue
Block a user