robin/laufey

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

Justin M. Keyesfeat(cwd)!: :lcd! (bang), rearrange :bcd/:lcd/… scope precedence #41194a4a544032a

master
16.4 KiB436 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each, setup, teardown =
5  t.describe, t.it, t.before_each, t.setup, t.teardown
6local clear = n.clear
7local command = n.command
8local eq = t.eq
9local eval = n.eval
10local request = n.request
11local is_os = t.is_os
12
13describe('autocmd DirChanged and DirChangedPre', function()
14  local curdir = t.fix_slashes(vim.uv.cwd())
15  local dirs = {
16    curdir .. '/Xtest-functional-autocmd-dirchanged.dir1',
17    curdir .. '/Xtest-functional-autocmd-dirchanged.dir2',
18    curdir .. '/Xtest-functional-autocmd-dirchanged.dir3',
19    curdir .. '/Xtest-functional-autocmd-dirchanged.dir4',
20  }
21  -- The same directories spelled with backslashes and uppercase: on Windows (case-insensitive)
22  -- filesystem) they name the same directories, so changing to them must not fire DirChanged.
23  local win_dirs = vim.tbl_map(function(d)
24    return (d:upper():gsub('/', '\\'))
25  end, dirs)
26
27  setup(function()
28    for _, dir in pairs(dirs) do
29      t.mkdir(dir)
30    end
31  end)
32  teardown(function()
33    for _, dir in pairs(dirs) do
34      n.rmdir(dir)
35    end
36  end)
37
38  before_each(function()
39    clear()
40    command(
41      'autocmd DirChangedPre * let [g:evpre, g:amatchpre, g:cdprecount] '
42        .. '= [copy(v:event), expand("<amatch>"), 1 + get(g:, "cdprecount", 0)]'
43    )
44    command(
45      'autocmd DirChanged * let [g:getcwd, g:ev, g:amatch, g:cdcount] '
46        .. '= [getcwd(), copy(v:event), expand("<amatch>"), 1 + get(g:, "cdcount", 0)]'
47    )
48    -- Normalize path separators.
49    command(
50      [[autocmd DirChangedPre * let g:evpre['directory'] = substitute(g:evpre['directory'], '\\', '/', 'g')]]
51    )
52    command([[autocmd DirChanged * let g:ev['cwd'] = substitute(g:ev['cwd'], '\\', '/', 'g')]])
53    command([[autocmd DirChanged * let g:getcwd = substitute(g:getcwd, '\\', '/', 'g')]])
54  end)
55
56  --- Runs `cmd` and asserts that it fires no DirChangedPre/DirChanged event.
57  local function assert_no_event(cmd)
58    local counts = eval('[g:cdprecount, g:cdcount]')
59    command('let [g:evpre, g:ev] = [{}, {}]')
60    command(cmd)
61    eq({}, eval('g:evpre'))
62    eq({}, eval('g:ev'))
63    eq(counts, eval('[g:cdprecount, g:cdcount]'))
64  end
65
66  it('set v:event and <amatch>', function()
67    command('lcd ' .. dirs[1])
68    eq({ directory = dirs[1], scope = 'window', changed_window = false }, eval('g:evpre'))
69    eq({ cwd = dirs[1], scope = 'window', changed_window = false }, eval('g:ev'))
70    eq('window', eval('g:amatchpre'))
71    eq('window', eval('g:amatch'))
72    eq(1, eval('g:cdprecount'))
73    eq(1, eval('g:cdcount'))
74
75    command('tcd ' .. dirs[2])
76    eq({ directory = dirs[2], scope = 'tabpage', changed_window = false }, eval('g:evpre'))
77    eq({ cwd = dirs[2], scope = 'tabpage', changed_window = false }, eval('g:ev'))
78    eq('tabpage', eval('g:amatchpre'))
79    eq('tabpage', eval('g:amatch'))
80    eq(2, eval('g:cdprecount'))
81    eq(2, eval('g:cdcount'))
82
83    command('cd ' .. dirs[3])
84    eq({ directory = dirs[3], scope = 'global', changed_window = false }, eval('g:evpre'))
85    eq({ cwd = dirs[3], scope = 'global', changed_window = false }, eval('g:ev'))
86    eq('global', eval('g:amatchpre'))
87    eq('global', eval('g:amatch'))
88    eq(3, eval('g:cdprecount'))
89    eq(3, eval('g:cdcount'))
90
91    command('bcd ' .. dirs[4])
92    eq({ directory = dirs[4], scope = 'buffer', changed_window = false }, eval('g:evpre'))
93    eq({ cwd = dirs[4], scope = 'buffer', changed_window = false }, eval('g:ev'))
94    eq('buffer', eval('g:amatchpre'))
95    eq('buffer', eval('g:amatch'))
96    eq(4, eval('g:cdprecount'))
97    eq(4, eval('g:cdcount'))
98
99    -- Buffer-local dir is NOT cleared by a wider scope.
100    assert_no_event('lcd ' .. dirs[1])
101    assert_no_event('tcd ' .. dirs[2])
102    assert_no_event('cd ' .. dirs[3])
103
104    -- ":bcd!" reports the scope that becomes effective, not the one that was unset.
105    -- The overridden ":cd" above still reset the window/tab scopes, so that is the global dir.
106    command('bcd!')
107    eq({ cwd = dirs[3], scope = 'global', changed_window = false }, eval('g:ev'))
108    eq('global', eval('g:amatch'))
109
110    -- Same for ":lcd!", landing on the tab-local dir.
111    command('tcd ' .. dirs[2])
112    command('lcd ' .. dirs[1])
113    command('lcd!')
114    eq({ cwd = dirs[2], scope = 'tabpage', changed_window = false }, eval('g:ev'))
115    eq('tabpage', eval('g:amatch'))
116  end)
117
118  it('DirChanged set getcwd() during event #6260', function()
119    command('lcd ' .. dirs[1])
120    eq(dirs[1], eval('g:getcwd'))
121
122    command('tcd ' .. dirs[2])
123    eq(dirs[2], eval('g:getcwd'))
124
125    command('cd ' .. dirs[3])
126    eq(dirs[3], eval('g:getcwd'))
127
128    command('bcd ' .. dirs[4])
129    eq(dirs[4], eval('g:getcwd'))
130  end)
131
132  it('disallow recursion', function()
133    command('set shellslash')
134    -- Set up a _nested_ handler.
135    command('autocmd DirChanged * nested lcd ' .. dirs[3])
136    command('lcd ' .. dirs[1])
137    eq({ cwd = dirs[1], scope = 'window', changed_window = false }, eval('g:ev'))
138    eq(1, eval('g:cdcount'))
139    -- autocmd changed to dirs[3], but did NOT trigger another DirChanged.
140    eq(dirs[3], eval('getcwd()'))
141  end)
142
143  it('only DirChangedPre is triggered if :cd fails', function()
144    command('let g:ev = {}')
145    command('let g:cdcount = 0')
146
147    local status1, err1 = pcall(function()
148      command('lcd ' .. dirs[1] .. '/doesnotexist')
149    end)
150    eq(
151      { directory = dirs[1] .. '/doesnotexist', scope = 'window', changed_window = false },
152      eval('g:evpre')
153    )
154    eq({}, eval('g:ev'))
155    eq('window', eval('g:amatchpre'))
156    eq(1, eval('g:cdprecount'))
157    eq(0, eval('g:cdcount'))
158
159    local status2, err2 = pcall(function()
160      command('lcd ' .. dirs[2] .. '/doesnotexist')
161    end)
162    eq(
163      { directory = dirs[2] .. '/doesnotexist', scope = 'window', changed_window = false },
164      eval('g:evpre')
165    )
166    eq({}, eval('g:ev'))
167    eq('window', eval('g:amatchpre'))
168    eq(2, eval('g:cdprecount'))
169    eq(0, eval('g:cdcount'))
170
171    local status3, err3 = pcall(function()
172      command('lcd ' .. dirs[3] .. '/doesnotexist')
173    end)
174    eq(
175      { directory = dirs[3] .. '/doesnotexist', scope = 'window', changed_window = false },
176      eval('g:evpre')
177    )
178    eq({}, eval('g:ev'))
179    eq('window', eval('g:amatchpre'))
180    eq(3, eval('g:cdprecount'))
181    eq(0, eval('g:cdcount'))
182
183    eq(false, status1)
184    eq(false, status2)
185    eq(false, status3)
186
187    eq('E344:', string.match(err1, 'E%d*:'))
188    eq('E344:', string.match(err2, 'E%d*:'))
189    eq('E344:', string.match(err3, 'E%d*:'))
190  end)
191
192  it("triggered by 'autochdir'", function()
193    command('set autochdir')
194
195    command('split ' .. dirs[1] .. '/foo')
196    eq({ directory = dirs[1], scope = 'window', changed_window = false }, eval('g:evpre'))
197    eq({ cwd = dirs[1], scope = 'window', changed_window = false }, eval('g:ev'))
198    eq('auto', eval('g:amatchpre'))
199    eq('auto', eval('g:amatch'))
200    eq(1, eval('g:cdprecount'))
201    eq(1, eval('g:cdcount'))
202
203    command('split ' .. dirs[2] .. '/bar')
204    eq({ directory = dirs[2], scope = 'window', changed_window = false }, eval('g:evpre'))
205    eq({ cwd = dirs[2], scope = 'window', changed_window = false }, eval('g:ev'))
206    eq('auto', eval('g:amatch'))
207    eq(2, eval('g:cdprecount'))
208    eq(2, eval('g:cdcount'))
209  end)
210
211  it('not triggered if directory has not changed', function()
212    local scopes = { lcd = 'window', bcd = 'buffer', tcd = 'tabpage', cd = 'global' }
213    -- ":bcd" last: a buffer-local dir overrides the other scopes.
214    for i, cmd in ipairs({ 'lcd', 'tcd', 'cd', 'bcd' }) do
215      local scope = scopes[cmd]
216      command(('%s %s'):format(cmd, dirs[i]))
217      eq({ directory = dirs[i], scope = scope, changed_window = false }, eval('g:evpre'))
218      eq({ cwd = dirs[i], scope = scope, changed_window = false }, eval('g:ev'))
219      eq(scope, eval('g:amatchpre'))
220      eq(scope, eval('g:amatch'))
221      eq(i, eval('g:cdprecount'))
222      eq(i, eval('g:cdcount'))
223      assert_no_event(('%s %s'):format(cmd, dirs[i]))
224      if is_os('win') then
225        assert_no_event(('%s %s'):format(cmd, win_dirs[i]))
226      end
227    end
228
229    command('bcd!') -- 5th event: the global dir becomes effective again.
230    command('set autochdir')
231
232    command(('split %s/foo'):format(dirs[2]))
233    eq({ directory = dirs[2], scope = 'window', changed_window = false }, eval('g:evpre'))
234    eq({ cwd = dirs[2], scope = 'window', changed_window = false }, eval('g:ev'))
235    eq('auto', eval('g:amatchpre'))
236    eq('auto', eval('g:amatch'))
237    eq(6, eval('g:cdprecount'))
238    eq(6, eval('g:cdcount'))
239    assert_no_event(('split %s/bar'):format(dirs[2]))
240    if is_os('win') then
241      assert_no_event(('split %s/baz'):format(win_dirs[2]))
242    end
243  end)
244
245  it('triggered by switching to win/tab with different CWD #6054', function()
246    command('lcd ' .. dirs[3]) -- window 3
247    command('split ' .. dirs[2] .. '/foo') -- window 2
248    command('lcd ' .. dirs[2])
249    command('split ' .. dirs[1] .. '/bar') -- window 1
250    command('lcd ' .. dirs[1])
251
252    command('2wincmd w') -- window 2
253    eq({ directory = dirs[2], scope = 'window', changed_window = true }, eval('g:evpre'))
254    eq({ cwd = dirs[2], scope = 'window', changed_window = true }, eval('g:ev'))
255    eq('window', eval('g:amatchpre'))
256    eq('window', eval('g:amatch'))
257
258    eq(4, eval('g:cdprecount'))
259    eq(4, eval('g:cdcount'))
260    assert_no_event('tabnew') -- tab 2 (same CWD)
261    command('tcd ' .. dirs[3])
262    command('tabnext') -- tab 1 (no tab-local CWD)
263    eq({ directory = dirs[2], scope = 'window', changed_window = true }, eval('g:evpre'))
264    eq({ cwd = dirs[2], scope = 'window', changed_window = true }, eval('g:ev'))
265    eq('window', eval('g:amatchpre'))
266    eq('window', eval('g:amatch'))
267    command('tabnext') -- tab 2
268    eq({ directory = dirs[3], scope = 'tabpage', changed_window = true }, eval('g:evpre'))
269    eq({ cwd = dirs[3], scope = 'tabpage', changed_window = true }, eval('g:ev'))
270    eq('tabpage', eval('g:amatchpre'))
271    eq('tabpage', eval('g:amatch'))
272    eq(7, eval('g:cdprecount'))
273    eq(7, eval('g:cdcount'))
274
275    command('tabnext') -- tab 1
276    command('3wincmd w') -- window 3
277    eq(9, eval('g:cdprecount'))
278    eq(9, eval('g:cdcount'))
279    assert_no_event('tabnext') -- tab 2 (has the *same* CWD)
280
281    if is_os('win') then
282      assert_no_event('tabnew') -- tab 3 (same CWD)
283      assert_no_event('tcd ' .. win_dirs[3])
284      assert_no_event('tabnext') -- tab 1
285      assert_no_event('tabprevious') -- tab 3
286      assert_no_event('tabprevious') -- tab 2
287      assert_no_event('tabprevious') -- tab 1
288      assert_no_event('lcd ' .. win_dirs[3]) -- window 3
289      assert_no_event('tabnext') -- tab 2
290      assert_no_event('tabnext') -- tab 3
291      assert_no_event('tabnext') -- tab 1
292      assert_no_event('tabprevious') -- tab 3
293    end
294  end)
295
296  it('triggered by switching to buf/tab with different CWD', function()
297    local files = {
298      dirs[1] .. '/file',
299      dirs[2] .. '/file',
300      dirs[3] .. '/file',
301    }
302
303    command('e ' .. files[3]) -- buffer 3
304    command('e ' .. files[2]) -- buffer 2
305    command('e ' .. files[1]) -- buffer 1
306
307    command('bcd ' .. dirs[1])
308    command('b ' .. files[2]) -- Switch to buffer 2
309    command('bcd ' .. dirs[2])
310    command('b ' .. files[3]) -- Switch to buffer 3
311    command('bcd ' .. dirs[3])
312
313    eq({ directory = dirs[3], scope = 'buffer', changed_window = false }, eval('g:evpre'))
314    eq({ cwd = dirs[3], scope = 'buffer', changed_window = false }, eval('g:ev'))
315    eq('buffer', eval('g:amatchpre'))
316    eq('buffer', eval('g:amatch'))
317
318    eq(5, eval('g:cdprecount'))
319    eq(5, eval('g:cdcount'))
320    command('tabnew') -- tab 2: its new empty buffer has no local CWD, reverts to global
321    eq({ cwd = curdir, scope = 'global', changed_window = false }, eval('g:ev'))
322    eq(6, eval('g:cdprecount'))
323    eq(6, eval('g:cdcount'))
324    command('tcd ' .. dirs[2])
325    command('tabnext') -- tab 1 (no tab-local CWD)
326    eq({ directory = dirs[3], scope = 'buffer', changed_window = true }, eval('g:evpre'))
327    eq({ cwd = dirs[3], scope = 'buffer', changed_window = true }, eval('g:ev'))
328    eq('buffer', eval('g:amatchpre'))
329    eq('buffer', eval('g:amatch'))
330    command('tabnext') -- tab 2
331    eq({ directory = dirs[2], scope = 'tabpage', changed_window = true }, eval('g:evpre'))
332    eq({ cwd = dirs[2], scope = 'tabpage', changed_window = true }, eval('g:ev'))
333    eq('tabpage', eval('g:amatchpre'))
334    eq('tabpage', eval('g:amatch'))
335    eq(9, eval('g:cdprecount'))
336    eq(9, eval('g:cdcount'))
337
338    command('tabnext') -- tab 1
339    command('b ' .. files[2]) -- buffer 2
340    eq(11, eval('g:cdprecount'))
341    eq(11, eval('g:cdcount'))
342    assert_no_event('tabnext') -- tab 2 (has the *same* CWD)
343
344    if is_os('win') then
345      assert_no_event('tabnew') -- tab 3 (same CWD: inherits the tab-local CWD)
346      assert_no_event('tcd ' .. win_dirs[2])
347      assert_no_event('tabnext') -- tab 1
348      assert_no_event('tabprevious') -- tab 3
349      assert_no_event('tabprevious') -- tab 2
350      assert_no_event('tabprevious') -- tab 1
351      assert_no_event('bcd ' .. win_dirs[2]) -- buffer 2
352      assert_no_event('tabnext') -- tab 2
353      assert_no_event('tabnext') -- tab 3
354      assert_no_event('tabnext') -- tab 1
355      assert_no_event('tabprevious') -- tab 3
356    end
357  end)
358
359  it('triggered by switching to buf/win with different CWD', function()
360    command('lcd ' .. dirs[3]) -- window 3
361    command(('split %s/file'):format(dirs[2])) -- window 2
362    command('lcd ' .. dirs[2])
363    command(('split %s/file'):format(dirs[1])) -- window 1
364    command('lcd ' .. dirs[1])
365
366    -- All windows have a window-local CWD
367
368    command('2wincmd w') -- window 2
369    eq({ directory = dirs[2], scope = 'window', changed_window = true }, eval('g:evpre'))
370    eq({ cwd = dirs[2], scope = 'window', changed_window = true }, eval('g:ev'))
371    eq('window', eval('g:amatchpre'))
372    eq('window', eval('g:amatch'))
373
374    eq(4, eval('g:cdprecount'))
375    eq(4, eval('g:cdcount'))
376    command('bcd ' .. dirs[1]) -- window 2 now has a buffer with buffer-local CWD (and no window-local CWD)
377    eq({ directory = dirs[1], scope = 'buffer', changed_window = false }, eval('g:evpre'))
378    eq({ cwd = dirs[1], scope = 'buffer', changed_window = false }, eval('g:ev'))
379    eq('buffer', eval('g:amatchpre'))
380    eq('buffer', eval('g:amatch'))
381    eq(5, eval('g:cdprecount'))
382    eq(5, eval('g:cdcount'))
383    command('3wincmd w') -- window 3 (window-local CWD)
384    eq(6, eval('g:cdprecount'))
385    eq(6, eval('g:cdcount'))
386    command('bcd ' .. dirs[2]) -- window 3 now has a buffer with buffer-local CWD (and no window-local CWD)
387    command('1wincmd w') -- window 1 (window-local CWD)
388    eq({ directory = dirs[1], scope = 'window', changed_window = true }, eval('g:evpre'))
389    eq({ cwd = dirs[1], scope = 'window', changed_window = true }, eval('g:ev'))
390    eq('window', eval('g:amatchpre'))
391    eq('window', eval('g:amatch'))
392    command('2wincmd w') -- window 2 (buffer-local CWD)
393    eq({ directory = dirs[1], scope = 'window', changed_window = true }, eval('g:evpre')) -- buffer-local CWD
394    eq({ cwd = dirs[1], scope = 'window', changed_window = true }, eval('g:ev'))
395    eq('window', eval('g:amatchpre'))
396    eq('window', eval('g:amatch'))
397    eq(8, eval('g:cdprecount'))
398    eq(8, eval('g:cdcount'))
399
400    assert_no_event('1wincmd w') -- window 1 (window-local cwd)
401    -- No event: window-local CWD has higher priority than the buffer-local CWD.
402    assert_no_event(('b %s/file'):format(dirs[2])) -- buffer 2 (has buffer-local cwd)
403  end)
404
405  it('triggered by nvim_set_current_dir()', function()
406    request('nvim_set_current_dir', dirs[1])
407    eq({ directory = dirs[1], scope = 'global', changed_window = false }, eval('g:evpre'))
408    eq({ cwd = dirs[1], scope = 'global', changed_window = false }, eval('g:ev'))
409    eq(1, eval('g:cdprecount'))
410    eq(1, eval('g:cdcount'))
411
412    request('nvim_set_current_dir', dirs[2])
413    eq({ directory = dirs[2], scope = 'global', changed_window = false }, eval('g:evpre'))
414    eq({ cwd = dirs[2], scope = 'global', changed_window = false }, eval('g:ev'))
415    eq(2, eval('g:cdprecount'))
416    eq(2, eval('g:cdcount'))
417
418    eq(
419      'Vim:E344: Can\'t find directory "/doesnotexist" in cdpath',
420      t.pcall_err(request, 'nvim_set_current_dir', '/doesnotexist')
421    )
422    eq({ directory = '/doesnotexist', scope = 'global', changed_window = false }, eval('g:evpre'))
423    eq(3, eval('g:cdprecount'))
424    eq(2, eval('g:cdcount'))
425  end)
426
427  it('work when local to buffer', function()
428    command('let g:triggeredpre = 0')
429    command('let g:triggered = 0')
430    command('autocmd DirChangedPre <buffer> let g:triggeredpre = 1')
431    command('autocmd DirChanged <buffer> let g:triggered = 1')
432    command('cd ' .. dirs[1])
433    eq(1, eval('g:triggeredpre'))
434    eq(1, eval('g:triggered'))
435  end)
436end)