Skip to content

Commit

Permalink
Return a Result from get_path instead of panicking
Browse files Browse the repository at this point in the history
  • Loading branch information
kellda committed Feb 9, 2021
1 parent cdc3305 commit bdb97b9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
17 changes: 7 additions & 10 deletions src/cli/self_update/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 10,15 @@ use winreg::{
};

#[cfg(not(unix))]
pub fn get_path() -> Option<RegValue> {
pub fn get_path() -> std::io::Result<Option<RegValue>> {
let root = RegKey::predef(HKEY_CURRENT_USER);
let environment = root
.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE)
.unwrap();
match environment.get_raw_value("PATH") {
Ok(val) => Some(val),
Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => None,
Err(e) => panic!(
"Error getting PATH: {}\nBetter abort to avoid trashing it.",
e
),
Ok(val) => Ok(Some(val)),
Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(e),
}
}

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

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

f();
}

#[cfg(unix)]
pub fn get_path() -> Option<()> {
None
pub fn get_path() -> std::io::Result<Option<()>> {
Ok(None)
}

#[cfg(unix)]
Expand Down
9 changes: 5 additions & 4 deletions tests/cli-paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,14 318,15 @@ mod windows {
expect_ok(config, &INIT_NONE);
assert!(
get_path()
.unwrap()
.unwrap()
.to_string()
.contains(path.trim_matches('"')),
format!("`{}` not in `{}`", path, get_path().unwrap())
format!("`{}` not in `{}`", path, get_path().unwrap().unwrap())
);

expect_ok(config, &["rustup", "self", "uninstall", "-y"]);
assert!(!get_path().unwrap().to_string().contains(&path));
assert!(!get_path().unwrap().unwrap().to_string().contains(&path));
})
});
}
Expand Down Expand Up @@ -367,10 368,10 @@ mod windows {
};

expect_ok(config, &INIT_NONE);
assert_eq!(get_path().unwrap(), expected);
assert_eq!(get_path().unwrap().unwrap(), expected);

expect_ok(config, &["rustup", "self", "uninstall", "-y"]);
assert_eq!(get_path().unwrap(), reg_value);
assert_eq!(get_path().unwrap().unwrap(), reg_value);
})
});
}
Expand Down

0 comments on commit bdb97b9

Please sign in to comment.