robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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