From 1584fad1c21fbf5013e58f18fcf6fe1722629d1d Mon Sep 17 00:00:00 2001 From: Lin Yihai Date: Fri, 1 Dec 2023 18:18:05 +0800 Subject: [PATCH] Add tracker verification for test case `uninstall_running_binary` --- src/cargo/ops/cargo_uninstall.rs | 10 +-- .../ops/common_for_install_and_uninstall.rs | 2 +- tests/testsuite/install.rs | 65 ++++++++++++++++--- 3 files changed, 61 insertions(+), 16 deletions(-) diff --git a/src/cargo/ops/cargo_uninstall.rs b/src/cargo/ops/cargo_uninstall.rs index 7da773f618aa..7b9fdccd4cb7 100644 --- a/src/cargo/ops/cargo_uninstall.rs +++ b/src/cargo/ops/cargo_uninstall.rs @@ -135,18 +135,18 @@ fn uninstall_pkgid( } } - let to_remove: Vec<&String> = { + let to_remove = { if bins.is_empty() { - installed.iter().collect() + installed } else { - bins.iter().collect() + bins } }; for bin in to_remove { - let bin_path = dst.join(bin); + let bin_path = dst.join(&bin); config.shell().status("Removing", bin_path.display())?; - tracker.remove_bin_then_save(pkgid, bin, &bin_path)?; + tracker.remove_bin_then_save(pkgid, &bin, &bin_path)?; } Ok(()) diff --git a/src/cargo/ops/common_for_install_and_uninstall.rs b/src/cargo/ops/common_for_install_and_uninstall.rs index 117368957256..3da593403698 100644 --- a/src/cargo/ops/common_for_install_and_uninstall.rs +++ b/src/cargo/ops/common_for_install_and_uninstall.rs @@ -321,7 +321,7 @@ impl InstallTracker { self.v2.remove(pkg_id, bins); } - /// Remove a bin after it successfully had been removed in disk and the save the tracker at last. + /// Remove a bin after it successfully had been removed in disk and then save the tracker at last. pub fn remove_bin_then_save( &mut self, pkg_id: PackageId, diff --git a/tests/testsuite/install.rs b/tests/testsuite/install.rs index b2ffeed79197..8259c5289b18 100644 --- a/tests/testsuite/install.rs +++ b/tests/testsuite/install.rs @@ -2508,7 +2508,36 @@ fn install_incompat_msrv() { .with_status(101).run(); } -#[cfg(windows)] +fn v1_path() -> PathBuf { + cargo_home().join(".crates.toml") +} + +fn v2_path() -> PathBuf { + cargo_home().join(".crates2.json") +} + +fn load_crates1() -> toml::Value { + toml::from_str(&fs::read_to_string(v1_path()).unwrap()).unwrap() +} + +fn load_crates2() -> serde_json::Value { + serde_json::from_str(&fs::read_to_string(v2_path()).unwrap()).unwrap() +} + +fn assert_tracker_noexistence(key: &str) { + let data = load_crates1(); + assert!(data["v1"].get(key).is_none()); + let data = load_crates2(); + assert!(data["installs"][key].is_null()); +} + +fn assert_tracker_existence(key: &str) { + let data = load_crates1(); + assert!(!data["v1"].get(key).is_none()); + let data = load_crates2(); + assert!(!data["installs"][key].is_null()); +} + #[cargo_test] fn uninstall_running_binary() { Package::new("foo", "0.0.1") @@ -2560,17 +2589,33 @@ fn uninstall_running_binary() { .join(cargo_test_support::install::exe("foo")); let t = thread::spawn(|| ProcessBuilder::new(foo_bin).exec().unwrap()); + let key: &str = "foo 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)"; - cargo_process("uninstall foo") - .with_status(101) - .with_stderr_contains("[ERROR] failed to remove file `[CWD]/home/.cargo/bin/foo[EXE]`") - .run(); - assert_has_installed_exe(cargo_home(), "foo"); + #[cfg(windows)] + { + cargo_process("uninstall foo") + .with_status(101) + .with_stderr_contains("[ERROR] failed to remove file `[CWD]/home/.cargo/bin/foo[EXE]`") + .run(); + assert_has_installed_exe(cargo_home(), "foo"); + assert_tracker_existence(key); + t.join().unwrap(); + cargo_process("uninstall foo") + .with_stderr("[REMOVING] [CWD]/home/.cargo/bin/foo[EXE]") + .run(); + assert_has_not_installed_exe(cargo_home(), "foo"); + assert_tracker_noexistence(key); + }; + + #[cfg(unix)] + { + cargo_process("uninstall foo") + .with_stderr("[REMOVING] [CWD]/home/.cargo/bin/foo[EXE]") + .run(); + assert_tracker_noexistence(key); + t.join().unwrap(); + }; - t.join().unwrap(); - cargo_process("uninstall foo") - .with_stderr("[REMOVING] [CWD]/home/.cargo/bin/foo[EXE]") - .run(); cargo_process("install foo") .with_stderr( "\