robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
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 clear =n . clear 6local command =n . command 7local eq =t . eq 8local api =n . api 9local retry =t . retry 10local write_file =t . write_file 11local sleep =vim . uv . sleep 12 13--- Returns true if the autoread module is watching the given buffer 14--- (defaults to the current buffer). 15local function is_watching ( bufnr ) 16return n . exec_lua ( function ( b ) 17return require ( 'nvim.autoread' ). _is_watching ( b or vim . api . nvim_get_current_buf ()) 18end , bufnr ) 19end 20 21--- Shortens the 'autoread' debounce window so each test doesn't pay the 100ms time-cost. 22local function shorten_debounce () 23n . exec_lua ( [[require('nvim.autoread')._set_debounce(10)]] ) 24end 25 26--- Edits a fresh tempfile with the given initial content and asserts the watcher attached. 27--- Returns the file path. 28local function open_watched ( content ) 29local path =t . tmpname () 30write_file ( path , content ) 31command ( 'edit ' .. path ) 32eq ( true , is_watching ()) 33return path 34end 35 36describe ( 'autoread file watcher' , function () 37before_each ( function () 38clear ( { args ={ '--clean' } } ) 39shorten_debounce () 40end ) 41 42it ( 'watches file opened on startup (nvim foo.txt)' , function () 43local path =t . tmpname () 44write_file ( path , 'startup original\n' ) 45 46-- Spawn nvim with the file passed on the command line. This exercises the 47-- boot order: plugins must load before the initial file is read so that 48-- the BufReadPost autocmd is registered in time to attach a watcher. 49clear ( { args ={ '--clean' , path } } ) 50shorten_debounce () 51 52eq ( { 'startup original' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 53eq ( true , is_watching ()) 54 55write_file ( path , 'startup changed\n' ) 56retry ( nil , 3000 , function () 57eq ( { 'startup changed' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 58end ) 59end ) 60 61it ( 'reloads on external change; survives hide; undoable; bdelete stops watch' , function () 62local path =open_watched ( 'original content\n' ) 63local bufnr =api . nvim_get_current_buf () 64 65-- 1. Plain external change reloads the visible buffer. 66write_file ( path , 'new content\n' ) 67retry ( nil , 3000 , function () 68eq ( { 'new content' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 69end ) 70 71-- 2. Hide the buffer; watcher stays attached and still reloads. 72command ( 'set hidden' ) 73command ( 'enew' ) 74eq ( true , is_watching ( bufnr )) 75write_file ( path , 'while hidden\n' ) 76retry ( nil , 3000 , function () 77eq ( { 'while hidden' } , api . nvim_buf_get_lines ( bufnr , 0 , - 1 , true )) 78end ) 79 80-- 3. The reload is undoable. Done last so the resulting modified state 81-- (buffer ≠ disk) doesn't block earlier auto-reload assertions. 82command ( 'buffer ' .. bufnr ) 83command ( 'silent undo' ) 84eq ( { 'new content' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 85 86-- 4. bdelete stops the watcher. 87command ( 'enew!' ) 88command ( 'bdelete! ' .. bufnr ) 89eq ( false , is_watching ( bufnr )) 90end ) 91 92it ( 'does not reload when buffer has unsaved changes (conflict)' , function () 93local path =open_watched ( 'original\n' ) 94 95api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'local change' } ) 96eq ( true , api . nvim_get_option_value ( 'modified' , { buf =0 } )) 97 98write_file ( path , 'external change\n' ) 99 100-- Give the watcher time to fire; the buffer must NOT be reloaded because 101-- it has unsaved changes (autoread only reloads unmodified buffers). 102sleep ( 50 ) 103-- Also do a manual :checktime to be sure 104command ( 'silent! checktime' ) 105-- Buffer should still have local changes (autoread doesn't override modified buffers) 106eq ( { 'local change' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 107end ) 108 109it ( 'tracks autoread option changes' , function () 110local path =open_watched ( 'original\n' ) 111 112command ( 'setlocal noautoread' ) 113eq ( false , is_watching ()) 114 115-- Modify externally while 'noautoread'. 116write_file ( path , 'while disabled\n' ) 117sleep ( 50 ) 118eq ( { 'original' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 119 120-- Re-enable autoread 121command ( 'setlocal autoread' ) 122eq ( true , is_watching ()) 123 124-- Modify again 125write_file ( path , 'after reenable\n' ) 126retry ( nil , 3000 , function () 127eq ( { 'after reenable' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 128end ) 129end ) 130 131it ( 'handles file deletion gracefully' , function () 132local path =open_watched ( 'will be deleted\n' ) 133 134os . remove ( path ) 135 136retry ( nil , 3000 , function () 137eq ( false , is_watching ()) 138end ) 139-- Buffer content remains unchanged. 140eq ( { 'will be deleted' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 141end ) 142 143it ( 'coalesces rapid changes via debouncing' , function () 144-- Use a wide debounce window so all write_file calls reliably land inside it. 145n . exec_lua ( [[require('nvim.autoread')._set_debounce(200)]] ) 146 147local path =open_watched ( 'v1\n' ) 148 149-- Count buffer reloads triggered by the watcher. 150n . exec_lua ( [[ 151_G.reloads = 0 152vim.api.nvim_create_autocmd('FileChangedShellPost', { 153callback = function() _G.reloads = _G.reloads + 1 end, 154}) 155]] ) 156 157-- 4 back-to-back writes well inside one debounce window. 158write_file ( path , 'v2\n' ) 159write_file ( path , 'v3\n' ) 160write_file ( path , 'v4\n' ) 161write_file ( path , 'final\n' ) 162 163retry ( nil , 3000 , function () 164eq ( { 'final' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 165end ) 166 167-- Debouncing collapses 4 writes into (ideally) 1 reload. But we assert "<=2" bc OS filewatch 168-- events may arrive in multiple batches under CI load => a straggler may arrive after the 169-- debounce window and trigger a second reload. 170-- The buffer already holds "final" (checked above); without debouncing each write would reload. 171sleep ( 250 ) 172local reloads =n . exec_lua ( 'return _G.reloads' ) 173t . ok ( reloads >= 1 and reloads <= 2 , '1 or 2 reloads (4 writes coalesced)' , reloads ) 174end ) 175 176it ( "bumps 'busy' on each watched buffer while a reload is pending" , function () 177-- Use a long debounce-window so we can sample 'busy' during pending autoreads. 178n . exec_lua ( [[require('nvim.autoread')._set_debounce(5000)]] ) 179 180local path1 =open_watched ( 'a1\n' ) 181local buf1 =api . nvim_get_current_buf () 182command ( 'enew' ) 183local path2 =open_watched ( 'a2\n' ) 184local buf2 =api . nvim_get_current_buf () 185 186eq ( 0 , api . nvim_get_option_value ( 'busy' , { buf =buf1 } )) 187eq ( 0 , api . nvim_get_option_value ( 'busy' , { buf =buf2 } )) 188 189-- Trigger external changes on both watched files concurrently. 190write_file ( path1 , 'b1\n' ) 191write_file ( path2 , 'b2\n' ) 192 193eq ( 194true , 195n . exec_lua ( function ( b1 , b2 ) 196return vim . wait ( 2000 , function () 197return vim . bo [ b1 ]. busy == 1 and vim . bo [ b2 ]. busy == 1 198end , 5 ) 199end , buf1 , buf2 ) 200) 201 202-- A short debounce + fresh write lets the pending reload complete, clearing 'busy'. 203n . exec_lua ( [[require('nvim.autoread')._set_debounce(10)]] ) 204write_file ( path1 , 'c1\n' ) 205write_file ( path2 , 'c2\n' ) 206 207-- Confirm busy=0 after the autoread. 208retry ( nil , 3000 , function () 209eq ( { 'c1' } , api . nvim_buf_get_lines ( buf1 , 0 , - 1 , true )) 210eq ( { 'c2' } , api . nvim_buf_get_lines ( buf2 , 0 , - 1 , true )) 211eq ( 0 , api . nvim_get_option_value ( 'busy' , { buf =buf1 } )) 212eq ( 0 , api . nvim_get_option_value ( 'busy' , { buf =buf2 } )) 213end ) 214end ) 215 216it ( 'handles autocmd error during reload' , function () 217local path =open_watched ( 'original\n' ) 218local bufnr =api . nvim_get_current_buf () 219 220-- Define a broken autocmd. 221n . exec_lua ( [[ 222vim.api.nvim_create_autocmd('FileChangedShellPost', { 223callback = function() error('boom from test autocmd') end, 224}) 225]] ) 226 227write_file ( path , 'changed\n' ) 228 229-- autoread should surface the error, and do its cleanup despite the failed autocmd. 230retry ( nil , 3000 , function () 231t . matches ( 'autoread:.*boom from test autocmd' , n . eval ( 'v:errmsg' )) 232eq ( 0 , api . nvim_get_option_value ( 'busy' , { buf =bufnr } )) 233end ) 234end ) 235 236it ( 'detects changes after atomic rename (external editor save)' , function () 237local path =open_watched ( 'original\n' ) 238 239-- Atomic save: write to temp file, rename over target. 240local tmp =path .. '.tmp' 241write_file ( tmp , 'after rename\n' ) 242assert ( vim . uv . fs_rename ( tmp , path )) 243 244retry ( nil , 3000 , function () 245eq ( { 'after rename' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 246end ) 247-- Watcher re-established on the new inode. 248eq ( true , is_watching ()) 249 250-- Subsequent plain writes still reload. 251write_file ( path , 'second change\n' ) 252retry ( nil , 3000 , function () 253eq ( { 'second change' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 254end ) 255end ) 256end )