From f3683b7cdc0086415560e15cc4f03be01e60bab2 Mon Sep 17 00:00:00 2001 From: Merlin Diavova Date: Sat, 29 Jan 2022 22:44:52 +0000 Subject: [PATCH] os: add an .exit_code field to os.Command (#13321) * Added os.Command.exit_code * vfmt vlib/os/os_test.v * extract os.Command to os.v, add a dummy panicing implementation on windows, just for parity, fix os_test.v on macos, skip test_command on windows. Co-authored-by: Merlin Diavova Co-authored-by: Delyan Angelov --- vlib/os/os.v | 11 +++++++++++ vlib/os/os_nix.c.v | 16 +++------------- vlib/os/os_test.v | 33 +++++++++++++++++++++++++++++++++ vlib/os/os_windows.c.v | 14 ++++++++++++++ 4 files changed, 61 insertions(+), 13 deletions(-) diff --git a/vlib/os/os.v b/vlib/os/os.v index d51f863fef..0d6b67b254 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -24,6 +24,17 @@ pub: // stderr string // TODO } +pub struct Command { +mut: + f voidptr +pub mut: + eof bool + exit_code int +pub: + path string + redirect_stdout bool +} + [unsafe] pub fn (mut result Result) free() { unsafe { result.output.free() } diff --git a/vlib/os/os_nix.c.v b/vlib/os/os_nix.c.v index 5e9f78ba3a..883e636f61 100644 --- a/vlib/os/os_nix.c.v +++ b/vlib/os/os_nix.c.v @@ -364,16 +364,6 @@ pub fn execute(cmd string) Result { } } -pub struct Command { -mut: - f voidptr -pub mut: - eof bool -pub: - path string - redirect_stdout bool -} - [manualfree] pub fn (mut c Command) start() ? { pcmd := c.path + ' 2>&1' @@ -412,9 +402,9 @@ pub fn (mut c Command) read_line() string { return final } -pub fn (c &Command) close() ? { - exit_code := vpclose(c.f) - if exit_code == 127 { +pub fn (mut c Command) close() ? { + c.exit_code = vpclose(c.f) + if c.exit_code == 127 { return error_with_code('error', 127) } } diff --git a/vlib/os/os_test.v b/vlib/os/os_test.v index 7d18a6a3f8..3d5d906cb7 100644 --- a/vlib/os/os_test.v +++ b/vlib/os/os_test.v @@ -861,3 +861,36 @@ fn test_execute() ? { assert hexresult.starts_with('7374617274004d4944444c450066696e697368') assert hexresult.ends_with('0a7878') } + +fn test_command() { + if os.user_os() == 'windows' { + eprintln('>>> os.Command is not implemented fully on Windows yet') + return + } + mut cmd := os.Command{ + path: 'ls' + } + + cmd.start() or { panic(err) } + for !cmd.eof { + cmd.read_line() + } + + cmd.close() or { panic(err) } + // dump( cmd ) + assert cmd.exit_code == 0 + + // This will return a non 0 code + mut cmd_to_fail := os.Command{ + path: 'ls -M' + } + + cmd_to_fail.start() or { panic(err) } + for !cmd_to_fail.eof { + cmd_to_fail.read_line() + } + + cmd_to_fail.close() or { panic(err) } + // dump( cmd_to_fail ) + assert cmd_to_fail.exit_code != 0 // 2 on linux, 1 on macos +} diff --git a/vlib/os/os_windows.c.v b/vlib/os/os_windows.c.v index 171b230c49..fb20968af7 100644 --- a/vlib/os/os_windows.c.v +++ b/vlib/os/os_windows.c.v @@ -543,3 +543,17 @@ pub fn getegid() int { pub fn posix_set_permission_bit(path_s string, mode u32, enable bool) { // windows has no concept of a permission mask, so do nothing } + +// + +pub fn (mut c Command) start() ? { + panic('not implemented') +} + +pub fn (mut c Command) read_line() string { + panic('not implemented') +} + +pub fn (mut c Command) close() ? { + panic('not implemented') +}