@@ -2522,28 +2522,32 @@ fn assert_tracker_noexistence(key: &str) {
2522
2522
2523
2523
#[ cargo_test]
2524
2524
fn uninstall_running_binary ( ) {
2525
+ use std:: io:: Write ;
2526
+
2525
2527
Package :: new ( "foo" , "0.0.1" )
2526
- . file ( "src/lib.rs" , "" )
2527
2528
. file (
2528
2529
"Cargo.toml" ,
2529
2530
r#"
2530
- [package]
2531
- name = "foo"
2532
- version = "0.0.1"
2533
-
2534
- [[bin]]
2535
- name = "foo"
2536
- path = "src/main.rs"
2537
- "# ,
2531
+ [package]
2532
+ name = "foo"
2533
+ version = "0.0.1"
2534
+ "# ,
2538
2535
)
2539
2536
. file (
2540
2537
"src/main.rs" ,
2541
2538
r#"
2542
- use std::{{thread, time}};
2543
- fn main() {
2544
- thread::sleep(time::Duration::from_secs(3));
2545
- }
2546
- "# ,
2539
+ use std::net::TcpStream;
2540
+ use std::env::var;
2541
+ use std::io::Read;
2542
+ fn main() {
2543
+ for i in 0..2 {
2544
+ TcpStream::connect(&var("__ADDR__").unwrap()[..])
2545
+ .unwrap()
2546
+ .read_to_end(&mut Vec::new())
2547
+ .unwrap();
2548
+ }
2549
+ }
2550
+ "# ,
2547
2551
)
2548
2552
. publish ( ) ;
2549
2553
@@ -2565,15 +2569,26 @@ fn uninstall_running_binary() {
2565
2569
assert_has_installed_exe ( cargo_home ( ) , "foo" ) ;
2566
2570
2567
2571
let foo_bin = cargo_home ( ) . join ( "bin" ) . join ( exe ( "foo" ) ) ;
2568
- let t = thread:: spawn ( || ProcessBuilder :: new ( foo_bin) . exec ( ) . unwrap ( ) ) ;
2572
+ let l = std:: net:: TcpListener :: bind ( "127.0.0.1:0" ) . unwrap ( ) ;
2573
+ let addr = l. local_addr ( ) . unwrap ( ) . to_string ( ) ;
2574
+ let t = thread:: spawn ( move || {
2575
+ ProcessBuilder :: new ( foo_bin)
2576
+ . env ( "__ADDR__" , addr)
2577
+ . exec ( )
2578
+ . unwrap ( ) ;
2579
+ } ) ;
2569
2580
let key = "foo 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" ;
2570
2581
2571
2582
#[ cfg( windows) ]
2572
2583
{
2584
+ // Ensure foo is running before the first `cargo uninstall` call
2585
+ l. accept ( ) . unwrap ( ) . 0 . write_all ( & [ 1 ] ) . unwrap ( ) ;
2573
2586
cargo_process ( "uninstall foo" )
2574
2587
. with_status ( 101 )
2575
2588
. with_stderr_contains ( "[ERROR] failed to remove file `[CWD]/home/.cargo/bin/foo[EXE]`" )
2576
2589
. run ( ) ;
2590
+ // Ensure foo is stopped before the second `cargo uninstall` call
2591
+ l. accept ( ) . unwrap ( ) . 0 . write_all ( & [ 1 ] ) . unwrap ( ) ;
2577
2592
t. join ( ) . unwrap ( ) ;
2578
2593
cargo_process ( "uninstall foo" )
2579
2594
. with_stderr ( "[REMOVING] [CWD]/home/.cargo/bin/foo[EXE]" )
@@ -2582,9 +2597,12 @@ fn uninstall_running_binary() {
2582
2597
2583
2598
#[ cfg( not( windows) ) ]
2584
2599
{
2600
+ // Ensure foo is running before the first `cargo uninstall` call
2601
+ l. accept ( ) . unwrap ( ) . 0 . write_all ( & [ 1 ] ) . unwrap ( ) ;
2585
2602
cargo_process ( "uninstall foo" )
2586
2603
. with_stderr ( "[REMOVING] [CWD]/home/.cargo/bin/foo[EXE]" )
2587
2604
. run ( ) ;
2605
+ l. accept ( ) . unwrap ( ) . 0 . write_all ( & [ 1 ] ) . unwrap ( ) ;
2588
2606
t. join ( ) . unwrap ( ) ;
2589
2607
} ;
2590
2608
0 commit comments