From 3ec485505f724163bcc65fafbc00163d9ff47a22 Mon Sep 17 00:00:00 2001 From: Axel Kittenberger Date: Thu, 18 Nov 2010 13:27:55 +0000 Subject: [PATCH] added lua testlib --- tests/testlib.lua | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 tests/testlib.lua diff --git a/tests/testlib.lua b/tests/testlib.lua new file mode 100755 index 0000000..a38b03a --- /dev/null +++ b/tests/testlib.lua @@ -0,0 +1,47 @@ +-- 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', 'r') + local s = f:read('*a') + f:close() + s = s:gsub('[\n\r]+', ' ') + s = s:match("^%s*(.-)%s*$") + return s +end + +----- +-- spawns a subprocess. +-- +function spawn(...) + local pid = posix.fork() + if pid < 0 then + cwriteln("Error, failed fork!") + os.exit(-1) + end + if lpid == 0 then + posix.exec(...) + -- should not return + cwriteln("Error, failed to spawn: ", ...) + os.exit(-1); + end + return pid +end + +print(mktempd()) +