Skip to content

Commit bdb97b9

Browse files
committed
Return a Result from get_path instead of panicking
1 parent cdc3305 commit bdb97b9

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

src/cli/self_update/test.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,15 @@ use winreg::{
1010
};
1111

1212
#[cfg(not(unix))]
13-
pub fn get_path() -> Option<RegValue> {
13+
pub fn get_path() -> std::io::Result<Option<RegValue>> {
1414
let root = RegKey::predef(HKEY_CURRENT_USER);
1515
let environment = root
1616
.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE)
1717
.unwrap();
1818
match environment.get_raw_value("PATH") {
19-
Ok(val) => Some(val),
20-
Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => None,
21-
Err(e) => panic!(
22-
"Error getting PATH: {}\nBetter abort to avoid trashing it.",
23-
e
24-
),
19+
Ok(val) => Ok(Some(val)),
20+
Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
21+
Err(e) => Err(e),
2522
}
2623
}
2724

@@ -48,15 +45,15 @@ pub fn with_saved_path(f: &dyn Fn()) {
4845

4946
// On windows these tests mess with the user's PATH. Save
5047
// and restore them here to keep from trashing things.
51-
let saved_path = get_path();
48+
let saved_path = get_path().expect("Error getting PATH: Better abort to avoid trashing it.");
5249
let _g = scopeguard::guard(saved_path, restore_path);
5350

5451
f();
5552
}
5653

5754
#[cfg(unix)]
58-
pub fn get_path() -> Option<()> {
59-
None
55+
pub fn get_path() -> std::io::Result<Option<()>> {
56+
Ok(None)
6057
}
6158

6259
#[cfg(unix)]

tests/cli-paths.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,14 +318,15 @@ mod windows {
318318
expect_ok(config, &INIT_NONE);
319319
assert!(
320320
get_path()
321+
.unwrap()
321322
.unwrap()
322323
.to_string()
323324
.contains(path.trim_matches('"')),
324-
format!("`{}` not in `{}`", path, get_path().unwrap())
325+
format!("`{}` not in `{}`", path, get_path().unwrap().unwrap())
325326
);
326327

327328
expect_ok(config, &["rustup", "self", "uninstall", "-y"]);
328-
assert!(!get_path().unwrap().to_string().contains(&path));
329+
assert!(!get_path().unwrap().unwrap().to_string().contains(&path));
329330
})
330331
});
331332
}
@@ -367,10 +368,10 @@ mod windows {
367368
};
368369

369370
expect_ok(config, &INIT_NONE);
370-
assert_eq!(get_path().unwrap(), expected);
371+
assert_eq!(get_path().unwrap().unwrap(), expected);
371372

372373
expect_ok(config, &["rustup", "self", "uninstall", "-y"]);
373-
assert_eq!(get_path().unwrap(), reg_value);
374+
assert_eq!(get_path().unwrap().unwrap(), reg_value);
374375
})
375376
});
376377
}

0 commit comments

Comments
 (0)