From d9f15aec7c7d946955b7fb19df6ca9626f55b2f2 Mon Sep 17 00:00:00 2001 From: Renaud Guillard Date: Mon, 25 Jan 2021 19:18:48 +0100 Subject: [PATCH 1/3] os.outputof: add a second argument to select which stream to output local o, e = os.outputof(cmd, streams) Where streams could be one of - "output" Only return standard output stream content - "error" Only return standard error stream content - "both" (default) Return both streams content --- src/base/os.lua | 17 +++++++++++++++-- tests/base/test_os.lua | 22 ++++++++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/base/os.lua b/src/base/os.lua index e89e266dd1..276d5cf1d6 100644 --- a/src/base/os.lua +++ b/src/base/os.lua @@ -455,12 +455,25 @@ -- -- Run a shell command and return the output. +-- +-- @param cmd Command to execute +-- @param streams Standard stream(s) to output +-- Must be one of +-- - "botn" (default) +-- - "output" Return standard output stream content only +-- - "error" Return standard error stream content only -- - function os.outputof(cmd) + function os.outputof(cmd, streams) cmd = path.normalize(cmd) + local redirection = " 2>&1" + if streams == "output" then + redirection = " 2>/dev/null" + elseif streams == "error" then + redirection = " 2>&1 1>/dev/null" + end - local pipe = io.popen(cmd .. " 2>&1") + local pipe = io.popen(cmd .. redirection) local result = pipe:read('*a') local success, what, code = pipe:close() if success then diff --git a/tests/base/test_os.lua b/tests/base/test_os.lua index e11807a8a5..62315dea9c 100644 --- a/tests/base/test_os.lua +++ b/tests/base/test_os.lua @@ -184,8 +184,26 @@ end end end - - + + -- Check outputof content + function suite.outputof_streams_output() + if (os.istarget("macosx") + or os.istarget("linux") + or os.istarget("solaris") + or os.istarget("bsd")) + and os.isdir (_TESTS_DIR) + then + local ob, e = os.outputof ("ls " .. _TESTS_DIR .. "/base") + local oo, e = os.outputof ("ls " .. _TESTS_DIR .. "/base", "output") + test.isequal (oo, ob) + local s, e = string.find (oo, "test_os.lua") + test.istrue(s ~= nil) + + local o, e = os.outputof ("ls " .. cwd .. "/base", "error") + test.istrue(o == nil or #o == 0) + end + end + -- -- os.translateCommand() tests -- From 7f67a3ba6798e63bf5450d910008bfe03d8966bb Mon Sep 17 00:00:00 2001 From: Renaud Guillard Date: Sun, 7 Mar 2021 16:02:57 +0100 Subject: [PATCH 2/3] Fix typo Co-authored-by: Samuel Surtees <11887457+samsinsane@users.noreply.github.com> --- src/base/os.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/base/os.lua b/src/base/os.lua index 276d5cf1d6..68c6761336 100644 --- a/src/base/os.lua +++ b/src/base/os.lua @@ -459,7 +459,7 @@ -- @param cmd Command to execute -- @param streams Standard stream(s) to output -- Must be one of --- - "botn" (default) +-- - "both" (default) -- - "output" Return standard output stream content only -- - "error" Return standard error stream content only -- From 8e75e1084bde54dfacf525e32173f139c1c6517e Mon Sep 17 00:00:00 2001 From: Renaud Guillard Date: Sun, 7 Mar 2021 16:24:19 +0100 Subject: [PATCH 3/3] os.outputof () raise error on invalid streams argument value --- src/base/os.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/base/os.lua b/src/base/os.lua index 68c6761336..c3308b9bb6 100644 --- a/src/base/os.lua +++ b/src/base/os.lua @@ -466,11 +466,16 @@ function os.outputof(cmd, streams) cmd = path.normalize(cmd) - local redirection = " 2>&1" - if streams == "output" then + streams = streams or "both" + local redirection + if streams == "both" then + redirection = " 2>&1" + elseif streams == "output" then redirection = " 2>/dev/null" elseif streams == "error" then redirection = " 2>&1 1>/dev/null" + else + error ('Invalid stream(s) selection. "output", "error", or "both" expected.') end local pipe = io.popen(cmd .. redirection)