robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each =t . describe , t . it , t . before_each 5local eq =t . eq 6local exec_lua =n . exec_lua 7local clear =n . clear 8local is_ci =t . is_ci 9local is_os =t . is_os 10local skip =t . skip 11 12-- Create a file via a rename to avoid multiple 13-- events which can happen with some backends on some platforms 14local function touch ( path ) 15local tmp =t . tmpname () 16assert ( vim . uv . fs_rename ( tmp , path )) 17end 18 19describe ( 'vim._watch' , function () 20before_each ( function () 21clear () 22end ) 23 24it ( 'watchdirs() does not scan excluded subtrees' , function () 25local root_dir =t . tmpname ( false ) 26t . finally ( function () 27n . rmdir ( root_dir ) 28end ) 29n . mkdir_p ( root_dir .. '/src/deep' ) 30n . mkdir_p ( root_dir .. '/node_modules/pkg/excluded/deep' ) 31 32-- Also cover a pattern that matches the directory but not its descendants. 33for _ , pattern in ipairs ( { '**/node_modules/*/**' , '**/excluded' } ) do 34local scanned =exec_lua ( function ( root , exclude_pattern ) 35root =vim . fs . normalize ( root ) 36local scanned ={} 37local fs_scandir =vim . uv . fs_scandir 38vim . uv . fs_scandir =function ( path , ... ) 39scanned [ # scanned + 1 ] =path : sub ( # root + 1 ) 40return fs_scandir ( path , ... ) 41end 42local cancel =vim . _watch . watchdirs ( root , { 43exclude_pattern =vim . glob . to_lpeg ( exclude_pattern ), 44} , function () end ) 45vim . uv . fs_scandir =fs_scandir 46cancel () 47table . sort ( scanned ) 48return scanned 49end , root_dir , pattern ) 50 51eq ( { '' , '/node_modules' , '/node_modules/pkg' , '/src' , '/src/deep' } , scanned , pattern ) 52end 53end ) 54 55it ( 'watchdirs() tolerates directories deleted during setup' , function () 56local root_dir =t . tmpname ( false ) 57t . finally ( function () 58n . rmdir ( root_dir ) 59end ) 60n . mkdir_p ( root_dir .. '/gone' ) 61 62exec_lua ( function ( root ) 63local dir =vim . fs . dir 64vim . fs . dir =function ( path , opts ) 65local iter =dir ( path , opts ) 66return function () 67local name , kind =iter () 68if name == 'gone' then 69-- Delete after enumeration, before the backend starts watching it. 70assert ( vim . uv . fs_rmdir ( root .. '/gone' )) 71end 72return name , kind 73end 74end 75local cancel =vim . _watch . watchdirs ( root , { on_error =error } , function () end ) 76vim . fs . dir =dir 77cancel () 78end , root_dir ) 79end ) 80 81it ( 'inotify() reports failure to start the process' , function () 82exec_lua ( function () 83vim . env . PATH ='' 84local errors ={} 85local cancel =vim . _watch . inotify ( '.' , { 86on_error =function ( err ) 87errors [ # errors + 1 ] =err 88end , 89} , function () end ) 90assert ( vim . _watch . active (). inotify == 0 ) 91cancel () 92assert ( # errors == 1 , vim . inspect ( errors )) 93assert ( errors [ 1 ]: find ( 'ENOENT' , 1 , true ), errors [ 1 ]) 94end ) 95end ) 96 97it ( 'releases earlier subscriptions when setup raises' , function () 98exec_lua ( function () 99local starts , stops =0 , 0 100vim . system =function () 101starts =starts + 1 102if starts == 2 then 103error ( 'cannot start watcher' ) 104end 105return { 106kill =function () 107stops =stops + 1 108end , 109} 110end 111 112local ok , err =pcall ( vim . _watch . inotify , { 113{ path ='/one' } , 114{ path ='/two' } , 115} , { on_error =error } , function () end ) 116 117assert ( not ok and err : find ( 'cannot start watcher' , 1 , true )) 118assert ( starts == 2 and stops == 1 , 'started watchers must be stopped on error' ) 119assert ( vim . _watch . active (). inotify == 0 ) 120end ) 121end ) 122 123it ( 'inotify() groups rules by root and filters file events' , function () 124local events =exec_lua ( function () 125local outputs ={} 126vim . system =function ( cmd , opts ) 127local root =cmd [ # cmd ] 128assert ( not outputs [ root ], 'rules for one root must share a watcher' ) 129outputs [ root ] =opts . stdout 130return { 131kill =function () 132outputs [ root ] =nil 133end , 134} 135end 136 137local watch =vim . _watch 138local T =watch . FileChangeType 139local python ='/one/*.py' 140local events ={} 141local cancel =watch . inotify ( { 142{ path ='/one' , pattern =python , events ={ T . Changed } } , 143{ path ='/one' , pattern =python , events ={ T . Created , T . Changed } } , 144{ path ='/one' , pattern ='**/*.txt' } , 145{ path ='/two' } , 146} , { 147include_pattern ='**/*.py' , 148exclude_pattern ='**/ignored.py' , 149on_error =error , 150} , function ( path , change_type ) 151events [ # events + 1 ] ={ path , change_type } 152end ) 153assert ( watch . active (). inotify == 2 ) 154 155outputs [ '/one' ]( nil , '/one/ MODIFY file.py\n' ) -- Matches two rules, reported once. 156outputs [ '/one' ]( nil , '/one/ CREATE file.py\n' ) 157outputs [ '/one' ]( nil , '/one/ DELETE file.py\n' ) -- Wrong event type. 158outputs [ '/one' ]( nil , '/one/ MODIFY file.txt\n' ) -- Global include still applies. 159outputs [ '/one' ]( nil , '/one/ MODIFY ignored.py\n' ) 160outputs [ '/two' ]( nil , '/two/ DELETE file.py\n' ) -- No per-rule filters. 161cancel () 162cancel () 163assert ( not next ( outputs )) 164assert ( watch . active (). inotify == 0 ) 165return events 166end ) 167eq ( { { '/one/file.py' , 2 } , { '/one/file.py' , 1 } , { '/two/file.py' , 3 } } , events ) 168end ) 169 170it ( 'inotify() shares glob watches and releases subscriptions independently' , function () 171exec_lua ( function () 172local root =vim . fs . normalize ( assert ( vim . uv . cwd ())) 173local prefix =vim . fn . escape ( root , '\\/*?[]{}#' ) .. '/' 174local processes ={} 175vim . system =function ( _ , opts ) 176processes [ # processes + 1 ] =opts 177return { 178kill =function () 179assert ( not opts . stopped , 'process stopped twice' ) 180opts . stopped =true 181end , 182} 183end 184local changes , errors ={ 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 } 185local function subscribe ( id , pattern ) 186local path =id == 1 and root or { { path =root .. '/' } } 187return vim . _watch . inotify ( path , { 188include_pattern ={ prefix .. ( pattern or '*.{py,lua}' ) } , 189exclude_pattern ={ '**/ignored.py' } , 190on_error =function () 191errors [ id ] =errors [ id ] + 1 192end , 193} , function () 194changes [ id ] =changes [ id ] + 1 195end ) 196end 197local cancel_a =subscribe ( 1 ) 198local cancel_b =subscribe ( 2 ) 199assert ( # processes == 1 , 'equivalent globs must share a backend' ) 200assert ( vim . _watch . active (). inotify == 1 ) 201processes [ 1 ]. stdout ( nil , root .. '/ CREATE file.py' ) 202assert ( vim . deep_equal ( changes , { 1 , 1 , 0 , 0 } )) 203cancel_a () 204cancel_a () 205assert ( not processes [ 1 ]. stopped ) 206processes [ 1 ]. stdout ( nil , root .. '/ MODIFY file.py' ) 207assert ( vim . deep_equal ( changes , { 1 , 2 , 0 , 0 } )) 208 209processes [ 1 ]. stderr ( nil , 'watcher failed' ) 210assert ( vim . deep_equal ( errors , { 0 , 1 , 0 , 0 } )) 211assert ( vim . _watch . active (). inotify == 1 , 'backend remains owned after an error' ) 212local cancel_c =subscribe ( 3 ) 213assert ( # processes == 2 , 'failed backend must not be reused' ) 214local cancel_d =subscribe ( 4 , '*.txt' ) 215assert ( # processes == 3 , 'different filters must not share a backend' ) 216processes [ 2 ]. stdout ( nil , root .. '/ CREATE file.py' ) 217processes [ 3 ]. stdout ( nil , root .. '/ CREATE file.py' ) 218processes [ 3 ]. stdout ( nil , root .. '/ CREATE file.txt' ) 219assert ( vim . deep_equal ( changes , { 1 , 2 , 1 , 1 } )) 220cancel_b () 221assert ( processes [ 1 ]. stopped and not processes [ 2 ]. stopped ) 222cancel_c () 223cancel_d () 224assert ( processes [ 2 ]. stopped and processes [ 3 ]. stopped ) 225assert ( vim . _watch . active (). inotify == 0 ) 226end ) 227end ) 228 229it ( 'inotify() skips invalid globs without disabling sharing' , function () 230exec_lua ( function () 231local root =assert ( vim . uv . cwd ()) 232local outputs ={} 233vim . system =function ( _ , opts ) 234outputs [ # outputs + 1 ] =opts . stdout 235return { kill =function () end } 236end 237 238local changes , errors , cancels =0 , {} , {} 239for i =1 , 2 do 240errors [ i ] ={} 241cancels [ i ] =vim . _watch . inotify ( { 242{ path =root , pattern ='a/**b' } , 243{ path =root , pattern ={ '{foo}' , '**/*.lua' } } , 244} , { 245on_error =function ( err ) 246errors [ i ][ # errors [ i ] + 1 ] =err 247end , 248} , function () 249changes =changes + 1 250end ) 251end 252 253assert ( # errors [ 1 ] == 2 and # errors [ 2 ] == 2 , 'each subscriber receives its own glob errors' ) 254assert ( # outputs == 1 , 'valid rules still share a backend' ) 255outputs [ 1 ]( nil , root .. '/ MODIFY file.lua\n' ) 256assert ( changes == 2 ) 257cancels [ 1 ]() 258cancels [ 2 ]() 259assert ( vim . _watch . active (). inotify == 0 ) 260end ) 261end ) 262 263it ( 'watchdirs() keeps directories watched when only their files match a glob' , function () 264local root_dir =t . tmpname ( false ) 265n . mkdir_p ( root_dir .. '/src' ) 266t . finally ( function () 267n . rmdir ( root_dir ) 268end ) 269exec_lua ( function ( root ) 270root =vim . fs . normalize ( root ) 271local callbacks ={} 272vim . uv . new_fs_event =function () 273return { 274start =function ( _ , path , _ , callback ) 275callbacks [ path ] =callback 276end , 277is_closing =function () 278return false 279end , 280close =function () end , 281} 282end 283local events ={} 284local cancel =vim . _watch . watchdirs ( root , { 285include_pattern ='**/*.py' , 286debounce =1 , 287} , function ( path ) 288events [ # events + 1 ] =path 289end ) 290assert ( callbacks [ root .. '/src' ], 'existing parent directory must be watched' ) 291vim . fn . mkdir ( root .. '/new' ) 292callbacks [ root ]( nil , 'new' , { rename =true } ) 293assert ( 294vim . wait ( 1000 , function () 295return callbacks [ root .. '/new' ] ~= nil 296end ), 297'new parent directory must be watched' 298) 299vim . fn . writefile ( {} , root .. '/new/file.py' ) 300callbacks [ root .. '/new' ]( nil , 'file.py' , { rename =true } ) 301assert ( vim . wait ( 1000 , function () 302return # events == 1 303end )) 304assert ( events [ 1 ] == root .. '/new/file.py' ) 305cancel () 306end , root_dir ) 307end ) 308 309local function run ( watchfunc ) 310local function do_watch ( root_dir , watchfunc_ ) 311exec_lua ( 312[[ 313local root_dir, watchfunc = ... 314 315_G.events = {} 316 317_G.stop_watch = vim._watch[watchfunc](root_dir, { 318debounce = 100, 319include_pattern = vim.lpeg.P(root_dir) * vim.lpeg.P("/file") ^ -1, 320exclude_pattern = vim.lpeg.P(root_dir .. '/file.unwatched'), 321}, function(path, change_type) 322table.insert(_G.events, { path = path, change_type = change_type }) 323end) 324]] , 325root_dir , 326watchfunc_ 327) 328end 329 330it ( watchfunc .. '() reports nonexistent paths to on_error' , function () 331if watchfunc == 'inotify' then 332skip ( n . fn . executable ( 'inotifywait' ) == 0 , 'inotifywait not found' ) 333skip ( is_os ( 'bsd' ), 'inotifywait on bsd CI seems to expect path to exist?' ) 334skip ( t . is_arch ( 's390x' ), 'inotifywait not available on s390x CI' ) 335end 336 337exec_lua ( function ( backend ) 338local errors ={} 339local cancel =vim . _watch [ backend ]( '/i am /very/funny.go' , { 340on_error =function ( err ) 341errors [ # errors + 1 ] =err 342end , 343} , function () 344error ( 'Unexpected file change' ) 345end ) 346assert ( vim . wait ( 2000 , function () 347return # errors > 0 348end )) 349if backend ~= 'inotify' then 350assert ( errors [ 1 ]: match ( '^ENOENT:' ), errors [ 1 ]) 351end 352assert ( errors [ 1 ]: find ( '(watching /i am /very/funny.go)' , 1 , true ), errors [ 1 ]) 353cancel () 354end , watchfunc ) 355end ) 356 357if watchfunc ~= 'inotify' then 358it ( watchfunc .. '() logs startup failures without on_error' , function () 359local logfile =exec_lua ( function ( backend ) 360local logfile =vim . fs . joinpath ( vim . fn . stdpath ( 'log' ), 'nvim-watch.log' ) 361vim . fn . writefile ( {} , logfile ) 362local cancel =vim . _watch [ backend ]( '/i am /very/funny.go' , {} , function () 363error ( 'Unexpected file change' ) 364end ) 365assert ( vim . _watch . active ()[ backend ] == 0 ) 366cancel () 367return logfile 368end , watchfunc ) 369t . assert_log ( '%[ERROR%].-ENOENT:' , logfile ) 370end ) 371end 372 373it ( watchfunc .. '() detects file changes' , function () 374if watchfunc == 'inotify' then 375skip ( is_os ( 'win' ), 'N/A: inotify not supported on Windows' ) 376skip ( is_os ( 'mac' ), 'flaky test on mac' ) 377skip ( not is_ci () and n . fn . executable ( 'inotifywait' ) == 0 , 'inotifywait not found' ) 378skip ( t . is_arch ( 's390x' ), 'inotifywait not available on s390x CI' ) 379end 380 381-- Note: because this is not `elseif`, BSD is skipped for *all* cases...? 382if watchfunc == 'watch' then 383skip ( is_os ( 'mac' ), 'flaky test on mac' ) 384skip ( is_os ( 'bsd' ), 'Stopped working on bsd after 3ca967387c49c754561c3b11a574797504d40f38' ) 385elseif watchfunc == 'watchdirs' and is_os ( 'mac' ) then 386skip ( true , 'weird failure since macOS 14 CI, see bbf208784ca279178ba0075b60d3e9c80f11da7a' ) 387else 388skip ( 389is_os ( 'bsd' ), 390'kqueue only reports events on watched folder itself, not contained files #26110' 391) 392end 393 394local expected_events =0 395--- Waits for a new event, or fails if no events are triggered. 396local function wait_for_event () 397expected_events =expected_events + 1 398exec_lua ( 399[[ 400local expected_events = ... 401assert( 402vim.wait(3000, function() 403return #_G.events == expected_events 404end), 405string.format( 406'Timed out waiting for expected event no. %d. Current events seen so far: %s', 407expected_events, 408vim.inspect(events) 409) 410) 411]] , 412expected_events 413) 414end 415 416local root_dir =vim . uv . fs_mkdtemp ( vim . fs . dirname ( t . tmpname ( false )) .. '/nvim_XXXXXXXXXX' ) 417local unwatched_path =root_dir .. '/file.unwatched' 418local watched_path =root_dir .. '/file' 419 420do_watch ( root_dir , watchfunc ) 421 422if watchfunc ~= 'watch' then 423vim . uv . sleep ( 200 ) 424end 425 426touch ( watched_path ) 427touch ( unwatched_path ) 428wait_for_event () 429 430os . remove ( watched_path ) 431os . remove ( unwatched_path ) 432wait_for_event () 433 434exec_lua [[_G.stop_watch()]] 435-- No events should come through anymore 436 437vim . uv . sleep ( 100 ) 438touch ( watched_path ) 439vim . uv . sleep ( 100 ) 440os . remove ( watched_path ) 441vim . uv . sleep ( 100 ) 442 443eq ( { 444{ 445change_type =exec_lua ( [[return vim._watch.FileChangeType.Created]] ), 446path =root_dir .. '/file' , 447} , 448{ 449change_type =exec_lua ( [[return vim._watch.FileChangeType.Deleted]] ), 450path =root_dir .. '/file' , 451} , 452} , exec_lua [[return _G.events]] ) 453end ) 454end 455 456run ( 'watch' ) 457run ( 'watchdirs' ) 458run ( 'inotify' ) 459end )