fix(automation): Claude Code install says when the older entry could not be removed

`install_claude_code` ignored the result of `claude mcp remove`, so a failed
remove (on CI, a freshly written script briefly busy) led to a second
"already exists" and an error claiming the entry had been removed. It now
reports the failed remove with the command to run instead, and the test
covers it.

Also the CLI tests for the new `mcp install` subcommand: a stray word after
`mcp` is an unrecognized subcommand, and it has its own man page.
This commit is contained in:
David Mann committed 2026-11-12 13:58:43 +01:00
1 parent a16144256f
commit 466f010a58
3 files changed
+30 -4

No files matched your search

+3 -1
View File
@@ -163,7 +163,9 @@ fn server_usage_error(args: &[&str], expected: &str) {
#[test]
fn server_modes_reject_ignored_or_conflicting_options() {
for sub in ["mcp", "serve"] {
server_usage_error(&[sub, "stray.psd"], "unexpected argument 'stray.psd'");
// `mcp` has a subcommand (`mcp install`), so clap reads a stray word there as one.
let stray = if sub == "mcp" { "unrecognized subcommand 'stray.psd'" } else { "unexpected argument 'stray.psd'" };
server_usage_error(&[sub, "stray.psd"], stray);
for flag in ["--control-token", "--control-token-file"] {
server_usage_error(&[sub, flag, "unused"], if sub == "mcp" { "requires --bridge" } else { "requires --port" });
}
@@ -49,6 +49,8 @@ fn the_example_writes_the_files_the_packages_install() {
let mut expected: Vec<String> = completions.map(String::from).into();
expected.push("photocraft-cli.1".into());
expected.extend(subcommands.iter().map(|s| format!("photocraft-cli-{s}.1")));
// Nested subcommands get their own page too (the packages install every `*.1`).
expected.push("photocraft-cli-mcp-install.1".into());
expected.sort();
assert_eq!(written, expected);
std::fs::remove_dir_all(&dir).unwrap();
+25 -3
View File
@@ -595,7 +595,18 @@ fn install_claude_code(launch: &McpLaunch, env: &ClientEnv) -> Result<Installed,
let mut replaced = false;
let exists = || run(&["mcp", "get", SERVER_NAME]).is_ok_and(|out| out.status.success());
if result.as_ref().is_ok_and(|out| !out.status.success()) && exists() {
let _ = run(&["mcp", "remove", "--scope", "user", SERVER_NAME]);
// Say so when the older entry could not be removed: adding again would only fail with
// "already exists", and the message must not claim the entry is gone.
let why_not = match run(&["mcp", "remove", "--scope", "user", SERVER_NAME]) {
Ok(out) if out.status.success() => None,
Ok(out) => Some(String::from_utf8_lossy(if out.stderr.is_empty() { &out.stdout } else { &out.stderr }).trim().to_string()),
Err(e) => Some(format!("{}: {e}", claude.display())),
};
if let Some(why) = why_not {
return Err(AutomationError::Other(format!(
"Claude Code already has a `{SERVER_NAME}` entry and removing it failed ({why}); run this in a terminal to replace it: claude mcp remove --scope user {SERVER_NAME} && {by_hand}"
)));
}
replaced = true;
result = run(&add);
}
@@ -882,10 +893,12 @@ mod tests {
let log = home.join("calls.txt");
let fail = home.join("fail");
let exists = home.join("exists");
let keep = home.join("keep");
// `add` fails while an entry exists (as claude's does) or while `fail` is there; `remove`
// removes the entry.
// removes the entry, or fails while `keep` is there.
let script = format!(
"#!/bin/sh\nprintf 'PATH=%s\\n' \"$PATH\" >> '{log}'\nprintf '%s ' \"$@\" >> '{log}'\nprintf '\\n' >> '{log}'\n\
if [ \"$2\" = remove ] && [ -e '{keep}' ]; then echo 'locked' >&2; exit 1; fi\n\
if [ \"$2\" = remove ]; then rm -f '{exists}'; exit 0; fi\n\
if [ \"$2\" = get ]; then [ -e '{exists}' ]; exit $?; fi\n\
if [ \"$2\" = add ] && [ -e '{exists}' ]; then echo 'already exists' >&2; exit 1; fi\n\
@@ -893,7 +906,8 @@ mod tests {
if [ \"$2\" = add ]; then : > '{exists}'; fi\n",
log = log.display(),
fail = fail.display(),
exists = exists.display()
exists = exists.display(),
keep = keep.display()
);
let claude = bin.join("claude");
std::fs::write(&claude, script).unwrap();
@@ -929,6 +943,14 @@ mod tests {
let e = run().unwrap_err().to_string();
assert!(e.contains("boom") && !e.contains("was removed"), "{e}");
assert!(!std::fs::read_to_string(&log).unwrap().contains("mcp remove"));
// An entry exists and removing it fails: the error says so, and does not claim it went.
std::fs::remove_file(&fail).unwrap();
std::fs::write(&exists, "").unwrap();
std::fs::write(&keep, "").unwrap();
let e = run().unwrap_err().to_string();
assert!(e.contains("removing it failed (locked)") && !e.contains("was removed"), "{e}");
assert!(e.contains("claude mcp remove --scope user photocraft && claude mcp add"), "{e}");
assert!(exists.exists(), "the older entry is still there");
let _ = std::fs::remove_dir_all(&home);
}