aboutsummaryrefslogtreecommitdiff
path: root/test/unit/helpers.lua
diff options
context:
space:
mode:
Diffstat (limited to 'test/unit/helpers.lua')
-rw-r--r--test/unit/helpers.lua86
1 files changed, 85 insertions, 1 deletions
diff --git a/test/unit/helpers.lua b/test/unit/helpers.lua
index 20fdf6f85d..832cbcbd4a 100644
--- a/test/unit/helpers.lua
+++ b/test/unit/helpers.lua
@@ -238,7 +238,7 @@ if posix ~= nil then
wait = posix.wait,
exit = posix._exit,
}
-else
+elseif syscall ~= nil then
sc = {
fork = syscall.fork,
pipe = function()
@@ -257,6 +257,90 @@ else
wait = syscall.wait,
exit = syscall.exit,
}
+else
+ cimport('./test/unit/fixtures/posix.h')
+ sc = {
+ fork = function()
+ return tonumber(ffi.C.fork())
+ end,
+ pipe = function()
+ local ret = ffi.new('int[2]', {-1, -1})
+ ffi.errno(0)
+ local res = ffi.C.pipe(ret)
+ if (res ~= 0) then
+ local err = ffi.errno(0)
+ assert(res == 0, ("pipe() error: %u: %s"):format(
+ err, ffi.string(ffi.C.strerror(err))))
+ end
+ assert(ret[0] ~= -1 and ret[1] ~= -1)
+ return ret[0], ret[1]
+ end,
+ read = function(rd, len)
+ local ret = ffi.new('char[?]', len, {0})
+ local total_bytes_read = 0
+ ffi.errno(0)
+ while total_bytes_read < len do
+ local bytes_read = tonumber(ffi.C.read(
+ rd,
+ ffi.cast('void*', ret + total_bytes_read),
+ len - total_bytes_read))
+ if bytes_read == -1 then
+ local err = ffi.errno(0)
+ if err ~= libnvim.kPOSIXErrnoEINTR then
+ assert(false, ("read() error: %u: %s"):format(
+ err, ffi.string(ffi.C.strerror(err))))
+ end
+ elseif bytes_read == 0 then
+ break
+ else
+ total_bytes_read = total_bytes_read + bytes_read
+ end
+ end
+ return ffi.string(ret, total_bytes_read)
+ end,
+ write = function(wr, s)
+ local wbuf = to_cstr(s)
+ local total_bytes_written = 0
+ ffi.errno(0)
+ while total_bytes_written < #s do
+ local bytes_written = tonumber(ffi.C.write(
+ wr,
+ ffi.cast('void*', wbuf + total_bytes_written),
+ #s - total_bytes_written))
+ if bytes_written == -1 then
+ local err = ffi.errno(0)
+ if err ~= libnvim.kPOSIXErrnoEINTR then
+ assert(false, ("write() error: %u: %s"):format(
+ err, ffi.string(ffi.C.strerror(err))))
+ end
+ elseif bytes_written == 0 then
+ break
+ else
+ total_bytes_written = total_bytes_written + bytes_written
+ end
+ end
+ return total_bytes_written
+ end,
+ close = ffi.C.close,
+ wait = function(pid)
+ ffi.errno(0)
+ while true do
+ local r = ffi.C.waitpid(pid, nil, libnvim.kPOSIXWaitWUNTRACED)
+ if r == -1 then
+ local err = ffi.errno(0)
+ if err == libnvim.kPOSIXErrnoECHILD then
+ break
+ elseif err ~= libnvim.kPOSIXErrnoEINTR then
+ assert(false, ("waitpid() error: %u: %s"):format(
+ err, ffi.string(ffi.C.strerror(err))))
+ end
+ else
+ assert(r == pid)
+ end
+ end
+ end,
+ exit = ffi.C._exit,
+ }
end
local function gen_itp(it)