robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
13.8 KiB419 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each, after_each, pending =
5  t.describe, t.it, t.before_each, t.after_each, t.pending
6local eq, eval, clear, write_file, source, insert =
7  t.eq, n.eval, n.clear, t.write_file, n.source, n.insert
8local pcall_err = t.pcall_err
9local command = n.command
10local feed_command = n.feed_command
11local fn = n.fn
12local api = n.api
13local skip = t.skip
14local is_os = t.is_os
15local read_file = t.read_file
16
17local fname = 'Xtest-functional-ex_cmds-write'
18local fname_bak = fname .. '~'
19local fname_broken = fname_bak .. 'broken'
20
21describe(':write', function()
22  local function cleanup()
23    n.rmdir('Xtest_write')
24    os.remove('Xtest_bkc_file.txt')
25    os.remove('Xtest_bkc_link.txt')
26    os.remove('Xtest_fifo')
27    os.remove(fname)
28    os.remove(fname_bak)
29    os.remove(fname_broken)
30  end
31  before_each(function()
32    clear()
33    cleanup()
34  end)
35  after_each(function()
36    cleanup()
37  end)
38
39  it('&backupcopy=auto preserves symlinks', function()
40    command('set backupcopy=auto')
41    write_file('Xtest_bkc_file.txt', 'content0')
42    if is_os('win') then
43      command('silent !mklink Xtest_bkc_link.txt Xtest_bkc_file.txt')
44    else
45      command('silent !ln -s Xtest_bkc_file.txt Xtest_bkc_link.txt')
46    end
47    if eval('v:shell_error') ~= 0 then
48      pending('Cannot create symlink')
49    end
50    source([[
51      edit Xtest_bkc_link.txt
52      call setline(1, ['content1'])
53      write
54    ]])
55    eq(eval("['content1']"), eval("readfile('Xtest_bkc_file.txt')"))
56    eq(eval("['content1']"), eval("readfile('Xtest_bkc_link.txt')"))
57  end)
58
59  it('&backupcopy=no replaces symlink with new file', function()
60    command('set backupcopy=no')
61    write_file('Xtest_bkc_file.txt', 'content0')
62    if is_os('win') then
63      command('silent !mklink Xtest_bkc_link.txt Xtest_bkc_file.txt')
64    else
65      command('silent !ln -s Xtest_bkc_file.txt Xtest_bkc_link.txt')
66    end
67    if eval('v:shell_error') ~= 0 then
68      pending('Cannot create symlink')
69    end
70    source([[
71      edit Xtest_bkc_link.txt
72      call setline(1, ['content1'])
73      write
74    ]])
75    eq(eval("['content0']"), eval("readfile('Xtest_bkc_file.txt')"))
76    eq(eval("['content1']"), eval("readfile('Xtest_bkc_link.txt')"))
77  end)
78
79  it('appends FIFO file', function()
80    -- mkfifo creates read-only .lnk files on Windows
81    if is_os('win') or eval("executable('mkfifo')") == 0 then
82      pending('N/A: missing "mkfifo" command')
83    end
84
85    local text = 'some fifo text from write_spec'
86    assert(os.execute('mkfifo Xtest_fifo'))
87    insert(text)
88
89    -- Blocks until a consumer reads the FIFO.
90    feed_command('write >> Xtest_fifo')
91
92    -- Read the FIFO, this will unblock the :write above.
93    local fifo = assert(io.open('Xtest_fifo'))
94    eq(text .. '\n', fifo:read('*all'))
95    fifo:close()
96  end)
97
98  it('++p creates missing parent directories', function()
99    eq(0, eval("filereadable('p_opt.txt')"))
100    command('write ++p p_opt.txt')
101    eq(1, eval("filereadable('p_opt.txt')"))
102    os.remove('p_opt.txt')
103
104    eq(0, eval("filereadable('p_opt.txt')"))
105    command('write ++p ./p_opt.txt')
106    eq(1, eval("filereadable('p_opt.txt')"))
107    os.remove('p_opt.txt')
108
109    eq(0, eval("filereadable('Xtest_write/write/p_opt.txt')"))
110    command('write ++p Xtest_write/write/p_opt.txt')
111    eq(1, eval("filereadable('Xtest_write/write/p_opt.txt')"))
112
113    eq(0, eval("filereadable('Xtest_write/write2/p_opt.txt')"))
114    eq(0, eval("filereadable('Xtest_write/write2/p_opt2.txt')"))
115    eq(0, eval("filereadable('Xtest_write/write3/p_opt3.txt')"))
116    command('file Xtest_write/write2/p_opt.txt')
117    command('set modified')
118    command('sp Xtest_write/write2/p_opt2.txt')
119    command('set modified')
120    command('sp Xtest_write/write3/p_opt3.txt')
121    -- don't set p_opt3.txt modified - assert it isn't written
122    -- and that write3/ isn't created
123    command('wall ++p')
124    eq(1, eval("filereadable('Xtest_write/write2/p_opt.txt')"))
125    eq(1, eval("filereadable('Xtest_write/write2/p_opt2.txt')"))
126    eq(0, eval("filereadable('Xtest_write/write3/p_opt3.txt')"))
127    t.matches(
128      'E474: Invalid argument',
129      pcall_err(command, 'read ++edits Xtest_write/write/p_opt.txt')
130    )
131
132    eq('Vim(write):E32: No file name', pcall_err(command, 'write ++p Xotherdir/'))
133    eq(
134      ('Vim(write):E17: "' .. fn.fnamemodify('.', ':p:h') .. '" is a directory'),
135      pcall_err(command, 'write ++p .')
136    )
137    eq(
138      ('Vim(write):E17: "' .. fn.fnamemodify('.', ':p:h') .. '" is a directory'),
139      pcall_err(command, 'write ++p ./')
140    )
141
142    t.matches(
143      'E474: Invalid argument',
144      pcall_err(command, 'write ++patate Xtest_write/garbage.txt')
145    )
146  end)
147
148  it('errors out correctly', function()
149    command('let $HOME=""')
150    eq(fn.fnamemodify('.', ':p:h'), fn.fnamemodify('.', ':p:h:~'))
151    -- Message from check_overwrite
152    eq(
153      ('Vim(write):E17: "' .. fn.fnamemodify('.', ':p:h') .. '" is a directory'),
154      pcall_err(command, 'write .')
155    )
156    api.nvim_set_option_value('writeany', true, {})
157    -- Message from buf_write
158    eq('Vim(write):E502: "." is a directory', pcall_err(command, 'write .'))
159    fn.mkdir(fname_bak)
160    api.nvim_set_option_value('backupdir', '.', {})
161    api.nvim_set_option_value('backup', true, {})
162    write_file(fname, 'content0')
163    command('edit ' .. fname)
164    fn.setline(1, 'TTY')
165    eq("Vim(write):E510: Can't make backup file (add ! to override)", pcall_err(command, 'write'))
166    api.nvim_set_option_value('backup', false, {})
167    fn.setfperm(fname, 'r--------')
168    eq(
169      'Vim(write):E505: "Xtest-functional-ex_cmds-write" is read-only (add ! to override)',
170      pcall_err(command, 'write')
171    )
172    if is_os('win') then
173      eq(0, os.execute('del /q/f ' .. fname))
174      eq(0, os.execute('rd /q/s ' .. fname_bak))
175    else
176      eq(true, os.remove(fname))
177      eq(true, os.remove(fname_bak))
178    end
179    write_file(fname_bak, 'TTYX')
180    skip(is_os('win'), [[FIXME: pcall_err(command, 'write!') outputs 0 in Windows]])
181    vim.uv.fs_symlink(fname_bak .. ('/xxxxx'):rep(20), fname)
182    eq("Vim(write):E166: Can't open linked file for writing", pcall_err(command, 'write!'))
183  end)
184
185  it('fails converting a trailing incomplete sequence', function()
186    -- From https://github.com/neovim/neovim/issues/36990, an invalid UTF-8 sequence at the end of
187    -- the file during conversion testing can overwrite the rest of the file during the real
188    -- conversion.
189
190    api.nvim_buf_set_lines(0, 0, 1, true, { 'line 1', 'line 2', 'aaabbb\235\128' })
191    command('set noendofline nofixendofline')
192
193    eq(
194      "Vim(write):E513: Write error, conversion failed in line 3 (make 'fenc' empty to override)",
195      pcall_err(command, 'write ++enc=latin1 ' .. fname)
196    )
197  end)
198
199  it('converts to latin1 with an invalid sequence at buffer boundary', function()
200    -- From https://github.com/neovim/neovim/issues/36990, an invalid UTF-8 sequence that falls
201    -- right at the end of the 8 KiB buffer used for encoding conversions causes subsequent data to
202    -- be overwritten.
203
204    local content = string.rep('a', 1024 * 8 - 1) .. '\251' .. string.rep('b', 20)
205    api.nvim_buf_set_lines(0, 0, 1, true, { content })
206    command('set noendofline nofixendofline fenc=latin1')
207    command('write ' .. fname)
208
209    local tail = string.sub(read_file(fname) or '', -10)
210    eq('bbbbbbbbbb', tail)
211  end)
212
213  it('converts to CP1251 with iconv', function()
214    api.nvim_buf_set_lines(
215      0,
216      0,
217      1,
218      true,
219      { 'Привет, мир!', 'Это простой тест.' }
220    )
221    command('write ++enc=cp1251 ++ff=unix ' .. fname)
222
223    eq(
224      '\207\240\232\226\229\242, \236\232\240!\n'
225        .. '\221\242\238 \239\240\238\241\242\238\233 \242\229\241\242.\n',
226      read_file(fname)
227    )
228  end)
229
230  it('converts to GB18030 with iconv', function()
231    api.nvim_buf_set_lines(0, 0, 1, true, { '你好,世界!', '这是一个测试。' })
232    command('write ++enc=gb18030 ++ff=unix ' .. fname)
233
234    eq(
235      '\196\227\186\195\163\172\202\192\189\231\163\161\n'
236        .. '\213\226\202\199\210\187\184\246\178\226\202\212\161\163\n',
237      read_file(fname)
238    )
239  end)
240
241  it('converts to Shift_JIS with iconv', function()
242    api.nvim_buf_set_lines(
243      0,
244      0,
245      1,
246      true,
247      { 'こんにちは、世界!', 'これはテストです。' }
248    )
249    command('write ++enc=sjis ++ff=unix ' .. fname)
250
251    eq(
252      '\130\177\130\241\130\201\130\191\130\205\129A\144\162\138E\129I\n'
253        .. '\130\177\130\234\130\205\131e\131X\131g\130\197\130\183\129B\n',
254      read_file(fname)
255    )
256  end)
257
258  it('fails converting an illegal sequence with iconv', function()
259    api.nvim_buf_set_lines(0, 0, 1, true, { 'line 1', 'aaa\128bbb' })
260
261    eq(
262      "Vim(write):E513: Write error, conversion failed (make 'fenc' empty to override)",
263      pcall_err(command, 'write ++enc=cp1251 ' .. fname)
264    )
265  end)
266
267  it('unaffected if a BufWritePre autocmd changes CWD away from the file #41838', function()
268    -- Leaving the file's directory clears its short name; the write must fall back to the full one.
269    local dir = vim.fs.normalize(t.tmpname(false))
270    t.mkdir(dir)
271    t.mkdir(dir .. '/other')
272    local file = dir .. '/f.txt'
273    command('edit ' .. file)
274    command('lcd ' .. dir)
275    eq('f.txt', fn.bufname('%'))
276    command(('autocmd BufWritePre <buffer> lcd %s/other'):format(dir))
277    api.nvim_buf_set_lines(0, 0, -1, true, { 'hello' })
278    command('write')
279
280    eq(file, fn.bufname('%'))
281    eq({ 'hello' }, fn.readfile(file))
282  end)
283
284  it('unaffected if a Progress handler changes CWD #41417', function()
285    -- ASAN catches the read of the freed name. Also assert the written file name/contents.
286    local dir = vim.fs.normalize(t.tmpname(false))
287    t.mkdir(dir)
288    local file = dir .. '/f.txt'
289    write_file(file, 'one\n')
290    command('edit ' .. file)
291    command('lcd ' .. dir)
292    eq('f.txt', fn.bufname('%'))
293    -- Frees every buffer's short name, twice; the net CWD is unchanged.
294    command(('autocmd Progress * lcd %s | lcd %s'):format(vim.fs.dirname(dir), dir))
295    api.nvim_buf_set_lines(0, 0, -1, true, { 'one', 'two' })
296    command('write')
297
298    eq('f.txt', fn.bufname('%'))
299    eq({ 'one', 'two' }, fn.readfile(file))
300    eq({ 'f.txt' }, fn.readdir(dir))
301  end)
302
303  it('handles a multi-byte sequence crossing the buffer boundary converting with iconv', function()
304    local content = string.rep('a', 1024 * 8 - 1) .. 'Дbbbbb'
305    api.nvim_buf_set_lines(0, 0, 1, true, { content })
306    -- Skip the backup so we're testing the "checking" phase also.
307    command('set nowritebackup')
308    command('write ++enc=cp1251 ++ff=unix ' .. fname)
309
310    local expected = string.rep('a', 1024 * 8 - 1) .. '\196bbbbb\n'
311    eq(expected, read_file(fname))
312  end)
313end)
314
315describe(':update', function()
316  before_each(function()
317    clear()
318    fn.mkdir('Xtest_update', 'p')
319  end)
320
321  after_each(function()
322    fn.delete('Xtest_update', 'rf')
323  end)
324
325  it('works for a new buffer', function()
326    command('edit Xtest_update/foo/bar/nonexist.taz | update ++p')
327    eq(1, eval("filereadable('Xtest_update/foo/bar/nonexist.taz')"))
328  end)
329
330  it('writes modified buffer', function()
331    command('edit Xtest_update/modified.txt')
332    command('call setline(1, "hello world")')
333    command('update')
334    eq({ 'hello world' }, fn.readfile('Xtest_update/modified.txt'))
335  end)
336
337  it('does not write unmodified existing file', function()
338    local filename = 'Xtest_update/existing.txt'
339    local fd = io.open(filename, 'w')
340    fd:write('content')
341    fd:close()
342    local mtime_before = fn.getftime(filename)
343    command('edit ' .. filename)
344    command('update')
345    eq(mtime_before, fn.getftime(filename))
346  end)
347
348  it('creates parent directories with ++p', function()
349    command('edit Xtest_update/deep/nested/path/file.txt')
350    command('call setline(1, "content")')
351    command('update ++p')
352    eq(1, eval("filereadable('Xtest_update/deep/nested/path/file.txt')"))
353  end)
354
355  it('fails gracefully for unnamed buffer', function()
356    command('enew')
357    command('call setline(1, "some content")')
358    eq('Vim(update):E32: No file name', pcall_err(command, 'update'))
359  end)
360
361  it('respects readonly files', function()
362    local filename = 'Xtest_update/readonly.txt'
363    local fd = io.open(filename, 'w')
364    fd:write('readonly content')
365    fd:close()
366    command('edit ' .. filename)
367    command('set readonly')
368    command('call setline(1, "modified")')
369    eq(
370      "Vim(update):E45: 'readonly' option is set (add ! to override)",
371      pcall_err(command, 'update')
372    )
373
374    command('update!')
375    eq({ 'modified' }, fn.readfile('Xtest_update/readonly.txt'))
376  end)
377
378  it('can write line ranges', function()
379    command('edit Xtest_update/range.txt')
380    command('call setline(1, ["line1", "line2", "line3", "line4"])')
381    command('2,3update!')
382    eq({ 'line2', 'line3' }, fn.readfile('Xtest_update/range.txt'))
383  end)
384
385  it('can append to existing file', function()
386    local filename = 'Xtest_update/append.txt'
387    local fd = io.open(filename, 'w')
388    fd:write('existing\n')
389    fd:close()
390    command('edit Xtest_update/new_content.txt')
391    command('call setline(1, "new content")')
392    command('update >> ' .. filename)
393
394    eq({ 'existing', 'new content' }, fn.readfile('Xtest_update/append.txt'))
395  end)
396
397  it('triggers autocmds properly', function()
398    command('autocmd BufWritePre * let g:write_pre = 1')
399    command('autocmd BufWritePost * let g:write_post = 1')
400
401    command('edit Xtest_update/autocmd.txt')
402    command('call setline(1, "trigger autocmds")')
403    command('update')
404
405    eq(1, eval('g:write_pre'))
406    eq(1, eval('g:write_post'))
407  end)
408
409  it('does not write acwrite buffer when unchanged', function()
410    command('file remote://test')
411    command('setlocal buftype=acwrite')
412    command('let g:triggered = 0 | autocmd BufWriteCmd remote://* let g:triggered = 1')
413    command('update')
414    eq(0, eval('g:triggered'))
415    command('call setline(1, ["hello"])')
416    command('update')
417    eq(1, eval('g:triggered'))
418  end)
419end)