Skip to content

Commit da06413

Browse files
[Crane: crane-migration-python-to-go-full-apm-cli-rewrite] (#117)
* [Crane: crane-migration-python-to-go-full-apm-cli-rewrite] Iteration 79: fix cache subcommand --help routing; add --force/--yes flags and output parity Changes: - cmd/apm/cmd_cache.go: fix runCache to route subcommand --help to the subcommand handler instead of intercepting all --help flags at the top level (cache clean --help and cache prune --help now show their own usage, not the top-level cache menu) - cmd/apm/cmd_cache.go: add -f/--force and -y/--yes flags to cache clean help text; align output messages to Python ('Cleaning cache...' + 'Cache cleaned.' instead of 'Cache cleared: <dir>') - cmd/apm/parity_harness_test.go: add 3 new parity contract tests (GoCacheCleanHelp, GoCachePruneHelp, GoCacheCleanOutputMessages) Score: 1.0 (858/858 parity, 903 Go tests, 247 Python tests, all 13 gates pass) Run: https://github.com/githubnext/apm/actions/runs/27236411257 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * ci: trigger checks --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent e96b795 commit da06413

2 files changed

Lines changed: 61 additions & 9 deletions

File tree

cmd/apm/cmd_cache.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ func runCache(args []string) int {
4444
return 0
4545
}
4646

47-
for _, a := range args {
48-
if a == "--help" || a == "-h" {
49-
printCacheHelp()
50-
return 0
51-
}
47+
// Only intercept --help/-h when it is the very first argument (top-level
48+
// cache help). When a subcommand precedes --help (e.g. "cache clean --help"),
49+
// delegate to the subcommand handler so it can show its own usage.
50+
if args[0] == "--help" || args[0] == "-h" {
51+
printCacheHelp()
52+
return 0
5253
}
5354

5455
sub := args[0]
@@ -109,7 +110,9 @@ func runCacheClean(args []string) int {
109110
fmt.Println(" Remove all cached content")
110111
fmt.Println()
111112
fmt.Println("Options:")
112-
fmt.Println(" --help Show this message and exit.")
113+
fmt.Println(" -f, --force Skip confirmation prompt")
114+
fmt.Println(" -y, --yes Skip confirmation prompt")
115+
fmt.Println(" --help Show this message and exit.")
113116
return 0
114117
}
115118
}
@@ -121,7 +124,8 @@ func runCacheClean(args []string) int {
121124
fmt.Fprintf(os.Stderr, "[x] Failed to create cache dir: %v\n", mkErr)
122125
return 1
123126
}
124-
fmt.Printf("[+] Cache cleared: %s\n", dir)
127+
fmt.Println("[*] Cleaning cache...")
128+
fmt.Println("[+] Cache cleaned.")
125129
return 0
126130
}
127131
fmt.Fprintf(os.Stderr, "[x] Failed to read cache dir: %v\n", err)
@@ -133,7 +137,8 @@ func runCacheClean(args []string) int {
133137
return 1
134138
}
135139
}
136-
fmt.Printf("[+] Cache cleared: %s\n", dir)
140+
fmt.Println("[*] Cleaning cache...")
141+
fmt.Println("[+] Cache cleaned.")
137142
return 0
138143
}
139144

@@ -145,7 +150,8 @@ func runCachePrune(args []string) int {
145150
fmt.Println(" Remove cache entries older than N days")
146151
fmt.Println()
147152
fmt.Println("Options:")
148-
fmt.Println(" --days INTEGER Remove entries older than N days. [default: 30]")
153+
fmt.Println(" --days INTEGER Remove entries not accessed within this many days")
154+
fmt.Println(" [default: 30]")
149155
fmt.Println(" --help Show this message and exit.")
150156
return 0
151157
}

cmd/apm/parity_harness_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,52 @@ func TestParityHarnessGoCacheInfo(t *testing.T) {
375375
}
376376
}
377377

378+
// TestParityHarnessGoCacheCleanHelp verifies `apm cache clean --help` routes
379+
// to the subcommand help (not the top-level cache help).
380+
func TestParityHarnessGoCacheCleanHelp(t *testing.T) {
381+
r := runBothInTempRepo(t, minimalApmYML, "cache", "clean", "--help")
382+
assertGoExitCode(t, r, 0)
383+
assertPythonVsGoExitCode(t, r)
384+
if !strings.Contains(r.GoStdout, "Usage: apm cache clean") {
385+
t.Errorf("Go `apm cache clean --help` does not show subcommand help: %q", r.GoStdout)
386+
}
387+
if !strings.Contains(r.GoStdout, "--force") || !strings.Contains(r.GoStdout, "--yes") {
388+
t.Errorf("Go `apm cache clean --help` missing --force/--yes flags: %q", r.GoStdout)
389+
}
390+
}
391+
392+
// TestParityHarnessGoCachePruneHelp verifies `apm cache prune --help` routes
393+
// to the subcommand help (not the top-level cache help).
394+
func TestParityHarnessGoCachePruneHelp(t *testing.T) {
395+
r := runBothInTempRepo(t, minimalApmYML, "cache", "prune", "--help")
396+
assertGoExitCode(t, r, 0)
397+
assertPythonVsGoExitCode(t, r)
398+
if !strings.Contains(r.GoStdout, "Usage: apm cache prune") {
399+
t.Errorf("Go `apm cache prune --help` does not show subcommand help: %q", r.GoStdout)
400+
}
401+
if !strings.Contains(r.GoStdout, "--days") {
402+
t.Errorf("Go `apm cache prune --help` missing --days flag: %q", r.GoStdout)
403+
}
404+
}
405+
406+
// TestParityHarnessGoCacheCleanOutputMessages verifies `apm cache clean --yes`
407+
// outputs the same messages as Python.
408+
func TestParityHarnessGoCacheCleanOutputMessages(t *testing.T) {
409+
// Use an isolated temp cache dir so the test does not clear the user's real
410+
// cache and both CLIs operate on the same fresh directory.
411+
cacheDir := t.TempDir()
412+
t.Setenv("APM_CACHE_DIR", cacheDir)
413+
r := runBothInTempRepo(t, minimalApmYML, "cache", "clean", "--yes")
414+
assertGoExitCode(t, r, 0)
415+
assertPythonVsGoExitCode(t, r)
416+
if !strings.Contains(r.GoStdout, "Cleaning cache") {
417+
t.Errorf("Go `apm cache clean --yes` missing 'Cleaning cache' in stdout: %q", r.GoStdout)
418+
}
419+
if !strings.Contains(r.GoStdout, "Cache cleaned") {
420+
t.Errorf("Go `apm cache clean --yes` missing 'Cache cleaned' in stdout: %q", r.GoStdout)
421+
}
422+
}
423+
378424
// TestParityHarnessGoConfigHelp verifies `apm config --help`.
379425
func TestParityHarnessGoConfigHelp(t *testing.T) {
380426
out, _, code := runGo(t, "config", "--help")

0 commit comments

Comments
 (0)