Skip to content

Commit

Permalink
Fix os.execute stderr redirection
Browse files Browse the repository at this point in the history
Closes #20986
  • Loading branch information
hholst80 committed May 2, 2024
1 parent 12ca2f2 commit 5caa795
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
5 changes: 1 addition & 4 deletions vlib/os/os_nix.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
}
Expand Down
10 changes: 10 additions & 0 deletions vlib/os/os_test.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 5caa795

Please sign in to comment.