Skip to content

Commit e52c335

Browse files
committed
Convert another self-update test to unit test
The needed refactoring to do this cleanly created a common code path for handling of unicode-path detection, so I removed a now-redundant test.
1 parent 2d7e616 commit e52c335

File tree

3 files changed

+56
-87
lines changed

3 files changed

+56
-87
lines changed

src/cli/self_update/windows.rs

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -143,26 +143,11 @@ pub fn do_add_to_path(methods: &[PathUpdateMethod]) -> Result<()> {
143143
use winreg::enums::{RegType, HKEY_CURRENT_USER, KEY_READ, KEY_WRITE};
144144
use winreg::{RegKey, RegValue};
145145

146-
let old_path = if let Some(s) = get_windows_path_var()? {
147-
s
148-
} else {
149-
// Non-unicode path
150-
return Ok(());
146+
let new_path = match _with_path_cargo_home_bin(_add_to_path)? {
147+
Some(new_path) => new_path,
148+
None => return Ok(()), // No need to set the path
151149
};
152150

153-
let mut new_path = utils::cargo_home()?
154-
.join("bin")
155-
.to_string_lossy()
156-
.into_owned();
157-
if old_path.contains(&new_path) {
158-
return Ok(());
159-
}
160-
161-
if !old_path.is_empty() {
162-
new_path.push_str(";");
163-
new_path.push_str(&old_path);
164-
}
165-
166151
let root = RegKey::predef(HKEY_CURRENT_USER);
167152
let environment = root
168153
.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE)
@@ -222,8 +207,23 @@ fn get_windows_path_var() -> Result<Option<String>> {
222207
}
223208
}
224209

