helpers.lua 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. local ffi = require('ffi')
  2. local formatc = require('test.unit.formatc')
  3. local Set = require('test.unit.set')
  4. local Preprocess = require('test.unit.preprocess')
  5. local Paths = require('test.config.paths')
  6. local global_helpers = require('test.helpers')
  7. local assert = require('luassert')
  8. local say = require('say')
  9. local posix = nil
  10. local syscall = nil
  11. local check_cores = global_helpers.check_cores
  12. local dedent = global_helpers.dedent
  13. local neq = global_helpers.neq
  14. local map = global_helpers.tbl_map
  15. local eq = global_helpers.eq
  16. local trim = global_helpers.trim
  17. -- C constants.
  18. local NULL = ffi.cast('void*', 0)
  19. local OK = 1
  20. local FAIL = 0
  21. local cimport
  22. -- add some standard header locations
  23. for _, p in ipairs(Paths.include_paths) do
  24. Preprocess.add_to_include_path(p)
  25. end
  26. local child_pid = nil
  27. local function only_separate(func)
  28. return function(...)
  29. if child_pid ~= 0 then
  30. error('This function must be run in a separate process only')
  31. end
  32. return func(...)
  33. end
  34. end
  35. local child_calls_init = {}
  36. local child_calls_mod = nil
  37. local child_calls_mod_once = nil
  38. local function child_call(func, ret)
  39. return function(...)
  40. local child_calls = child_calls_mod or child_calls_init
  41. if child_pid ~= 0 then
  42. child_calls[#child_calls + 1] = {func=func, args={...}}
  43. return ret
  44. else
  45. return func(...)
  46. end
  47. end
  48. end
  49. -- Run some code at the start of the child process, before running the test
  50. -- itself. Is supposed to be run in `before_each`.
  51. local function child_call_once(func, ...)
  52. if child_pid ~= 0 then
  53. child_calls_mod_once[#child_calls_mod_once + 1] = {
  54. func=func, args={...}}
  55. else
  56. func(...)
  57. end
  58. end
  59. local child_cleanups_mod_once = nil
  60. -- Run some code at the end of the child process, before exiting. Is supposed to
  61. -- be run in `before_each` because `after_each` is run after child has exited.
  62. local function child_cleanup_once(func, ...)
  63. local child_cleanups = child_cleanups_mod_once
  64. if child_pid ~= 0 then
  65. child_cleanups[#child_cleanups + 1] = {func=func, args={...}}
  66. else
  67. func(...)
  68. end
  69. end
  70. local libnvim = nil
  71. local lib = setmetatable({}, {
  72. __index = only_separate(function(_, idx)
  73. return libnvim[idx]
  74. end),
  75. __newindex = child_call(function(_, idx, val)
  76. libnvim[idx] = val
  77. end),
  78. })
  79. local init = only_separate(function()
  80. -- load neovim shared library
  81. libnvim = ffi.load(Paths.test_libnvim_path)
  82. for _, c in ipairs(child_calls_init) do
  83. c.func(unpack(c.args))
  84. end
  85. libnvim.time_init()
  86. libnvim.event_init()
  87. libnvim.early_init(nil)
  88. if child_calls_mod then
  89. for _, c in ipairs(child_calls_mod) do
  90. c.func(unpack(c.args))
  91. end
  92. end
  93. if child_calls_mod_once then
  94. for _, c in ipairs(child_calls_mod_once) do
  95. c.func(unpack(c.args))
  96. end
  97. child_calls_mod_once = nil
  98. end
  99. end)
  100. local deinit = only_separate(function()
  101. if child_cleanups_mod_once then
  102. for _, c in ipairs(child_cleanups_mod_once) do
  103. c.func(unpack(c.args))
  104. end
  105. child_cleanups_mod_once = nil
  106. end
  107. end)
  108. -- a Set that keeps around the lines we've already seen
  109. local cdefs_init = Set:new()
  110. local cdefs_mod = nil
  111. local imported = Set:new()
  112. local pragma_pack_id = 1
  113. -- some things are just too complex for the LuaJIT C parser to digest. We
  114. -- usually don't need them anyway.
  115. local function filter_complex_blocks(body)
  116. local result = {}
  117. for line in body:gmatch("[^\r\n]+") do
  118. if not (string.find(line, "(^)", 1, true) ~= nil
  119. or string.find(line, "_ISwupper", 1, true)
  120. or string.find(line, "_Float")
  121. or string.find(line, "msgpack_zone_push_finalizer")
  122. or string.find(line, "msgpack_unpacker_reserve_buffer")
  123. or string.find(line, "UUID_NULL") -- static const uuid_t UUID_NULL = {...}
  124. or string.find(line, "inline _Bool")) then
  125. result[#result + 1] = line
  126. end
  127. end
  128. return table.concat(result, "\n")
  129. end
  130. local cdef = ffi.cdef
  131. local cimportstr
  132. local previous_defines_init = ''
  133. local preprocess_cache_init = {}
  134. local previous_defines_mod = ''
  135. local preprocess_cache_mod = nil
  136. local function is_child_cdefs()
  137. return (os.getenv('NVIM_TEST_MAIN_CDEFS') ~= '1')
  138. end
  139. -- use this helper to import C files, you can pass multiple paths at once,
  140. -- this helper will return the C namespace of the nvim library.
  141. cimport = function(...)
  142. local previous_defines, preprocess_cache, cdefs
  143. if is_child_cdefs() and preprocess_cache_mod then
  144. preprocess_cache = preprocess_cache_mod
  145. previous_defines = previous_defines_mod
  146. cdefs = cdefs_mod
  147. else
  148. preprocess_cache = preprocess_cache_init
  149. previous_defines = previous_defines_init
  150. cdefs = cdefs_init
  151. end
  152. for _, path in ipairs({...}) do
  153. if not (path:sub(1, 1) == '/' or path:sub(1, 1) == '.'
  154. or path:sub(2, 2) == ':') then
  155. path = './' .. path
  156. end
  157. if not preprocess_cache[path] then
  158. local body
  159. body, previous_defines = Preprocess.preprocess(previous_defines, path)
  160. -- format it (so that the lines are "unique" statements), also filter out
  161. -- Objective-C blocks
  162. if os.getenv('NVIM_TEST_PRINT_I') == '1' then
  163. local lnum = 0
  164. for line in body:gmatch('[^\n]+') do
  165. lnum = lnum + 1
  166. print(lnum, line)
  167. end
  168. end
  169. body = formatc(body)
  170. body = filter_complex_blocks(body)
  171. -- add the formatted lines to a set
  172. local new_cdefs = Set:new()
  173. for line in body:gmatch("[^\r\n]+") do
  174. line = trim(line)
  175. -- give each #pragma pack an unique id, so that they don't get removed
  176. -- if they are inserted into the set
  177. -- (they are needed in the right order with the struct definitions,
  178. -- otherwise luajit has wrong memory layouts for the sturcts)
  179. if line:match("#pragma%s+pack") then
  180. line = line .. " // " .. pragma_pack_id
  181. pragma_pack_id = pragma_pack_id + 1
  182. end
  183. new_cdefs:add(line)
  184. end
  185. -- subtract the lines we've already imported from the new lines, then add
  186. -- the new unique lines to the old lines (so they won't be imported again)
  187. new_cdefs:diff(cdefs)
  188. cdefs:union(new_cdefs)
  189. -- request a sorted version of the new lines (same relative order as the
  190. -- original preprocessed file) and feed that to the LuaJIT ffi
  191. local new_lines = new_cdefs:to_table()
  192. if os.getenv('NVIM_TEST_PRINT_CDEF') == '1' then
  193. for lnum, line in ipairs(new_lines) do
  194. print(lnum, line)
  195. end
  196. end
  197. body = table.concat(new_lines, '\n')
  198. preprocess_cache[path] = body
  199. end
  200. cimportstr(preprocess_cache, path)
  201. end
  202. return lib
  203. end
  204. local cimport_immediate = function(...)
  205. local saved_pid = child_pid
  206. child_pid = 0
  207. local err, emsg = pcall(cimport, ...)
  208. child_pid = saved_pid
  209. if not err then
  210. emsg = tostring(emsg)
  211. io.stderr:write(emsg .. '\n')
  212. assert(false)
  213. else
  214. return lib
  215. end
  216. end
  217. local function _cimportstr(preprocess_cache, path)
  218. if imported:contains(path) then
  219. return lib
  220. end
  221. local body = preprocess_cache[path]
  222. if body == '' then
  223. return lib
  224. end
  225. cdef(body)
  226. imported:add(path)
  227. return lib
  228. end
  229. if is_child_cdefs() then
  230. cimportstr = child_call(_cimportstr, lib)
  231. else
  232. cimportstr = _cimportstr
  233. end
  234. local function alloc_log_new()
  235. local log = {
  236. log={},
  237. lib=cimport('./src/nvim/memory.h'),
  238. original_functions={},
  239. null={['\0:is_null']=true},
  240. }
  241. local allocator_functions = {'malloc', 'free', 'calloc', 'realloc'}
  242. function log:save_original_functions()
  243. for _, funcname in ipairs(allocator_functions) do
  244. if not self.original_functions[funcname] then
  245. self.original_functions[funcname] = self.lib['mem_' .. funcname]
  246. end
  247. end
  248. end
  249. log.save_original_functions = child_call(log.save_original_functions)
  250. function log:set_mocks()
  251. for _, k in ipairs(allocator_functions) do
  252. do
  253. local kk = k
  254. self.lib['mem_' .. k] = function(...)
  255. local log_entry = {func=kk, args={...}}
  256. self.log[#self.log + 1] = log_entry
  257. if kk == 'free' then
  258. self.original_functions[kk](...)
  259. else
  260. log_entry.ret = self.original_functions[kk](...)
  261. end
  262. for i, v in ipairs(log_entry.args) do
  263. if v == nil then
  264. -- XXX This thing thinks that {NULL} ~= {NULL}.
  265. log_entry.args[i] = self.null
  266. end
  267. end
  268. if self.hook then self:hook(log_entry) end
  269. if log_entry.ret then
  270. return log_entry.ret
  271. end
  272. end
  273. end
  274. end
  275. end
  276. log.set_mocks = child_call(log.set_mocks)
  277. function log:clear()
  278. self.log = {}
  279. end
  280. function log:check(exp)
  281. eq(exp, self.log)
  282. self:clear()
  283. end
  284. function log:clear_tmp_allocs(clear_null_frees)
  285. local toremove = {}
  286. local allocs = {}
  287. for i, v in ipairs(self.log) do
  288. if v.func == 'malloc' or v.func == 'calloc' then
  289. allocs[tostring(v.ret)] = i
  290. elseif v.func == 'realloc' or v.func == 'free' then
  291. if allocs[tostring(v.args[1])] then
  292. toremove[#toremove + 1] = allocs[tostring(v.args[1])]
  293. if v.func == 'free' then
  294. toremove[#toremove + 1] = i
  295. end
  296. elseif clear_null_frees and v.args[1] == self.null then
  297. toremove[#toremove + 1] = i
  298. end
  299. if v.func == 'realloc' then
  300. allocs[tostring(v.ret)] = i
  301. end
  302. end
  303. end
  304. table.sort(toremove)
  305. for i = #toremove,1,-1 do
  306. table.remove(self.log, toremove[i])
  307. end
  308. end
  309. function log:restore_original_functions()
  310. -- Do nothing: set mocks live in a separate process
  311. return
  312. --[[
  313. [ for k, v in pairs(self.original_functions) do
  314. [ self.lib['mem_' .. k] = v
  315. [ end
  316. ]]
  317. end
  318. function log:setup()
  319. log:save_original_functions()
  320. log:set_mocks()
  321. end
  322. function log:before_each()
  323. return
  324. end
  325. function log:after_each()
  326. log:restore_original_functions()
  327. end
  328. log:setup()
  329. return log
  330. end
  331. -- take a pointer to a C-allocated string and return an interned
  332. -- version while also freeing the memory
  333. local function internalize(cdata, len)
  334. ffi.gc(cdata, ffi.C.free)
  335. return ffi.string(cdata, len)
  336. end
  337. local cstr = ffi.typeof('char[?]')
  338. local function to_cstr(string)
  339. return cstr(#string + 1, string)
  340. end
  341. local sc
  342. if posix ~= nil then
  343. sc = {
  344. fork = posix.fork,
  345. pipe = posix.pipe,
  346. read = posix.read,
  347. write = posix.write,
  348. close = posix.close,
  349. wait = posix.wait,
  350. exit = posix._exit,
  351. }
  352. elseif syscall ~= nil then
  353. sc = {
  354. fork = syscall.fork,
  355. pipe = function()
  356. local ret = {syscall.pipe()}
  357. return ret[3], ret[4]
  358. end,
  359. read = function(rd, len)
  360. return rd:read(nil, len)
  361. end,
  362. write = function(wr, s)
  363. return wr:write(s)
  364. end,
  365. close = function(p)
  366. return p:close()
  367. end,
  368. wait = syscall.wait,
  369. exit = syscall.exit,
  370. }
  371. else
  372. cimport_immediate('./test/unit/fixtures/posix.h')
  373. sc = {
  374. fork = function()
  375. return tonumber(ffi.C.fork())
  376. end,
  377. pipe = function()
  378. local ret = ffi.new('int[2]', {-1, -1})
  379. ffi.errno(0)
  380. local res = ffi.C.pipe(ret)
  381. if (res ~= 0) then
  382. local err = ffi.errno(0)
  383. assert(res == 0, ("pipe() error: %u: %s"):format(
  384. err, ffi.string(ffi.C.strerror(err))))
  385. end
  386. assert(ret[0] ~= -1 and ret[1] ~= -1)
  387. return ret[0], ret[1]
  388. end,
  389. read = function(rd, len)
  390. local ret = ffi.new('char[?]', len, {0})
  391. local total_bytes_read = 0
  392. ffi.errno(0)
  393. while total_bytes_read < len do
  394. local bytes_read = tonumber(ffi.C.read(
  395. rd,
  396. ffi.cast('void*', ret + total_bytes_read),
  397. len - total_bytes_read))
  398. if bytes_read == -1 then
  399. local err = ffi.errno(0)
  400. if err ~= ffi.C.kPOSIXErrnoEINTR then
  401. assert(false, ("read() error: %u: %s"):format(
  402. err, ffi.string(ffi.C.strerror(err))))
  403. end
  404. elseif bytes_read == 0 then
  405. break
  406. else
  407. total_bytes_read = total_bytes_read + bytes_read
  408. end
  409. end
  410. return ffi.string(ret, total_bytes_read)
  411. end,
  412. write = function(wr, s)
  413. local wbuf = to_cstr(s)
  414. local total_bytes_written = 0
  415. ffi.errno(0)
  416. while total_bytes_written < #s do
  417. local bytes_written = tonumber(ffi.C.write(
  418. wr,
  419. ffi.cast('void*', wbuf + total_bytes_written),
  420. #s - total_bytes_written))
  421. if bytes_written == -1 then
  422. local err = ffi.errno(0)
  423. if err ~= ffi.C.kPOSIXErrnoEINTR then
  424. assert(false, ("write() error: %u: %s ('%s')"):format(
  425. err, ffi.string(ffi.C.strerror(err)), s))
  426. end
  427. elseif bytes_written == 0 then
  428. break
  429. else
  430. total_bytes_written = total_bytes_written + bytes_written
  431. end
  432. end
  433. return total_bytes_written
  434. end,
  435. close = ffi.C.close,
  436. wait = function(pid)
  437. ffi.errno(0)
  438. while true do
  439. local r = ffi.C.waitpid(pid, nil, ffi.C.kPOSIXWaitWUNTRACED)
  440. if r == -1 then
  441. local err = ffi.errno(0)
  442. if err == ffi.C.kPOSIXErrnoECHILD then
  443. break
  444. elseif err ~= ffi.C.kPOSIXErrnoEINTR then
  445. assert(false, ("waitpid() error: %u: %s"):format(
  446. err, ffi.string(ffi.C.strerror(err))))
  447. end
  448. else
  449. assert(r == pid)
  450. end
  451. end
  452. end,
  453. exit = ffi.C._exit,
  454. }
  455. end
  456. local function format_list(lst)
  457. local ret = ''
  458. for _, v in ipairs(lst) do
  459. if ret ~= '' then ret = ret .. ', ' end
  460. ret = ret .. assert:format({v, n=1})[1]
  461. end
  462. return ret
  463. end
  464. if os.getenv('NVIM_TEST_PRINT_SYSCALLS') == '1' then
  465. for k_, v_ in pairs(sc) do
  466. (function(k, v)
  467. sc[k] = function(...)
  468. local rets = {v(...)}
  469. io.stderr:write(('%s(%s) = %s\n'):format(k, format_list({...}),
  470. format_list(rets)))
  471. return unpack(rets)
  472. end
  473. end)(k_, v_)
  474. end
  475. end
  476. local function just_fail(_)
  477. return false
  478. end
  479. say:set('assertion.just_fail.positive', '%s')
  480. say:set('assertion.just_fail.negative', '%s')
  481. assert:register('assertion', 'just_fail', just_fail,
  482. 'assertion.just_fail.positive',
  483. 'assertion.just_fail.negative')
  484. local hook_fnamelen = 30
  485. local hook_sfnamelen = 30
  486. local hook_numlen = 5
  487. local hook_msglen = 1 + 1 + 1 + (1 + hook_fnamelen) + (1 + hook_sfnamelen) + (1 + hook_numlen) + 1
  488. local tracehelp = dedent([[
  489. Trace: either in the format described below or custom debug output starting
  490. with `>`. Latter lines still have the same width in byte.
  491. ┌ Trace type: _r_eturn from function , function _c_all, _l_ine executed,
  492. │ _t_ail return, _C_ount (should not actually appear),
  493. │ _s_aved from previous run for reference, _>_ for custom debug
  494. │ output.
  495. │┏ Function type: _L_ua function, _C_ function, _m_ain part of chunk,
  496. │┃ function that did _t_ail call.
  497. │┃┌ Function name type: _g_lobal, _l_ocal, _m_ethod, _f_ield, _u_pvalue,
  498. │┃│ space for unknown.
  499. │┃│ ┏ Source file name ┌ Function name ┏ Line
  500. │┃│ ┃ (trunc to 30 bytes, no .lua) │ (truncated to last 30 bytes) ┃ number
  501. CWN SSSSSSSSSSSSSSSSSSSSSSSSSSSSSS:FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF:LLLLL\n
  502. ]])
  503. local function child_sethook(wr)
  504. local trace_level = os.getenv('NVIM_TEST_TRACE_LEVEL')
  505. if not trace_level or trace_level == '' then
  506. trace_level = 0
  507. else
  508. trace_level = tonumber(trace_level)
  509. end
  510. if trace_level <= 0 then
  511. return
  512. end
  513. local trace_only_c = trace_level <= 1
  514. local prev_info, prev_reason, prev_lnum
  515. local function hook(reason, lnum, use_prev)
  516. local info = nil
  517. if use_prev then
  518. info = prev_info
  519. elseif reason ~= 'tail return' then -- tail return
  520. info = debug.getinfo(2, 'nSl')
  521. end
  522. if trace_only_c and (not info or info.what ~= 'C') and not use_prev then
  523. if info.source:sub(-9) == '_spec.lua' then
  524. prev_info = info
  525. prev_reason = 'saved'
  526. prev_lnum = lnum
  527. end
  528. return
  529. end
  530. if trace_only_c and not use_prev and prev_reason then
  531. hook(prev_reason, prev_lnum, true)
  532. prev_reason = nil
  533. end
  534. local whatchar = ' '
  535. local namewhatchar = ' '
  536. local funcname = ''
  537. local source = ''
  538. local msgchar = reason:sub(1, 1)
  539. if reason == 'count' then
  540. msgchar = 'C'
  541. end
  542. if info then
  543. funcname = (info.name or ''):sub(1, hook_fnamelen)
  544. whatchar = info.what:sub(1, 1)
  545. namewhatchar = info.namewhat:sub(1, 1)
  546. if namewhatchar == '' then
  547. namewhatchar = ' '
  548. end
  549. source = info.source
  550. if source:sub(1, 1) == '@' then
  551. if source:sub(-4, -1) == '.lua' then
  552. source = source:sub(1, -5)
  553. end
  554. source = source:sub(-hook_sfnamelen, -1)
  555. end
  556. lnum = lnum or info.currentline
  557. end
  558. -- assert(-1 <= lnum and lnum <= 99999)
  559. local lnum_s
  560. if lnum == -1 then
  561. lnum_s = 'nknwn'
  562. else
  563. lnum_s = ('%u'):format(lnum)
  564. end
  565. local msg = ( -- lua does not support %*
  566. ''
  567. .. msgchar
  568. .. whatchar
  569. .. namewhatchar
  570. .. ' '
  571. .. source .. (' '):rep(hook_sfnamelen - #source)
  572. .. ':'
  573. .. funcname .. (' '):rep(hook_fnamelen - #funcname)
  574. .. ':'
  575. .. ('0'):rep(hook_numlen - #lnum_s) .. lnum_s
  576. .. '\n'
  577. )
  578. -- eq(hook_msglen, #msg)
  579. sc.write(wr, msg)
  580. end
  581. debug.sethook(hook, 'crl')
  582. end
  583. local trace_end_msg = ('E%s\n'):format((' '):rep(hook_msglen - 2))
  584. local _debug_log
  585. local debug_log = only_separate(function(...)
  586. return _debug_log(...)
  587. end)
  588. local function itp_child(wr, func)
  589. _debug_log = function(s)
  590. s = s:sub(1, hook_msglen - 2)
  591. sc.write(wr, '>' .. s .. (' '):rep(hook_msglen - 2 - #s) .. '\n')
  592. end
  593. local status, result = pcall(init)
  594. if status then
  595. collectgarbage('stop')
  596. child_sethook(wr)
  597. status, result = pcall(func)
  598. debug.sethook()
  599. end
  600. sc.write(wr, trace_end_msg)
  601. if not status then
  602. local emsg = tostring(result)
  603. if #emsg > 99999 then
  604. emsg = emsg:sub(1, 99999)
  605. end
  606. sc.write(wr, ('-\n%05u\n%s'):format(#emsg, emsg))
  607. deinit()
  608. else
  609. sc.write(wr, '+\n')
  610. deinit()
  611. end
  612. collectgarbage('restart')
  613. collectgarbage()
  614. sc.write(wr, '$\n')
  615. sc.close(wr)
  616. sc.exit(status and 0 or 1)
  617. end
  618. local function check_child_err(rd)
  619. local trace = {}
  620. local did_traceline = false
  621. local maxtrace = tonumber(os.getenv('NVIM_TEST_MAXTRACE')) or 1024
  622. while true do
  623. local traceline = sc.read(rd, hook_msglen)
  624. if #traceline ~= hook_msglen then
  625. if #traceline == 0 then
  626. break
  627. else
  628. trace[#trace + 1] = 'Partial read: <' .. trace .. '>\n'
  629. end
  630. end
  631. if traceline == trace_end_msg then
  632. did_traceline = true
  633. break
  634. end
  635. trace[#trace + 1] = traceline
  636. if #trace > maxtrace then
  637. table.remove(trace, 1)
  638. end
  639. end
  640. local res = sc.read(rd, 2)
  641. if #res == 2 then
  642. local err = ''
  643. if res ~= '+\n' then
  644. eq('-\n', res)
  645. local len_s = sc.read(rd, 5)
  646. local len = tonumber(len_s)
  647. neq(0, len)
  648. if os.getenv('NVIM_TEST_TRACE_ON_ERROR') == '1' and #trace ~= 0 then
  649. err = '\nTest failed, trace:\n' .. tracehelp
  650. for _, traceline in ipairs(trace) do
  651. err = err .. traceline
  652. end
  653. end
  654. err = err .. sc.read(rd, len + 1)
  655. end
  656. local eres = sc.read(rd, 2)
  657. if eres ~= '$\n' then
  658. if #trace == 0 then
  659. err = '\nTest crashed, no trace available (check NVIM_TEST_TRACE_LEVEL)\n'
  660. else
  661. err = '\nTest crashed, trace:\n' .. tracehelp
  662. for i = 1, #trace do
  663. err = err .. trace[i]
  664. end
  665. end
  666. if not did_traceline then
  667. err = err .. '\nNo end of trace occurred'
  668. end
  669. local cc_err, cc_emsg = pcall(check_cores, Paths.test_luajit_prg, true)
  670. if not cc_err then
  671. err = err .. '\ncheck_cores failed: ' .. cc_emsg
  672. end
  673. end
  674. if err ~= '' then
  675. assert.just_fail(err)
  676. end
  677. end
  678. end
  679. local function itp_parent(rd, pid, allow_failure)
  680. local err, emsg = pcall(check_child_err, rd)
  681. sc.wait(pid)
  682. sc.close(rd)
  683. if not err then
  684. if allow_failure then
  685. io.stderr:write('Errorred out:\n' .. tostring(emsg) .. '\n')
  686. os.execute([[
  687. sh -c "source ci/common/test.sh
  688. check_core_dumps --delete \"]] .. Paths.test_luajit_prg .. [[\""]])
  689. else
  690. error(emsg)
  691. end
  692. end
  693. end
  694. local function gen_itp(it)
  695. child_calls_mod = {}
  696. child_calls_mod_once = {}
  697. child_cleanups_mod_once = {}
  698. preprocess_cache_mod = map(function(v) return v end, preprocess_cache_init)
  699. previous_defines_mod = previous_defines_init
  700. cdefs_mod = cdefs_init:copy()
  701. local function itp(name, func, allow_failure)
  702. if allow_failure and os.getenv('NVIM_TEST_RUN_FAILING_TESTS') ~= '1' then
  703. -- FIXME Fix tests with this true
  704. return
  705. end
  706. it(name, function()
  707. local rd, wr = sc.pipe()
  708. child_pid = sc.fork()
  709. if child_pid == 0 then
  710. sc.close(rd)
  711. itp_child(wr, func)
  712. else
  713. sc.close(wr)
  714. local saved_child_pid = child_pid
  715. child_pid = nil
  716. itp_parent(rd, saved_child_pid, allow_failure)
  717. end
  718. end)
  719. end
  720. return itp
  721. end
  722. local function cppimport(path)
  723. return cimport(Paths.test_source_path .. '/test/includes/pre/' .. path)
  724. end
  725. cimport('./src/nvim/types.h', './src/nvim/main.h', './src/nvim/os/time.h')
  726. local function conv_enum(etab, eval)
  727. local n = tonumber(eval)
  728. return etab[n] or n
  729. end
  730. local function array_size(arr)
  731. return ffi.sizeof(arr) / ffi.sizeof(arr[0])
  732. end
  733. local function kvi_size(kvi)
  734. return array_size(kvi.init_array)
  735. end
  736. local function kvi_init(kvi)
  737. kvi.capacity = kvi_size(kvi)
  738. kvi.items = kvi.init_array
  739. return kvi
  740. end
  741. local function kvi_destroy(kvi)
  742. if kvi.items ~= kvi.init_array then
  743. lib.xfree(kvi.items)
  744. end
  745. end
  746. local function kvi_new(ct)
  747. return kvi_init(ffi.new(ct))
  748. end
  749. local function make_enum_conv_tab(m, values, skip_pref, set_cb)
  750. child_call_once(function()
  751. local ret = {}
  752. for _, v in ipairs(values) do
  753. local str_v = v
  754. if v:sub(1, #skip_pref) == skip_pref then
  755. str_v = v:sub(#skip_pref + 1)
  756. end
  757. ret[tonumber(m[v])] = str_v
  758. end
  759. set_cb(ret)
  760. end)
  761. end
  762. local function ptr2addr(ptr)
  763. return tonumber(ffi.cast('intptr_t', ffi.cast('void *', ptr)))
  764. end
  765. local s = ffi.new('char[64]', {0})
  766. local function ptr2key(ptr)
  767. ffi.C.snprintf(s, ffi.sizeof(s), '%p', ffi.cast('void *', ptr))
  768. return ffi.string(s)
  769. end
  770. local module = {
  771. cimport = cimport,
  772. cppimport = cppimport,
  773. internalize = internalize,
  774. ffi = ffi,
  775. lib = lib,
  776. cstr = cstr,
  777. to_cstr = to_cstr,
  778. NULL = NULL,
  779. OK = OK,
  780. FAIL = FAIL,
  781. alloc_log_new = alloc_log_new,
  782. gen_itp = gen_itp,
  783. only_separate = only_separate,
  784. child_call_once = child_call_once,
  785. child_cleanup_once = child_cleanup_once,
  786. sc = sc,
  787. conv_enum = conv_enum,
  788. array_size = array_size,
  789. kvi_destroy = kvi_destroy,
  790. kvi_size = kvi_size,
  791. kvi_init = kvi_init,
  792. kvi_new = kvi_new,
  793. make_enum_conv_tab = make_enum_conv_tab,
  794. ptr2addr = ptr2addr,
  795. ptr2key = ptr2key,
  796. debug_log = debug_log,
  797. }
  798. module = global_helpers.tbl_extend('error', module, global_helpers)
  799. return function()
  800. return module
  801. end