diff --git a/crates/graphicle/src/terminal.rs b/crates/graphicle/src/terminal.rs index c7ef591..943b2e7 100644 --- a/crates/graphicle/src/terminal.rs +++ b/crates/graphicle/src/terminal.rs @@ -1064,6 +1064,7 @@ fn shell_quote(value: &str) -> String { /// in a container. `graphicle update` and `graphicle uninstall` both use it. pub fn installed(exe: &Path) -> Install { let cargo_bin = std::env::var_os("CARGO_HOME") + .filter(|value| !value.is_empty()) .map(PathBuf::from) .or_else(|| home_dir().map(|h| h.join(".cargo"))) .map(|cargo| cargo.join("bin")); diff --git a/crates/graphicle/tests/it/ux_terminal.rs b/crates/graphicle/tests/it/ux_terminal.rs index 94851e9..6d5f8d2 100644 --- a/crates/graphicle/tests/it/ux_terminal.rs +++ b/crates/graphicle/tests/it/ux_terminal.rs @@ -89,8 +89,18 @@ impl Sandbox { } /// The program with this sandbox's environment, in `cwd`. fn command(&self, cwd: &Path, args: &[&str], env: &[(&str, &str)]) -> Command { + self.command_of(Path::new(env!("CARGO_BIN_EXE_graphicle")), cwd, args, env) + } + /// [`Sandbox::command`] for `program`. + fn command_of( + &self, + program: &Path, + cwd: &Path, + args: &[&str], + env: &[(&str, &str)], + ) -> Command { let home = self.home(); - let mut command = Command::new(env!("CARGO_BIN_EXE_graphicle")); + let mut command = Command::new(program); command .current_dir(cwd) .arg("--data-dir") @@ -916,6 +926,78 @@ fn update_without_a_feed_says_how_to_update_and_sync_defaults_to_this_folder() { assert!(again.contains("Code graph: 7 nodes"), "{again}"); } +/// Update and uninstall treat an empty CARGO_HOME as unset. +#[test] +fn update_and_uninstall_treat_an_empty_cargo_home_as_unset() { + let sandbox = Sandbox::new(); + let cargo_bin = sandbox.home().join(".cargo").join("bin"); + std::fs::create_dir_all(&cargo_bin).unwrap(); + let program = cargo_bin.join(crate::child_env::program("graphicle")); + if std::fs::hard_link(env!("CARGO_BIN_EXE_graphicle"), &program).is_err() { + std::fs::copy(env!("CARGO_BIN_EXE_graphicle"), &program).unwrap(); + } + for env in [&[][..], &[("CARGO_HOME", "")][..]] { + let update = json_of( + &crate::fork_guard::spawn(&mut sandbox.command_of( + &program, + &sandbox.elsewhere(), + &["update"], + env, + )) + .unwrap() + .wait_with_output() + .unwrap(), + ); + assert_eq!(update["installed"], "cargo", "{update}"); + let commands = update["update"].as_array().unwrap(); + assert!( + commands.iter().any(|command| { + command.as_str() == Some("cargo install --path runtime --locked --force") + }), + "{update}" + ); + } + let custom = sandbox.root.join("cargo"); + std::fs::create_dir_all(custom.join("bin")).unwrap(); + let that_program = custom + .join("bin") + .join(crate::child_env::program("graphicle")); + if std::fs::hard_link(env!("CARGO_BIN_EXE_graphicle"), &that_program).is_err() { + std::fs::copy(env!("CARGO_BIN_EXE_graphicle"), &that_program).unwrap(); + } + let update = json_of( + &crate::fork_guard::spawn(&mut sandbox.command_of( + &that_program, + &sandbox.elsewhere(), + &["update"], + &[("CARGO_HOME", custom.to_str().unwrap())], + )) + .unwrap() + .wait_with_output() + .unwrap(), + ); + assert_eq!(update["installed"], "cargo", "{update}"); + std::fs::create_dir_all(sandbox.data()).unwrap(); + let report = json_of( + &crate::fork_guard::spawn(&mut sandbox.command_of( + &program, + &sandbox.elsewhere(), + &["uninstall"], + &[("CARGO_HOME", "")], + )) + .unwrap() + .wait_with_output() + .unwrap(), + ); + assert_eq!(report["remove_program"]["method"], "cargo", "{report:#}"); + assert!( + report["remove_program"]["command"] + .as_str() + .is_some_and(|command| command.starts_with("cargo uninstall ")), + "{report:#}" + ); +} + #[test] fn open_reuses_a_running_server_and_prints_the_address_without_a_browser() { let sandbox = Sandbox::new();