210+
// Returns None if the existing old_path does not need changing, otherwise
211+
// prepends the path_str to old_path, handling empty old_path appropriately.
212+
fn _add_to_path(old_path: &str, path_str: String) -> Option<String> {
213+
if old_path.is_empty() {
214+
Some(path_str)
215+
} else if old_path.contains(&path_str) {
216+
None
217+
} else {
218+
let mut new_path = path_str.clone();
219+
new_path.push_str(";");
220+
new_path.push_str(&old_path);
221+
Some(new_path)
222+
}
223+
}
224+
225225
// Returns None if the existing old_path does not need changing
226-
fn _remove_from_path(old_path: &str, path_str: &str) -> Option<String> {
226+
fn _remove_from_path(old_path: &str, path_str: String) -> Option<String> {
227227
let idx = old_path.find(&path_str)?;
228228
// If there's a trailing semicolon (likely, since we probably added one
229229
// during install), include that in the substring to remove. We don't search
@@ -244,20 +244,16 @@ fn _remove_from_path(old_path: &str, path_str: &str) -> Option<String> {
244244
Some(new_path)
245245
}
246246

247-
fn _path_without_cargo_home_bin() -> Result<Option<String>> {
248-
let old_path = if let Some(s) = get_windows_path_var()? {
249-
s
250-
} else {
251-
// Non-unicode path
252-
return Ok(None);
253-
};
254-
247+
fn _with_path_cargo_home_bin<F>(f: F) -> Result<Option<String>>
248+
where
249+
F: FnOnce(&str, String) -> Option<String>,
250+
{
251+
let windows_path = get_windows_path_var()?;
255252
let path_str = utils::cargo_home()?
256253
.join("bin")
257254
.to_string_lossy()
258255
.into_owned();
259-
260-
Ok(_remove_from_path(&old_path, &path_str))
256+
Ok(windows_path.and_then(|old_path| f(&old_path, path_str)))
261257
}
262258

263259
pub fn do_remove_from_path(methods: &[PathUpdateMethod]) -> Result<()> {
@@ -271,7 +267,7 @@ pub fn do_remove_from_path(methods: &[PathUpdateMethod]) -> Result<()> {
271267
use winreg::enums::{RegType, HKEY_CURRENT_USER, KEY_READ, KEY_WRITE};
272268
use winreg::{RegKey, RegValue};
273269

274-
let new_path = match _path_without_cargo_home_bin()? {
270+
let new_path = match _with_path_cargo_home_bin(_remove_from_path)? {
275271
Some(new_path) => new_path,
276272
None => return Ok(()), // No need to set the path
277273
};
@@ -503,9 +499,26 @@ mod tests {
503499
}
504500

505501
#[test]
506-
fn windows_uninstall_doesnt_mess_with_a_non_unicode_path() {
502+
fn windows_install_does_not_add_path_twice() {
503+
assert_eq!(
504+
None,
505+
super::_add_to_path(
506+
r"c:\users\example\.cargo\bin;foo",
507+
r"c:\users\example\.cargo\bin".into()
508+
)
509+
);
510+
}
511+
512+
#[test]
513+
fn windows_doesnt_mess_with_a_non_unicode_path() {
507514
// This writes an error, so we want a sink for it.
508-
let tp = Box::new(currentprocess::TestProcess::default());
515+
let tp = Box::new(currentprocess::TestProcess {
516+
vars: [("HOME".to_string(), "/unused".to_string())]
517+
.iter()
518+
.cloned()
519+
.collect(),
520+
..Default::default()
521+
});
509522
with_registry_edits(&|| {
510523
currentprocess::with(tp.clone(), || {
511524
let root = RegKey::predef(HKEY_CURRENT_USER);
@@ -522,7 +535,10 @@ mod tests {
522535
};
523536
environment.set_raw_value("PATH", &reg_value).unwrap();
524537
// Ok(None) signals no change to the PATH setting layer
525-
assert_eq!(None, super::_path_without_cargo_home_bin().unwrap());
538+
fn panic(_: &str, _: String) -> Option<String> {
539+
panic!("called");
540+
}
541+
assert_eq!(None, super::_with_path_cargo_home_bin(panic).unwrap());
526542
})
527543
});
528544
assert_eq!(
@@ -538,7 +554,7 @@ mod tests {
538554
"foo",
539555
super::_remove_from_path(
540556
r"c:\users\example\.cargo\bin;foo",
541-
r"c:\users\example\.cargo\bin"
557+
r"c:\users\example\.cargo\bin".into()
542558
)
543559
.unwrap()
544560
)
@@ -550,7 +566,7 @@ mod tests {
550566
"foo",
551567
super::_remove_from_path(
552568
r"foo;c:\users\example\.cargo\bin",
553-
r"c:\users\example\.cargo\bin"
569+
r"c:\users\example\.cargo\bin".into()
554570
)
555571
.unwrap()
556572
)

src/currentprocess.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,11 @@ impl ProcessSource for OSProcess {
191191

192192
#[derive(Clone, Debug, Default)]
193193
pub struct TestProcess {
194-
cwd: PathBuf,
195-
args: Vec<String>,
196-
vars: HashMap<String, String>,
197-
id: u64,
198-
stdin: TestStdinInner,
194+
pub cwd: PathBuf,
195+
pub args: Vec<String>,
196+
pub vars: HashMap<String, String>,
197+
pub id: u64,
198+
pub stdin: TestStdinInner,
199199
pub stdout: TestWriterInner,
200200
pub stderr: TestWriterInner,
201201
}

tests/cli-self-upd.rs

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -506,18 +506,6 @@ fn install_adds_path() {
506506
});
507507
}
508508

509-
#[test]
510-
#[cfg(windows)]
511-
fn install_does_not_add_path_twice() {
512-
setup(&|config| {
513-
expect_ok(config, &["rustup-init", "-y"]);
514-
expect_ok(config, &["rustup-init", "-y"]);
515-
516-
let path = config.cargodir.join("bin").to_string_lossy().to_string();
517-
assert_eq!(get_path().unwrap().matches(&path).count(), 1);
518-
});
519-
}
520-
521509
#[test]
522510
#[cfg(windows)]
523511
fn uninstall_removes_path() {
@@ -1091,41 +1079,6 @@ fn windows_handle_empty_path_registry_key() {
10911079
});
10921080
}
10931081

1094-
#[test]
1095-
#[cfg(windows)]
1096-
fn install_doesnt_mess_with_a_non_unicode_path() {
1097-
use winreg::enums::{RegType, HKEY_CURRENT_USER, KEY_READ, KEY_WRITE};
1098-
use winreg::{RegKey, RegValue};
1099-
1100-
setup(&|config| {
1101-
let root = RegKey::predef(HKEY_CURRENT_USER);
1102-
let environment = root
1103-
.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE)
1104-
.unwrap();
1105-
1106-
let reg_value = RegValue {
1107-
bytes: vec![
1108-
0x00, 0xD8, // leading surrogate
1109-
0x01, 0x01, // bogus trailing surrogate
1110-
0x00, 0x00,
1111-
], // null
1112-
vtype: RegType::REG_EXPAND_SZ,
1113-
};
1114-
environment.set_raw_value("PATH", &reg_value).unwrap();
1115-
1116-
expect_stderr_ok(config, &["rustup-init", "-y"],
1117-
"the registry key HKEY_CURRENT_USER\\Environment\\PATH does not contain valid Unicode. \
1118-
Not modifying the PATH variable");
1119-
1120-
let root = RegKey::predef(HKEY_CURRENT_USER);
1121-
let environment = root
1122-
.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE)
1123-
.unwrap();
1124-
let path = environment.get_raw_value("PATH").unwrap();
1125-
assert_eq!(path.bytes, reg_value.bytes);
1126-
});
1127-
}
1128-
11291082
#[test]
11301083
#[ignore] // untestable
11311084
fn install_but_rustup_is_installed() {}

0 commit comments

Comments
 (0)