Skip to content
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
22 changes: 20 additions & 2 deletions src/base/os.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 20 additions & 2 deletions tests/base/test_os.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
--
Expand Down