Log PowerShell stderr in Windows tests

This commit is contained in:
Esdras Renan 2025-10-20 22:05:58 -03:00
parent 0a0106c0f3
commit 6234924878

View file

@ -723,6 +723,39 @@ fn collect_windows_extended() -> serde_json::Value {
serde_json::from_str(trimmed).ok() serde_json::from_str(trimmed).ok()
} }
fn decode_powershell_text(bytes: &[u8]) -> Option<String> {
if bytes.is_empty() {
return None;
}
if bytes.starts_with(&[0xFF, 0xFE]) {
return decode_utf16_le_to_string(&bytes[2..]);
}
if bytes.len() >= 2 && bytes[1] == 0 {
if let Some(s) = decode_utf16_le_to_string(bytes) {
return Some(s);
}
}
if bytes.contains(&0) {
if let Some(s) = decode_utf16_le_to_string(bytes) {
return Some(s);
}
}
std::str::from_utf8(bytes).map(|s| s.to_string()).ok()
}
fn decode_utf16_le_to_string(bytes: &[u8]) -> Option<String> {
if bytes.len() % 2 != 0 {
return None;
}
let utf16: Vec<u16> = bytes
.chunks_exact(2)
.map(|chunk| u16::from_le_bytes([chunk[0], chunk[1]]))
.collect();
String::from_utf16(&utf16)
.ok()
.map(|s| s.trim().to_string())
}
fn encode_ps_script(script: &str) -> String { fn encode_ps_script(script: &str) -> String {
let mut bytes = Vec::with_capacity(script.len() * 2); let mut bytes = Vec::with_capacity(script.len() * 2);
for unit in script.encode_utf16() { for unit in script.encode_utf16() {
@ -749,6 +782,11 @@ fn collect_windows_extended() -> serde_json::Value {
.output() .output()
.ok()?; .ok()?;
if out.stdout.is_empty() { if out.stdout.is_empty() {
if cfg!(test) && !out.stderr.is_empty() {
if let Some(err) = decode_powershell_text(&out.stderr) {
eprintln!("[collect_windows_extended] PowerShell stderr for `{cmd}`: {err}");
}
}
return None; return None;
} }
parse_powershell_json(&out.stdout) parse_powershell_json(&out.stdout)