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