Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix os.execute stderr redirection #21404

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
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
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