Skip to content

Commit

Permalink
Add test case for cargo uninstall while try to uninstalling a runni…
Browse files Browse the repository at this point in the history
…ng Bin.
  • Loading branch information
linyihai committed Nov 29, 2023
1 parent 09be0ee commit 7ac6873
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/cargo/ops/cargo_uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,14 @@ fn uninstall_pkgid(
tracker.remove(pkgid, &bins);
}

// This should have been handled last, but now restore it to reproduce the problem on the Windows platform.
// See issue #3364.
tracker.save()?;

for bin in to_remove {
config.shell().status("Removing", bin.display())?;
paths::remove_file(bin)?;
}

// Only Save the tracker when remove Bin successfully.
tracker.save()?;
Ok(())
}
75 changes: 75 additions & 0 deletions tests/testsuite/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2507,3 +2507,78 @@ fn install_incompat_msrv() {
")
.with_status(101).run();
}

#[cargo_test]
fn uninstall_running_binary() {
Package::new("foo", "0.0.1")
.file("src/lib.rs", "")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
[[bin]]
name = "foo"
path = "src/main.rs"
"#,)
.file(
"src/main.rs",
r#"
use std::{{thread, time}};
fn main() {
println!("start longrunning job.");
thread::sleep(time::Duration::from_secs(3));
println!("end longrunning job.");
}
"#)
.publish();

cargo_process("install foo")
.with_stderr(
"\
[UPDATING] `[..]` index
[DOWNLOADING] crates ...
[DOWNLOADED] foo v0.0.1 (registry [..])
[INSTALLING] foo v0.0.1
[COMPILING] foo v0.0.1
[FINISHED] release [optimized] target(s) in [..]
[INSTALLING] [CWD]/home/.cargo/bin/foo[EXE]
[INSTALLED] package `foo v0.0.1` (executable `foo[EXE]`)
[WARNING] be sure to add `[..]` to your PATH to be able to run the installed binaries
",
)
.run();
assert_has_installed_exe(cargo_home(), "foo");

use cargo_util::ProcessBuilder;
use std::thread;
let foo_bin = cargo_test_support::install::cargo_home()
.join("bin")
.join(cargo_test_support::install::exe("foo"));

let t = thread::spawn(|| ProcessBuilder::new(foo_bin).exec().unwrap());

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");

t.join().unwrap();
cargo_process("uninstall foo")
.with_stderr("[REMOVING] [CWD]/home/.cargo/bin/foo[EXE]")
.run();
cargo_process("install foo")
.with_stderr(
"\
[UPDATING] `[..]` index
[INSTALLING] foo v0.0.1
[COMPILING] foo v0.0.1
[FINISHED] release [optimized] target(s) in [..]
[INSTALLING] [CWD]/home/.cargo/bin/foo[EXE]
[INSTALLED] package `foo v0.0.1` (executable `foo[EXE]`)
[WARNING] be sure to add `[..]` to your PATH to be able to run the installed binaries
",
)
.run();
}

0 comments on commit 7ac6873

Please sign in to comment.