Avoid expect in Windows PowerShell tests

This commit is contained in:
Chris Bookholt
2026-06-30 12:10:07 -07:00
parent 8f7f517bb4
commit 58dfb5fb54
3 changed files with 37 additions and 29 deletions

View File

@@ -617,11 +617,13 @@ mod tests {
#[cfg(windows)]
#[test]
fn windows_powershell_full_path_is_safe() {
let powershell = crate::command_safety::trusted_windows_powershell_invocation_path()
.expect("Windows PowerShell must exist at the authoritative System known folder");
let powershell = powershell
.to_str()
.expect("the Windows System known folder must be valid UTF-8");
let Some(powershell) = crate::command_safety::trusted_windows_powershell_invocation_path()
else {
panic!("Windows PowerShell must exist at the authoritative System known folder");
};
let Some(powershell) = powershell.to_str() else {
panic!("the Windows System known folder must be valid UTF-8");
};
assert!(is_known_safe_command(&vec_str(&[
powershell,

View File

@@ -460,11 +460,12 @@ mod tests {
#[test]
fn production_resolver_handles_multiple_windows_powershell_requests() {
let powershell = trusted_windows_powershell_invocation_path()
.expect("Windows PowerShell must exist at the authoritative System known folder");
let powershell = powershell
.to_str()
.expect("the Windows System known folder must be valid UTF-8");
let Some(powershell) = trusted_windows_powershell_invocation_path() else {
panic!("Windows PowerShell must exist at the authoritative System known folder");
};
let Some(powershell) = powershell.to_str() else {
panic!("the Windows System known folder must be valid UTF-8");
};
let first = try_parse_powershell_ast_commands(powershell, "Get-Content 'foo bar'")
.map(PowershellParseOutcome::Commands)
@@ -498,9 +499,9 @@ mod tests {
);
return;
};
let pwsh = pwsh
.to_str()
.expect("the Program Files known folder must be valid UTF-8");
let Some(pwsh) = pwsh.to_str() else {
panic!("the Program Files known folder must be valid UTF-8");
};
assert_eq!(
try_parse_powershell_ast_commands(pwsh, "pwd && ls"),
@@ -509,14 +510,16 @@ mod tests {
}
fn trusted_windows_powershell_parser() -> PathBuf {
let invocation_path = trusted_windows_powershell_invocation_path()
.expect("Windows PowerShell must exist at the authoritative System known folder");
trusted_powershell_parser_executable(
invocation_path
.to_str()
.expect("the Windows System known folder must be valid UTF-8"),
)
.expect("the production trust resolver must accept Windows PowerShell")
let Some(invocation_path) = trusted_windows_powershell_invocation_path() else {
panic!("Windows PowerShell must exist at the authoritative System known folder");
};
let Some(invocation_path_str) = invocation_path.to_str() else {
panic!("the Windows System known folder must be valid UTF-8");
};
let Some(parser) = trusted_powershell_parser_executable(invocation_path_str) else {
panic!("the production trust resolver must accept Windows PowerShell");
};
parser
}
#[test]

View File

@@ -222,19 +222,22 @@ mod tests {
use std::sync::LazyLock;
static WINDOWS_POWERSHELL_EXE: LazyLock<String> = LazyLock::new(|| {
crate::command_safety::trusted_windows_powershell_invocation_path()
.expect("Windows PowerShell must exist at the authoritative System known folder")
.to_str()
.expect("the Windows System known folder must be valid UTF-8")
.to_string()
let Some(path) = crate::command_safety::trusted_windows_powershell_invocation_path() else {
panic!("Windows PowerShell must exist at the authoritative System known folder");
};
let Some(path) = path.to_str() else {
panic!("the Windows System known folder must be valid UTF-8");
};
path.to_string()
});
fn installed_pwsh() -> Option<&'static str> {
static STANDARD_PWSH_EXE: LazyLock<Option<String>> = LazyLock::new(|| {
crate::command_safety::trusted_standard_pwsh_invocation_path().map(|path| {
path.to_str()
.expect("the Program Files known folder must be valid UTF-8")
.to_string()
let Some(path) = path.to_str() else {
panic!("the Program Files known folder must be valid UTF-8");
};
path.to_string()
})
});
STANDARD_PWSH_EXE.as_deref()