unittests: Use own bindings to libc syscall wrappers

This commit is contained in:
ZyX 2016-11-01 10:38:03 +03:00
parent 3adecd3ede
commit 1edb3ccc36
3 changed files with 97 additions and 9 deletions

View File

@ -0,0 +1,11 @@
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>
#include <stdlib.h>
enum {
kPOSIXErrnoEINTR = EINTR,
kPOSIXErrnoECHILD = ECHILD,
kPOSIXWaitWUNTRACED = WUNTRACED,
};

View File

@ -238,7 +238,7 @@ if posix ~= nil then
wait = posix.wait, wait = posix.wait,
exit = posix._exit, exit = posix._exit,
} }
else elseif syscall ~= nil then
sc = { sc = {
fork = syscall.fork, fork = syscall.fork,
pipe = function() pipe = function()
@ -257,6 +257,90 @@ else
wait = syscall.wait, wait = syscall.wait,
exit = syscall.exit, 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 end
local function gen_itp(it) local function gen_itp(it)

View File

@ -172,12 +172,5 @@ if(USE_BUNDLED_BUSTED)
add_custom_target(nvim-client add_custom_target(nvim-client
DEPENDS ${HOSTDEPS_LIB_DIR}/luarocks/rocks/nvim-client) DEPENDS ${HOSTDEPS_LIB_DIR}/luarocks/rocks/nvim-client)
add_custom_command(OUTPUT ${HOSTDEPS_BIN_DIR}/luaposix list(APPEND THIRD_PARTY_DEPS busted luacheck nvim-client)
COMMAND ${LUAROCKS_BINARY}
ARGS build https://raw.githubusercontent.com/luaposix/luaposix/release-v33.4.0/luaposix-33.4.0-1.rockspec ${LUAROCKS_BUILDARGS}
DEPENDS luarocks)
add_custom_target(luaposix
DEPENDS ${HOSTDEPS_BIN_DIR}/luaposix)
list(APPEND THIRD_PARTY_DEPS busted luacheck nvim-client luaposix)
endif() endif()