Skip to content

Commit db144c9

Browse files
committed
Add tracker verification for test case uninstall_running_binary
1 parent 1e3b423 commit db144c9

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
lines changed

src/cargo/ops/cargo_uninstall.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,18 @@ fn uninstall_pkgid(
135135
}
136136
}
137137

138-
let to_remove: Vec<&String> = {
138+
let to_remove = {
139139
if bins.is_empty() {
140-
installed.iter().collect()
140+
installed
141141
} else {
142-
bins.iter().collect()
142+
bins
143143
}
144144
};
145145

146146
for bin in to_remove {
147-
let bin_path = dst.join(bin);
147+
let bin_path = dst.join(&bin);
148148
config.shell().status("Removing", bin_path.display())?;
149-
tracker.remove_bin_then_save(pkgid, bin, &bin_path)?;
149+
tracker.remove_bin_then_save(pkgid, &bin, &bin_path)?;
150150
}
151151

152152
Ok(())

src/cargo/ops/common_for_install_and_uninstall.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ impl InstallTracker {
321321
self.v2.remove(pkg_id, bins);
322322
}
323323

324-
/// Remove a bin after it successfully had been removed in disk and the save the tracker at last.
324+
/// Remove a bin after it successfully had been removed in disk and then save the tracker at last.
325325
pub fn remove_bin_then_save(
326326
&mut self,
327327
pkg_id: PackageId,

tests/testsuite/install.rs

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::fs::{self, OpenOptions};
44
use std::io::prelude::*;
55
use std::path::Path;
6+
use std::thread;
67

78
use cargo_test_support::compare;
89
use cargo_test_support::cross_compile;
@@ -11,10 +12,10 @@ use cargo_test_support::registry::{self, registry_path, Package};
1112
use cargo_test_support::{
1213
basic_manifest, cargo_process, no_such_file_err_msg, project, project_in, symlink_supported, t,
1314
};
14-
use cargo_util::ProcessError;
15+
use cargo_util::{ProcessBuilder, ProcessError};
1516

1617
use cargo_test_support::install::{
17-
assert_has_installed_exe, assert_has_not_installed_exe, cargo_home,
18+
assert_has_installed_exe, assert_has_not_installed_exe, cargo_home, exe,
1819
};
1920
use cargo_test_support::paths::{self, CargoPathExt};
2021
use std::env;
@@ -2508,7 +2509,17 @@ fn install_incompat_msrv() {
25082509
.with_status(101).run();
25092510
}
25102511

2511-
#[cfg(windows)]
2512+
fn assert_tracker_noexistence(key: &str) {
2513+
let v1_data: toml::Value =
2514+
toml::from_str(&fs::read_to_string(cargo_home().join(".crates.toml")).unwrap()).unwrap();
2515+
let v2_data: serde_json::Value =
2516+
serde_json::from_str(&fs::read_to_string(cargo_home().join(".crates2.json")).unwrap())
2517+
.unwrap();
2518+
2519+
assert!(v1_data["v1"].get(key).is_none());
2520+
assert!(v2_data["installs"][key].is_null());
2521+
}
2522+
25122523
#[cargo_test]
25132524
fn uninstall_running_binary() {
25142525
Package::new("foo", "0.0.1")
@@ -2553,24 +2564,33 @@ fn uninstall_running_binary() {
25532564
.run();
25542565
assert_has_installed_exe(cargo_home(), "foo");
25552566

2556-
use cargo_util::ProcessBuilder;
2557-
use std::thread;
2558-
let foo_bin = cargo_test_support::install::cargo_home()
2559-
.join("bin")
2560-
.join(cargo_test_support::install::exe("foo"));
2561-
2567+
let foo_bin = cargo_home().join("bin").join(exe("foo"));
25622568
let t = thread::spawn(|| ProcessBuilder::new(foo_bin).exec().unwrap());
2569+
let key = "foo 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)";
25632570

2564-
cargo_process("uninstall foo")
2565-
.with_status(101)
2566-
.with_stderr_contains("[ERROR] failed to remove file `[CWD]/home/.cargo/bin/foo[EXE]`")
2567-
.run();
2568-
assert_has_installed_exe(cargo_home(), "foo");
2571+
#[cfg(windows)]
2572+
{
2573+
cargo_process("uninstall foo")
2574+
.with_status(101)
2575+
.with_stderr_contains("[ERROR] failed to remove file `[CWD]/home/.cargo/bin/foo[EXE]`")
2576+
.run();
2577+
t.join().unwrap();
2578+
cargo_process("uninstall foo")
2579+
.with_stderr("[REMOVING] [CWD]/home/.cargo/bin/foo[EXE]")
2580+
.run();
2581+
};
2582+
2583+
#[cfg(not(windows))]
2584+
{
2585+
cargo_process("uninstall foo")
2586+
.with_stderr("[REMOVING] [CWD]/home/.cargo/bin/foo[EXE]")
2587+
.run();
2588+
t.join().unwrap();
2589+
};
2590+
2591+
assert_has_not_installed_exe(cargo_home(), "foo");
2592+
assert_tracker_noexistence(key);
25692593

2570-
t.join().unwrap();
2571-
cargo_process("uninstall foo")
2572-
.with_stderr("[REMOVING] [CWD]/home/.cargo/bin/foo[EXE]")
2573-
.run();
25742594
cargo_process("install foo")
25752595
.with_stderr(
25762596
"\

0 commit comments

Comments
 (0)