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 7ebe6d5 commit 0b55e98
Show file tree
Hide file tree
Showing 10 changed files with 333 additions and 384 deletions.
46 changes: 16 additions & 30 deletions src/cli/self_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1333,25 +1333,23 @@ pub(crate) fn cleanup_self_updater(process: &Process) -> Result<()> {
mod tests {
use std::collections::HashMap;

use anyhow::Result;

use rustup_macros::unit_test as test;

use crate::cli::common;
use crate::cli::self_update::InstallOpts;
use crate::dist::dist::{PartialToolchainDesc, Profile};
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);
let mut cfg =
common::set_globals(tp.process.current_dir().unwrap(), false, false, &tp.process)
.unwrap();

let opts = InstallOpts {
default_host_triple: None,
Expand All @@ -1363,30 +1361,23 @@ mod tests {
no_update_toolchain: false,
};

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(),
opts.install(&mut cfg)
.unwrap() // result
.unwrap() // option
);
Ok(())
})?;
assert_eq!(
"stable"
.parse::<PartialToolchainDesc>()
.unwrap()
.resolve(&cfg.get_default_host_triple().unwrap())
.unwrap(),
opts.install(&mut cfg)
.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 @@ -1400,12 +1391,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());
}
}
146 changes: 67 additions & 79 deletions src/cli/self_update/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ mod tests {

use rustup_macros::unit_test as test;

use crate::currentprocess::{self, Process};
use crate::currentprocess::TestProcess;
use crate::test::with_saved_path;

fn wide(str: &str) -> Vec<u16> {
Expand Down 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,87 +798,78 @@ 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}"),
}
});
}

#[test]
fn windows_doesnt_mess_with_a_non_string_path() {
// This writes an error, so we want a sink for it.
let tp = currentprocess::TestProcess::with_vars(
let tp = TestProcess::with_vars(
[("HOME".to_string(), "/unused".to_string())]
.iter()
.cloned()
.collect(),
);
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"), &tp.process).unwrap()
);
});
assert_eq!(
r"warning: the registry key HKEY_CURRENT_USER\Environment\PATH is not a string. Not modifying the PATH variable
",
String::from_utf8(tp.get_stderr()).unwrap()
String::from_utf8(tp.stderr()).unwrap()
);
}

#[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 0b55e98

Please sign in to comment.