comparison src/testdir/test_writefile.vim @ 20625:116c7bd5e980 v8.2.0866

patch 8.2.0866: not enough tests for buffer writing Commit: https://github.com/vim/vim/commit/494e9069cb32620f7688a7cb128a3feff827639e Author: Bram Moolenaar <Bram@vim.org> Date: Sun May 31 21:28:02 2020 +0200 patch 8.2.0866: not enough tests for buffer writing Problem: Not enough tests for buffer writing. Solution: Add more tests. Use CheckRunVimInTerminal in more places. (Yegappan Lakshmanan, closes #6167)
author Bram Moolenaar <Bram@vim.org>
date Sun, 31 May 2020 21:30:03 +0200
parents 2fb397573541
children d8628d75c47a
comparison
equal deleted inserted replaced
20624:f782c48d548a 20625:116c7bd5e980
1 " Tests for the writefile() function and some :write commands. 1 " Tests for the writefile() function and some :write commands.
2 2
3 source check.vim 3 source check.vim
4 source term_util.vim
4 5
5 func Test_writefile() 6 func Test_writefile()
6 let f = tempname() 7 let f = tempname()
7 call writefile(["over","written"], f, "b") 8 call writefile(["over","written"], f, "b")
8 call writefile(["hello","world"], f, "b") 9 call writefile(["hello","world"], f, "b")
205 call setline(1, ['L1']) 206 call setline(1, ['L1'])
206 call assert_fails('saveas Xfile', 'E139:') 207 call assert_fails('saveas Xfile', 'E139:')
207 close! 208 close!
208 enew | only 209 enew | only
209 call delete('Xfile') 210 call delete('Xfile')
210
211 call writefile(test_null_list(), 'Xfile')
212 call assert_false(filereadable('Xfile'))
213 call writefile(test_null_blob(), 'Xfile')
214 call assert_false(filereadable('Xfile'))
215 call assert_fails('call writefile([], "")', 'E482:')
216 endfunc 211 endfunc
217 212
218 func Test_write_errors() 213 func Test_write_errors()
219 " Test for writing partial buffer 214 " Test for writing partial buffer
220 call writefile(['L1', 'L2', 'L3'], 'Xfile') 215 call writefile(['L1', 'L2', 'L3'], 'Xfile')
255 call writefile(test_null_list(), 'Xfile') 250 call writefile(test_null_list(), 'Xfile')
256 call assert_false(filereadable('Xfile')) 251 call assert_false(filereadable('Xfile'))
257 call writefile(test_null_blob(), 'Xfile') 252 call writefile(test_null_blob(), 'Xfile')
258 call assert_false(filereadable('Xfile')) 253 call assert_false(filereadable('Xfile'))
259 call assert_fails('call writefile([], "")', 'E482:') 254 call assert_fails('call writefile([], "")', 'E482:')
255
256 " very long file name
257 let long_fname = repeat('n', 5000)
258 call assert_fails('exe "w " .. long_fname', 'E75:')
259 call assert_fails('call writefile([], long_fname)', 'E482:')
260 endfunc
261
262 " Test for writing to a file which is modified after Vim read it
263 func Test_write_file_mtime()
264 CheckEnglish
265 CheckRunVimInTerminal
266
267 " First read the file into a buffer
268 call writefile(["Line1", "Line2"], 'Xfile')
269 let old_ftime = getftime('Xfile')
270 let buf = RunVimInTerminal('Xfile', #{rows : 10})
271 call term_wait(buf)
272 call term_sendkeys(buf, ":set noswapfile\<CR>")
273 call term_wait(buf)
274
275 " Modify the file directly. Make sure the file modification time is
276 " different. Note that on Linux/Unix, the file is considered modified
277 " outside, only if the difference is 2 seconds or more
278 sleep 1
279 call writefile(["Line3", "Line4"], 'Xfile')
280 let new_ftime = getftime('Xfile')
281 while new_ftime - old_ftime < 2
282 sleep 100m
283 call writefile(["Line3", "Line4"], 'Xfile')
284 let new_ftime = getftime('Xfile')
285 endwhile
286
287 " Try to overwrite the file and check for the prompt
288 call term_sendkeys(buf, ":w\<CR>")
289 call term_wait(buf)
290 call WaitForAssert({-> assert_equal("WARNING: The file has been changed since reading it!!!", term_getline(buf, 9))})
291 call assert_equal("Do you really want to write to it (y/n)?",
292 \ term_getline(buf, 10))
293 call term_sendkeys(buf, "n\<CR>")
294 call term_wait(buf)
295 call assert_equal(new_ftime, getftime('Xfile'))
296 call term_sendkeys(buf, ":w\<CR>")
297 call term_wait(buf)
298 call term_sendkeys(buf, "y\<CR>")
299 call term_wait(buf)
300 call WaitForAssert({-> assert_equal('Line2', readfile('Xfile')[1])})
301
302 " clean up
303 call StopVimInTerminal(buf)
304 call delete('Xfile')
305 endfunc
306
307 " Test for an autocmd unloading a buffer during a write command
308 func Test_write_autocmd_unloadbuf_lockmark()
309 augroup WriteTest
310 autocmd BufWritePre Xfile enew | write
311 augroup END
312 e Xfile
313 call assert_fails('lockmarks write', 'E203:')
314 augroup WriteTest
315 au!
316 augroup END
317 augroup! WriteTest
318 endfunc
319
320 " Test for writing a buffer with 'acwrite' but without autocmds
321 func Test_write_acwrite_error()
322 new Xfile
323 call setline(1, ['line1', 'line2', 'line3'])
324 set buftype=acwrite
325 call assert_fails('write', 'E676:')
326 call assert_fails('1,2write!', 'E676:')
327 call assert_fails('w >>', 'E676:')
328 close!
329 endfunc
330
331 " Test for adding and removing lines from an autocmd when writing a buffer
332 func Test_write_autocmd_add_remove_lines()
333 new Xfile
334 call setline(1, ['aaa', 'bbb', 'ccc', 'ddd'])
335
336 " Autocmd deleting lines from the file when writing a partial file
337 augroup WriteTest2
338 au!
339 autocmd FileWritePre Xfile 1,2d
340 augroup END
341 call assert_fails('2,3w!', 'E204:')
342
343 " Autocmd adding lines to a file when writing a partial file
344 augroup WriteTest2
345 au!
346 autocmd FileWritePre Xfile call append(0, ['xxx', 'yyy'])
347 augroup END
348 %d
349 call setline(1, ['aaa', 'bbb', 'ccc', 'ddd'])
350 1,2w!
351 call assert_equal(['xxx', 'yyy', 'aaa', 'bbb'], readfile('Xfile'))
352
353 " Autocmd deleting lines from the file when writing the whole file
354 augroup WriteTest2
355 au!
356 autocmd BufWritePre Xfile 1,2d
357 augroup END
358 %d
359 call setline(1, ['aaa', 'bbb', 'ccc', 'ddd'])
360 w
361 call assert_equal(['ccc', 'ddd'], readfile('Xfile'))
362
363 augroup WriteTest2
364 au!
365 augroup END
366 augroup! WriteTest2
367
368 close!
369 call delete('Xfile')
370 endfunc
371
372 " Test for writing to a readonly file
373 func Test_write_readonly()
374 " In Cirrus-CI, the freebsd tests are run under a root account. So this test
375 " doesn't fail.
376 CheckNotBSD
377 call writefile([], 'Xfile')
378 call setfperm('Xfile', "r--------")
379 edit Xfile
380 set noreadonly
381 call assert_fails('write', 'E505:')
382 let save_cpo = &cpo
383 set cpo+=W
384 call assert_fails('write!', 'E504:')
385 let &cpo = save_cpo
386 call delete('Xfile')
260 endfunc 387 endfunc
261 388
262 " vim: shiftwidth=2 sts=2 expandtab 389 " vim: shiftwidth=2 sts=2 expandtab