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
This commit is contained in:
Renaud Guillard 2021-01-25 19:18:48 +01:00
parent 1b7d93e6d3
commit d9f15aec7c
2 changed files with 35 additions and 4 deletions

View File

@ -455,12 +455,25 @@
-- --
-- Run a shell command and return the output. -- 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) 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 result = pipe:read('*a')
local success, what, code = pipe:close() local success, what, code = pipe:close()
if success then if success then

View File

@ -184,8 +184,26 @@
end end
end 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 -- os.translateCommand() tests
-- --