robin/laufey

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

robinmerge: 9a29622b3b00ddb601

master
25.5 KiB737 linesraw
1-- Specs for :cd, :tcd, :lcd and getcwd()
2
3local t = require('test.testutil')
4local n = require('test.functional.testnvim')()
5local Screen = require('test.functional.ui.screen')
6
7local describe, it, before_each, after_each = t.describe, t.it, t.before_each, t.after_each
8local eq = t.eq
9local neq = t.neq
10local exec_lua = n.exec_lua
11local pcall_err = t.pcall_err
12local call = n.call
13local clear = n.clear
14local command = n.command
15local pathsep = n.get_pathsep()
16local skip = t.skip
17local is_os = t.is_os
18local mkdir = t.mkdir
19
20-- These directories will be created for testing
21local directories = {
22  tab = 'Xtest-functional-ex_cmds-cd_spec.tab', -- Tab
23  window = 'Xtest-functional-ex_cmds-cd_spec.window', -- Window
24  buffer = 'Xtest-functional-ex_cmds-cd_spec.buffer', -- Buffer
25  global = 'Xtest-functional-ex_cmds-cd_spec.global', -- New global
26}
27
28local tmpfile = 'Xtest-functional-ex_cmds-cd_spec-tmpfile'
29local startdir ---@type string `getcwd()` at session start (set by `before_each`)
30
31local function join(...)
32  return table.concat({ ... }, pathsep)
33end
34
35-- Get the current working directory.
36local cwd = function(...)
37  return call('getcwd', ...)
38end -- effective working dir
39local wcwd = function()
40  return cwd(0)
41end -- window dir
42local bcwd = function()
43  return cwd(-1, -1, 0)
44end -- buffer dir
45local tcwd = function()
46  return cwd(-1, 0)
47end -- tab dir
48
49-- Same, except these tell us if there is a working directory at all
50local lwd = function(...)
51  return call('haslocaldir', ...)
52end -- effective working dir
53local wlwd = function()
54  return lwd(0)
55end -- window dir
56local blwd = function()
57  return lwd(-1, -1, 0)
58end -- buffer dir
59local tlwd = function()
60  return lwd(-1, 0)
61end -- tab dir
62--local glwd = function() return eval('haslocaldir(-1, -1)') end  -- global dir
63
64before_each(function()
65  clear()
66  for _, d in pairs(directories) do
67    mkdir(d)
68  end
69  startdir = cwd()
70end)
71
72after_each(function()
73  for _, d in pairs(directories) do
74    n.rmdir(d)
75  end
76end)
77
78-- Test both the `cd` and `chdir` variants
79for _, cmd in ipairs { 'cd', 'chdir' } do
80  describe(':' .. cmd, function()
81    describe('using explicit scope', function()
82      it('for window', function()
83        local globalDir = startdir
84        local globalwin = call('winnr')
85        local tabnr = call('tabpagenr')
86
87        -- Everything matches globalDir to start
88        eq(globalDir, cwd(globalwin))
89        eq(globalDir, cwd(globalwin, tabnr))
90        eq(0, lwd(globalwin))
91        eq(0, lwd(globalwin, tabnr))
92
93        command('bot split')
94        local localwin = call('winnr')
95        -- Initial window is still using globalDir
96        eq(globalDir, cwd(localwin))
97        eq(globalDir, cwd(localwin, tabnr))
98        eq(0, lwd(globalwin))
99        eq(0, lwd(globalwin, tabnr))
100
101        command('silent l' .. cmd .. ' ' .. directories.window)
102        -- From window with local dir, the original window
103        -- is still reporting the global dir
104        eq(globalDir, cwd(globalwin))
105        eq(globalDir, cwd(globalwin, tabnr))
106        eq(0, lwd(globalwin))
107        eq(0, lwd(globalwin, tabnr))
108
109        -- Window with local dir reports as such
110        eq(globalDir .. pathsep .. directories.window, cwd(localwin))
111        eq(globalDir .. pathsep .. directories.window, cwd(localwin, tabnr))
112        eq(1, lwd(localwin))
113        eq(1, lwd(localwin, tabnr))
114
115        command('tabnew')
116        -- From new tabpage, original window reports global dir
117        eq(globalDir, cwd(globalwin, tabnr))
118        eq(0, lwd(globalwin, tabnr))
119
120        -- From new tabpage, local window reports as such
121        eq(globalDir .. pathsep .. directories.window, cwd(localwin, tabnr))
122        eq(1, lwd(localwin, tabnr))
123      end)
124
125      it('for tabpage', function()
126        local globalDir = startdir
127        local globaltab = call('tabpagenr')
128
129        -- Everything matches globalDir to start
130        eq(globalDir, cwd(-1, 0))
131        eq(globalDir, cwd(-1, globaltab))
132        eq(0, lwd(-1, 0))
133        eq(0, lwd(-1, globaltab))
134
135        command('tabnew')
136        command('silent t' .. cmd .. ' ' .. directories.tab)
137        local localtab = call('tabpagenr')
138
139        -- From local tabpage, original tab reports globalDir
140        eq(globalDir, cwd(-1, globaltab))
141        eq(0, lwd(-1, globaltab))
142
143        -- new tab reports local
144        eq(globalDir .. pathsep .. directories.tab, cwd(-1, 0))
145        eq(globalDir .. pathsep .. directories.tab, cwd(-1, localtab))
146        eq(1, lwd(-1, 0))
147        eq(1, lwd(-1, localtab))
148
149        command('tabnext')
150        -- From original tabpage, local reports as such
151        eq(globalDir .. pathsep .. directories.tab, cwd(-1, localtab))
152        eq(1, lwd(-1, localtab))
153      end)
154
155      it('for buffer', function()
156        local globalDir = startdir
157        -- Create two buffers
158        command(('e %s1'):format(tmpfile))
159        command(('e %s%s%s2'):format(directories.buffer, pathsep, tmpfile))
160
161        -- Initially matches globalDir
162        eq(globalDir, cwd())
163        eq(0, lwd())
164
165        -- Change buffer-local directory to subdirectory
166        command('bcd ' .. directories.buffer)
167        eq(join(globalDir, directories.buffer), cwd())
168        eq(1, blwd())
169
170        -- ":bcd -" changes to the previous directory, still buffer-scoped.
171        command('bcd -')
172        eq(globalDir, cwd())
173        eq(1, blwd())
174        command('bcd -')
175        eq(join(globalDir, directories.buffer), cwd())
176        eq(1, blwd())
177
178        -- Verify Other buffer is unchanged
179        command('b# ')
180        eq(globalDir, cwd())
181        eq(0, blwd())
182
183        -- A new buffer created with :edit does not inherit the buffer-local directory.
184        command('b# ')
185        command(('e %s3'):format(tmpfile))
186        eq(0, blwd())
187        eq(globalDir, cwd())
188
189        -- getcwd({winnr}) reports the window's own scope chain: the buffer scope is separate, and
190        -- visible only via the {bufnr} form.
191        command(('split %s%s%s2'):format(directories.buffer, pathsep, tmpfile))
192        local bufnr = call('winbufnr', 1)
193        command('wincmd p')
194        eq(globalDir, cwd())
195        eq({ globalDir, globalDir }, { cwd(1), cwd(2) })
196        eq(join(globalDir, directories.buffer), cwd(-1, -1, bufnr))
197
198        -- The {bufnr} form skips window and tab: a buffer belongs to no particular window/tabpage.
199        command('lcd ' .. directories.window)
200        command('tcd ' .. join('..', directories.tab))
201        eq(
202          { join(globalDir, directories.tab), join(globalDir, directories.tab) },
203          { cwd(0), tcwd() }
204        )
205        eq(globalDir, cwd(-1, -1, 0))
206      end)
207    end)
208
209    describe('getcwd(-1, -1)', function()
210      it('works', function()
211        eq(startdir, cwd(-1, -1))
212        eq(0, lwd(-1, -1))
213      end)
214
215      it('with tab-local dir', function()
216        command('silent t' .. cmd .. ' ' .. directories.tab)
217        eq(startdir, cwd(-1, -1))
218        eq(0, lwd(-1, -1))
219      end)
220
221      it('with window-local dir', function()
222        command('silent l' .. cmd .. ' ' .. directories.window)
223        eq(startdir, cwd(-1, -1))
224        eq(0, lwd(-1, -1))
225      end)
226
227      it('with buffer-local dir', function()
228        command(('silent b%s %s'):format(cmd, directories.buffer))
229        eq(startdir, cwd(-1, -1))
230        eq(0, lwd(-1, -1))
231
232        -- Must behave the same if bufnr is -1
233        eq(startdir, cwd(-1, -1, -1))
234        eq(0, lwd(-1, -1, -1))
235      end)
236    end)
237
238    describe('Local directory gets inherited', function()
239      it('by tabs', function()
240        local globalDir = startdir
241
242        -- Create a new tab and change directory
243        command('tabnew')
244        command('silent t' .. cmd .. ' ' .. directories.tab)
245        eq(globalDir .. pathsep .. directories.tab, tcwd())
246
247        -- Create a new tab and verify it has inherited the directory
248        command('tabnew')
249        eq(globalDir .. pathsep .. directories.tab, tcwd())
250
251        -- Change tab and change back, verify that directories are correct
252        command('tabnext')
253        eq(globalDir, tcwd())
254        command('tabprevious')
255        eq(globalDir .. pathsep .. directories.tab, tcwd())
256      end)
257    end)
258
259    it('works', function()
260      local globalDir = startdir
261      -- Create a new tab first and verify that is has the same working dir
262      command('tabnew')
263      eq(globalDir, cwd())
264      eq(globalDir, tcwd()) -- has no tab-local directory
265      eq(0, tlwd())
266      eq(globalDir, wcwd()) -- has no window-local directory
267      eq(0, wlwd())
268
269      -- Change tab-local working directory and verify it is different
270      command('silent t' .. cmd .. ' ' .. directories.tab)
271      eq(globalDir .. pathsep .. directories.tab, cwd())
272      eq(cwd(), tcwd()) -- working directory matches tab directory
273      eq(1, tlwd())
274      eq(cwd(), wcwd()) -- still no window-directory
275      eq(0, wlwd())
276
277      -- Create a new window in this tab to test `:lcd`
278      command('new')
279      eq(1, tlwd()) -- Still tab-local working directory
280      eq(0, wlwd()) -- Still no window-local working directory
281      eq(globalDir .. pathsep .. directories.tab, cwd())
282      command('silent l' .. cmd .. ' ../' .. directories.window)
283      eq(globalDir .. pathsep .. directories.window, cwd())
284      eq(globalDir .. pathsep .. directories.tab, tcwd())
285      eq(1, wlwd())
286
287      -- Verify the first window still has the tab local directory
288      command('wincmd w')
289      eq(globalDir .. pathsep .. directories.tab, cwd())
290      eq(globalDir .. pathsep .. directories.tab, tcwd())
291      eq(0, wlwd()) -- No window-local directory
292
293      -- Change back to initial tab and verify working directory has stayed
294      command('tabnext')
295      eq(globalDir, cwd())
296      eq(0, tlwd())
297      eq(0, wlwd())
298
299      -- Verify global changes don't affect local ones
300      command('silent ' .. cmd .. ' ' .. directories.global)
301      eq(globalDir .. pathsep .. directories.global, cwd())
302      command('tabnext')
303      eq(globalDir .. pathsep .. directories.tab, cwd())
304      eq(globalDir .. pathsep .. directories.tab, tcwd())
305      eq(0, wlwd()) -- Still no window-local directory in this window
306
307      -- Unless the global change happened in a tab with local directory
308      command('silent ' .. cmd .. ' ..')
309      eq(globalDir, cwd())
310      eq(0, tlwd())
311      eq(0, wlwd())
312      -- Which also affects the first tab
313      command('tabnext')
314      eq(globalDir, cwd())
315
316      -- But not in a window with its own local directory
317      command('tabnext | wincmd w')
318      eq(globalDir .. pathsep .. directories.window, cwd())
319      eq(0, tlwd())
320      eq(globalDir .. pathsep .. directories.window, wcwd())
321    end)
322
323    it('works when mixing tab-local and buffer-local directories', function()
324      local globalDir = startdir
325
326      -- Create two buffers for testing. One in each tab
327      command(('e %s1'):format(tmpfile))
328      command('tabnew')
329      command(('e %s2'):format(tmpfile))
330
331      -- Verify that buffer 2 has the same working directory
332      eq(globalDir, cwd())
333      eq(globalDir, tcwd()) -- Has no tab-local directory
334      eq(0, tlwd())
335      eq(globalDir, bcwd()) -- Has no buffer-local directory
336      eq(0, blwd())
337
338      -- Change tab-local working directory and verify it is different
339      command(('silent t%s %s'):format(cmd, directories.tab))
340      eq(join(globalDir, directories.tab), cwd())
341      eq(cwd(), tcwd()) -- Working directory matches tab directory
342      eq(1, tlwd())
343      eq(globalDir, bcwd()) -- Still no buffer-directory: the buffer form skips the tab scope
344      eq(0, blwd())
345
346      -- Change buffer 2's buffer-local directory
347      command(('silent b%s ..%s%s'):format(cmd, pathsep, directories.buffer))
348      eq(join(globalDir, directories.buffer), cwd())
349      eq(join(globalDir, directories.tab), tcwd())
350      eq(cwd(), bcwd()) -- Has no buffer-directory
351      eq(1, blwd())
352
353      -- Verify the first tab has no local-directory
354      command('tabfirst')
355      eq(0, tlwd())
356
357      -- Verify buffer 2 has buffer-local directory even in first tab
358      command(('b %s2'):format(tmpfile)) -- Switch to buffer 2
359      eq(join(globalDir, directories.buffer), bcwd())
360      eq(0, tlwd()) -- Still no tab-local directory
361
362      -- Verify buffer 1 did not have its directory changed
363      command('b#') -- Switch to buffer 1
364      eq(globalDir, cwd())
365      eq(0, blwd()) -- No window-buffer directory
366    end)
367    it('works when mixing window local and buffer local directories', function()
368      local globalDir = startdir
369      -- Create a new window first and verify that is has the same working directory
370      command('new')
371      eq(globalDir, cwd())
372      eq(globalDir, wcwd()) -- Has no window-local directory
373      eq(0, tlwd())
374      eq(globalDir, bcwd()) -- Has no buffer-local directory
375      eq(0, blwd())
376
377      -- Create a buffer in current window
378      command('e ' .. tmpfile)
379
380      -- Change buffer-local working directory and verify it is different
381      command(('silent b%s %s'):format(cmd, directories.buffer))
382      eq(join(globalDir, directories.buffer), cwd())
383      eq(cwd(), bcwd()) -- Working directory matches buffer directory
384      eq(1, blwd())
385      eq(globalDir, wcwd()) -- Still no window-directory
386      eq(0, wlwd())
387
388      -- :lcd sets the window-local dir WITHOUT clearing the narrower buffer-local one.
389      command(('silent l%s ../%s'):format(cmd, directories.window))
390      eq(join(globalDir, directories.buffer), cwd())
391      eq({ 1, 1 }, { blwd(), wlwd() })
392      eq(join(globalDir, directories.window), wcwd())
393
394      -- Window-local dir applies if the buffer-local one is unset.
395      command(('silent b%s!'):format(cmd))
396      eq(0, blwd())
397      eq(join(globalDir, directories.window), cwd())
398
399      -- Verify other window is unaffected.
400      command('wincmd w')
401      command('b ' .. tmpfile)
402      eq(globalDir, cwd())
403      command('wincmd w')
404      eq(join(globalDir, directories.window), cwd())
405    end)
406  end)
407end
408
409for _, cmd in ipairs { 'bcd', 'bchdir' } do
410  describe(':' .. cmd, function()
411    it('works after deleting the only buffer', function()
412      command(('%s %s'):format(cmd, directories.buffer))
413      command('bd') -- delete buffer
414    end)
415
416    it('buffer-local directory is NOT sticky/inherited', function()
417      local bufdir = join(startdir, directories.buffer)
418
419      command('edit ' .. tmpfile)
420      command(('%s %s'):format(cmd, directories.buffer))
421      eq(bufdir, cwd())
422
423      -- A new buffer starts without a buffer-local directory.
424      command('new')
425      eq(startdir, cwd())
426      eq(0, blwd())
427      command('close')
428      eq(bufdir, cwd())
429      command('enew')
430      eq(startdir, cwd())
431      eq(0, blwd())
432      command('b# ')
433      eq(bufdir, cwd())
434
435      -- Recycling an empty unnamed buffer (:edit) drops its directory with it.
436      command('enew')
437      command(('%s %s'):format(cmd, directories.buffer))
438      eq(bufdir, cwd())
439      command('edit ' .. tmpfile .. '2')
440      eq(startdir, cwd())
441      eq(0, blwd())
442
443      -- But re-editing the same buffer keeps it, like any other buffer-local state. #41213
444      command(('%s %s'):format(cmd, directories.buffer))
445      command('let b:kept = 1')
446      command('edit!')
447      eq({ 1, bufdir, 1 }, { blwd(), cwd(), n.eval('get(b:, "kept", 0)') })
448    end)
449
450    it('is not cleared or overridden by :lcd/:tcd/:cd', function()
451      local bufdir = join(startdir, directories.buffer)
452
453      command('edit ' .. tmpfile)
454      command(('%s %s'):format(cmd, directories.buffer))
455      -- Paths are relative to `bufdir`, which stays in effect throughout.
456      command('lcd ' .. join('..', directories.window))
457      eq({ 1, bufdir, join(startdir, directories.window) }, { blwd(), cwd(), wcwd() })
458
459      command('tcd ' .. join('..', directories.tab))
460      eq({ 1, bufdir, join(startdir, directories.tab) }, { blwd(), cwd(), tcwd() })
461
462      command('cd ..')
463      eq({ 1, bufdir, startdir }, { blwd(), cwd(), cwd(-1, -1) })
464
465      -- The overridden ":cd" still reset the window/tab scopes, so ":bcd!" lands on the global dir.
466      command(('%s!'):format(cmd))
467      eq({ 0, 0, 0, startdir }, { blwd(), wlwd(), tlwd(), cwd() })
468    end)
469  end)
470end
471
472describe(':lcd!/:tcd!/:bcd! (bang)', function()
473  it('clear only their own scope', function()
474    local bufdir = join(startdir, directories.buffer)
475    local windir = join(startdir, directories.window)
476    local tabdir = join(startdir, directories.tab)
477
478    command('tcd ' .. directories.tab)
479    command('lcd ' .. join('..', directories.window))
480    command('bcd ' .. join('..', directories.buffer))
481    eq({ 1, 1, 1 }, { blwd(), wlwd(), tlwd() })
482    eq(bufdir, cwd())
483
484    command('bcd!') -- Buffer scope gone: the window-local dir applies.
485    eq({ 0, 1, 1 }, { blwd(), wlwd(), tlwd() })
486    eq(windir, cwd())
487
488    command('lcd!') -- Window scope gone: the tab-local dir applies.
489    eq({ 0, 0, 1 }, { blwd(), wlwd(), tlwd() })
490    eq(tabdir, cwd())
491
492    command('tcd!') -- Tab scope gone: back to the global dir.
493    eq({ 0, 0, 0 }, { blwd(), wlwd(), tlwd() })
494    eq(startdir, cwd())
495
496    command('lcd!') -- No-op when the scope has no local directory.
497    eq(startdir, cwd())
498
499    -- :cd! does not unset.
500    command('lcd ' .. directories.window)
501    command('cd! ' .. join('..', directories.global))
502    eq(join(startdir, directories.global), cwd())
503    eq(0, wlwd()) -- ":cd" cleared it, as always.
504
505    -- Legacy: bang WITH arg ":lcd! {path}" is just ":lcd {path}".
506    command('lcd! ' .. join('..', directories.window))
507    eq({ 1, join(startdir, directories.window) }, { wlwd(), cwd() })
508  end)
509end)
510
511describe('cd during temp context-switch', function()
512  it(':bcd/:tcd/:lcd persists in target scope, does not leak into original context', function()
513    local exec_lua = n.exec_lua
514    local bufdir = join(startdir, directories.buffer)
515    local windir = join(startdir, directories.window)
516    local tabdir = join(startdir, directories.tab)
517
518    --- Creates a loaded, hidden buffer.
519    local function hidden_buf(name)
520      local b = call('bufadd', name)
521      call('bufload', b)
522      return b
523    end
524
525    --- Runs `vim.cmd[cmd](dir)` with buffer `b` as temporary curbuf.
526    local function cd_in_buf_call(b, cmd, dir)
527      exec_lua(function(b_, cmd_, d)
528        vim.api.nvim_buf_call(b_, function()
529          vim.cmd[cmd_](d)
530        end)
531      end, b, cmd, dir)
532    end
533
534    -- :bcd on a hidden buffer via nvim_buf_call() persists; the caller's cwd is unchanged.
535    local hidden = hidden_buf('Xtest-cd-hidden')
536    cd_in_buf_call(hidden, 'bcd', bufdir)
537    eq({ 1, bufdir, startdir }, { lwd(-1, -1, hidden), cwd(-1, -1, hidden), cwd() })
538
539    -- :lcd targets the temporary window, which is discarded; the caller's cwd is unchanged.
540    cd_in_buf_call(hidden, 'lcd', windir)
541    eq({ 0, startdir }, { wlwd(), cwd() })
542
543    -- :lcd via win_execute() persists on the target window; the CWD outside it is unchanged.
544    command('split')
545    call('win_execute', call('win_getid', 2), ('lcd %s'):format(windir))
546    eq({ 1, windir, startdir }, { lwd(2), cwd(2), cwd() })
547    command('only')
548
549    -- Entering a hidden buffer must not consume the global dir.
550    command('lcd ' .. windir)
551    exec_lua(function(b)
552      vim.api.nvim_buf_call(b, function() end)
553    end, hidden)
554    eq({ windir, startdir }, { cwd(), cwd(-1, -1) })
555    command('lcd!')
556
557    -- An autocmd handler targeting a hidden buffer can set its buffer-local dir; the caller's
558    -- cwd is unchanged.
559    local hidden2 = hidden_buf('Xtest-cd-hidden2')
560    exec_lua(function(b, d)
561      vim.api.nvim_create_autocmd('TermRequest', {
562        buffer = b,
563        once = true,
564        callback = function()
565          vim.cmd.bcd(d)
566        end,
567      })
568      vim.api.nvim_exec_autocmds('TermRequest', { buffer = b, data = { sequence = 'x' } })
569    end, hidden2, bufdir)
570    eq({ 1, bufdir, startdir }, { lwd(-1, -1, hidden2), cwd(-1, -1, hidden2), cwd() })
571
572    -- :tcd targets the tabpage, shared with the temp context: it persists and changes the caller's
573    -- cwd. `globaldir` (the pre-switch cwd, set by :tcd) is kept, so :tcd! can return to it.
574    cd_in_buf_call(hidden, 'tcd', tabdir)
575    eq({ 1, tabdir, tabdir, startdir }, { tlwd(), tcwd(), cwd(), cwd(-1, -1) })
576  end)
577
578  it('buffer names/statusline updated after switch back #41424', function()
579    local screen = Screen.new(40, 8)
580    local bufdir = join(startdir, directories.buffer)
581    command('edit ' .. join(bufdir, tmpfile))
582    command('bcd ' .. bufdir)
583    command('split Xtest-cd-other') -- Buffer with no local dir.
584    local otherwin = call('win_getid')
585    command('wincmd p')
586    eq({ bufdir, tmpfile }, { cwd(), call('bufname', '%') })
587
588    -- 'statusline' shows buffer names, so it must be redrawn exactly when they change.
589    exec_lua([[
590      _G.evals = 0
591      function _G.stl()
592        _G.evals = _G.evals + 1
593        return 'STL'
594      end
595      vim.o.laststatus = 2
596      vim.o.statusline = '%!v:lua.stl()'
597    ]])
598    screen:expect({ any = 'STL' })
599
600    -- Entering a window leaves `bufdir`, since the buffer there has no local dir.
601    exec_lua(
602      [[
603      vim._with({ win = ... }, function()
604        vim.cmd('split | close')
605        vim.api.nvim__redraw({ flush = true }) -- Paints the names relative to the other CWD.
606        _G.inner = _G.evals
607      end)
608      vim.api.nvim__redraw({ flush = true })
609    ]],
610      otherwin
611    )
612
613    -- Names relative to the old CWD would cause ":write" to target nonsense.
614    eq({ bufdir, tmpfile }, { cwd(), call('bufname', '%') })
615    command('write')
616
617    -- The switch moved the CWD, so the names changed and back: 'statusline' follows them.
618    neq(exec_lua('return _G.inner'), exec_lua('return _G.evals'))
619
620    -- Without a local dir the switch doesn't move the CWD, so nothing needs a redraw. #41561
621    command('bcd!')
622    exec_lua('vim.api.nvim__redraw({ flush = true })')
623    local evals = exec_lua('return _G.evals')
624    call('win_execute', otherwin, 'let g:x = 1')
625    exec_lua('vim.api.nvim__redraw({ flush = true })')
626    eq(evals, exec_lua('return _G.evals'))
627  end)
628
629  it("nvim_open_win / nvim_win_set_buf keep the caller's cwd", function()
630    local bufdir = join(startdir, directories.buffer)
631    command('bcd ' .. bufdir)
632    eq(bufdir, cwd())
633
634    -- A transient switch to a scratch float must not reset the caller's cwd.
635    local float = call('nvim_open_win', call('nvim_create_buf', true, true), false, {
636      relative = 'editor',
637      width = 20,
638      height = 5,
639      row = 1,
640      col = 1,
641    })
642    eq(bufdir, cwd())
643
644    call('nvim_win_set_buf', float, call('nvim_create_buf', true, true))
645    eq(bufdir, cwd())
646
647    call('nvim_win_close', float, true)
648    eq(bufdir, cwd())
649    command('bcd!')
650
651    -- Switching to a window with no local dir must not consume the global dir. #41238
652    local windir = join(startdir, directories.window)
653    command('split')
654    command('lcd ' .. windir)
655    call('nvim_win_set_buf', call('win_getid', 2), call('nvim_create_buf', true, true))
656    eq({ windir, startdir }, { cwd(), cwd(-1, -1) })
657    command('wincmd w')
658    eq({ startdir, startdir }, { cwd(), cwd(-1, -1) })
659    command('only')
660
661    -- Setting a :bcd buffer into another window, should not modify the caller's CWD.
662    local bcdbuf = call('nvim_create_buf', true, true)
663    n.exec_lua(function(b, d)
664      vim.api.nvim_buf_call(b, function()
665        vim.cmd.bcd(d)
666      end)
667    end, bcdbuf, bufdir)
668    command('split')
669    call('nvim_win_set_buf', call('win_getid', 2), bcdbuf)
670    eq({ bufdir, startdir, startdir }, { cwd(-1, -1, bcdbuf), cwd(), cwd(-1, -1) })
671    command('only')
672  end)
673end)
674
675-- Test legal parameters for 'getcwd' and 'haslocaldir'
676for _, cmd in ipairs { 'getcwd', 'haslocaldir' } do
677  describe(cmd .. '()', function()
678    it('validation', function()
679      local err474 = 'Vim:E474: Invalid argument'
680      eq(err474, pcall_err(call, cmd, 'some string'))
681      eq(err474, pcall_err(call, cmd, 1.5))
682      eq(err474, pcall_err(call, cmd, { 1, 2 }))
683      eq(err474, pcall_err(call, cmd, { key = 'value' }))
684      eq(err474, pcall_err(call, cmd, -2))
685      -- Funcref is not representable over RPC.
686      eq(
687        'Vim(call):E474: Invalid argument',
688        pcall_err(command, ('call %s(function("tr"))'):format(cmd))
689      )
690
691      -- -1 preceded by an argument >= 0
692      local err5001 = 'Vim:E5001: Argument cannot be -1 if preceding argument is >= 0.'
693      eq(err5001, pcall_err(call, cmd, 0, -1))
694      eq(err5001, pcall_err(call, cmd, 2, 3, -1))
695      eq(err5001, pcall_err(call, cmd, -1, 0, -1))
696      eq(err5001, pcall_err(call, cmd, 0, -1, 0))
697      -- Buffer scope requires window and tab args to be -1.
698      local err5006 = 'Vim:E5006: Window and tab scope must be -1 when using buffer scope'
699      eq(err5006, pcall_err(call, cmd, 0, 0, 0))
700      eq(err5006, pcall_err(call, cmd, 1, 2, 3))
701      eq('Vim:E5007: Cannot find buffer number.', pcall_err(call, cmd, -1, -1, 99999))
702      eq(
703        ('Vim:E118: Too many arguments for function: %s'):format(cmd),
704        pcall_err(call, cmd, 0, 0, 0, 0)
705      )
706    end)
707  end)
708end
709
710describe('getcwd()', function()
711  it('returns empty string if working directory does not exist', function()
712    skip(is_os('win'), 'N/A for Windows')
713    command('cd ' .. directories.global)
714    command("call delete('../" .. directories.global .. "', 'd')")
715    eq('', n.eval('getcwd()'))
716  end)
717
718  it("works with 'autochdir' after local directory was set (#9892)", function()
719    local curdir = cwd()
720    command('lcd ' .. directories.global)
721    command('lcd -')
722    command('set autochdir')
723    command('edit ' .. directories.global .. '/foo')
724    eq(curdir .. pathsep .. directories.global, cwd())
725    eq(curdir, wcwd())
726    call('mkdir', 'bar')
727    command('edit ' .. 'bar/foo')
728    eq(curdir .. pathsep .. directories.global .. pathsep .. 'bar', cwd())
729    eq(curdir, wcwd())
730    command('lcd ..')
731    eq(curdir .. pathsep .. directories.global, cwd())
732    eq(curdir .. pathsep .. directories.global, wcwd())
733    command('edit')
734    eq(curdir .. pathsep .. directories.global .. pathsep .. 'bar', cwd())
735    eq(curdir .. pathsep .. directories.global, wcwd())
736  end)
737end)