lsyncd/tests/testlib.lua
2010-11-19 10:38:21 +00:00

49 lines
833 B
Lua
Executable File

-- common testing environment
require("posix")
-- escape codes to colorize output on terminal
local c1="\027[47;34m"
local c0="\027[0m"
---
-- writes colorized
--
function cwriteln(...)
io.write(c1, ...)
io.write(c0, "\n")
end
-----
-- creates a tmp directory
function mktempd()
local f = io.popen('mktemp -d --tmpdir ltest.XXX', 'r')
local s = f:read('*a')
f:close()
s = s:gsub('[\n\r]+', ' ')
s = s:match("^%s*(.-)%s*$")
return s
end
-----
-- spawns a subprocess.
--
-- @returns the processes pid
--
function spawn(...)
args = {...}
cwriteln("spawning: ", table.concat(args, " "))
local pid = posix.fork()
if pid < 0 then
cwriteln("Error, failed fork!")
os.exit(-1)
end
if pid == 0 then
posix.exec(...)
-- should not return
cwriteln("Error, failed to spawn: ", ...)
os.exit(-1);
end
return pid
end