Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions cmd/apm/cmd_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ func runCache(args []string) int {
return 0
}

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

sub := args[0]
Expand Down Expand Up @@ -109,7 +110,9 @@ func runCacheClean(args []string) int {
fmt.Println(" Remove all cached content")
fmt.Println()
fmt.Println("Options:")
fmt.Println(" --help Show this message and exit.")
fmt.Println(" -f, --force Skip confirmation prompt")
fmt.Println(" -y, --yes Skip confirmation prompt")
fmt.Println(" --help Show this message and exit.")
return 0
}
}
Expand All @@ -121,7 +124,8 @@ func runCacheClean(args []string) int {
fmt.Fprintf(os.Stderr, "[x] Failed to create cache dir: %v\n", mkErr)
return 1
}
fmt.Printf("[+] Cache cleared: %s\n", dir)
fmt.Println("[*] Cleaning cache...")
fmt.Println("[+] Cache cleaned.")
return 0
}
fmt.Fprintf(os.Stderr, "[x] Failed to read cache dir: %v\n", err)
Expand All @@ -133,7 +137,8 @@ func runCacheClean(args []string) int {
return 1
}
}
fmt.Printf("[+] Cache cleared: %s\n", dir)
fmt.Println("[*] Cleaning cache...")
fmt.Println("[+] Cache cleaned.")
return 0
}

Expand All @@ -145,7 +150,8 @@ func runCachePrune(args []string) int {
fmt.Println(" Remove cache entries older than N days")
fmt.Println()
fmt.Println("Options:")
fmt.Println(" --days INTEGER Remove entries older than N days. [default: 30]")
fmt.Println(" --days INTEGER Remove entries not accessed within this many days")
fmt.Println(" [default: 30]")
fmt.Println(" --help Show this message and exit.")
return 0
}
Expand Down
46 changes: 46 additions & 0 deletions cmd/apm/parity_harness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,52 @@ func TestParityHarnessGoCacheInfo(t *testing.T) {
}
}

// TestParityHarnessGoCacheCleanHelp verifies `apm cache clean --help` routes
// to the subcommand help (not the top-level cache help).
func TestParityHarnessGoCacheCleanHelp(t *testing.T) {
r := runBothInTempRepo(t, minimalApmYML, "cache", "clean", "--help")
assertGoExitCode(t, r, 0)
assertPythonVsGoExitCode(t, r)
if !strings.Contains(r.GoStdout, "Usage: apm cache clean") {
t.Errorf("Go `apm cache clean --help` does not show subcommand help: %q", r.GoStdout)
}
if !strings.Contains(r.GoStdout, "--force") || !strings.Contains(r.GoStdout, "--yes") {
t.Errorf("Go `apm cache clean --help` missing --force/--yes flags: %q", r.GoStdout)
}
}

// TestParityHarnessGoCachePruneHelp verifies `apm cache prune --help` routes
// to the subcommand help (not the top-level cache help).
func TestParityHarnessGoCachePruneHelp(t *testing.T) {
r := runBothInTempRepo(t, minimalApmYML, "cache", "prune", "--help")
assertGoExitCode(t, r, 0)
assertPythonVsGoExitCode(t, r)
if !strings.Contains(r.GoStdout, "Usage: apm cache prune") {
t.Errorf("Go `apm cache prune --help` does not show subcommand help: %q", r.GoStdout)
}
if !strings.Contains(r.GoStdout, "--days") {
t.Errorf("Go `apm cache prune --help` missing --days flag: %q", r.GoStdout)
}
}

// TestParityHarnessGoCacheCleanOutputMessages verifies `apm cache clean --yes`
// outputs the same messages as Python.
func TestParityHarnessGoCacheCleanOutputMessages(t *testing.T) {
// Use an isolated temp cache dir so the test does not clear the user's real
// cache and both CLIs operate on the same fresh directory.
cacheDir := t.TempDir()
t.Setenv("APM_CACHE_DIR", cacheDir)
r := runBothInTempRepo(t, minimalApmYML, "cache", "clean", "--yes")
assertGoExitCode(t, r, 0)
assertPythonVsGoExitCode(t, r)
if !strings.Contains(r.GoStdout, "Cleaning cache") {
t.Errorf("Go `apm cache clean --yes` missing 'Cleaning cache' in stdout: %q", r.GoStdout)
}
if !strings.Contains(r.GoStdout, "Cache cleaned") {
t.Errorf("Go `apm cache clean --yes` missing 'Cache cleaned' in stdout: %q", r.GoStdout)
}
}

// TestParityHarnessGoConfigHelp verifies `apm config --help`.
func TestParityHarnessGoConfigHelp(t *testing.T) {
out, _, code := runGo(t, "config", "--help")
Expand Down
Loading