Skip to content

Commit 230c766

Browse files
committed
WIP
1 parent df41415 commit 230c766

File tree

14 files changed

+892
-853
lines changed

14 files changed

+892
-853
lines changed

src/cli/self_update.rs

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,46 +1291,48 @@ mod tests {
12911291

12921292
#[test]
12931293
async fn default_toolchain_is_stable() {
1294-
with_rustup_home(|home| async move {
1295-
let mut vars = HashMap::new();
1296-
home.apply(&mut vars);
1297-
let tp = Box::new(currentprocess::TestProcess {
1298-
vars,
1299-
..Default::default()
1300-
});
1301-
currentprocess::with_tokio(tp.clone(), async {
1302-
// TODO: we could pass in a custom cfg to get notification
1303-
// callbacks rather than output to the tp sink.
1304-
let mut cfg = common::set_globals(false, false).unwrap();
1294+
with_rustup_home(|home| {
1295+
Box::pin(async move {
1296+
let mut vars = HashMap::new();
1297+
home.apply(&mut vars);
1298+
let tp = Box::new(currentprocess::TestProcess {
1299+
vars,
1300+
..Default::default()
1301+
});
1302+
currentprocess::with_tokio(tp.clone(), async {
1303+
// TODO: we could pass in a custom cfg to get notification
1304+
// callbacks rather than output to the tp sink.
1305+
let mut cfg = common::set_globals(false, false).unwrap();
1306+
assert_eq!(
1307+
"stable"
1308+
.parse::<PartialToolchainDesc>()
1309+
.unwrap()
1310+
.resolve(&cfg.get_default_host_triple().unwrap())
1311+
.unwrap(),
1312+
super::_install_selection(
1313+
&mut cfg,
1314+
None, // No toolchain specified
1315+
"default", // default profile
1316+
None,
1317+
true,
1318+
&[],
1319+
&[],
1320+
)
1321+
.unwrap() // result
1322+
.unwrap() // option
1323+
);
1324+
Ok::<(), anyhow::Error>(())
1325+
})
1326+
.unwrap();
13051327
assert_eq!(
1306-
"stable"
1307-
.parse::<PartialToolchainDesc>()
1308-
.unwrap()
1309-
.resolve(&cfg.get_default_host_triple().unwrap())
1310-
.unwrap(),
1311-
super::_install_selection(
1312-
&mut cfg,
1313-
None, // No toolchain specified
1314-
"default", // default profile
1315-
None,
1316-
true,
1317-
&[],
1318-
&[],
1319-
)
1320-
.unwrap() // result
1321-
.unwrap() // option
1322-
);
1323-
Ok::<(), anyhow::Error>(())
1324-
})?;
1325-
assert_eq!(
1326-
for_host!(
1327-
r"info: profile set to 'default'
1328+
for_host!(
1329+
r"info: profile set to 'default'
13281330
info: default host triple is {0}
13291331
"
1330-
),
1331-
String::from_utf8(tp.get_stderr()).unwrap()
1332-
);
1333-
Ok(())
1332+
),
1333+
String::from_utf8(tp.get_stderr()).unwrap()
1334+
);
1335+
})
13341336
})
13351337
.await
13361338
.unwrap();

src/cli/self_update/test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Support for functional tests.
22
3-
use std::sync::Mutex;
3+
use std::{future::Future, pin::Pin, sync::Mutex};
44

55
#[cfg(not(unix))]
66
use winreg::{
@@ -35,7 +35,7 @@ fn restore_path(p: Option<RegValue>) {
3535
}
3636

3737
/// Support testing of code that mutates global path state
38-
pub fn with_saved_path(f: &mut dyn FnMut()) {
38+
pub async fn with_saved_path(f: &dyn Fn() -> Pin<Box<dyn Future<Output = ()> + Send>>) {
3939
// Lock protects concurrent mutation of registry
4040
static LOCK: Mutex<()> = Mutex::new(());
4141
let _g = LOCK.lock();
@@ -45,7 +45,7 @@ pub fn with_saved_path(f: &mut dyn FnMut()) {
4545
let saved_path = get_path().expect("Error getting PATH: Better abort to avoid trashing it.");
4646
let _g = scopeguard::guard(saved_path, restore_path);
4747

48-
f();
48+
f().await;
4949
}
5050

5151
#[cfg(unix)]

src/cli/self_update/windows.rs

Lines changed: 103 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -747,118 +747,132 @@ mod tests {
747747
#[test]
748748
async fn windows_path_regkey_type() {
749749
// per issue #261, setting PATH should use REG_EXPAND_SZ.
750-
let tp = Box::new(currentprocess::TestProcess::default());
750+
751751
with_saved_path(&mut || {
752-
currentprocess::with_tokio(tp.clone(), async {
753-
let root = RegKey::predef(HKEY_CURRENT_USER);
754-
let environment = root
755-
.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE)
756-
.unwrap();
757-
environment.delete_value("PATH").unwrap();
758-
759-
{
760-
// Can't compare the Results as Eq isn't derived; thanks error-chain.
761-
#![allow(clippy::unit_cmp)]
762-
assert_eq!((), super::_apply_new_path(Some(wide("foo"))).unwrap());
763-
}
764-
let root = RegKey::predef(HKEY_CURRENT_USER);
765-
let environment = root
766-
.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE)
767-
.unwrap();
768-
let path = environment.get_raw_value("PATH").unwrap();
769-
assert_eq!(path.vtype, RegType::REG_EXPAND_SZ);
770-
assert_eq!(super::to_winreg_bytes(wide("foo")), &path.bytes[..]);
752+
let tp = Box::new(currentprocess::TestProcess::default());
753+
Box::pin(async move {
754+
currentprocess::with_tokio(tp.clone(), async move {
755+
let root = RegKey::predef(HKEY_CURRENT_USER);
756+
let environment = root
757+
.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE)
758+
.unwrap();
759+
environment.delete_value("PATH").unwrap();
760+
761+
{
762+
// Can't compare the Results as Eq isn't derived; thanks error-chain.
763+
#![allow(clippy::unit_cmp)]
764+
assert_eq!((), super::_apply_new_path(Some(wide("foo"))).unwrap());
765+
}
766+
let root = RegKey::predef(HKEY_CURRENT_USER);
767+
let environment = root
768+
.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE)
769+
.unwrap();
770+
let path = environment.get_raw_value("PATH").unwrap();
771+
assert_eq!(path.vtype, RegType::REG_EXPAND_SZ);
772+
assert_eq!(super::to_winreg_bytes(wide("foo")), &path.bytes[..]);
773+
})
771774
})
772-
});
775+
})
776+
.await;
773777
}
774778

775779
#[test]
776780
async fn windows_path_delete_key_when_empty() {
777-
use std::io;
778-
// during uninstall the PATH key may end up empty; if so we should
779-
// delete it.
780-
let tp = Box::new(currentprocess::TestProcess::default());
781781
with_saved_path(&mut || {
782-
currentprocess::with_tokio(tp.clone(), async {
783-
let root = RegKey::predef(HKEY_CURRENT_USER);
784-
let environment = root
785-
.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE)
786-
.unwrap();
787-
environment
788-
.set_raw_value(
789-
"PATH",
790-
&RegValue {
791-
bytes: super::to_winreg_bytes(wide("foo")),
792-
vtype: RegType::REG_EXPAND_SZ,
793-
},
794-
)
795-
.unwrap();
796-
797-
{
798-
// Can't compare the Results as Eq isn't derived; thanks error-chain.
799-
#![allow(clippy::unit_cmp)]
800-
assert_eq!((), super::_apply_new_path(Some(Vec::new())).unwrap());
801-
}
802-
let reg_value = environment.get_raw_value("PATH");
803-
match reg_value {
804-
Ok(_) => panic!("key not deleted"),
805-
Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
806-
Err(ref e) => panic!("error {e}"),
807-
}
782+
Box::pin(async move {
783+
use std::io;
784+
// during uninstall the PATH key may end up empty; if so we should
785+
// delete it.
786+
let tp = Box::new(currentprocess::TestProcess::default());
787+
788+
currentprocess::with_tokio(tp.clone(), async {
789+
let root = RegKey::predef(HKEY_CURRENT_USER);
790+
let environment = root
791+
.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE)
792+
.unwrap();
793+
environment
794+
.set_raw_value(
795+
"PATH",
796+
&RegValue {
797+
bytes: super::to_winreg_bytes(wide("foo")),
798+
vtype: RegType::REG_EXPAND_SZ,
799+
},
800+
)
801+
.unwrap();
802+
803+
{
804+
// Can't compare the Results as Eq isn't derived; thanks error-chain.
805+
#![allow(clippy::unit_cmp)]
806+
assert_eq!((), super::_apply_new_path(Some(Vec::new())).unwrap());
807+
}
808+
let reg_value = environment.get_raw_value("PATH");
809+
match reg_value {
810+
Ok(_) => panic!("key not deleted"),
811+
Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
812+
Err(ref e) => panic!("error {e}"),
813+
}
814+
})
808815
})
809-
});
816+
})
817+
.await;
810818
}
811819

812820
#[test]
813821
async fn windows_doesnt_mess_with_a_non_string_path() {
814-
// This writes an error, so we want a sink for it.
815-
let tp = Box::new(currentprocess::TestProcess {
816-
vars: [("HOME".to_string(), "/unused".to_string())]
817-
.iter()
818-
.cloned()
819-
.collect(),
820-
..Default::default()
821-
});
822822
with_saved_path(&mut || {
823-
currentprocess::with_tokio(tp.clone(), async {
824-
let root = RegKey::predef(HKEY_CURRENT_USER);
825-
let environment = root
826-
.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE)
827-
.unwrap();
828-
let reg_value = RegValue {
829-
bytes: vec![0x12, 0x34],
830-
vtype: RegType::REG_BINARY,
831-
};
832-
environment.set_raw_value("PATH", &reg_value).unwrap();
833-
// Ok(None) signals no change to the PATH setting layer
823+
Box::pin(async move {
824+
// This writes an error, so we want a sink for it.
825+
let tp = Box::new(currentprocess::TestProcess {
826+
vars: [("HOME".to_string(), "/unused".to_string())]
827+
.iter()
828+
.cloned()
829+
.collect(),
830+
..Default::default()
831+
});
832+
833+
currentprocess::with_tokio(tp.clone(), async {
834+
let root = RegKey::predef(HKEY_CURRENT_USER);
835+
let environment = root
836+
.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE)
837+
.unwrap();
838+
let reg_value = RegValue {
839+
bytes: vec![0x12, 0x34],
840+
vtype: RegType::REG_BINARY,
841+
};
842+
environment.set_raw_value("PATH", &reg_value).unwrap();
843+
// Ok(None) signals no change to the PATH setting layer
844+
assert_eq!(
845+
None,
846+
super::_with_path_cargo_home_bin(|_, _| panic!("called")).unwrap()
847+
);
848+
});
834849
assert_eq!(
835-
None,
836-
super::_with_path_cargo_home_bin(|_, _| panic!("called")).unwrap()
850+
r"warning: the registry key HKEY_CURRENT_USER\Environment\PATH is not a string. Not modifying the PATH variable
851+
",
852+
String::from_utf8(tp.get_stderr()).unwrap()
837853
);
838854
})
839-
});
840-
assert_eq!(
841-
r"warning: the registry key HKEY_CURRENT_USER\Environment\PATH is not a string. Not modifying the PATH variable
842-
",
843-
String::from_utf8(tp.get_stderr()).unwrap()
844-
);
855+
}).await;
845856
}
846857

847858
#[test]
848859
async fn windows_treat_missing_path_as_empty() {
849860
// during install the PATH key may be missing; treat it as empty
850-
let tp = Box::new(currentprocess::TestProcess::default());
851861
with_saved_path(&mut || {
852-
currentprocess::with_tokio(tp.clone(), async {
853-
let root = RegKey::predef(HKEY_CURRENT_USER);
854-
let environment = root
855-
.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE)
856-
.unwrap();
857-
environment.delete_value("PATH").unwrap();
858-
859-
assert_eq!(Some(Vec::new()), super::get_windows_path_var().unwrap());
862+
Box::pin(async move {
863+
let tp = Box::new(currentprocess::TestProcess::default());
864+
currentprocess::with_tokio(tp.clone(), async {
865+
let root = RegKey::predef(HKEY_CURRENT_USER);
866+
let environment = root
867+
.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE)
868+
.unwrap();
869+
environment.delete_value("PATH").unwrap();
870+
871+
assert_eq!(Some(Vec::new()), super::get_windows_path_var().unwrap());
872+
})
860873
})
861-
});
874+
})
875+
.await;
862876
}
863877

864878
#[test]

0 commit comments

Comments
 (0)