diff --git a/src/base/os.lua b/src/base/os.lua index e89e266d..c3308b9b 100644 --- a/src/base/os.lua +++ b/src/base/os.lua @@ -455,12 +455,30 @@ -- -- Run a shell command and return the output. +-- +-- @param cmd Command to execute +-- @param streams Standard stream(s) to output +-- Must be one of +-- - "both" (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) + 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 .. " 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 e11807a8..62315dea 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 --