Skip to content

Commit 0fd4fd3

Browse files
committed
Add test case for cargo uninstall while try to uninstalling a running Bin.
1 parent 09be0ee commit 0fd4fd3

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

src/cargo/ops/cargo_uninstall.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,14 @@ fn uninstall_pkgid(
147147
tracker.remove(pkgid, &bins);
148148
}
149149

150+
// This should have been handled last, but now restore it to reproduce the problem on the Windows platform.
151+
// See issue #3364.
152+
tracker.save()?;
153+
150154
for bin in to_remove {
151155
config.shell().status("Removing", bin.display())?;
152156
paths::remove_file(bin)?;
153157
}
154158

155-
// Only Save the tracker when remove Bin successfully.
156-
tracker.save()?;
157159
Ok(())
158160
}

tests/testsuite/install.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2507,3 +2507,83 @@ fn install_incompat_msrv() {
25072507
")
25082508
.with_status(101).run();
25092509
}
2510+
2511+
#[cfg(windows)]
2512+
#[cargo_test]
2513+
fn uninstall_running_binary() {
2514+
Package::new("foo", "0.0.1")
2515+
.file("src/lib.rs", "")
2516+
.file(
2517+
"Cargo.toml",
2518+
r#"
2519+
[package]
2520+
name = "foo"
2521+
version = "0.0.1"
2522+
2523+
[[bin]]
2524+
name = "foo"
2525+
path = "src/main.rs"
2526+
"#,
2527+
)
2528+
.file(
2529+
"src/main.rs",
2530+
r#"
2531+
use std::{{thread, time}};
2532+
fn main() {
2533+
println!("start longrunning job.");
2534+
thread::sleep(time::Duration::from_secs(3));
2535+
println!("end longrunning job.");
2536+
}
2537+
"#,
2538+
)
2539+
.publish();
2540+
2541+
cargo_process("install foo")
2542+
.with_stderr(
2543+
"\
2544+
[UPDATING] `[..]` index
2545+
[DOWNLOADING] crates ...
2546+
[DOWNLOADED] foo v0.0.1 (registry [..])
2547+
[INSTALLING] foo v0.0.1
2548+
[COMPILING] foo v0.0.1
2549+
[FINISHED] release [optimized] target(s) in [..]
2550+
[INSTALLING] [CWD]/home/.cargo/bin/foo[EXE]
2551+
[INSTALLED] package `foo v0.0.1` (executable `foo[EXE]`)
2552+
[WARNING] be sure to add `[..]` to your PATH to be able to run the installed binaries
2553+
",
2554+
)
2555+
.run();
2556+
assert_has_installed_exe(cargo_home(), "foo");
2557+
2558+
use cargo_util::ProcessBuilder;
2559+
use std::thread;
2560+
let foo_bin = cargo_test_support::install::cargo_home()
2561+
.join("bin")
2562+
.join(cargo_test_support::install::exe("foo"));
2563+
2564+
let t = thread::spawn(|| ProcessBuilder::new(foo_bin).exec().unwrap());
2565+
2566+
cargo_process("uninstall foo")
2567+
.with_status(101)
2568+
.with_stderr_contains("[ERROR] failed to remove file `[CWD]/home/.cargo/bin/foo[EXE]`")
2569+
.run();
2570+
assert_has_installed_exe(cargo_home(), "foo");
2571+
2572+
t.join().unwrap();
2573+
cargo_process("uninstall foo")
2574+
.with_stderr("[REMOVING] [CWD]/home/.cargo/bin/foo[EXE]")
2575+
.run();
2576+
cargo_process("install foo")
2577+
.with_stderr(
2578+
"\
2579+
[UPDATING] `[..]` index
2580+
[INSTALLING] foo v0.0.1
2581+
[COMPILING] foo v0.0.1
2582+
[FINISHED] release [optimized] target(s) in [..]
2583+
[INSTALLING] [CWD]/home/.cargo/bin/foo[EXE]
2584+
[INSTALLED] package `foo v0.0.1` (executable `foo[EXE]`)
2585+
[WARNING] be sure to add `[..]` to your PATH to be able to run the installed binaries
2586+
",
2587+
)
2588+
.run();
2589+
}

0 commit comments

Comments
 (0)