Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions codex-rs/shell-command/src/powershell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use crate::shell_detect::detect_shell_type;

const POWERSHELL_FLAGS: &[&str] = &["-nologo", "-noprofile", "-command", "-c"];

/// Prefixed command for powershell shell calls to force UTF-8 console output.
pub const UTF8_OUTPUT_PREFIX: &str = "[Console]::OutputEncoding=[System.Text.Encoding]::UTF8;\n";
/// Prefixed command for powershell shell calls to request UTF-8 console output.
pub const UTF8_OUTPUT_PREFIX: &str =
"try { [Console]::OutputEncoding=[System.Text.Encoding]::UTF8 } catch {}\n";

pub fn prefix_powershell_script_with_utf8(command: &[String]) -> Vec<String> {
let Some((_, script)) = extract_powershell_command(command) else {
Expand Down Expand Up @@ -151,9 +152,11 @@ fn is_powershellish_executable_available(powershell_or_pwsh_exe: &std::path::Pat

#[cfg(test)]
mod tests {
use super::UTF8_OUTPUT_PREFIX;
use super::extract_powershell_command;
#[cfg(windows)]
use super::parse_powershell_command_into_plain_commands;
use super::prefix_powershell_script_with_utf8;

#[test]
fn extracts_basic_powershell_command() {
Expand Down Expand Up @@ -202,6 +205,37 @@ mod tests {
assert_eq!(script, "Get-ChildItem | Select-String foo");
}

#[test]
fn prefixes_powershell_command_with_best_effort_utf8() {
let cmd = vec![
"powershell".to_string(),
"-Command".to_string(),
"Write-Host hi".to_string(),
];

let prefixed = prefix_powershell_script_with_utf8(&cmd);

assert_eq!(
prefixed,
vec![
"powershell".to_string(),
"-Command".to_string(),
format!("{UTF8_OUTPUT_PREFIX}Write-Host hi"),
]
);
}

#[test]
fn does_not_duplicate_utf8_prefix() {
let cmd = vec![
"powershell".to_string(),
"-Command".to_string(),
format!("{UTF8_OUTPUT_PREFIX}Write-Host hi"),
];

assert_eq!(prefix_powershell_script_with_utf8(&cmd), cmd);
}

#[cfg(windows)]
#[test]
fn parses_plain_powershell_commands() {
Expand Down
Loading