os: fix os.execute stderr redirection (fix #20986) (#21404)

This commit is contained in:
Henrik Holst 2024-05-02 17:16:02 -04:00 committed by GitHub
parent 4dc46ed9e6
commit c7fb7553b7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 4 deletions

View File

@ -339,10 +339,7 @@ pub fn mkdir(path string, params MkdirParams) ! {
// execute starts the specified command, waits for it to complete, and returns its output.
@[manualfree]
pub fn execute(cmd string) Result {
// if cmd.contains(';') || cmd.contains('&&') || cmd.contains('||') || cmd.contains('\n') {
// return Result{ exit_code: -1, output: ';, &&, || and \\n are not allowed in shell commands' }
// }
pcmd := if cmd.contains('2>') { cmd.clone() } else { '${cmd} 2>&1' }
pcmd := 'exec 2>&1;${cmd}'
defer {
unsafe { pcmd.free() }
}

View File

@ -907,6 +907,16 @@ fn test_execute_with_stderr_redirection() {
assert os.exists(stderr_path)
}
fn test_execute_with_linefeeds() {
if os.user_os() == 'windows' {
return
}
result := os.execute('true\n')
assert result.exit_code == 0
result2 := os.execute('false\n')
assert result2.exit_code == 1
}
fn test_command() {
if os.user_os() == 'windows' {
eprintln('>>> os.Command is not implemented fully on Windows yet')