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