Skip to content

Commit

Permalink
Remove currentprocess::with()
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Jun 15, 2024
1 parent 8cd7ebe commit 0202bd6
Show file tree
Hide file tree
Showing 10 changed files with 335 additions and 389 deletions.
63 changes: 25 additions & 38 deletions src/cli/self_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1359,56 +1359,48 @@ 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() {
with_rustup_home(|home| {
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::<PartialToolchainDesc>()
.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::<PartialToolchainDesc>()
.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(())
})
Expand All @@ -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());
}
}
139 changes: 64 additions & 75 deletions src/cli/self_update/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[..]);
});
}

Expand All @@ -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}"),
}
});
}

Expand All @@ -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", &reg_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", &reg_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
Expand All @@ -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());
});
}

Expand Down
Loading

0 comments on commit 0202bd6

Please sign in to comment.