comparison src/testdir/test_writefile.vim @ 30051:13b02c1ea0f7 v9.0.0363

patch 9.0.0363: common names in test files causes tests to be flaky Commit: https://github.com/vim/vim/commit/b18b49699776485150b71626069a40d12d2c5590 Author: Bram Moolenaar <Bram@vim.org> Date: Fri Sep 2 21:55:50 2022 +0100 patch 9.0.0363: common names in test files causes tests to be flaky Problem: Common names in test files causes tests to be flaky. Solution: Use more specific names.
author Bram Moolenaar <Bram@vim.org>
date Fri, 02 Sep 2022 23:00:04 +0200
parents d891115c0aea
children a542dfb1c1a2
comparison
equal deleted inserted replaced
30050:4d0a9c80a90c 30051:13b02c1ea0f7
16 call assert_equal("good", l[2]) 16 call assert_equal("good", l[2])
17 call assert_equal("morning", l[3]) 17 call assert_equal("morning", l[3])
18 call assert_equal("vimmers", l[4]) 18 call assert_equal("vimmers", l[4])
19 call delete(f) 19 call delete(f)
20 20
21 call assert_fails('call writefile("text", "Xfile")', 'E475: Invalid argument: writefile() first argument must be a List or a Blob') 21 call assert_fails('call writefile("text", "Xwffile")', 'E475: Invalid argument: writefile() first argument must be a List or a Blob')
22 endfunc 22 endfunc
23 23
24 func Test_writefile_ignore_regexp_error() 24 func Test_writefile_ignore_regexp_error()
25 write Xt[z-a]est.txt 25 write Xt[z-a]est.txt
26 call delete('Xt[z-a]est.txt') 26 call delete('Xt[z-a]est.txt')
27 endfunc 27 endfunc
28 28
29 func Test_writefile_fails_gently() 29 func Test_writefile_fails_gently()
30 call assert_fails('call writefile(["test"], "Xfile", [])', 'E730:') 30 call assert_fails('call writefile(["test"], "Xwffile", [])', 'E730:')
31 call assert_false(filereadable("Xfile")) 31 call assert_false(filereadable("Xwffile"))
32 call delete("Xfile") 32 call delete("Xwffile")
33 33
34 call assert_fails('call writefile(["test", [], [], [], "tset"], "Xfile")', 'E730:') 34 call assert_fails('call writefile(["test", [], [], [], "tset"], "Xwffile")', 'E730:')
35 call assert_false(filereadable("Xfile")) 35 call assert_false(filereadable("Xwffile"))
36 call delete("Xfile") 36 call delete("Xwffile")
37 37
38 call assert_fails('call writefile([], "Xfile", [])', 'E730:') 38 call assert_fails('call writefile([], "Xwffile", [])', 'E730:')
39 call assert_false(filereadable("Xfile")) 39 call assert_false(filereadable("Xwffile"))
40 call delete("Xfile") 40 call delete("Xwffile")
41 41
42 call assert_fails('call writefile([], [])', 'E730:') 42 call assert_fails('call writefile([], [])', 'E730:')
43 endfunc 43 endfunc
44 44
45 func Test_writefile_fails_conversion() 45 func Test_writefile_fails_conversion()
50 " Without a backup file the write won't happen if there is a conversion 50 " Without a backup file the write won't happen if there is a conversion
51 " error. 51 " error.
52 set nobackup nowritebackup backupdir=. backupskip= 52 set nobackup nowritebackup backupdir=. backupskip=
53 new 53 new
54 let contents = ["line one", "line two"] 54 let contents = ["line one", "line two"]
55 call writefile(contents, 'Xfile') 55 call writefile(contents, 'Xwfcfile')
56 edit Xfile 56 edit Xwfcfile
57 call setline(1, ["first line", "cannot convert \u010b", "third line"]) 57 call setline(1, ["first line", "cannot convert \u010b", "third line"])
58 call assert_fails('write ++enc=cp932', 'E513:') 58 call assert_fails('write ++enc=cp932', 'E513:')
59 call assert_equal(contents, readfile('Xfile')) 59 call assert_equal(contents, readfile('Xwfcfile'))
60 60
61 " With 'backupcopy' set, if there is a conversion error, the backup file is 61 " With 'backupcopy' set, if there is a conversion error, the backup file is
62 " still created. 62 " still created.
63 set backupcopy=yes writebackup& backup& 63 set backupcopy=yes writebackup& backup&
64 call delete('Xfile' .. &backupext) 64 call delete('Xwfcfile' .. &backupext)
65 call assert_fails('write ++enc=cp932', 'E513:') 65 call assert_fails('write ++enc=cp932', 'E513:')
66 call assert_equal(contents, readfile('Xfile')) 66 call assert_equal(contents, readfile('Xwfcfile'))
67 call assert_equal(contents, readfile('Xfile' .. &backupext)) 67 call assert_equal(contents, readfile('Xwfcfile' .. &backupext))
68 set backupcopy& 68 set backupcopy&
69 %bw! 69 %bw!
70 70
71 " Conversion error during write 71 " Conversion error during write
72 new 72 new
73 call setline(1, ["\U10000000"]) 73 call setline(1, ["\U10000000"])
74 let output = execute('write! ++enc=utf-16 Xfile') 74 let output = execute('write! ++enc=utf-16 Xwfcfile')
75 call assert_match('CONVERSION ERROR', output) 75 call assert_match('CONVERSION ERROR', output)
76 let output = execute('write! ++enc=ucs-2 Xfile') 76 let output = execute('write! ++enc=ucs-2 Xwfcfile')
77 call assert_match('CONVERSION ERROR', output) 77 call assert_match('CONVERSION ERROR', output)
78 call delete('Xfilz~') 78 call delete('Xfilz~')
79 call delete('Xfily~') 79 call delete('Xfily~')
80 %bw! 80 %bw!
81 81
82 call delete('Xfile') 82 call delete('Xwfcfile')
83 call delete('Xfile' .. &backupext) 83 call delete('Xwfcfile' .. &backupext)
84 bwipe! 84 bwipe!
85 set backup& writebackup& backupdir&vim backupskip&vim 85 set backup& writebackup& backupdir&vim backupskip&vim
86 endfunc 86 endfunc
87 87
88 func Test_writefile_fails_conversion2() 88 func Test_writefile_fails_conversion2()
92 endif 92 endif
93 " With a backup file the write happens even if there is a conversion error, 93 " With a backup file the write happens even if there is a conversion error,
94 " but then the backup file must remain 94 " but then the backup file must remain
95 set nobackup writebackup backupdir=. backupskip= 95 set nobackup writebackup backupdir=. backupskip=
96 let contents = ["line one", "line two"] 96 let contents = ["line one", "line two"]
97 call writefile(contents, 'Xfile_conversion_err') 97 call writefile(contents, 'Xwf2file_conversion_err')
98 edit Xfile_conversion_err 98 edit Xwf2file_conversion_err
99 call setline(1, ["first line", "cannot convert \u010b", "third line"]) 99 call setline(1, ["first line", "cannot convert \u010b", "third line"])
100 set fileencoding=latin1 100 set fileencoding=latin1
101 let output = execute('write') 101 let output = execute('write')
102 call assert_match('CONVERSION ERROR', output) 102 call assert_match('CONVERSION ERROR', output)
103 call assert_equal(contents, readfile('Xfile_conversion_err~')) 103 call assert_equal(contents, readfile('Xwf2file_conversion_err~'))
104 104
105 call delete('Xfile_conversion_err') 105 call delete('Xwf2file_conversion_err')
106 call delete('Xfile_conversion_err~') 106 call delete('Xwf2file_conversion_err~')
107 bwipe! 107 bwipe!
108 set backup& writebackup& backupdir&vim backupskip&vim 108 set backup& writebackup& backupdir&vim backupskip&vim
109 endfunc 109 endfunc
110 110
111 func SetFlag(timer) 111 func SetFlag(timer)
115 func Test_write_quit_split() 115 func Test_write_quit_split()
116 " Prevent exiting by splitting window on file write. 116 " Prevent exiting by splitting window on file write.
117 augroup testgroup 117 augroup testgroup
118 autocmd BufWritePre * split 118 autocmd BufWritePre * split
119 augroup END 119 augroup END
120 e! Xfile 120 e! Xwqsfile
121 call setline(1, 'nothing') 121 call setline(1, 'nothing')
122 wq 122 wq
123 123
124 if has('timers') 124 if has('timers')
125 " timer will not run if "exiting" is still set 125 " timer will not run if "exiting" is still set
128 sleep 50m 128 sleep 50m
129 call assert_equal(1, g:flag) 129 call assert_equal(1, g:flag)
130 unlet g:flag 130 unlet g:flag
131 endif 131 endif
132 au! testgroup 132 au! testgroup
133 bwipe Xfile 133 bwipe Xwqsfile
134 call delete('Xfile') 134 call delete('Xwqsfile')
135 endfunc 135 endfunc
136 136
137 func Test_nowrite_quit_split() 137 func Test_nowrite_quit_split()
138 " Prevent exiting by opening a help window. 138 " Prevent exiting by opening a help window.
139 e! Xfile 139 e! Xnqsfile
140 help 140 help
141 wincmd w 141 wincmd w
142 exe winnr() . 'q' 142 exe winnr() . 'q'
143 143
144 if has('timers') 144 if has('timers')
147 call timer_start(1, 'SetFlag') 147 call timer_start(1, 'SetFlag')
148 sleep 50m 148 sleep 50m
149 call assert_equal(1, g:flag) 149 call assert_equal(1, g:flag)
150 unlet g:flag 150 unlet g:flag
151 endif 151 endif
152 bwipe Xfile 152 bwipe Xnqsfile
153 endfunc 153 endfunc
154 154
155 func Test_writefile_sync_arg() 155 func Test_writefile_sync_arg()
156 " This doesn't check if fsync() works, only that the argument is accepted. 156 " This doesn't check if fsync() works, only that the argument is accepted.
157 call writefile(['one'], 'Xtest', 's') 157 call writefile(['one'], 'Xtest', 's')
211 " command. 211 " command.
212 func Test_write_pipe_to_cmd() 212 func Test_write_pipe_to_cmd()
213 CheckUnix 213 CheckUnix
214 new 214 new
215 call setline(1, ['L1', 'L2', 'L3', 'L4']) 215 call setline(1, ['L1', 'L2', 'L3', 'L4'])
216 2,3w !cat > Xfile 216 2,3w !cat > Xptfile
217 call assert_equal(['L2', 'L3'], readfile('Xfile')) 217 call assert_equal(['L2', 'L3'], readfile('Xptfile'))
218 close! 218 close!
219 call delete('Xfile') 219 call delete('Xptfile')
220 endfunc 220 endfunc
221 221
222 " Test for :saveas 222 " Test for :saveas
223 func Test_saveas() 223 func Test_saveas()
224 call assert_fails('saveas', 'E471:') 224 call assert_fails('saveas', 'E471:')
225 call writefile(['L1'], 'Xfile') 225 call writefile(['L1'], 'Xsafile')
226 new Xfile 226 new Xsafile
227 new 227 new
228 call setline(1, ['L1']) 228 call setline(1, ['L1'])
229 call assert_fails('saveas Xfile', 'E139:') 229 call assert_fails('saveas Xsafile', 'E139:')
230 close! 230 close!
231 enew | only 231 enew | only
232 call delete('Xfile') 232 call delete('Xsafile')
233 233
234 " :saveas should detect and set the file type. 234 " :saveas should detect and set the file type.
235 syntax on 235 syntax on
236 saveas! Xsaveas.pl 236 saveas! Xsaveas.pl
237 call assert_equal('perl', &filetype) 237 call assert_equal('perl', &filetype)
240 call delete('Xsaveas.pl') 240 call delete('Xsaveas.pl')
241 endfunc 241 endfunc
242 242
243 func Test_write_errors() 243 func Test_write_errors()
244 " Test for writing partial buffer 244 " Test for writing partial buffer
245 call writefile(['L1', 'L2', 'L3'], 'Xfile') 245 call writefile(['L1', 'L2', 'L3'], 'Xwefile')
246 new Xfile 246 new Xwefile
247 call assert_fails('1,2write', 'E140:') 247 call assert_fails('1,2write', 'E140:')
248 close! 248 close!
249 249
250 call assert_fails('w > Xtest', 'E494:') 250 call assert_fails('w > Xtest', 'E494:')
251 251
261 call setline(1, ['L1']) 261 call setline(1, ['L1'])
262 call assert_fails('wall', 'E141:') 262 call assert_fails('wall', 'E141:')
263 enew! 263 enew!
264 264
265 " Test for writing a 'readonly' file 265 " Test for writing a 'readonly' file
266 new Xfile 266 new Xwefile
267 set readonly 267 set readonly
268 call assert_fails('write', 'E45:') 268 call assert_fails('write', 'E45:')
269 close 269 close
270 270
271 " Test for writing to a read-only file 271 " Test for writing to a read-only file
272 new Xfile 272 new Xwefile
273 call setfperm('Xfile', 'r--r--r--') 273 call setfperm('Xwefile', 'r--r--r--')
274 call assert_fails('write', 'E505:') 274 call assert_fails('write', 'E505:')
275 call setfperm('Xfile', 'rw-rw-rw-') 275 call setfperm('Xwefile', 'rw-rw-rw-')
276 close 276 close
277 277
278 call delete('Xfile') 278 call delete('Xwefile')
279 279
280 call writefile(test_null_list(), 'Xfile') 280 call writefile(test_null_list(), 'Xwefile')
281 call assert_false(filereadable('Xfile')) 281 call assert_false(filereadable('Xwefile'))
282 call writefile(test_null_blob(), 'Xfile') 282 call writefile(test_null_blob(), 'Xwefile')
283 call assert_false(filereadable('Xfile')) 283 call assert_false(filereadable('Xwefile'))
284 call assert_fails('call writefile([], "")', 'E482:') 284 call assert_fails('call writefile([], "")', 'E482:')
285 285
286 " very long file name 286 " very long file name
287 let long_fname = repeat('n', 5000) 287 let long_fname = repeat('n', 5000)
288 call assert_fails('exe "w " .. long_fname', 'E75:') 288 call assert_fails('exe "w " .. long_fname', 'E75:')
303 func Test_write_file_mtime() 303 func Test_write_file_mtime()
304 CheckEnglish 304 CheckEnglish
305 CheckRunVimInTerminal 305 CheckRunVimInTerminal
306 306
307 " First read the file into a buffer 307 " First read the file into a buffer
308 call writefile(["Line1", "Line2"], 'Xfile') 308 call writefile(["Line1", "Line2"], 'Xwfmfile')
309 let old_ftime = getftime('Xfile') 309 let old_ftime = getftime('Xwfmfile')
310 let buf = RunVimInTerminal('Xfile', #{rows : 10}) 310 let buf = RunVimInTerminal('Xwfmfile', #{rows : 10})
311 call TermWait(buf) 311 call TermWait(buf)
312 call term_sendkeys(buf, ":set noswapfile\<CR>") 312 call term_sendkeys(buf, ":set noswapfile\<CR>")
313 call TermWait(buf) 313 call TermWait(buf)
314 314
315 " Modify the file directly. Make sure the file modification time is 315 " Modify the file directly. Make sure the file modification time is
316 " different. Note that on Linux/Unix, the file is considered modified 316 " different. Note that on Linux/Unix, the file is considered modified
317 " outside, only if the difference is 2 seconds or more 317 " outside, only if the difference is 2 seconds or more
318 sleep 1 318 sleep 1
319 call writefile(["Line3", "Line4"], 'Xfile') 319 call writefile(["Line3", "Line4"], 'Xwfmfile')
320 let new_ftime = getftime('Xfile') 320 let new_ftime = getftime('Xwfmfile')
321 while new_ftime - old_ftime < 2 321 while new_ftime - old_ftime < 2
322 sleep 100m 322 sleep 100m
323 call writefile(["Line3", "Line4"], 'Xfile') 323 call writefile(["Line3", "Line4"], 'Xwfmfile')
324 let new_ftime = getftime('Xfile') 324 let new_ftime = getftime('Xwfmfile')
325 endwhile 325 endwhile
326 326
327 " Try to overwrite the file and check for the prompt 327 " Try to overwrite the file and check for the prompt
328 call term_sendkeys(buf, ":w\<CR>") 328 call term_sendkeys(buf, ":w\<CR>")
329 call TermWait(buf) 329 call TermWait(buf)
330 call WaitForAssert({-> assert_equal("WARNING: The file has been changed since reading it!!!", term_getline(buf, 9))}) 330 call WaitForAssert({-> assert_equal("WARNING: The file has been changed since reading it!!!", term_getline(buf, 9))})
331 call assert_equal("Do you really want to write to it (y/n)?", 331 call assert_equal("Do you really want to write to it (y/n)?",
332 \ term_getline(buf, 10)) 332 \ term_getline(buf, 10))
333 call term_sendkeys(buf, "n\<CR>") 333 call term_sendkeys(buf, "n\<CR>")
334 call TermWait(buf) 334 call TermWait(buf)
335 call assert_equal(new_ftime, getftime('Xfile')) 335 call assert_equal(new_ftime, getftime('Xwfmfile'))
336 call term_sendkeys(buf, ":w\<CR>") 336 call term_sendkeys(buf, ":w\<CR>")
337 call TermWait(buf) 337 call TermWait(buf)
338 call term_sendkeys(buf, "y\<CR>") 338 call term_sendkeys(buf, "y\<CR>")
339 call TermWait(buf) 339 call TermWait(buf)
340 call WaitForAssert({-> assert_equal('Line2', readfile('Xfile')[1])}) 340 call WaitForAssert({-> assert_equal('Line2', readfile('Xwfmfile')[1])})
341 341
342 " clean up 342 " clean up
343 call StopVimInTerminal(buf) 343 call StopVimInTerminal(buf)
344 call delete('Xfile') 344 call delete('Xwfmfile')
345 endfunc 345 endfunc
346 346
347 " Test for an autocmd unloading a buffer during a write command 347 " Test for an autocmd unloading a buffer during a write command
348 func Test_write_autocmd_unloadbuf_lockmark() 348 func Test_write_autocmd_unloadbuf_lockmark()
349 augroup WriteTest 349 augroup WriteTest
350 autocmd BufWritePre Xfile enew | write 350 autocmd BufWritePre Xwaufile enew | write
351 augroup END 351 augroup END
352 e Xfile 352 e Xwaufile
353 call assert_fails('lockmarks write', ['E32:', 'E203:']) 353 call assert_fails('lockmarks write', ['E32:', 'E203:'])
354 augroup WriteTest 354 augroup WriteTest
355 au! 355 au!
356 augroup END 356 augroup END
357 augroup! WriteTest 357 augroup! WriteTest
358 endfunc 358 endfunc
359 359
360 " Test for writing a buffer with 'acwrite' but without autocmds 360 " Test for writing a buffer with 'acwrite' but without autocmds
361 func Test_write_acwrite_error() 361 func Test_write_acwrite_error()
362 new Xfile 362 new Xwaefile
363 call setline(1, ['line1', 'line2', 'line3']) 363 call setline(1, ['line1', 'line2', 'line3'])
364 set buftype=acwrite 364 set buftype=acwrite
365 call assert_fails('write', 'E676:') 365 call assert_fails('write', 'E676:')
366 call assert_fails('1,2write!', 'E676:') 366 call assert_fails('1,2write!', 'E676:')
367 call assert_fails('w >>', 'E676:') 367 call assert_fails('w >>', 'E676:')
368 close! 368 close!
369 endfunc 369 endfunc
370 370
371 " Test for adding and removing lines from an autocmd when writing a buffer 371 " Test for adding and removing lines from an autocmd when writing a buffer
372 func Test_write_autocmd_add_remove_lines() 372 func Test_write_autocmd_add_remove_lines()
373 new Xfile 373 new Xwaafile
374 call setline(1, ['aaa', 'bbb', 'ccc', 'ddd']) 374 call setline(1, ['aaa', 'bbb', 'ccc', 'ddd'])
375 375
376 " Autocmd deleting lines from the file when writing a partial file 376 " Autocmd deleting lines from the file when writing a partial file
377 augroup WriteTest2 377 augroup WriteTest2
378 au! 378 au!
379 autocmd FileWritePre Xfile 1,2d 379 autocmd FileWritePre Xwaafile 1,2d
380 augroup END 380 augroup END
381 call assert_fails('2,3w!', 'E204:') 381 call assert_fails('2,3w!', 'E204:')
382 382
383 " Autocmd adding lines to a file when writing a partial file 383 " Autocmd adding lines to a file when writing a partial file
384 augroup WriteTest2 384 augroup WriteTest2
385 au! 385 au!
386 autocmd FileWritePre Xfile call append(0, ['xxx', 'yyy']) 386 autocmd FileWritePre Xwaafile call append(0, ['xxx', 'yyy'])
387 augroup END 387 augroup END
388 %d 388 %d
389 call setline(1, ['aaa', 'bbb', 'ccc', 'ddd']) 389 call setline(1, ['aaa', 'bbb', 'ccc', 'ddd'])
390 1,2w! 390 1,2w!
391 call assert_equal(['xxx', 'yyy', 'aaa', 'bbb'], readfile('Xfile')) 391 call assert_equal(['xxx', 'yyy', 'aaa', 'bbb'], readfile('Xwaafile'))
392 392
393 " Autocmd deleting lines from the file when writing the whole file 393 " Autocmd deleting lines from the file when writing the whole file
394 augroup WriteTest2 394 augroup WriteTest2
395 au! 395 au!
396 autocmd BufWritePre Xfile 1,2d 396 autocmd BufWritePre Xwaafile 1,2d
397 augroup END 397 augroup END
398 %d 398 %d
399 call setline(1, ['aaa', 'bbb', 'ccc', 'ddd']) 399 call setline(1, ['aaa', 'bbb', 'ccc', 'ddd'])
400 w 400 w
401 call assert_equal(['ccc', 'ddd'], readfile('Xfile')) 401 call assert_equal(['ccc', 'ddd'], readfile('Xwaafile'))
402 402
403 augroup WriteTest2 403 augroup WriteTest2
404 au! 404 au!
405 augroup END 405 augroup END
406 augroup! WriteTest2 406 augroup! WriteTest2
407 407
408 close! 408 close!
409 call delete('Xfile') 409 call delete('Xwaafile')
410 endfunc 410 endfunc
411 411
412 " Test for writing to a readonly file 412 " Test for writing to a readonly file
413 func Test_write_readonly() 413 func Test_write_readonly()
414 call writefile([], 'Xfile') 414 call writefile([], 'Xwrofile')
415 call setfperm('Xfile', "r--------") 415 call setfperm('Xwrofile', "r--------")
416 edit Xfile 416 edit Xwrofile
417 set noreadonly backupskip= 417 set noreadonly backupskip=
418 call assert_fails('write', 'E505:') 418 call assert_fails('write', 'E505:')
419 let save_cpo = &cpo 419 let save_cpo = &cpo
420 set cpo+=W 420 set cpo+=W
421 call assert_fails('write!', 'E504:') 421 call assert_fails('write!', 'E504:')
422 let &cpo = save_cpo 422 let &cpo = save_cpo
423 call setline(1, ['line1']) 423 call setline(1, ['line1'])
424 write! 424 write!
425 call assert_equal(['line1'], readfile('Xfile')) 425 call assert_equal(['line1'], readfile('Xwrofile'))
426 426
427 " Auto-saving a readonly file should fail with 'autowriteall' 427 " Auto-saving a readonly file should fail with 'autowriteall'
428 %bw! 428 %bw!
429 e Xfile 429 e Xwrofile
430 set noreadonly autowriteall 430 set noreadonly autowriteall
431 call setline(1, ['aaaa']) 431 call setline(1, ['aaaa'])
432 call assert_fails('n', 'E505:') 432 call assert_fails('n', 'E505:')
433 set cpo+=W 433 set cpo+=W
434 call assert_fails('n', 'E504:') 434 call assert_fails('n', 'E504:')
435 set cpo-=W 435 set cpo-=W
436 set autowriteall& 436 set autowriteall&
437 437
438 set backupskip& 438 set backupskip&
439 call delete('Xfile') 439 call delete('Xwrofile')
440 %bw! 440 %bw!
441 endfunc 441 endfunc
442 442
443 " Test for 'patchmode' 443 " Test for 'patchmode'
444 func Test_patchmode() 444 func Test_patchmode()
445 call writefile(['one'], 'Xfile') 445 call writefile(['one'], 'Xpafile')
446 set patchmode=.orig nobackup backupskip= writebackup 446 set patchmode=.orig nobackup backupskip= writebackup
447 new Xfile 447 new Xpafile
448 call setline(1, 'two') 448 call setline(1, 'two')
449 " first write should create the .orig file 449 " first write should create the .orig file
450 write 450 write
451 call assert_equal(['one'], readfile('Xfile.orig')) 451 call assert_equal(['one'], readfile('Xpafile.orig'))
452 call setline(1, 'three') 452 call setline(1, 'three')
453 " subsequent writes should not create/modify the .orig file 453 " subsequent writes should not create/modify the .orig file
454 write 454 write
455 call assert_equal(['one'], readfile('Xfile.orig')) 455 call assert_equal(['one'], readfile('Xpafile.orig'))
456 456
457 " use 'patchmode' with 'nobackup' and 'nowritebackup' to create an empty 457 " use 'patchmode' with 'nobackup' and 'nowritebackup' to create an empty
458 " original file 458 " original file
459 call delete('Xfile') 459 call delete('Xpafile')
460 call delete('Xfile.orig') 460 call delete('Xpafile.orig')
461 %bw! 461 %bw!
462 set patchmode=.orig nobackup nowritebackup 462 set patchmode=.orig nobackup nowritebackup
463 edit Xfile 463 edit Xpafile
464 call setline(1, ['xxx']) 464 call setline(1, ['xxx'])
465 write 465 write
466 call assert_equal(['xxx'], readfile('Xfile')) 466 call assert_equal(['xxx'], readfile('Xpafile'))
467 call assert_equal([], readfile('Xfile.orig')) 467 call assert_equal([], readfile('Xpafile.orig'))
468 468
469 set patchmode& backup& backupskip& writebackup& 469 set patchmode& backup& backupskip& writebackup&
470 call delete('Xfile') 470 call delete('Xpafile')
471 call delete('Xfile.orig') 471 call delete('Xpafile.orig')
472 endfunc 472 endfunc
473 473
474 " Test for writing to a file in a readonly directory 474 " Test for writing to a file in a readonly directory
475 " NOTE: if you run tests as root this will fail. Don't run tests as root! 475 " NOTE: if you run tests as root this will fail. Don't run tests as root!
476 func Test_write_readonly_dir() 476 func Test_write_readonly_dir()
498 498
499 " Test for writing a file using invalid file encoding 499 " Test for writing a file using invalid file encoding
500 func Test_write_invalid_encoding() 500 func Test_write_invalid_encoding()
501 new 501 new
502 call setline(1, 'abc') 502 call setline(1, 'abc')
503 call assert_fails('write ++enc=axbyc Xfile', 'E213:') 503 call assert_fails('write ++enc=axbyc Xiefile', 'E213:')
504 close! 504 close!
505 endfunc 505 endfunc
506 506
507 " Tests for reading and writing files with conversion for Win32. 507 " Tests for reading and writing files with conversion for Win32.
508 func Test_write_file_encoding() 508 func Test_write_file_encoding()
513 let text =<< trim END 513 let text =<< trim END
514 1 utf-8 text: ╨Ф╨╗╤П Vim version 6.2. ╨Я╨╛╤Б╨╗╨╡╨┤╨╜╨╡╨╡ ╨╕╨╖╨╝╨╡╨╜╨╡╨╜╨╕╨╡: 1970 Jan 01 514 1 utf-8 text: ╨Ф╨╗╤П Vim version 6.2. ╨Я╨╛╤Б╨╗╨╡╨┤╨╜╨╡╨╡ ╨╕╨╖╨╝╨╡╨╜╨╡╨╜╨╕╨╡: 1970 Jan 01
515 2 cp1251 text: ─ы  Vim version 6.2. ╧юёыхфэхх шчьхэхэшх: 1970 Jan 01 515 2 cp1251 text: ─ы  Vim version 6.2. ╧юёыхфэхх шчьхэхэшх: 1970 Jan 01
516 3 cp866 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01 516 3 cp866 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01
517 END 517 END
518 call writefile(text, 'Xfile') 518 call writefile(text, 'Xwfefile')
519 edit Xfile 519 edit Xwfefile
520 520
521 " write tests: 521 " write tests:
522 " combine three values for 'encoding' with three values for 'fileencoding' 522 " combine three values for 'encoding' with three values for 'fileencoding'
523 " also write files for read tests 523 " also write files for read tests
524 call cursor(1, 1) 524 call cursor(1, 1)
525 set encoding=utf-8 525 set encoding=utf-8
526 .w! ++enc=utf-8 Xtest 526 .w! ++enc=utf-8 Xwfetest
527 .w ++enc=cp1251 >> Xtest 527 .w ++enc=cp1251 >> Xwfetest
528 .w ++enc=cp866 >> Xtest 528 .w ++enc=cp866 >> Xwfetest
529 .w! ++enc=utf-8 Xutf8 529 .w! ++enc=utf-8 Xutf8
530 let expected =<< trim END 530 let expected =<< trim END
531 1 utf-8 text: ╨Ф╨╗╤П Vim version 6.2. ╨Я╨╛╤Б╨╗╨╡╨┤╨╜╨╡╨╡ ╨╕╨╖╨╝╨╡╨╜╨╡╨╜╨╕╨╡: 1970 Jan 01 531 1 utf-8 text: ╨Ф╨╗╤П Vim version 6.2. ╨Я╨╛╤Б╨╗╨╡╨┤╨╜╨╡╨╡ ╨╕╨╖╨╝╨╡╨╜╨╡╨╜╨╕╨╡: 1970 Jan 01
532 1 utf-8 text: ─ы  Vim version 6.2. ╧юёыхфэхх шчьхэхэшх: 1970 Jan 01 532 1 utf-8 text: ─ы  Vim version 6.2. ╧юёыхфэхх шчьхэхэшх: 1970 Jan 01
533 1 utf-8 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01 533 1 utf-8 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01
534 END 534 END
535 call assert_equal(expected, readfile('Xtest')) 535 call assert_equal(expected, readfile('Xwfetest'))
536 536
537 call cursor(2, 1) 537 call cursor(2, 1)
538 set encoding=cp1251 538 set encoding=cp1251
539 .w! ++enc=utf-8 Xtest 539 .w! ++enc=utf-8 Xwfetest
540 .w ++enc=cp1251 >> Xtest 540 .w ++enc=cp1251 >> Xwfetest
541 .w ++enc=cp866 >> Xtest 541 .w ++enc=cp866 >> Xwfetest
542 .w! ++enc=cp1251 Xcp1251 542 .w! ++enc=cp1251 Xcp1251
543 let expected =<< trim END 543 let expected =<< trim END
544 2 cp1251 text: ╨Ф╨╗╤П Vim version 6.2. ╨Я╨╛╤Б╨╗╨╡╨┤╨╜╨╡╨╡ ╨╕╨╖╨╝╨╡╨╜╨╡╨╜╨╕╨╡: 1970 Jan 01 544 2 cp1251 text: ╨Ф╨╗╤П Vim version 6.2. ╨Я╨╛╤Б╨╗╨╡╨┤╨╜╨╡╨╡ ╨╕╨╖╨╝╨╡╨╜╨╡╨╜╨╕╨╡: 1970 Jan 01
545 2 cp1251 text: ─ы  Vim version 6.2. ╧юёыхфэхх шчьхэхэшх: 1970 Jan 01 545 2 cp1251 text: ─ы  Vim version 6.2. ╧юёыхфэхх шчьхэхэшх: 1970 Jan 01
546 2 cp1251 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01 546 2 cp1251 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01
547 END 547 END
548 call assert_equal(expected, readfile('Xtest')) 548 call assert_equal(expected, readfile('Xwfetest'))
549 549
550 call cursor(3, 1) 550 call cursor(3, 1)
551 set encoding=cp866 551 set encoding=cp866
552 .w! ++enc=utf-8 Xtest 552 .w! ++enc=utf-8 Xwfetest
553 .w ++enc=cp1251 >> Xtest 553 .w ++enc=cp1251 >> Xwfetest
554 .w ++enc=cp866 >> Xtest 554 .w ++enc=cp866 >> Xwfetest
555 .w! ++enc=cp866 Xcp866 555 .w! ++enc=cp866 Xcp866
556 let expected =<< trim END 556 let expected =<< trim END
557 3 cp866 text: ╨Ф╨╗╤П Vim version 6.2. ╨Я╨╛╤Б╨╗╨╡╨┤╨╜╨╡╨╡ ╨╕╨╖╨╝╨╡╨╜╨╡╨╜╨╕╨╡: 1970 Jan 01 557 3 cp866 text: ╨Ф╨╗╤П Vim version 6.2. ╨Я╨╛╤Б╨╗╨╡╨┤╨╜╨╡╨╡ ╨╕╨╖╨╝╨╡╨╜╨╡╨╜╨╕╨╡: 1970 Jan 01
558 3 cp866 text: ─ы  Vim version 6.2. ╧юёыхфэхх шчьхэхэшх: 1970 Jan 01 558 3 cp866 text: ─ы  Vim version 6.2. ╧юёыхфэхх шчьхэхэшх: 1970 Jan 01
559 3 cp866 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01 559 3 cp866 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01
560 END 560 END
561 call assert_equal(expected, readfile('Xtest')) 561 call assert_equal(expected, readfile('Xwfetest'))
562 562
563 " read three 'fileencoding's with utf-8 'encoding' 563 " read three 'fileencoding's with utf-8 'encoding'
564 set encoding=utf-8 fencs=utf-8,cp1251 564 set encoding=utf-8 fencs=utf-8,cp1251
565 e Xutf8 565 e Xutf8
566 .w! ++enc=utf-8 Xtest 566 .w! ++enc=utf-8 Xwfetest
567 e Xcp1251 567 e Xcp1251
568 .w ++enc=utf-8 >> Xtest 568 .w ++enc=utf-8 >> Xwfetest
569 set fencs=utf-8,cp866 569 set fencs=utf-8,cp866
570 e Xcp866 570 e Xcp866
571 .w ++enc=utf-8 >> Xtest 571 .w ++enc=utf-8 >> Xwfetest
572 let expected =<< trim END 572 let expected =<< trim END
573 1 utf-8 text: ╨Ф╨╗╤П Vim version 6.2. ╨Я╨╛╤Б╨╗╨╡╨┤╨╜╨╡╨╡ ╨╕╨╖╨╝╨╡╨╜╨╡╨╜╨╕╨╡: 1970 Jan 01 573 1 utf-8 text: ╨Ф╨╗╤П Vim version 6.2. ╨Я╨╛╤Б╨╗╨╡╨┤╨╜╨╡╨╡ ╨╕╨╖╨╝╨╡╨╜╨╡╨╜╨╕╨╡: 1970 Jan 01
574 2 cp1251 text: ╨Ф╨╗╤П Vim version 6.2. ╨Я╨╛╤Б╨╗╨╡╨┤╨╜╨╡╨╡ ╨╕╨╖╨╝╨╡╨╜╨╡╨╜╨╕╨╡: 1970 Jan 01 574 2 cp1251 text: ╨Ф╨╗╤П Vim version 6.2. ╨Я╨╛╤Б╨╗╨╡╨┤╨╜╨╡╨╡ ╨╕╨╖╨╝╨╡╨╜╨╡╨╜╨╕╨╡: 1970 Jan 01
575 3 cp866 text: ╨Ф╨╗╤П Vim version 6.2. ╨Я╨╛╤Б╨╗╨╡╨┤╨╜╨╡╨╡ ╨╕╨╖╨╝╨╡╨╜╨╡╨╜╨╕╨╡: 1970 Jan 01 575 3 cp866 text: ╨Ф╨╗╤П Vim version 6.2. ╨Я╨╛╤Б╨╗╨╡╨┤╨╜╨╡╨╡ ╨╕╨╖╨╝╨╡╨╜╨╡╨╜╨╕╨╡: 1970 Jan 01
576 END 576 END
577 call assert_equal(expected, readfile('Xtest')) 577 call assert_equal(expected, readfile('Xwfetest'))
578 578
579 " read three 'fileencoding's with cp1251 'encoding' 579 " read three 'fileencoding's with cp1251 'encoding'
580 set encoding=utf-8 fencs=utf-8,cp1251 580 set encoding=utf-8 fencs=utf-8,cp1251
581 e Xutf8 581 e Xutf8
582 .w! ++enc=cp1251 Xtest 582 .w! ++enc=cp1251 Xwfetest
583 e Xcp1251 583 e Xcp1251
584 .w ++enc=cp1251 >> Xtest 584 .w ++enc=cp1251 >> Xwfetest
585 set fencs=utf-8,cp866 585 set fencs=utf-8,cp866
586 e Xcp866 586 e Xcp866
587 .w ++enc=cp1251 >> Xtest 587 .w ++enc=cp1251 >> Xwfetest
588 let expected =<< trim END 588 let expected =<< trim END
589 1 utf-8 text: ─ы  Vim version 6.2. ╧юёыхфэхх шчьхэхэшх: 1970 Jan 01 589 1 utf-8 text: ─ы  Vim version 6.2. ╧юёыхфэхх шчьхэхэшх: 1970 Jan 01
590 2 cp1251 text: ─ы  Vim version 6.2. ╧юёыхфэхх шчьхэхэшх: 1970 Jan 01 590 2 cp1251 text: ─ы  Vim version 6.2. ╧юёыхфэхх шчьхэхэшх: 1970 Jan 01
591 3 cp866 text: ─ы  Vim version 6.2. ╧юёыхфэхх шчьхэхэшх: 1970 Jan 01 591 3 cp866 text: ─ы  Vim version 6.2. ╧юёыхфэхх шчьхэхэшх: 1970 Jan 01
592 END 592 END
593 call assert_equal(expected, readfile('Xtest')) 593 call assert_equal(expected, readfile('Xwfetest'))
594 594
595 " read three 'fileencoding's with cp866 'encoding' 595 " read three 'fileencoding's with cp866 'encoding'
596 set encoding=cp866 fencs=utf-8,cp1251 596 set encoding=cp866 fencs=utf-8,cp1251
597 e Xutf8 597 e Xutf8
598 .w! ++enc=cp866 Xtest 598 .w! ++enc=cp866 Xwfetest
599 e Xcp1251 599 e Xcp1251
600 .w ++enc=cp866 >> Xtest 600 .w ++enc=cp866 >> Xwfetest
601 set fencs=utf-8,cp866 601 set fencs=utf-8,cp866
602 e Xcp866 602 e Xcp866
603 .w ++enc=cp866 >> Xtest 603 .w ++enc=cp866 >> Xwfetest
604 let expected =<< trim END 604 let expected =<< trim END
605 1 utf-8 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01 605 1 utf-8 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01
606 2 cp1251 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01 606 2 cp1251 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01
607 3 cp866 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01 607 3 cp866 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01
608 END 608 END
609 call assert_equal(expected, readfile('Xtest')) 609 call assert_equal(expected, readfile('Xwfetest'))
610 610
611 call delete('Xfile') 611 call delete('Xwfefile')
612 call delete('Xtest') 612 call delete('Xwfetest')
613 call delete('Xutf8') 613 call delete('Xutf8')
614 call delete('Xcp1251') 614 call delete('Xcp1251')
615 call delete('Xcp866') 615 call delete('Xcp866')
616 let &encoding = save_encoding 616 let &encoding = save_encoding
617 let &fileencodings = save_fileencodings 617 let &fileencodings = save_fileencodings
633 let utf32le_bom = "\xFF\xFE\n\n" 633 let utf32le_bom = "\xFF\xFE\n\n"
634 let save_fileencoding = &fileencoding 634 let save_fileencoding = &fileencoding
635 set cpoptions+=S 635 set cpoptions+=S
636 636
637 " Check that editing a latin1 file doesn't see a BOM 637 " Check that editing a latin1 file doesn't see a BOM
638 call writefile(["\xFE\xFElatin-1"], 'Xtest1') 638 call writefile(["\xFE\xFElatin-1"], 'Xrwtest1')
639 edit Xtest1 639 edit Xrwtest1
640 call assert_equal('latin1', &fileencoding) 640 call assert_equal('latin1', &fileencoding)
641 call assert_equal(0, &bomb) 641 call assert_equal(0, &bomb)
642 set fenc=latin1 642 set fenc=latin1
643 write Xfile2 643 write Xrwfile2
644 call assert_equal(["\xFE\xFElatin-1", ''], readfile('Xfile2', 'b')) 644 call assert_equal(["\xFE\xFElatin-1", ''], readfile('Xrwfile2', 'b'))
645 set bomb fenc=latin1 645 set bomb fenc=latin1
646 write Xtest3 646 write Xrwtest3
647 call assert_equal(["\xFE\xFElatin-1", ''], readfile('Xtest3', 'b')) 647 call assert_equal(["\xFE\xFElatin-1", ''], readfile('Xrwtest3', 'b'))
648 set bomb& 648 set bomb&
649 649
650 " Check utf-8 BOM 650 " Check utf-8 BOM
651 %bw! 651 %bw!
652 call writefile([utf8_bom .. "utf-8"], 'Xtest1') 652 call writefile([utf8_bom .. "utf-8"], 'Xrwtest1')
653 edit! Xtest1 653 edit! Xrwtest1
654 call assert_equal('utf-8', &fileencoding) 654 call assert_equal('utf-8', &fileencoding)
655 call assert_equal(1, &bomb) 655 call assert_equal(1, &bomb)
656 call assert_equal('utf-8', getline(1)) 656 call assert_equal('utf-8', getline(1))
657 set fenc=latin1 657 set fenc=latin1
658 write! Xfile2 658 write! Xrwfile2
659 call assert_equal(['utf-8', ''], readfile('Xfile2', 'b')) 659 call assert_equal(['utf-8', ''], readfile('Xrwfile2', 'b'))
660 set fenc=utf-8 660 set fenc=utf-8
661 w! Xtest3 661 w! Xrwtest3
662 call assert_equal([utf8_bom .. "utf-8", ''], readfile('Xtest3', 'b')) 662 call assert_equal([utf8_bom .. "utf-8", ''], readfile('Xrwtest3', 'b'))
663 663
664 " Check utf-8 with an error (will fall back to latin-1) 664 " Check utf-8 with an error (will fall back to latin-1)
665 %bw! 665 %bw!
666 call writefile([utf8_bom .. "utf-8\x80err"], 'Xtest1') 666 call writefile([utf8_bom .. "utf-8\x80err"], 'Xrwtest1')
667 edit! Xtest1 667 edit! Xrwtest1
668 call assert_equal('latin1', &fileencoding) 668 call assert_equal('latin1', &fileencoding)
669 call assert_equal(0, &bomb) 669 call assert_equal(0, &bomb)
670 call assert_equal("\xC3\xAF\xC2\xBB\xC2\xBFutf-8\xC2\x80err", getline(1)) 670 call assert_equal("\xC3\xAF\xC2\xBB\xC2\xBFutf-8\xC2\x80err", getline(1))
671 set fenc=latin1 671 set fenc=latin1
672 write! Xfile2 672 write! Xrwfile2
673 call assert_equal([utf8_bom .. "utf-8\x80err", ''], readfile('Xfile2', 'b')) 673 call assert_equal([utf8_bom .. "utf-8\x80err", ''], readfile('Xrwfile2', 'b'))
674 set fenc=utf-8 674 set fenc=utf-8
675 w! Xtest3 675 w! Xrwtest3
676 call assert_equal(["\xC3\xAF\xC2\xBB\xC2\xBFutf-8\xC2\x80err", ''], 676 call assert_equal(["\xC3\xAF\xC2\xBB\xC2\xBFutf-8\xC2\x80err", ''],
677 \ readfile('Xtest3', 'b')) 677 \ readfile('Xrwtest3', 'b'))
678 678
679 " Check ucs-2 BOM 679 " Check ucs-2 BOM
680 %bw! 680 %bw!
681 call writefile([utf16be_bom .. "\nu\nc\ns\n-\n2\n"], 'Xtest1') 681 call writefile([utf16be_bom .. "\nu\nc\ns\n-\n2\n"], 'Xrwtest1')
682 edit! Xtest1 682 edit! Xrwtest1
683 call assert_equal('utf-16', &fileencoding) 683 call assert_equal('utf-16', &fileencoding)
684 call assert_equal(1, &bomb) 684 call assert_equal(1, &bomb)
685 call assert_equal('ucs-2', getline(1)) 685 call assert_equal('ucs-2', getline(1))
686 set fenc=latin1 686 set fenc=latin1
687 write! Xfile2 687 write! Xrwfile2
688 call assert_equal(["ucs-2", ''], readfile('Xfile2', 'b')) 688 call assert_equal(["ucs-2", ''], readfile('Xrwfile2', 'b'))
689 set fenc=ucs-2 689 set fenc=ucs-2
690 w! Xtest3 690 w! Xrwtest3
691 call assert_equal([utf16be_bom .. "\nu\nc\ns\n-\n2\n", ''], 691 call assert_equal([utf16be_bom .. "\nu\nc\ns\n-\n2\n", ''],
692 \ readfile('Xtest3', 'b')) 692 \ readfile('Xrwtest3', 'b'))
693 693
694 " Check ucs-2le BOM 694 " Check ucs-2le BOM
695 %bw! 695 %bw!
696 call writefile([utf16le_bom .. "u\nc\ns\n-\n2\nl\ne\n"], 'Xtest1') 696 call writefile([utf16le_bom .. "u\nc\ns\n-\n2\nl\ne\n"], 'Xrwtest1')
697 " Need to add a NUL byte after the NL byte 697 " Need to add a NUL byte after the NL byte
698 call writefile(0z00, 'Xtest1', 'a') 698 call writefile(0z00, 'Xrwtest1', 'a')
699 edit! Xtest1 699 edit! Xrwtest1
700 call assert_equal('utf-16le', &fileencoding) 700 call assert_equal('utf-16le', &fileencoding)
701 call assert_equal(1, &bomb) 701 call assert_equal(1, &bomb)
702 call assert_equal('ucs-2le', getline(1)) 702 call assert_equal('ucs-2le', getline(1))
703 set fenc=latin1 703 set fenc=latin1
704 write! Xfile2 704 write! Xrwfile2
705 call assert_equal(["ucs-2le", ''], readfile('Xfile2', 'b')) 705 call assert_equal(["ucs-2le", ''], readfile('Xrwfile2', 'b'))
706 set fenc=ucs-2le 706 set fenc=ucs-2le
707 w! Xtest3 707 w! Xrwtest3
708 call assert_equal([utf16le_bom .. "u\nc\ns\n-\n2\nl\ne\n", "\n"], 708 call assert_equal([utf16le_bom .. "u\nc\ns\n-\n2\nl\ne\n", "\n"],
709 \ readfile('Xtest3', 'b')) 709 \ readfile('Xrwtest3', 'b'))
710 710
711 " Check ucs-4 BOM 711 " Check ucs-4 BOM
712 %bw! 712 %bw!
713 call writefile([utf32be_bom .. "\n\n\nu\n\n\nc\n\n\ns\n\n\n-\n\n\n4\n\n\n"], 'Xtest1') 713 call writefile([utf32be_bom .. "\n\n\nu\n\n\nc\n\n\ns\n\n\n-\n\n\n4\n\n\n"], 'Xrwtest1')
714 edit! Xtest1 714 edit! Xrwtest1
715 call assert_equal('ucs-4', &fileencoding) 715 call assert_equal('ucs-4', &fileencoding)
716 call assert_equal(1, &bomb) 716 call assert_equal(1, &bomb)
717 call assert_equal('ucs-4', getline(1)) 717 call assert_equal('ucs-4', getline(1))
718 set fenc=latin1 718 set fenc=latin1
719 write! Xfile2 719 write! Xrwfile2
720 call assert_equal(["ucs-4", ''], readfile('Xfile2', 'b')) 720 call assert_equal(["ucs-4", ''], readfile('Xrwfile2', 'b'))
721 set fenc=ucs-4 721 set fenc=ucs-4
722 w! Xtest3 722 w! Xrwtest3
723 call assert_equal([utf32be_bom .. "\n\n\nu\n\n\nc\n\n\ns\n\n\n-\n\n\n4\n\n\n", ''], readfile('Xtest3', 'b')) 723 call assert_equal([utf32be_bom .. "\n\n\nu\n\n\nc\n\n\ns\n\n\n-\n\n\n4\n\n\n", ''], readfile('Xrwtest3', 'b'))
724 724
725 " Check ucs-4le BOM 725 " Check ucs-4le BOM
726 %bw! 726 %bw!
727 call writefile([utf32le_bom .. "u\n\n\nc\n\n\ns\n\n\n-\n\n\n4\n\n\nl\n\n\ne\n\n\n"], 'Xtest1') 727 call writefile([utf32le_bom .. "u\n\n\nc\n\n\ns\n\n\n-\n\n\n4\n\n\nl\n\n\ne\n\n\n"], 'Xrwtest1')
728 " Need to add three NUL bytes after the NL byte 728 " Need to add three NUL bytes after the NL byte
729 call writefile(0z000000, 'Xtest1', 'a') 729 call writefile(0z000000, 'Xrwtest1', 'a')
730 edit! Xtest1 730 edit! Xrwtest1
731 call assert_equal('ucs-4le', &fileencoding) 731 call assert_equal('ucs-4le', &fileencoding)
732 call assert_equal(1, &bomb) 732 call assert_equal(1, &bomb)
733 call assert_equal('ucs-4le', getline(1)) 733 call assert_equal('ucs-4le', getline(1))
734 set fenc=latin1 734 set fenc=latin1
735 write! Xfile2 735 write! Xrwfile2
736 call assert_equal(["ucs-4le", ''], readfile('Xfile2', 'b')) 736 call assert_equal(["ucs-4le", ''], readfile('Xrwfile2', 'b'))
737 set fenc=ucs-4le 737 set fenc=ucs-4le
738 w! Xtest3 738 w! Xrwtest3
739 call assert_equal([utf32le_bom .. "u\n\n\nc\n\n\ns\n\n\n-\n\n\n4\n\n\nl\n\n\ne\n\n\n", "\n\n\n"], readfile('Xtest3', 'b')) 739 call assert_equal([utf32le_bom .. "u\n\n\nc\n\n\ns\n\n\n-\n\n\n4\n\n\nl\n\n\ne\n\n\n", "\n\n\n"], readfile('Xrwtest3', 'b'))
740 740
741 set cpoptions-=S 741 set cpoptions-=S
742 let &fileencoding = save_fileencoding 742 let &fileencoding = save_fileencoding
743 call delete('Xtest1') 743 call delete('Xrwtest1')
744 call delete('Xfile2') 744 call delete('Xrwfile2')
745 call delete('Xtest3') 745 call delete('Xrwtest3')
746 %bw! 746 %bw!
747 endfunc 747 endfunc
748 748
749 func Test_read_write_bin() 749 func Test_read_write_bin()
750 " write file missing EOL 750 " write file missing EOL
771 CheckUnix 771 CheckUnix
772 set backupskip= 772 set backupskip=
773 " With the default 'backupcopy' setting, saving a symbolic link file 773 " With the default 'backupcopy' setting, saving a symbolic link file
774 " should not break the link. 774 " should not break the link.
775 set backupcopy& 775 set backupcopy&
776 call writefile(['1111'], 'Xfile1') 776 call writefile(['1111'], 'Xbcfile1')
777 silent !ln -s Xfile1 Xfile2 777 silent !ln -s Xbcfile1 Xbcfile2
778 new Xfile2 778 new Xbcfile2
779 call setline(1, ['2222']) 779 call setline(1, ['2222'])
780 write 780 write
781 close 781 close
782 call assert_equal(['2222'], readfile('Xfile1')) 782 call assert_equal(['2222'], readfile('Xbcfile1'))
783 call assert_equal('Xfile1', resolve('Xfile2')) 783 call assert_equal('Xbcfile1', resolve('Xbcfile2'))
784 call assert_equal('link', getftype('Xfile2')) 784 call assert_equal('link', getftype('Xbcfile2'))
785 call delete('Xfile1') 785 call delete('Xbcfile1')
786 call delete('Xfile2') 786 call delete('Xbcfile2')
787 787
788 " With the 'backupcopy' set to 'breaksymlink', saving a symbolic link file 788 " With the 'backupcopy' set to 'breaksymlink', saving a symbolic link file
789 " should break the link. 789 " should break the link.
790 set backupcopy=yes,breaksymlink 790 set backupcopy=yes,breaksymlink
791 call writefile(['1111'], 'Xfile1') 791 call writefile(['1111'], 'Xbcfile1')
792 silent !ln -s Xfile1 Xfile2 792 silent !ln -s Xbcfile1 Xbcfile2
793 new Xfile2 793 new Xbcfile2
794 call setline(1, ['2222']) 794 call setline(1, ['2222'])
795 write 795 write
796 close 796 close
797 call assert_equal(['1111'], readfile('Xfile1')) 797 call assert_equal(['1111'], readfile('Xbcfile1'))
798 call assert_equal(['2222'], readfile('Xfile2')) 798 call assert_equal(['2222'], readfile('Xbcfile2'))
799 call assert_equal('Xfile2', resolve('Xfile2')) 799 call assert_equal('Xbcfile2', resolve('Xbcfile2'))
800 call assert_equal('file', getftype('Xfile2')) 800 call assert_equal('file', getftype('Xbcfile2'))
801 call delete('Xfile1') 801 call delete('Xbcfile1')
802 call delete('Xfile2') 802 call delete('Xbcfile2')
803 set backupcopy& 803 set backupcopy&
804 804
805 " With the default 'backupcopy' setting, saving a hard link file 805 " With the default 'backupcopy' setting, saving a hard link file
806 " should not break the link. 806 " should not break the link.
807 set backupcopy& 807 set backupcopy&
808 call writefile(['1111'], 'Xfile1') 808 call writefile(['1111'], 'Xbcfile1')
809 silent !ln Xfile1 Xfile2 809 silent !ln Xbcfile1 Xbcfile2
810 new Xfile2 810 new Xbcfile2
811 call setline(1, ['2222']) 811 call setline(1, ['2222'])
812 write 812 write
813 close 813 close
814 call assert_equal(['2222'], readfile('Xfile1')) 814 call assert_equal(['2222'], readfile('Xbcfile1'))
815 call delete('Xfile1') 815 call delete('Xbcfile1')
816 call delete('Xfile2') 816 call delete('Xbcfile2')
817 817
818 " With the 'backupcopy' set to 'breaksymlink', saving a hard link file 818 " With the 'backupcopy' set to 'breaksymlink', saving a hard link file
819 " should break the link. 819 " should break the link.
820 set backupcopy=yes,breakhardlink 820 set backupcopy=yes,breakhardlink
821 call writefile(['1111'], 'Xfile1') 821 call writefile(['1111'], 'Xbcfile1')
822 silent !ln Xfile1 Xfile2 822 silent !ln Xbcfile1 Xbcfile2
823 new Xfile2 823 new Xbcfile2
824 call setline(1, ['2222']) 824 call setline(1, ['2222'])
825 write 825 write
826 call assert_equal(['1111'], readfile('Xfile1')) 826 call assert_equal(['1111'], readfile('Xbcfile1'))
827 call assert_equal(['2222'], readfile('Xfile2')) 827 call assert_equal(['2222'], readfile('Xbcfile2'))
828 call delete('Xfile1') 828 call delete('Xbcfile1')
829 call delete('Xfile2') 829 call delete('Xbcfile2')
830 830
831 " If a backup file is already present, then a slightly modified filename 831 " If a backup file is already present, then a slightly modified filename
832 " should be used as the backup file. Try with 'backupcopy' set to 'yes' and 832 " should be used as the backup file. Try with 'backupcopy' set to 'yes' and
833 " 'no'. 833 " 'no'.
834 %bw 834 %bw
835 call writefile(['aaaa'], 'Xfile') 835 call writefile(['aaaa'], 'Xbcfile')
836 call writefile(['bbbb'], 'Xfile.bak') 836 call writefile(['bbbb'], 'Xbcfile.bak')
837 set backupcopy=yes backupext=.bak 837 set backupcopy=yes backupext=.bak
838 new Xfile 838 new Xbcfile
839 call setline(1, ['cccc']) 839 call setline(1, ['cccc'])
840 write 840 write
841 close 841 close
842 call assert_equal(['cccc'], readfile('Xfile')) 842 call assert_equal(['cccc'], readfile('Xbcfile'))
843 call assert_equal(['bbbb'], readfile('Xfile.bak')) 843 call assert_equal(['bbbb'], readfile('Xbcfile.bak'))
844 set backupcopy=no backupext=.bak 844 set backupcopy=no backupext=.bak
845 new Xfile 845 new Xbcfile
846 call setline(1, ['dddd']) 846 call setline(1, ['dddd'])
847 write 847 write
848 close 848 close
849 call assert_equal(['dddd'], readfile('Xfile')) 849 call assert_equal(['dddd'], readfile('Xbcfile'))
850 call assert_equal(['bbbb'], readfile('Xfile.bak')) 850 call assert_equal(['bbbb'], readfile('Xbcfile.bak'))
851 call delete('Xfile') 851 call delete('Xbcfile')
852 call delete('Xfile.bak') 852 call delete('Xbcfile.bak')
853 853
854 " Write to a device file (in Unix-like systems) which cannot be backed up. 854 " Write to a device file (in Unix-like systems) which cannot be backed up.
855 if has('unix') 855 if has('unix')
856 set writebackup backupcopy=yes nobackup 856 set writebackup backupcopy=yes nobackup
857 new 857 new
878 878
879 " Test for writing a file with 'encoding' set to 'utf-16' 879 " Test for writing a file with 'encoding' set to 'utf-16'
880 func Test_write_utf16() 880 func Test_write_utf16()
881 new 881 new
882 call setline(1, ["\U00010001"]) 882 call setline(1, ["\U00010001"])
883 write ++enc=utf-16 Xfile 883 write ++enc=utf-16 Xw16file
884 bw! 884 bw!
885 call assert_equal(0zD800DC01, readfile('Xfile', 'B')[0:3]) 885 call assert_equal(0zD800DC01, readfile('Xw16file', 'B')[0:3])
886 call delete('Xfile') 886 call delete('Xw16file')
887 endfunc 887 endfunc
888 888
889 " Test for trying to save a backup file when the backup file is a symbolic 889 " Test for trying to save a backup file when the backup file is a symbolic
890 " link to the original file. The backup file should not be modified. 890 " link to the original file. The backup file should not be modified.
891 func Test_write_backup_symlink() 891 func Test_write_backup_symlink()
892 CheckUnix 892 CheckUnix
893 call mkdir('Xbackup') 893 call mkdir('Xbackup')
894 let save_backupdir = &backupdir 894 let save_backupdir = &backupdir
895 set backupdir=.,./Xbackup 895 set backupdir=.,./Xbackup
896 call writefile(['1111'], 'Xfile') 896 call writefile(['1111'], 'Xwbsfile')
897 silent !ln -s Xfile Xfile.bak 897 silent !ln -s Xwbsfile Xwbsfile.bak
898 898
899 new Xfile 899 new Xwbsfile
900 set backup backupcopy=yes backupext=.bak 900 set backup backupcopy=yes backupext=.bak
901 write 901 write
902 call assert_equal('link', getftype('Xfile.bak')) 902 call assert_equal('link', getftype('Xwbsfile.bak'))
903 call assert_equal('Xfile', resolve('Xfile.bak')) 903 call assert_equal('Xwbsfile', resolve('Xwbsfile.bak'))
904 " backup file should be created in the 'backup' directory 904 " backup file should be created in the 'backup' directory
905 if !has('bsd') 905 if !has('bsd')
906 " This check fails on FreeBSD 906 " This check fails on FreeBSD
907 call assert_true(filereadable('./Xbackup/Xfile.bak')) 907 call assert_true(filereadable('./Xbackup/Xwbsfile.bak'))
908 endif 908 endif
909 set backup& backupcopy& backupext& 909 set backup& backupcopy& backupext&
910 %bw 910 %bw
911 911
912 call delete('Xfile') 912 call delete('Xwbsfile')
913 call delete('Xfile.bak') 913 call delete('Xwbsfile.bak')
914 call delete('Xbackup', 'rf') 914 call delete('Xbackup', 'rf')
915 let &backupdir = save_backupdir 915 let &backupdir = save_backupdir
916 endfunc 916 endfunc
917 917
918 " Test for ':write ++bin' and ':write ++nobin' 918 " Test for ':write ++bin' and ':write ++nobin'
919 func Test_write_binary_file() 919 func Test_write_binary_file()
920 " create a file without an eol/eof character 920 " create a file without an eol/eof character
921 call writefile(0z616161, 'Xfile1', 'b') 921 call writefile(0z616161, 'Xwbfile1', 'b')
922 new Xfile1 922 new Xwbfile1
923 write ++bin Xfile2 923 write ++bin Xwbfile2
924 write ++nobin Xfile3 924 write ++nobin Xwbfile3
925 call assert_equal(0z616161, readblob('Xfile2')) 925 call assert_equal(0z616161, readblob('Xwbfile2'))
926 if has('win32') 926 if has('win32')
927 call assert_equal(0z6161610D.0A, readblob('Xfile3')) 927 call assert_equal(0z6161610D.0A, readblob('Xwbfile3'))
928 else 928 else
929 call assert_equal(0z6161610A, readblob('Xfile3')) 929 call assert_equal(0z6161610A, readblob('Xwbfile3'))
930 endif 930 endif
931 call delete('Xfile1') 931 call delete('Xwbfile1')
932 call delete('Xfile2') 932 call delete('Xwbfile2')
933 call delete('Xfile3') 933 call delete('Xwbfile3')
934 endfunc 934 endfunc
935 935
936 " Check that buffer is written before triggering QuitPre 936 " Check that buffer is written before triggering QuitPre
937 func Test_wq_quitpre_autocommand() 937 func Test_wq_quitpre_autocommand()
938 edit Xsomefile 938 edit Xsomefile