diff --git a/src/cli/self_update.rs b/src/cli/self_update.rs index 9be994db3f7..f01c2af2c59 100644 --- a/src/cli/self_update.rs +++ b/src/cli/self_update.rs @@ -1359,17 +1359,12 @@ pub(crate) fn valid_self_update_modes() -> String { mod tests { use std::collections::HashMap; - use anyhow::Result; - use rustup_macros::unit_test as test; use crate::cli::common; use crate::dist::dist::PartialToolchainDesc; use crate::test::{test_dir, with_rustup_home, Env}; - use crate::{ - currentprocess::{self, TestProcess}, - for_host, - }; + use crate::{currentprocess::TestProcess, for_host}; #[test] fn default_toolchain_is_stable() { @@ -1377,38 +1372,35 @@ mod tests { let mut vars = HashMap::new(); home.apply(&mut vars); let tp = TestProcess::with_vars(vars); - currentprocess::with(tp.clone().into(), || -> Result<()> { - // TODO: we could pass in a custom cfg to get notification - // callbacks rather than output to the tp sink. - let process = tp.clone().into(); - let mut cfg = common::set_globals(tp.cwd.clone(), false, false, &process).unwrap(); - assert_eq!( - "stable" - .parse::() - .unwrap() - .resolve(&cfg.get_default_host_triple().unwrap()) - .unwrap(), - super::_install_selection( - &mut cfg, - None, // No toolchain specified - "default", // default profile - None, - true, - &[], - &[], - ) - .unwrap() // result - .unwrap() // option - ); - Ok(()) - })?; + let mut cfg = + common::set_globals(tp.process.current_dir().unwrap(), false, false, &tp.process) + .unwrap(); + + assert_eq!( + "stable" + .parse::() + .unwrap() + .resolve(&cfg.get_default_host_triple().unwrap()) + .unwrap(), + super::_install_selection( + &mut cfg, + None, // No toolchain specified + "default", // default profile + None, + true, + &[], + &[], + ) + .unwrap() // result + .unwrap() // option + ); assert_eq!( for_host!( r"info: profile set to 'default' info: default host triple is {0} " ), - &String::from_utf8(tp.get_stderr()).unwrap() + &String::from_utf8(tp.stderr()).unwrap() ); Ok(()) }) @@ -1422,12 +1414,7 @@ info: default host triple is {0} let mut vars = HashMap::new(); vars.env("CARGO_HOME", cargo_home.to_string_lossy().to_string()); let tp = TestProcess::with_vars(vars); - currentprocess::with(tp.clone().into(), || -> Result<()> { - let process = tp.clone().into(); - super::install_bins(&process).unwrap(); - Ok(()) - }) - .unwrap(); + super::install_bins(&tp.process).unwrap(); assert!(cargo_home.exists()); } } diff --git a/src/cli/self_update/windows.rs b/src/cli/self_update/windows.rs index 0b2461b8dc8..2ead5c4ae92 100644 --- a/src/cli/self_update/windows.rs +++ b/src/cli/self_update/windows.rs @@ -771,28 +771,25 @@ mod tests { #[test] fn windows_path_regkey_type() { // per issue #261, setting PATH should use REG_EXPAND_SZ. - let tp = currentprocess::TestProcess::default(); with_saved_path(&mut || { - currentprocess::with(tp.clone().into(), || { - let root = RegKey::predef(HKEY_CURRENT_USER); - let environment = root - .open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE) - .unwrap(); - environment.delete_value("PATH").unwrap(); - - { - // Can't compare the Results as Eq isn't derived; thanks error-chain. - #![allow(clippy::unit_cmp)] - assert_eq!((), super::_apply_new_path(Some(wide("foo"))).unwrap()); - } - let root = RegKey::predef(HKEY_CURRENT_USER); - let environment = root - .open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE) - .unwrap(); - let path = environment.get_raw_value("PATH").unwrap(); - assert_eq!(path.vtype, RegType::REG_EXPAND_SZ); - assert_eq!(super::to_winreg_bytes(wide("foo")), &path.bytes[..]); - }) + let root = RegKey::predef(HKEY_CURRENT_USER); + let environment = root + .open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE) + .unwrap(); + environment.delete_value("PATH").unwrap(); + + { + // Can't compare the Results as Eq isn't derived; thanks error-chain. + #![allow(clippy::unit_cmp)] + assert_eq!((), super::_apply_new_path(Some(wide("foo"))).unwrap()); + } + let root = RegKey::predef(HKEY_CURRENT_USER); + let environment = root + .open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE) + .unwrap(); + let path = environment.get_raw_value("PATH").unwrap(); + assert_eq!(path.vtype, RegType::REG_EXPAND_SZ); + assert_eq!(super::to_winreg_bytes(wide("foo")), &path.bytes[..]); }); } @@ -801,35 +798,32 @@ mod tests { use std::io; // during uninstall the PATH key may end up empty; if so we should // delete it. - let tp = currentprocess::TestProcess::default(); with_saved_path(&mut || { - currentprocess::with(tp.clone().into(), || { - let root = RegKey::predef(HKEY_CURRENT_USER); - let environment = root - .open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE) - .unwrap(); - environment - .set_raw_value( - "PATH", - &RegValue { - bytes: super::to_winreg_bytes(wide("foo")), - vtype: RegType::REG_EXPAND_SZ, - }, - ) - .unwrap(); - - { - // Can't compare the Results as Eq isn't derived; thanks error-chain. - #![allow(clippy::unit_cmp)] - assert_eq!((), super::_apply_new_path(Some(Vec::new())).unwrap()); - } - let reg_value = environment.get_raw_value("PATH"); - match reg_value { - Ok(_) => panic!("key not deleted"), - Err(ref e) if e.kind() == io::ErrorKind::NotFound => {} - Err(ref e) => panic!("error {e}"), - } - }) + let root = RegKey::predef(HKEY_CURRENT_USER); + let environment = root + .open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE) + .unwrap(); + environment + .set_raw_value( + "PATH", + &RegValue { + bytes: super::to_winreg_bytes(wide("foo")), + vtype: RegType::REG_EXPAND_SZ, + }, + ) + .unwrap(); + + { + // Can't compare the Results as Eq isn't derived; thanks error-chain. + #![allow(clippy::unit_cmp)] + assert_eq!((), super::_apply_new_path(Some(Vec::new())).unwrap()); + } + let reg_value = environment.get_raw_value("PATH"); + match reg_value { + Ok(_) => panic!("key not deleted"), + Err(ref e) if e.kind() == io::ErrorKind::NotFound => {} + Err(ref e) => panic!("error {e}"), + } }); } @@ -845,22 +839,20 @@ mod tests { }; let process = Process::from(tp.clone()); with_saved_path(&mut || { - currentprocess::with(tp.clone().into(), || { - let root = RegKey::predef(HKEY_CURRENT_USER); - let environment = root - .open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE) - .unwrap(); - let reg_value = RegValue { - bytes: vec![0x12, 0x34], - vtype: RegType::REG_BINARY, - }; - environment.set_raw_value("PATH", ®_value).unwrap(); - // Ok(None) signals no change to the PATH setting layer - assert_eq!( - None, - super::_with_path_cargo_home_bin(|_, _| panic!("called"), &process).unwrap() - ); - }) + let root = RegKey::predef(HKEY_CURRENT_USER); + let environment = root + .open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE) + .unwrap(); + let reg_value = RegValue { + bytes: vec![0x12, 0x34], + vtype: RegType::REG_BINARY, + }; + environment.set_raw_value("PATH", ®_value).unwrap(); + // Ok(None) signals no change to the PATH setting layer + assert_eq!( + None, + super::_with_path_cargo_home_bin(|_, _| panic!("called"), &process).unwrap() + ); }); assert_eq!( r"warning: the registry key HKEY_CURRENT_USER\Environment\PATH is not a string. Not modifying the PATH variable @@ -872,17 +864,14 @@ mod tests { #[test] fn windows_treat_missing_path_as_empty() { // during install the PATH key may be missing; treat it as empty - let tp = currentprocess::TestProcess::default(); with_saved_path(&mut || { - currentprocess::with(tp.clone().into(), || { - let root = RegKey::predef(HKEY_CURRENT_USER); - let environment = root - .open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE) - .unwrap(); - environment.delete_value("PATH").unwrap(); - - assert_eq!(Some(Vec::new()), super::get_windows_path_var().unwrap()); - }) + let root = RegKey::predef(HKEY_CURRENT_USER); + let environment = root + .open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE) + .unwrap(); + environment.delete_value("PATH").unwrap(); + + assert_eq!(Some(Vec::new()), super::get_windows_path_var().unwrap()); }); } diff --git a/src/currentprocess.rs b/src/currentprocess.rs index 0f8c0b15cd1..e20436710e2 100644 --- a/src/currentprocess.rs +++ b/src/currentprocess.rs @@ -15,6 +15,7 @@ use std::{ sync::{Arc, Mutex}, }; +use tracing::subscriber::DefaultGuard; use tracing_subscriber::util::SubscriberInitExt; pub mod filesource; @@ -25,7 +26,7 @@ pub mod terminalsource; pub enum Process { OSProcess(OSProcess), #[cfg(feature = "test")] - TestProcess(TestProcess), + TestProcess(TestContext), } impl Process { @@ -140,37 +141,8 @@ impl home::env::Env for Process { } } -#[cfg(feature = "test")] -impl From for Process { - fn from(p: TestProcess) -> Self { - Self::TestProcess(p) - } -} - static HOOK_INSTALLED: Once = Once::new(); -/// Run a function in the context of a process definition. -/// -/// If the function panics, the process definition *in that thread* is cleared -/// by an implicitly installed global panic hook. -pub fn with(process: Process, f: F) -> R -where - F: FnOnce() -> R, -{ - ensure_hook(); - - PROCESS.with(|p| { - if let Some(old_p) = &*p.borrow() { - panic!("current process already set {old_p:?}"); - } - let _guard = crate::cli::log::tracing_subscriber(&process).set_default(); - *p.borrow_mut() = Some(process); - let result = f(); - *p.borrow_mut() = None; - result - }) -} - fn ensure_hook() { HOOK_INSTALLED.call_once(|| { let orig_hook = panic::take_hook(); @@ -279,54 +251,71 @@ impl Default for OSProcess { } // ------------ test process ---------------- -#[cfg(feature = "test")] -#[derive(Clone, Debug, Default)] + pub struct TestProcess { - pub cwd: PathBuf, - args: Vec, - vars: HashMap, - stdin: filesource::TestStdinInner, - stdout: filesource::TestWriterInner, - stderr: filesource::TestWriterInner, + pub process: Process, + #[allow(dead_code)] // guard is dropped at the end of the test + guard: DefaultGuard, } -#[cfg(feature = "test")] impl TestProcess { - pub fn with_vars(vars: HashMap) -> Self { - let mut new = Self::default(); - new.vars = vars; - new - } - pub fn new, A: AsRef>( cwd: P, args: &[A], vars: HashMap, stdin: &str, ) -> Self { - TestProcess { + Self::from(TestContext { cwd: cwd.as_ref().to_path_buf(), args: args.iter().map(|s| s.as_ref().to_string()).collect(), vars, stdin: Arc::new(Mutex::new(Cursor::new(stdin.to_string()))), stdout: Arc::new(Mutex::new(Vec::new())), stderr: Arc::new(Mutex::new(Vec::new())), - } + }) } - /// Extracts the stdout from the process - pub fn get_stdout(&self) -> Vec { - self.stdout - .lock() - .unwrap_or_else(|e| e.into_inner()) - .clone() + pub fn with_vars(vars: HashMap) -> Self { + let mut inner = TestContext::default(); + inner.vars = vars; + Self::from(inner) } /// Extracts the stderr from the process - pub fn get_stderr(&self) -> Vec { - self.stderr - .lock() - .unwrap_or_else(|e| e.into_inner()) - .clone() + pub fn stderr(&self) -> Vec { + let tp = match &self.process { + Process::TestProcess(tp) => tp, + _ => unreachable!(), + }; + + tp.stderr.lock().unwrap_or_else(|e| e.into_inner()).clone() } } + +impl From for TestProcess { + fn from(inner: TestContext) -> Self { + let inner = Process::TestProcess(inner); + let guard = crate::cli::log::tracing_subscriber(&inner).set_default(); + Self { + process: inner, + guard, + } + } +} + +impl Default for TestProcess { + fn default() -> Self { + Self::from(TestContext::default()) + } +} + +#[cfg(feature = "test")] +#[derive(Clone, Debug, Default)] +pub struct TestContext { + pub cwd: PathBuf, + args: Vec, + vars: HashMap, + stdin: filesource::TestStdinInner, + stdout: filesource::TestWriterInner, + stderr: filesource::TestWriterInner, +} diff --git a/src/currentprocess/terminalsource.rs b/src/currentprocess/terminalsource.rs index 14e2d5d8b68..a6d81d03bc9 100644 --- a/src/currentprocess/terminalsource.rs +++ b/src/currentprocess/terminalsource.rs @@ -241,26 +241,22 @@ mod tests { use rustup_macros::unit_test as test; use super::*; - use crate::{currentprocess, test::Env}; + use crate::currentprocess::TestProcess; + use crate::test::Env; #[test] fn term_color_choice() { fn assert_color_choice(env_val: &str, stream: StreamSelector, color_choice: ColorChoice) { let mut vars = HashMap::new(); vars.env("RUSTUP_TERM_COLOR", env_val); - let tp = currentprocess::TestProcess { - vars, - ..Default::default() - }; - currentprocess::with(tp.clone().into(), || { - let process = Process::from(tp); - let term = ColorableTerminal::new(stream, &process); - let inner = term.inner.lock().unwrap(); - assert!(matches!( - &*inner, - &TerminalInner::TestWriter(_, choice) if choice == color_choice - )); - }); + let tp = TestProcess::with_vars(vars); + + let term = ColorableTerminal::new(stream, &tp.process); + let inner = term.inner.lock().unwrap(); + assert!(matches!( + &*inner, + &TerminalInner::TestWriter(_, choice) if choice == color_choice + )); } assert_color_choice( diff --git a/src/diskio/test.rs b/src/diskio/test.rs index da1157ab67b..44259755133 100644 --- a/src/diskio/test.rs +++ b/src/diskio/test.rs @@ -7,7 +7,7 @@ use rustup_macros::unit_test as test; use crate::test::test_dir; use super::{get_executor, Executor, Item, Kind}; -use crate::currentprocess::{self, TestProcess}; +use crate::currentprocess::TestProcess; impl Item { /// The length of the file, for files (for stats) @@ -24,64 +24,61 @@ fn test_incremental_file(io_threads: &str) -> Result<()> { let mut vars = HashMap::new(); vars.insert("RUSTUP_IO_THREADS".to_string(), io_threads.to_string()); let tp = TestProcess::with_vars(vars); - currentprocess::with(tp.clone().into(), || -> Result<()> { - let mut written = 0; - let mut file_finished = false; - let process = tp.clone().into(); - let mut io_executor: Box = get_executor(None, 32 * 1024 * 1024, &process)?; - let (item, mut sender) = Item::write_file_segmented( - work_dir.path().join("scratch"), - 0o666, - io_executor.incremental_file_state(), - )?; - - // The file should be open and incomplete, and no completed chunks - assert!(io_executor.execute(item).collect::>().is_empty()); - - let mut chunk = io_executor.get_buffer(super::IO_CHUNK_SIZE); - chunk.extend(b"0123456789"); - chunk = chunk.finished(); - sender(chunk); - let mut chunk = io_executor.get_buffer(super::IO_CHUNK_SIZE); - chunk.extend(b"0123456789"); - chunk = chunk.finished(); - sender(chunk); - loop { - for work in io_executor.completed().collect::>() { - match work { - super::CompletedIo::Chunk(size) => written += size, - super::CompletedIo::Item(item) => unreachable!("{:?}", item), - } - } - if written == 20 { - break; + + let mut written = 0; + let mut file_finished = false; + let mut io_executor: Box = get_executor(None, 32 * 1024 * 1024, &tp.process)?; + let (item, mut sender) = Item::write_file_segmented( + work_dir.path().join("scratch"), + 0o666, + io_executor.incremental_file_state(), + )?; + + // The file should be open and incomplete, and no completed chunks + assert!(io_executor.execute(item).collect::>().is_empty()); + + let mut chunk = io_executor.get_buffer(super::IO_CHUNK_SIZE); + chunk.extend(b"0123456789"); + chunk = chunk.finished(); + sender(chunk); + let mut chunk = io_executor.get_buffer(super::IO_CHUNK_SIZE); + chunk.extend(b"0123456789"); + chunk = chunk.finished(); + sender(chunk); + loop { + for work in io_executor.completed().collect::>() { + match work { + super::CompletedIo::Chunk(size) => written += size, + super::CompletedIo::Item(item) => unreachable!("{:?}", item), } } - // sending a zero length chunk closes the file - let mut chunk = io_executor.get_buffer(super::IO_CHUNK_SIZE); - chunk = chunk.finished(); - sender(chunk); - loop { - for work in io_executor.completed().collect::>() { - match work { - super::CompletedIo::Chunk(_) => {} - super::CompletedIo::Item(_) => { - file_finished = true; - } + if written == 20 { + break; + } + } + // sending a zero length chunk closes the file + let mut chunk = io_executor.get_buffer(super::IO_CHUNK_SIZE); + chunk = chunk.finished(); + sender(chunk); + loop { + for work in io_executor.completed().collect::>() { + match work { + super::CompletedIo::Chunk(_) => {} + super::CompletedIo::Item(_) => { + file_finished = true; } } - if file_finished { - break; - } } + if file_finished { + break; + } + } - // no more work should be outstanding - assert!(file_finished); - assert!(io_executor.join().collect::>().is_empty()); - assert_eq!(io_executor.buffer_used(), 0); + // no more work should be outstanding + assert!(file_finished); + assert!(io_executor.join().collect::>().is_empty()); + assert_eq!(io_executor.buffer_used(), 0); - Ok(()) - })?; // We should be able to read back the file assert_eq!( std::fs::read_to_string(work_dir.path().join("scratch"))?, @@ -95,54 +92,51 @@ fn test_complete_file(io_threads: &str) -> Result<()> { let mut vars = HashMap::new(); vars.insert("RUSTUP_IO_THREADS".to_string(), io_threads.to_string()); let tp = TestProcess::with_vars(vars); - currentprocess::with(tp.clone().into(), || -> Result<()> { - let process = tp.clone().into(); - let mut io_executor: Box = get_executor(None, 32 * 1024 * 1024, &process)?; - let mut chunk = io_executor.get_buffer(10); - chunk.extend(b"0123456789"); - assert_eq!(chunk.len(), 10); - chunk = chunk.finished(); - let item = Item::write_file(work_dir.path().join("scratch"), 0o666, chunk); + + let mut io_executor: Box = get_executor(None, 32 * 1024 * 1024, &tp.process)?; + let mut chunk = io_executor.get_buffer(10); + chunk.extend(b"0123456789"); + assert_eq!(chunk.len(), 10); + chunk = chunk.finished(); + let item = Item::write_file(work_dir.path().join("scratch"), 0o666, chunk); + assert_eq!(item.size(), Some(10)); + let mut items = 0; + let mut check_item = |item: Item| { assert_eq!(item.size(), Some(10)); - let mut items = 0; - let mut check_item = |item: Item| { - assert_eq!(item.size(), Some(10)); - items += 1; - assert_eq!(1, items); - }; - let mut finished = false; - for work in io_executor.execute(item).collect::>() { - // The file might complete immediately - match work { - super::CompletedIo::Chunk(size) => unreachable!("{:?}", size), - super::CompletedIo::Item(item) => { - check_item(item); - finished = true; - } + items += 1; + assert_eq!(1, items); + }; + let mut finished = false; + for work in io_executor.execute(item).collect::>() { + // The file might complete immediately + match work { + super::CompletedIo::Chunk(size) => unreachable!("{:?}", size), + super::CompletedIo::Item(item) => { + check_item(item); + finished = true; } } - if !finished { - loop { - for work in io_executor.completed().collect::>() { - match work { - super::CompletedIo::Chunk(size) => unreachable!("{:?}", size), - super::CompletedIo::Item(item) => { - check_item(item); - finished = true; - } + } + if !finished { + loop { + for work in io_executor.completed().collect::>() { + match work { + super::CompletedIo::Chunk(size) => unreachable!("{:?}", size), + super::CompletedIo::Item(item) => { + check_item(item); + finished = true; } } - if finished { - break; - } + } + if finished { + break; } } - assert!(items > 0); - // no more work should be outstanding - assert!(io_executor.join().collect::>().is_empty()); + } + assert!(items > 0); + // no more work should be outstanding + assert!(io_executor.join().collect::>().is_empty()); - Ok(()) - })?; // We should be able to read back the file with correct content assert_eq!( std::fs::read_to_string(work_dir.path().join("scratch"))?, diff --git a/src/dist/component/tests.rs b/src/dist/component/tests.rs index 47506cbf0ec..42d394d4ea4 100644 --- a/src/dist/component/tests.rs +++ b/src/dist/component/tests.rs @@ -4,7 +4,7 @@ use std::path::PathBuf; use rustup_macros::unit_test as test; -use crate::currentprocess::{Process, TestProcess}; +use crate::currentprocess::TestProcess; use crate::dist::component::Transaction; use crate::dist::dist::DEFAULT_DIST_SERVER; use crate::dist::prefix::InstallPrefix; @@ -28,8 +28,8 @@ fn add_file() { ); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); let mut file = tx.add_file("c", PathBuf::from("foo/bar")).unwrap(); write!(file, "test").unwrap(); @@ -57,8 +57,8 @@ fn add_file_then_rollback() { ); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); tx.add_file("c", PathBuf::from("foo/bar")).unwrap(); drop(tx); @@ -80,8 +80,8 @@ fn add_file_that_exists() { let prefix = InstallPrefix::from(prefixdir.path()); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); fs::create_dir_all(prefixdir.path().join("foo")).unwrap(); utils::write_file("", &prefixdir.path().join("foo/bar"), "").unwrap(); @@ -112,8 +112,8 @@ fn copy_file() { let prefix = InstallPrefix::from(prefixdir.path()); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); let srcpath = srcdir.path().join("bar"); utils::write_file("", &srcpath, "").unwrap(); @@ -140,8 +140,8 @@ fn copy_file_then_rollback() { let prefix = InstallPrefix::from(prefixdir.path()); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); let srcpath = srcdir.path().join("bar"); utils::write_file("", &srcpath, "").unwrap(); @@ -168,8 +168,8 @@ fn copy_file_that_exists() { let prefix = InstallPrefix::from(prefixdir.path()); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); let srcpath = srcdir.path().join("bar"); utils::write_file("", &srcpath, "").unwrap(); @@ -205,8 +205,8 @@ fn copy_dir() { let prefix = InstallPrefix::from(prefixdir.path()); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); let srcpath1 = srcdir.path().join("foo"); let srcpath2 = srcdir.path().join("bar/baz"); @@ -240,8 +240,8 @@ fn copy_dir_then_rollback() { let prefix = InstallPrefix::from(prefixdir.path()); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); let srcpath1 = srcdir.path().join("foo"); let srcpath2 = srcdir.path().join("bar/baz"); @@ -275,8 +275,8 @@ fn copy_dir_that_exists() { let prefix = InstallPrefix::from(prefixdir.path()); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); fs::create_dir_all(prefix.path().join("a")).unwrap(); @@ -307,8 +307,8 @@ fn remove_file() { let prefix = InstallPrefix::from(prefixdir.path()); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); let filepath = prefixdir.path().join("foo"); utils::write_file("", &filepath, "").unwrap(); @@ -333,8 +333,8 @@ fn remove_file_then_rollback() { let prefix = InstallPrefix::from(prefixdir.path()); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); let filepath = prefixdir.path().join("foo"); utils::write_file("", &filepath, "").unwrap(); @@ -359,8 +359,8 @@ fn remove_file_that_not_exists() { let prefix = InstallPrefix::from(prefixdir.path()); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); let err = tx.remove_file("c", PathBuf::from("foo")).unwrap_err(); @@ -387,8 +387,8 @@ fn remove_dir() { let prefix = InstallPrefix::from(prefixdir.path()); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); let filepath = prefixdir.path().join("foo/bar"); fs::create_dir_all(filepath.parent().unwrap()).unwrap(); @@ -414,8 +414,8 @@ fn remove_dir_then_rollback() { let prefix = InstallPrefix::from(prefixdir.path()); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); let filepath = prefixdir.path().join("foo/bar"); fs::create_dir_all(filepath.parent().unwrap()).unwrap(); @@ -441,8 +441,8 @@ fn remove_dir_that_not_exists() { let prefix = InstallPrefix::from(prefixdir.path()); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); let err = tx.remove_dir("c", PathBuf::from("foo")).unwrap_err(); @@ -469,8 +469,8 @@ fn write_file() { let prefix = InstallPrefix::from(prefixdir.path()); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); let content = "hi".to_string(); tx.write_file("c", PathBuf::from("foo/bar"), content.clone()) @@ -497,8 +497,8 @@ fn write_file_then_rollback() { let prefix = InstallPrefix::from(prefixdir.path()); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); let content = "hi".to_string(); tx.write_file("c", PathBuf::from("foo/bar"), content) @@ -522,8 +522,8 @@ fn write_file_that_exists() { let prefix = InstallPrefix::from(prefixdir.path()); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); let content = "hi".to_string(); utils_raw::write_file(&prefix.path().join("a"), &content).unwrap(); @@ -554,8 +554,8 @@ fn modify_file_that_not_exists() { let prefix = InstallPrefix::from(prefixdir.path()); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); tx.modify_file(PathBuf::from("foo/bar")).unwrap(); tx.commit(); @@ -579,8 +579,8 @@ fn modify_file_that_exists() { let prefix = InstallPrefix::from(prefixdir.path()); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); let path = prefix.path().join("foo"); utils_raw::write_file(&path, "wow").unwrap(); @@ -604,8 +604,8 @@ fn modify_file_that_not_exists_then_rollback() { let prefix = InstallPrefix::from(prefixdir.path()); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); tx.modify_file(PathBuf::from("foo/bar")).unwrap(); drop(tx); @@ -627,8 +627,8 @@ fn modify_file_that_exists_then_rollback() { let prefix = InstallPrefix::from(prefixdir.path()); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); let path = prefix.path().join("foo"); utils_raw::write_file(&path, "wow").unwrap(); @@ -655,8 +655,8 @@ fn modify_twice_then_rollback() { let prefix = InstallPrefix::from(prefixdir.path()); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); let path = prefix.path().join("foo"); utils_raw::write_file(&path, "wow").unwrap(); @@ -683,8 +683,8 @@ fn do_multiple_op_transaction(rollback: bool) { let prefix = InstallPrefix::from(prefixdir.path()); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); // copy_file let relpath1 = PathBuf::from("bin/rustc"); @@ -785,8 +785,8 @@ fn rollback_failure_keeps_going() { let prefix = InstallPrefix::from(prefixdir.path()); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); write!(tx.add_file("", PathBuf::from("foo")).unwrap(), "").unwrap(); write!(tx.add_file("", PathBuf::from("bar")).unwrap(), "").unwrap(); diff --git a/src/dist/manifestation/tests.rs b/src/dist/manifestation/tests.rs index c6fe68c3a14..cba55e722bf 100644 --- a/src/dist/manifestation/tests.rs +++ b/src/dist/manifestation/tests.rs @@ -17,7 +17,7 @@ use url::Url; use rustup_macros::unit_test as test; use crate::{ - currentprocess::{self, Process, TestProcess}, + currentprocess::{Process, TestProcess}, dist::{ dist::{Profile, TargetTriple, ToolchainDesc, DEFAULT_DIST_SERVER}, download::DownloadCfg, @@ -581,8 +581,7 @@ fn setup_from_dist_server( &["rustup"], HashMap::default(), "", - ) - .into(); + ); let download_cfg = DownloadCfg { dist_root: "phony", @@ -591,12 +590,10 @@ fn setup_from_dist_server( notify_handler: &|event| { println!("{event}"); }, - process: &tp, + process: &tp.process, }; - currentprocess::with(tp.clone(), || { - f(url, &toolchain, &prefix, &download_cfg, &tmp_cx) - }); + f(url, &toolchain, &prefix, &download_cfg, &tmp_cx) } fn initial_install(comps: Compressions) { diff --git a/src/env_var.rs b/src/env_var.rs index 507a627aa36..d6b54e380cf 100644 --- a/src/env_var.rs +++ b/src/env_var.rs @@ -49,7 +49,7 @@ mod tests { use rustup_macros::unit_test as test; use super::*; - use crate::currentprocess::{self, TestProcess}; + use crate::currentprocess::TestProcess; use crate::test::{with_saved_path, Env}; #[test] @@ -61,45 +61,42 @@ mod tests { ); let tp = TestProcess::with_vars(vars); with_saved_path(&mut || { - currentprocess::with(tp.clone().into(), || { - let mut path_entries = vec![]; - let mut cmd = Command::new("test"); + let mut path_entries = vec![]; + let mut cmd = Command::new("test"); - let a = OsString::from("/home/a/.cargo/bin"); - let path_a = PathBuf::from(a); - path_entries.push(path_a); + let a = OsString::from("/home/a/.cargo/bin"); + let path_a = PathBuf::from(a); + path_entries.push(path_a); - let _a = OsString::from("/home/a/.cargo/bin"); - let _path_a = PathBuf::from(_a); - path_entries.push(_path_a); + let _a = OsString::from("/home/a/.cargo/bin"); + let _path_a = PathBuf::from(_a); + path_entries.push(_path_a); - let z = OsString::from("/home/z/.cargo/bin"); - let path_z = PathBuf::from(z); - path_entries.push(path_z); + let z = OsString::from("/home/z/.cargo/bin"); + let path_z = PathBuf::from(z); + path_entries.push(path_z); - let process = tp.clone().into(); - prepend_path("PATH", path_entries, &mut cmd, &process); - let envs: Vec<_> = cmd.get_envs().collect(); + prepend_path("PATH", path_entries, &mut cmd, &tp.process); + let envs: Vec<_> = cmd.get_envs().collect(); - assert_eq!( - envs, - &[( - OsStr::new("PATH"), - Some( - env::join_paths( - [ - "/home/z/.cargo/bin", - "/home/a/.cargo/bin", - "/home/b/.cargo/bin" - ] - .iter() - ) - .unwrap() - .as_os_str() + assert_eq!( + envs, + &[( + OsStr::new("PATH"), + Some( + env::join_paths( + [ + "/home/z/.cargo/bin", + "/home/a/.cargo/bin", + "/home/b/.cargo/bin" + ] + .iter() ) - ),] - ); - }); + .unwrap() + .as_os_str() + ) + ),] + ); }); } } diff --git a/src/test.rs b/src/test.rs index a165006ec6b..630810f4061 100644 --- a/src/test.rs +++ b/src/test.rs @@ -16,7 +16,7 @@ use std::process::Command; use anyhow::Result; pub use crate::cli::self_update::test::{with_saved_global_state, with_saved_path}; -use crate::currentprocess::{self, Process}; +use crate::currentprocess::TestProcess; use crate::dist::dist::TargetTriple; #[cfg(windows)] @@ -127,11 +127,8 @@ pub fn this_host_triple() -> String { // For windows, this host may be different to the target: we may be // building with i686 toolchain, but on an x86_64 host, so run the // actual detection logic and trust it. - let tp = currentprocess::TestProcess::default(); - return currentprocess::with(tp.clone().into(), || { - let process = Process::from(tp); - TargetTriple::from_host(&process).unwrap().to_string() - }); + let tp = TestProcess::default(); + return TargetTriple::from_host(&tp.process).unwrap().to_string(); } let arch = if cfg!(target_arch = "x86") { "i686" diff --git a/tests/suite/dist_install.rs b/tests/suite/dist_install.rs index 01e744dd052..cf80fc363e0 100644 --- a/tests/suite/dist_install.rs +++ b/tests/suite/dist_install.rs @@ -1,7 +1,7 @@ use std::fs::File; use std::io::Write; -use rustup::currentprocess::{Process, TestProcess}; +use rustup::currentprocess::TestProcess; use rustup::dist::component::Components; use rustup::dist::component::Transaction; use rustup::dist::component::{DirectoryPackage, Package}; @@ -118,8 +118,8 @@ fn basic_install() { Box::new(|_| ()), ); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); let components = Components::open(prefix).unwrap(); @@ -165,8 +165,8 @@ fn multiple_component_install() { Box::new(|_| ()), ); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); let components = Components::open(prefix).unwrap(); @@ -216,8 +216,8 @@ fn uninstall() { Box::new(|_| ()), ); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); let components = Components::open(prefix.clone()).unwrap(); @@ -229,10 +229,10 @@ fn uninstall() { // Now uninstall let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let mut tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); for component in components.list().unwrap() { - tx = component.uninstall(tx, &process).unwrap(); + tx = component.uninstall(tx, &tp.process).unwrap(); } tx.commit(); @@ -275,8 +275,8 @@ fn component_bad_version() { Box::new(|_| ()), ); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); let components = Components::open(prefix.clone()).unwrap(); @@ -322,8 +322,8 @@ fn install_to_prefix_that_does_not_exist() { Box::new(|_| ()), ); let notify = |_: Notification<'_>| (); - let process = Process::from(TestProcess::default()); - let tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &process); + let tp = TestProcess::default(); + let tx = Transaction::new(prefix.clone(), &tmp_cx, ¬ify, &tp.process); let components = Components::open(prefix).unwrap();