Skip to content

Commit

Permalink
Add tracker verification for test case uninstall_running_binary
Browse files Browse the repository at this point in the history
  • Loading branch information
linyihai committed Dec 1, 2023
1 parent 1e3b423 commit 1584fad
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/cargo/ops/cargo_uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/common_for_install_and_uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
65 changes: 55 additions & 10 deletions tests/testsuite/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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(
"\
Expand Down

0 comments on commit 1584fad

Please sign in to comment.