robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
13.9 KiB439 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4
5local describe, it, before_each, after_each, finally =
6  t.describe, t.it, t.before_each, t.after_each, t.finally
7local clear = n.clear
8local command = n.command
9local get_pathsep = n.get_pathsep
10local eq = t.eq
11local neq = t.neq
12local fn = n.fn
13local matches = t.matches
14local pesc = vim.pesc
15local rmdir = n.rmdir
16local sleep = vim.uv.sleep
17local api = n.api
18local skip = t.skip
19local is_os = t.is_os
20local mkdir = t.mkdir
21
22local file_prefix = 'Xtest-functional-ex_cmds-mksession_spec'
23
24describe(':mksession', function()
25  local session_file = file_prefix .. '.vim'
26  local tab_dir = file_prefix .. '.tab.d'
27  local buf_dir = file_prefix .. '.buf.d'
28
29  before_each(function()
30    clear()
31    mkdir(tab_dir)
32    mkdir(buf_dir)
33  end)
34
35  after_each(function()
36    os.remove(session_file)
37    rmdir(tab_dir)
38    rmdir(buf_dir)
39  end)
40
41  it('restores same :terminal buf in splits', function()
42    -- If the same :terminal is displayed in multiple windows, :mksession
43    -- should restore it as such.
44
45    -- Create three windows: first two from top show same terminal, third -
46    -- another one (created earlier).
47    command('terminal')
48    command('split')
49    command('terminal')
50    command('split')
51    command('mksession ' .. session_file)
52    command('%bwipeout!')
53
54    -- Create a new test instance of Nvim.
55    clear()
56    -- Restore session.
57    command('source ' .. session_file)
58
59    eq(fn.winbufnr(1), fn.winbufnr(2))
60    neq(fn.winbufnr(1), fn.winbufnr(3))
61  end)
62
63  -- common testing procedure for testing "sessionoptions-=terminal"
64  local function test_terminal_session_disabled(expected_buf_count)
65    command('set sessionoptions-=terminal')
66
67    command('mksession ' .. session_file)
68
69    -- Create a new test instance of Nvim.
70    clear()
71
72    -- Restore session.
73    command('source ' .. session_file)
74
75    eq(expected_buf_count, #api.nvim_list_bufs())
76  end
77
78  it('do not restore :terminal if not set in sessionoptions, terminal in curwin #13078', function()
79    local tmpfile_base = file_prefix .. '-tmpfile'
80    command('edit ' .. tmpfile_base)
81    command('terminal')
82
83    local buf_count = #api.nvim_list_bufs()
84    eq(2, buf_count)
85
86    eq('terminal', api.nvim_get_option_value('buftype', {}))
87
88    test_terminal_session_disabled(2)
89
90    -- no terminal should be set. As a side effect we end up with a blank buffer
91    eq('', api.nvim_get_option_value('buftype', { buf = api.nvim_list_bufs()[1] }))
92    eq('', api.nvim_get_option_value('buftype', { buf = api.nvim_list_bufs()[2] }))
93  end)
94
95  it('do not restore :terminal if not set in sessionoptions, terminal hidden #13078', function()
96    command('terminal')
97    local terminal_bufnr = api.nvim_get_current_buf()
98
99    local tmpfile_base = file_prefix .. '-tmpfile'
100    -- make terminal hidden by opening a new file
101    command('edit ' .. tmpfile_base .. '1')
102
103    local buf_count = #api.nvim_list_bufs()
104    eq(2, buf_count)
105
106    eq(1, fn.getbufinfo(terminal_bufnr)[1].hidden)
107
108    test_terminal_session_disabled(1)
109
110    -- no terminal should exist here
111    neq('', api.nvim_buf_get_name(api.nvim_list_bufs()[1]))
112  end)
113
114  it('do not restore :terminal if not set in sessionoptions, only buffer #13078', function()
115    command('terminal')
116    eq('terminal', api.nvim_get_option_value('buftype', {}))
117
118    local buf_count = #api.nvim_list_bufs()
119    eq(1, buf_count)
120
121    test_terminal_session_disabled(1)
122
123    -- no terminal should be set
124    eq('', api.nvim_get_option_value('buftype', {}))
125  end)
126
127  it('restores tab-local working directories', function()
128    local tmpfile_base = file_prefix .. '-tmpfile'
129    local cwd_dir = fn.getcwd()
130
131    -- :mksession does not save empty tabs, so create some buffers.
132    command('edit ' .. tmpfile_base .. '1')
133    command('tabnew')
134    command('edit ' .. tmpfile_base .. '2')
135    command('tcd ' .. tab_dir)
136    command('tabfirst')
137    command('mksession ' .. session_file)
138
139    -- Create a new test instance of Nvim.
140    clear()
141
142    command('source ' .. session_file)
143    -- First tab should have the original working directory.
144    command('tabnext 1')
145    eq(cwd_dir, fn.getcwd())
146    -- Second tab should have the tab-local working directory.
147    command('tabnext 2')
148    eq(cwd_dir .. get_pathsep() .. tab_dir, fn.getcwd())
149  end)
150
151  it('restores buffers with tab-local CWD', function()
152    local tmpfile_base = file_prefix .. '-tmpfile'
153    local cwd_dir = vim.fs.normalize(fn.getcwd())
154    local session_path = cwd_dir .. get_pathsep() .. session_file
155
156    command('edit ' .. tmpfile_base .. '1')
157    command('tcd ' .. tab_dir)
158    command('tabnew')
159    command('edit ' .. cwd_dir .. get_pathsep() .. tmpfile_base .. '2')
160    command('tabfirst')
161    command('mksession ' .. session_path)
162
163    -- Create a new test instance of Nvim.
164    clear()
165
166    -- Use :silent to avoid hit-enter prompt due to long path
167    command('silent source ' .. session_path)
168    command('tabnext 1')
169    eq(('%s/%s1'):format(cwd_dir, tmpfile_base), vim.fs.normalize(fn.expand('%:p')))
170    command('tabnext 2')
171    eq(('%s/%s2'):format(cwd_dir, tmpfile_base), vim.fs.normalize(fn.expand('%:p')))
172  end)
173
174  it('restores symlinked directory buffer names', function()
175    skip(is_os('win'), 'N/A for Windows')
176
177    local cwd_dir = t.fix_slashes(fn.getcwd())
178    local link_dir = file_prefix .. '-link'
179    assert(vim.uv.fs_symlink(assert(vim.uv.fs_realpath(tab_dir)), link_dir, { dir = true }))
180    finally(function()
181      os.remove(link_dir)
182    end)
183
184    command('set sessionoptions=buffers')
185    command('edit ' .. link_dir)
186    local expected = cwd_dir .. '/' .. link_dir .. '/'
187    eq(expected, api.nvim_buf_get_name(0))
188    command('mksession ' .. session_file)
189    command('%bwipeout!')
190    command('source ' .. session_file)
191    eq(expected, api.nvim_buf_get_name(0))
192  end)
193
194  it('restores a directory buffer for the CWD #40939', function()
195    local cwd_dir = t.fix_slashes(fn.getcwd())
196    local expected = cwd_dir .. '/'
197
198    command('set sessionoptions=buffers,curdir')
199    command('edit ' .. cwd_dir)
200    command('cd ' .. cwd_dir)
201    neq('', fn.bufname('%'))
202    eq(expected, api.nvim_buf_get_name(0))
203
204    command('mksession ' .. session_file)
205    command('%bwipeout!')
206    command('source ' .. session_file)
207    eq(expected, api.nvim_buf_get_name(0))
208  end)
209
210  it('restores CWD for :terminal buffers #11288', function()
211    local cwd_dir = fn.fnamemodify('.', ':p:~'):gsub([[/*$]], '')
212    local session_path = cwd_dir .. '/' .. session_file
213
214    command('cd ' .. tab_dir)
215    command('terminal')
216    command('cd ' .. cwd_dir)
217    command('mksession ' .. session_path)
218    command('sleep 5m | %bwipeout!')
219    if is_os('win') then
220      sleep(100) -- Make sure all child processes have exited.
221    end
222
223    -- Create a new test instance of Nvim.
224    clear()
225    command('silent source ' .. session_path)
226
227    local expected_cwd = cwd_dir .. '/' .. tab_dir
228    matches('^term://' .. pesc(expected_cwd) .. '//%d+:', fn.expand('%'))
229    -- Wait some time for the PTY process to call setsid() and chdir(), otherwise
230    -- SIGHUP may be sent to the parent, or after_each() may run before chdir().
231    command('sleep 5m | %bwipeout!')
232    if is_os('win') then
233      sleep(100) -- Make sure all child processes have exited.
234    end
235  end)
236
237  it('restores CWD for :terminal buffer at root directory #16988', function()
238    skip(is_os('win'), 'N/A for Windows')
239
240    local screen
241    local cwd_dir = fn.fnamemodify('.', ':p:~'):gsub([[[\/]*$]], '')
242    local session_path = cwd_dir .. '/' .. session_file
243
244    screen = Screen.new(50, 6, { rgb = false })
245    local expected_screen = [[
246      ^/                                                 |
247                                                        |
248      [Process exited 0]                                |
249                                                        |*3
250    ]]
251
252    command('cd /')
253    command('terminal echo $PWD')
254
255    -- Verify that the terminal's working directory is "/".
256    screen:expect(expected_screen)
257
258    command('cd ' .. cwd_dir)
259    command('mksession ' .. session_path)
260    command('%bwipeout!')
261
262    -- Create a new test instance of Nvim.
263    clear()
264    screen = Screen.new(50, 6, { rgb = false })
265    command('silent source ' .. session_path)
266
267    -- Verify that the terminal's working directory is "/".
268    screen:expect(expected_screen)
269  end)
270
271  it('restores a session when there is a float #18432', function()
272    local tmpfile = file_prefix .. '-tmpfile-float'
273
274    command('edit ' .. tmpfile)
275    eq(80, fn.winwidth(1))
276    command('30vsplit')
277    eq(2, #api.nvim_list_wins())
278    eq(30, fn.winwidth(1))
279    eq(49, fn.winwidth(2))
280    local buf = api.nvim_create_buf(false, true)
281    local config = {
282      relative = 'editor',
283      focusable = false,
284      width = 10,
285      height = 3,
286      row = 0,
287      col = 1,
288      style = 'minimal',
289    }
290    api.nvim_open_win(buf, false, config)
291    eq(3, #api.nvim_list_wins())
292    local cmdheight = api.nvim_get_option_value('cmdheight', {})
293    command('mksession ' .. session_file)
294
295    -- Create a new test instance of Nvim.
296    clear()
297
298    command('source ' .. session_file)
299
300    eq(tmpfile, fn.expand('%'))
301    -- Check that there are only two windows, which indicates the floating
302    -- window was not restored.
303    -- Don't use winnr('$') as that doesn't count unfocusable floating windows.
304    eq(2, #api.nvim_list_wins())
305    eq(30, fn.winwidth(1))
306    eq(49, fn.winwidth(2))
307    -- The command-line height should remain the same as it was.
308    eq(cmdheight, api.nvim_get_option_value('cmdheight', {}))
309
310    os.remove(tmpfile)
311  end)
312
313  it('fires session write autocmds only for :mksession', function()
314    command('let g:session_write_events = []')
315    command("autocmd SessionWritePre * call add(g:session_write_events, 'pre')")
316    command("autocmd SessionWritePost * call add(g:session_write_events, 'post')")
317    for _, cmd in ipairs({ 'mkview', 'mkvimrc', 'mkexrc' }) do
318      command(cmd .. '! ' .. session_file)
319      eq({}, api.nvim_eval('g:session_write_events'), cmd)
320    end
321    command('mksession! ' .. session_file)
322    eq({ 'pre', 'post' }, api.nvim_eval('g:session_write_events'))
323  end)
324
325  it('SessionWritePre handles editing buffer contents', function()
326    local tmpfile = file_prefix .. '-tmpfile-float'
327    command('edit ' .. tmpfile)
328    command("autocmd SessionWritePre * call append(0, 'foo') | write")
329    command('mksession ' .. session_file)
330
331    clear()
332    command('source ' .. session_file)
333    eq({ 'foo', '' }, api.nvim_buf_get_lines(0, 0, -1, true))
334    os.remove(tmpfile)
335  end)
336
337  it('SessionWritePre handles switching buffers', function()
338    command('autocmd SessionWritePre * new')
339    command('mksession ' .. session_file)
340
341    clear()
342    command('source ' .. session_file)
343    -- both buffers are saved
344    eq(2, #api.nvim_list_bufs())
345  end)
346
347  it('SessionWritePre handles buffer removal', function()
348    -- :bdelete and :bwipeout differ for "live" Nvim but are equivalent
349    -- from :mksession's perspective, so one test covers both.
350    api.nvim_buf_set_name(0, 'foo')
351    command('autocmd SessionWritePre * bwipeout!')
352    command('mksession ' .. session_file)
353
354    clear()
355    command('source ' .. session_file)
356    -- gone for good
357    eq(
358      nil,
359      vim.iter(api.nvim_list_bufs()):find(function(b)
360        return api.nvim_buf_get_name(b) == 'foo'
361      end)
362    )
363  end)
364
365  it('SessionWritePre handles splits', function()
366    command('autocmd SessionWritePre * split | vsplit')
367    command('mksession ' .. session_file)
368
369    clear()
370    command('source ' .. session_file)
371    -- contains all splits
372    eq(3, #api.nvim_tabpage_list_wins(0))
373  end)
374
375  it('restores buffer-local working directories', function()
376    -- "'" and " " in the names must survive the session file's string quoting and escaping.
377    local tmpfile_base = file_prefix .. "-tmp'file"
378    local quote_dir = file_prefix .. ".b'uf d"
379    local session_file2 = session_file .. '2'
380    local cwd_dir = fn.getcwd()
381    mkdir(quote_dir)
382    finally(function()
383      rmdir(quote_dir)
384      os.remove(session_file2)
385    end)
386
387    command(('edit %s1'):format(tmpfile_base))
388    command(('edit %s2'):format(tmpfile_base))
389    command('bcd ' .. fn.fnameescape(quote_dir))
390    command('b# ')
391    command('mksession ' .. session_file)
392    command('set sessionoptions-=curdir')
393    command('mksession ' .. session_file2)
394
395    -- Create a new test instance of Nvim.
396    clear()
397
398    command('silent source ' .. session_file)
399    command(('b %s%s2'):format(get_pathsep(), tmpfile_base))
400    eq(('%s%s%s'):format(cwd_dir, get_pathsep(), quote_dir), fn.getcwd())
401
402    -- Without "curdir" in 'sessionoptions' no buffer-local directory is restored.
403    clear()
404    command('silent source ' .. session_file2)
405    command(('b %s%s2'):format(get_pathsep(), tmpfile_base))
406    eq(cwd_dir, fn.getcwd())
407    eq(0, fn.haslocaldir(-1, -1, 0))
408  end)
409
410  it('restores buffer-local working directories when used with tabs', function()
411    local tmpfile_base = file_prefix .. '-tmpfile'
412    local cwd_dir = fn.getcwd()
413
414    command(('edit %s1'):format(tmpfile_base))
415    command('tabnew ')
416    command(('edit %s2'):format(tmpfile_base))
417    command('tcd ' .. tab_dir)
418    command('bcd ..')
419    command('bcd ' .. buf_dir)
420    command('tabfirst')
421    command('mksession ' .. session_file)
422
423    -- Create a new test instance of Nvim.
424    clear()
425
426    command('source ' .. session_file)
427    -- The ":tcd" during restore must not clear the buffer-local directory of the tab's
428    -- first-loaded buffer: it must be set before that tab is ever (re)visited.
429    eq(1, fn.haslocaldir(-1, -1, fn.bufnr(('%s2'):format(tmpfile_base))))
430    -- First tab should have the original working directory.
431    command('tabnext 1')
432    eq(cwd_dir, fn.getcwd())
433    -- Second tab should have the tab-local working directory.
434    command('tabnext 2')
435    eq(('%s%s%s'):format(cwd_dir, get_pathsep(), tab_dir), fn.getcwd(-1, 0))
436    -- Buffer in second tab should have buffer-local working directory
437    eq(('%s%s%s'):format(cwd_dir, get_pathsep(), buf_dir), fn.getcwd(-1, -1, 0))
438  end)
439end)