blob: e58093074ca3b12785ac63531be8f6da8fbfe161 [file] [log] [blame]
Bram Moolenaarb40c2572019-10-19 21:01:05 +02001" Tests for the writefile() function and some :write commands.
Bram Moolenaar19a16692016-09-01 22:19:47 +02002
Bram Moolenaarea3db912020-02-02 15:32:13 +01003source check.vim
Bram Moolenaar494e9062020-05-31 21:28:02 +02004source term_util.vim
Bram Moolenaarea3db912020-02-02 15:32:13 +01005
Bram Moolenaar8cf91282017-06-13 19:38:37 +02006func Test_writefile()
Bram Moolenaar19a16692016-09-01 22:19:47 +02007 let f = tempname()
8 call writefile(["over","written"], f, "b")
9 call writefile(["hello","world"], f, "b")
10 call writefile(["!", "good"], f, "a")
11 call writefile(["morning"], f, "ab")
12 call writefile(["", "vimmers"], f, "ab")
13 let l = readfile(f)
14 call assert_equal("hello", l[0])
15 call assert_equal("world!", l[1])
16 call assert_equal("good", l[2])
17 call assert_equal("morning", l[3])
18 call assert_equal("vimmers", l[4])
19 call delete(f)
Bram Moolenaar18a2b872020-03-19 13:08:45 +010020
21 call assert_fails('call writefile("text", "Xfile")', 'E475: Invalid argument: writefile() first argument must be a List or a Blob')
Bram Moolenaar8cf91282017-06-13 19:38:37 +020022endfunc
23
Bram Moolenaarb40c2572019-10-19 21:01:05 +020024func Test_writefile_ignore_regexp_error()
25 write Xt[z-a]est.txt
26 call delete('Xt[z-a]est.txt')
27endfunc
28
Bram Moolenaar8cf91282017-06-13 19:38:37 +020029func Test_writefile_fails_gently()
30 call assert_fails('call writefile(["test"], "Xfile", [])', 'E730:')
31 call assert_false(filereadable("Xfile"))
32 call delete("Xfile")
33
34 call assert_fails('call writefile(["test", [], [], [], "tset"], "Xfile")', 'E730:')
35 call assert_false(filereadable("Xfile"))
36 call delete("Xfile")
37
38 call assert_fails('call writefile([], "Xfile", [])', 'E730:')
39 call assert_false(filereadable("Xfile"))
40 call delete("Xfile")
41
42 call assert_fails('call writefile([], [])', 'E730:')
43endfunc
Bram Moolenaare6bf6552017-06-27 22:11:51 +020044
45func Test_writefile_fails_conversion()
Bram Moolenaar39536dd2019-01-29 22:58:21 +010046 if !has('iconv') || has('sun')
Bram Moolenaare6bf6552017-06-27 22:11:51 +020047 return
48 endif
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020049 " Without a backup file the write won't happen if there is a conversion
50 " error.
Bram Moolenaarc28cb5b2019-05-31 20:42:09 +020051 set nobackup nowritebackup backupdir=. backupskip=
Bram Moolenaare6bf6552017-06-27 22:11:51 +020052 new
53 let contents = ["line one", "line two"]
54 call writefile(contents, 'Xfile')
55 edit Xfile
56 call setline(1, ["first line", "cannot convert \u010b", "third line"])
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020057 call assert_fails('write ++enc=cp932', 'E513:')
Bram Moolenaare6bf6552017-06-27 22:11:51 +020058 call assert_equal(contents, readfile('Xfile'))
59
60 call delete('Xfile')
61 bwipe!
Bram Moolenaarc28cb5b2019-05-31 20:42:09 +020062 set backup& writebackup& backupdir&vim backupskip&vim
Bram Moolenaare6bf6552017-06-27 22:11:51 +020063endfunc
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +020064
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020065func Test_writefile_fails_conversion2()
66 if !has('iconv') || has('sun')
67 return
68 endif
69 " With a backup file the write happens even if there is a conversion error,
70 " but then the backup file must remain
Bram Moolenaarc28cb5b2019-05-31 20:42:09 +020071 set nobackup writebackup backupdir=. backupskip=
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020072 let contents = ["line one", "line two"]
73 call writefile(contents, 'Xfile_conversion_err')
74 edit Xfile_conversion_err
75 call setline(1, ["first line", "cannot convert \u010b", "third line"])
76 set fileencoding=latin1
77 let output = execute('write')
78 call assert_match('CONVERSION ERROR', output)
79 call assert_equal(contents, readfile('Xfile_conversion_err~'))
80
81 call delete('Xfile_conversion_err')
82 call delete('Xfile_conversion_err~')
83 bwipe!
Bram Moolenaarc28cb5b2019-05-31 20:42:09 +020084 set backup& writebackup& backupdir&vim backupskip&vim
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020085endfunc
86
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +020087func SetFlag(timer)
88 let g:flag = 1
89endfunc
90
91func Test_write_quit_split()
92 " Prevent exiting by splitting window on file write.
93 augroup testgroup
94 autocmd BufWritePre * split
95 augroup END
96 e! Xfile
97 call setline(1, 'nothing')
98 wq
99
100 if has('timers')
101 " timer will not run if "exiting" is still set
102 let g:flag = 0
103 call timer_start(1, 'SetFlag')
104 sleep 50m
105 call assert_equal(1, g:flag)
106 unlet g:flag
107 endif
108 au! testgroup
109 bwipe Xfile
110 call delete('Xfile')
111endfunc
112
113func Test_nowrite_quit_split()
114 " Prevent exiting by opening a help window.
115 e! Xfile
116 help
117 wincmd w
118 exe winnr() . 'q'
119
120 if has('timers')
121 " timer will not run if "exiting" is still set
122 let g:flag = 0
123 call timer_start(1, 'SetFlag')
124 sleep 50m
125 call assert_equal(1, g:flag)
126 unlet g:flag
127 endif
128 bwipe Xfile
129endfunc
Bram Moolenaar7567d0b2017-11-16 23:04:15 +0100130
131func Test_writefile_sync_arg()
132 " This doesn't check if fsync() works, only that the argument is accepted.
133 call writefile(['one'], 'Xtest', 's')
134 call writefile(['two'], 'Xtest', 'S')
135 call delete('Xtest')
136endfunc
Bram Moolenaar83799a72017-11-25 17:24:09 +0100137
138func Test_writefile_sync_dev_stdout()
Bram Moolenaarb86abad2020-08-01 16:08:19 +0200139 CheckUnix
Bram Moolenaar9980b372018-04-21 20:12:35 +0200140 if filewritable('/dev/stdout')
141 " Just check that this doesn't cause an error.
142 call writefile(['one'], '/dev/stdout')
143 else
144 throw 'Skipped: /dev/stdout is not writable'
145 endif
Bram Moolenaar83799a72017-11-25 17:24:09 +0100146endfunc
Bram Moolenaar8c9e7b02018-08-30 13:07:17 +0200147
148func Test_writefile_autowrite()
149 set autowrite
150 new
151 next Xa Xb Xc
152 call setline(1, 'aaa')
153 next
154 call assert_equal(['aaa'], readfile('Xa'))
155 call setline(1, 'bbb')
156 call assert_fails('edit XX')
157 call assert_false(filereadable('Xb'))
158
159 set autowriteall
160 edit XX
161 call assert_equal(['bbb'], readfile('Xb'))
162
163 bwipe!
164 call delete('Xa')
165 call delete('Xb')
166 set noautowrite
167endfunc
168
169func Test_writefile_autowrite_nowrite()
170 set autowrite
171 new
172 next Xa Xb Xc
173 set buftype=nowrite
174 call setline(1, 'aaa')
175 let buf = bufnr('%')
176 " buffer contents silently lost
177 edit XX
178 call assert_false(filereadable('Xa'))
179 rewind
180 call assert_equal('', getline(1))
181
182 bwipe!
183 set noautowrite
184endfunc
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100185
186" Test for ':w !<cmd>' to pipe lines from the current buffer to an external
187" command.
188func Test_write_pipe_to_cmd()
Bram Moolenaarea3db912020-02-02 15:32:13 +0100189 CheckUnix
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100190 new
191 call setline(1, ['L1', 'L2', 'L3', 'L4'])
192 2,3w !cat > Xfile
193 call assert_equal(['L2', 'L3'], readfile('Xfile'))
194 close!
195 call delete('Xfile')
196endfunc
197
198" Test for :saveas
199func Test_saveas()
200 call assert_fails('saveas', 'E471:')
201 call writefile(['L1'], 'Xfile')
202 new Xfile
203 new
204 call setline(1, ['L1'])
205 call assert_fails('saveas Xfile', 'E139:')
206 close!
207 enew | only
208 call delete('Xfile')
209endfunc
210
211func Test_write_errors()
212 " Test for writing partial buffer
213 call writefile(['L1', 'L2', 'L3'], 'Xfile')
214 new Xfile
215 call assert_fails('1,2write', 'E140:')
216 close!
217
Bram Moolenaar4f5776c2020-02-12 22:15:19 +0100218 call assert_fails('w > Xtest', 'E494:')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100219
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100220 " Try to overwrite a directory
221 if has('unix')
222 call mkdir('Xdir1')
223 call assert_fails('write Xdir1', 'E17:')
224 call delete('Xdir1', 'd')
225 endif
226
227 " Test for :wall for a buffer with no name
228 enew | only
229 call setline(1, ['L1'])
230 call assert_fails('wall', 'E141:')
231 enew!
232
233 " Test for writing a 'readonly' file
234 new Xfile
235 set readonly
236 call assert_fails('write', 'E45:')
237 close
238
239 " Test for writing to a read-only file
240 new Xfile
241 call setfperm('Xfile', 'r--r--r--')
242 call assert_fails('write', 'E505:')
243 call setfperm('Xfile', 'rw-rw-rw-')
244 close
245
246 call delete('Xfile')
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200247
248 call writefile(test_null_list(), 'Xfile')
249 call assert_false(filereadable('Xfile'))
250 call writefile(test_null_blob(), 'Xfile')
251 call assert_false(filereadable('Xfile'))
252 call assert_fails('call writefile([], "")', 'E482:')
Bram Moolenaar494e9062020-05-31 21:28:02 +0200253
254 " very long file name
255 let long_fname = repeat('n', 5000)
256 call assert_fails('exe "w " .. long_fname', 'E75:')
257 call assert_fails('call writefile([], long_fname)', 'E482:')
258endfunc
259
260" Test for writing to a file which is modified after Vim read it
261func Test_write_file_mtime()
262 CheckEnglish
263 CheckRunVimInTerminal
264
265 " First read the file into a buffer
266 call writefile(["Line1", "Line2"], 'Xfile')
267 let old_ftime = getftime('Xfile')
268 let buf = RunVimInTerminal('Xfile', #{rows : 10})
269 call term_wait(buf)
270 call term_sendkeys(buf, ":set noswapfile\<CR>")
271 call term_wait(buf)
272
273 " Modify the file directly. Make sure the file modification time is
274 " different. Note that on Linux/Unix, the file is considered modified
275 " outside, only if the difference is 2 seconds or more
276 sleep 1
277 call writefile(["Line3", "Line4"], 'Xfile')
278 let new_ftime = getftime('Xfile')
279 while new_ftime - old_ftime < 2
280 sleep 100m
281 call writefile(["Line3", "Line4"], 'Xfile')
282 let new_ftime = getftime('Xfile')
283 endwhile
284
285 " Try to overwrite the file and check for the prompt
286 call term_sendkeys(buf, ":w\<CR>")
287 call term_wait(buf)
288 call WaitForAssert({-> assert_equal("WARNING: The file has been changed since reading it!!!", term_getline(buf, 9))})
289 call assert_equal("Do you really want to write to it (y/n)?",
290 \ term_getline(buf, 10))
291 call term_sendkeys(buf, "n\<CR>")
292 call term_wait(buf)
293 call assert_equal(new_ftime, getftime('Xfile'))
294 call term_sendkeys(buf, ":w\<CR>")
295 call term_wait(buf)
296 call term_sendkeys(buf, "y\<CR>")
297 call term_wait(buf)
298 call WaitForAssert({-> assert_equal('Line2', readfile('Xfile')[1])})
299
300 " clean up
301 call StopVimInTerminal(buf)
302 call delete('Xfile')
303endfunc
304
305" Test for an autocmd unloading a buffer during a write command
306func Test_write_autocmd_unloadbuf_lockmark()
307 augroup WriteTest
308 autocmd BufWritePre Xfile enew | write
309 augroup END
310 e Xfile
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200311 call assert_fails('lockmarks write', ['E32', 'E203:'])
Bram Moolenaar494e9062020-05-31 21:28:02 +0200312 augroup WriteTest
313 au!
314 augroup END
315 augroup! WriteTest
316endfunc
317
318" Test for writing a buffer with 'acwrite' but without autocmds
319func Test_write_acwrite_error()
320 new Xfile
321 call setline(1, ['line1', 'line2', 'line3'])
322 set buftype=acwrite
323 call assert_fails('write', 'E676:')
324 call assert_fails('1,2write!', 'E676:')
325 call assert_fails('w >>', 'E676:')
326 close!
327endfunc
328
329" Test for adding and removing lines from an autocmd when writing a buffer
330func Test_write_autocmd_add_remove_lines()
331 new Xfile
332 call setline(1, ['aaa', 'bbb', 'ccc', 'ddd'])
333
334 " Autocmd deleting lines from the file when writing a partial file
335 augroup WriteTest2
336 au!
337 autocmd FileWritePre Xfile 1,2d
338 augroup END
339 call assert_fails('2,3w!', 'E204:')
340
341 " Autocmd adding lines to a file when writing a partial file
342 augroup WriteTest2
343 au!
344 autocmd FileWritePre Xfile call append(0, ['xxx', 'yyy'])
345 augroup END
346 %d
347 call setline(1, ['aaa', 'bbb', 'ccc', 'ddd'])
348 1,2w!
349 call assert_equal(['xxx', 'yyy', 'aaa', 'bbb'], readfile('Xfile'))
350
351 " Autocmd deleting lines from the file when writing the whole file
352 augroup WriteTest2
353 au!
354 autocmd BufWritePre Xfile 1,2d
355 augroup END
356 %d
357 call setline(1, ['aaa', 'bbb', 'ccc', 'ddd'])
358 w
359 call assert_equal(['ccc', 'ddd'], readfile('Xfile'))
360
361 augroup WriteTest2
362 au!
363 augroup END
364 augroup! WriteTest2
365
366 close!
367 call delete('Xfile')
368endfunc
369
370" Test for writing to a readonly file
371func Test_write_readonly()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200372 call writefile([], 'Xfile')
373 call setfperm('Xfile', "r--------")
374 edit Xfile
Bram Moolenaarb86abad2020-08-01 16:08:19 +0200375 set noreadonly backupskip=
Bram Moolenaar494e9062020-05-31 21:28:02 +0200376 call assert_fails('write', 'E505:')
377 let save_cpo = &cpo
378 set cpo+=W
379 call assert_fails('write!', 'E504:')
380 let &cpo = save_cpo
Bram Moolenaar1de5f7c2020-06-11 19:22:43 +0200381 call setline(1, ['line1'])
382 write!
383 call assert_equal(['line1'], readfile('Xfile'))
Bram Moolenaarb86abad2020-08-01 16:08:19 +0200384 set backupskip&
Bram Moolenaar494e9062020-05-31 21:28:02 +0200385 call delete('Xfile')
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100386endfunc
387
Bram Moolenaar1de5f7c2020-06-11 19:22:43 +0200388" Test for 'patchmode'
389func Test_patchmode()
Bram Moolenaar1de5f7c2020-06-11 19:22:43 +0200390 call writefile(['one'], 'Xfile')
Bram Moolenaarb86abad2020-08-01 16:08:19 +0200391 set patchmode=.orig nobackup backupskip= writebackup
Bram Moolenaar1de5f7c2020-06-11 19:22:43 +0200392 new Xfile
393 call setline(1, 'two')
394 " first write should create the .orig file
395 write
Bram Moolenaar1de5f7c2020-06-11 19:22:43 +0200396 call assert_equal(['one'], readfile('Xfile.orig'))
397 call setline(1, 'three')
398 " subsequent writes should not create/modify the .orig file
399 write
400 call assert_equal(['one'], readfile('Xfile.orig'))
Bram Moolenaarb86abad2020-08-01 16:08:19 +0200401 set patchmode& backup& backupskip& writebackup&
Bram Moolenaar1de5f7c2020-06-11 19:22:43 +0200402 call delete('Xfile')
403 call delete('Xfile.orig')
404endfunc
405
406" Test for writing to a file in a readonly directory
407func Test_write_readonly_dir()
Bram Moolenaarb86abad2020-08-01 16:08:19 +0200408 " On MS-Windows, modifying files in a read-only directory is allowed.
409 CheckUnix
Bram Moolenaar1de5f7c2020-06-11 19:22:43 +0200410 call mkdir('Xdir')
411 call writefile(['one'], 'Xdir/Xfile1')
412 call setfperm('Xdir', 'r-xr--r--')
413 " try to create a new file in the directory
414 new Xdir/Xfile2
415 call setline(1, 'two')
416 call assert_fails('write', 'E212:')
417 " try to create a backup file in the directory
418 edit! Xdir/Xfile1
Bram Moolenaarb86abad2020-08-01 16:08:19 +0200419 set backupdir=./Xdir backupskip=
Bram Moolenaar1de5f7c2020-06-11 19:22:43 +0200420 set patchmode=.orig
421 call assert_fails('write', 'E509:')
422 call setfperm('Xdir', 'rwxr--r--')
423 call delete('Xdir', 'rf')
Bram Moolenaarb86abad2020-08-01 16:08:19 +0200424 set backupdir& backupskip& patchmode&
Bram Moolenaar1de5f7c2020-06-11 19:22:43 +0200425endfunc
426
Bram Moolenaarb340bae2020-06-15 19:51:56 +0200427" Test for writing a file using invalid file encoding
428func Test_write_invalid_encoding()
429 new
430 call setline(1, 'abc')
431 call assert_fails('write ++enc=axbyc Xfile', 'E213:')
432 close!
433endfunc
434
Bram Moolenaar622b3562020-07-27 20:02:41 +0200435" Tests for reading and writing files with conversion for Win32.
436func Test_write_file_encoding()
437 CheckMSWindows
438 let save_encoding = &encoding
439 let save_fileencodings = &fileencodings
440 set encoding& fileencodings&
441 let text =<< trim END
442 1 utf-8 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01
443 2 cp1251 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01
444 3 cp866 text: „«ï Vim version 6.2. ®á«¥¤­¥¥ ¨§¬¥­¥­¨¥: 1970 Jan 01
445 END
446 call writefile(text, 'Xfile')
447 edit Xfile
448
449 " write tests:
450 " combine three values for 'encoding' with three values for 'fileencoding'
451 " also write files for read tests
452 call cursor(1, 1)
453 set encoding=utf-8
454 .w! ++enc=utf-8 Xtest
455 .w ++enc=cp1251 >> Xtest
456 .w ++enc=cp866 >> Xtest
457 .w! ++enc=utf-8 Xutf8
458 let expected =<< trim END
459 1 utf-8 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01
460 1 utf-8 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01
461 1 utf-8 text: „«ï Vim version 6.2. ®á«¥¤­¥¥ ¨§¬¥­¥­¨¥: 1970 Jan 01
462 END
463 call assert_equal(expected, readfile('Xtest'))
464
465 call cursor(2, 1)
466 set encoding=cp1251
467 .w! ++enc=utf-8 Xtest
468 .w ++enc=cp1251 >> Xtest
469 .w ++enc=cp866 >> Xtest
470 .w! ++enc=cp1251 Xcp1251
471 let expected =<< trim END
472 2 cp1251 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01
473 2 cp1251 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01
474 2 cp1251 text: „«ï Vim version 6.2. ®á«¥¤­¥¥ ¨§¬¥­¥­¨¥: 1970 Jan 01
475 END
476 call assert_equal(expected, readfile('Xtest'))
477
478 call cursor(3, 1)
479 set encoding=cp866
480 .w! ++enc=utf-8 Xtest
481 .w ++enc=cp1251 >> Xtest
482 .w ++enc=cp866 >> Xtest
483 .w! ++enc=cp866 Xcp866
484 let expected =<< trim END
485 3 cp866 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01
486 3 cp866 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01
487 3 cp866 text: „«ï Vim version 6.2. ®á«¥¤­¥¥ ¨§¬¥­¥­¨¥: 1970 Jan 01
488 END
489 call assert_equal(expected, readfile('Xtest'))
490
491 " read three 'fileencoding's with utf-8 'encoding'
492 set encoding=utf-8 fencs=utf-8,cp1251
493 e Xutf8
494 .w! ++enc=utf-8 Xtest
495 e Xcp1251
496 .w ++enc=utf-8 >> Xtest
497 set fencs=utf-8,cp866
498 e Xcp866
499 .w ++enc=utf-8 >> Xtest
500 let expected =<< trim END
501 1 utf-8 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01
502 2 cp1251 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01
503 3 cp866 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01
504 END
505 call assert_equal(expected, readfile('Xtest'))
506
507 " read three 'fileencoding's with cp1251 'encoding'
508 set encoding=utf-8 fencs=utf-8,cp1251
509 e Xutf8
510 .w! ++enc=cp1251 Xtest
511 e Xcp1251
512 .w ++enc=cp1251 >> Xtest
513 set fencs=utf-8,cp866
514 e Xcp866
515 .w ++enc=cp1251 >> Xtest
516 let expected =<< trim END
517 1 utf-8 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01
518 2 cp1251 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01
519 3 cp866 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01
520 END
521 call assert_equal(expected, readfile('Xtest'))
522
523 " read three 'fileencoding's with cp866 'encoding'
524 set encoding=cp866 fencs=utf-8,cp1251
525 e Xutf8
526 .w! ++enc=cp866 Xtest
527 e Xcp1251
528 .w ++enc=cp866 >> Xtest
529 set fencs=utf-8,cp866
530 e Xcp866
531 .w ++enc=cp866 >> Xtest
532 let expected =<< trim END
533 1 utf-8 text: „«ï Vim version 6.2. ®á«¥¤­¥¥ ¨§¬¥­¥­¨¥: 1970 Jan 01
534 2 cp1251 text: „«ï Vim version 6.2. ®á«¥¤­¥¥ ¨§¬¥­¥­¨¥: 1970 Jan 01
535 3 cp866 text: „«ï Vim version 6.2. ®á«¥¤­¥¥ ¨§¬¥­¥­¨¥: 1970 Jan 01
536 END
537 call assert_equal(expected, readfile('Xtest'))
538
539 call delete('Xfile')
540 call delete('Xtest')
541 call delete('Xutf8')
542 call delete('Xcp1251')
543 call delete('Xcp866')
544 let &encoding = save_encoding
545 let &fileencodings = save_fileencodings
546 %bw!
547endfunc
548
Bram Moolenaarb61ef012020-07-29 16:08:21 +0200549" Test for writing and reading a file starting with a BOM.
550" Byte Order Mark (BOM) character for various encodings is below:
551" UTF-8 : EF BB BF
552" UTF-16 (BE): FE FF
553" UTF-16 (LE): FF FE
554" UTF-32 (BE): 00 00 FE FF
555" UTF-32 (LE): FF FE 00 00
556func Test_readwrite_file_with_bom()
557 let utf8_bom = "\xEF\xBB\xBF"
558 let utf16be_bom = "\xFE\xFF"
559 let utf16le_bom = "\xFF\xFE"
560 let utf32be_bom = "\n\n\xFE\xFF"
561 let utf32le_bom = "\xFF\xFE\n\n"
562 let save_fileencoding = &fileencoding
563 set cpoptions+=S
564
565 " Check that editing a latin1 file doesn't see a BOM
566 call writefile(["\xFE\xFElatin-1"], 'Xtest1')
567 edit Xtest1
568 call assert_equal('latin1', &fileencoding)
569 call assert_equal(0, &bomb)
570 set fenc=latin1
571 write Xfile2
572 call assert_equal(["\xFE\xFElatin-1", ''], readfile('Xfile2', 'b'))
573 set bomb fenc=latin1
574 write Xtest3
575 call assert_equal(["\xFE\xFElatin-1", ''], readfile('Xtest3', 'b'))
576 set bomb&
577
578 " Check utf-8 BOM
579 %bw!
580 call writefile([utf8_bom .. "utf-8"], 'Xtest1')
581 edit! Xtest1
582 call assert_equal('utf-8', &fileencoding)
583 call assert_equal(1, &bomb)
584 call assert_equal('utf-8', getline(1))
585 set fenc=latin1
586 write! Xfile2
587 call assert_equal(['utf-8', ''], readfile('Xfile2', 'b'))
588 set fenc=utf-8
589 w! Xtest3
590 call assert_equal([utf8_bom .. "utf-8", ''], readfile('Xtest3', 'b'))
591
592 " Check utf-8 with an error (will fall back to latin-1)
593 %bw!
594 call writefile([utf8_bom .. "utf-8\x80err"], 'Xtest1')
595 edit! Xtest1
596 call assert_equal('latin1', &fileencoding)
597 call assert_equal(0, &bomb)
598 call assert_equal("\xC3\xAF\xC2\xBB\xC2\xBFutf-8\xC2\x80err", getline(1))
599 set fenc=latin1
600 write! Xfile2
601 call assert_equal([utf8_bom .. "utf-8\x80err", ''], readfile('Xfile2', 'b'))
602 set fenc=utf-8
603 w! Xtest3
604 call assert_equal(["\xC3\xAF\xC2\xBB\xC2\xBFutf-8\xC2\x80err", ''],
605 \ readfile('Xtest3', 'b'))
606
607 " Check ucs-2 BOM
608 %bw!
609 call writefile([utf16be_bom .. "\nu\nc\ns\n-\n2\n"], 'Xtest1')
610 edit! Xtest1
611 call assert_equal('utf-16', &fileencoding)
612 call assert_equal(1, &bomb)
613 call assert_equal('ucs-2', getline(1))
614 set fenc=latin1
615 write! Xfile2
616 call assert_equal(["ucs-2", ''], readfile('Xfile2', 'b'))
617 set fenc=ucs-2
618 w! Xtest3
619 call assert_equal([utf16be_bom .. "\nu\nc\ns\n-\n2\n", ''],
620 \ readfile('Xtest3', 'b'))
621
622 " Check ucs-2le BOM
623 %bw!
624 call writefile([utf16le_bom .. "u\nc\ns\n-\n2\nl\ne\n"], 'Xtest1')
625 " Need to add a NUL byte after the NL byte
626 call writefile(0z00, 'Xtest1', 'a')
627 edit! Xtest1
628 call assert_equal('utf-16le', &fileencoding)
629 call assert_equal(1, &bomb)
630 call assert_equal('ucs-2le', getline(1))
631 set fenc=latin1
632 write! Xfile2
633 call assert_equal(["ucs-2le", ''], readfile('Xfile2', 'b'))
634 set fenc=ucs-2le
635 w! Xtest3
636 call assert_equal([utf16le_bom .. "u\nc\ns\n-\n2\nl\ne\n", "\n"],
637 \ readfile('Xtest3', 'b'))
638
639 " Check ucs-4 BOM
640 %bw!
641 call writefile([utf32be_bom .. "\n\n\nu\n\n\nc\n\n\ns\n\n\n-\n\n\n4\n\n\n"], 'Xtest1')
642 edit! Xtest1
643 call assert_equal('ucs-4', &fileencoding)
644 call assert_equal(1, &bomb)
645 call assert_equal('ucs-4', getline(1))
646 set fenc=latin1
647 write! Xfile2
648 call assert_equal(["ucs-4", ''], readfile('Xfile2', 'b'))
649 set fenc=ucs-4
650 w! Xtest3
651 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'))
652
653 " Check ucs-4le BOM
654 %bw!
655 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')
656 " Need to add three NUL bytes after the NL byte
657 call writefile(0z000000, 'Xtest1', 'a')
658 edit! Xtest1
659 call assert_equal('ucs-4le', &fileencoding)
660 call assert_equal(1, &bomb)
661 call assert_equal('ucs-4le', getline(1))
662 set fenc=latin1
663 write! Xfile2
664 call assert_equal(["ucs-4le", ''], readfile('Xfile2', 'b'))
665 set fenc=ucs-4le
666 w! Xtest3
667 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'))
668
669 set cpoptions-=S
670 let &fileencoding = save_fileencoding
671 call delete('Xtest1')
672 call delete('Xtest2')
673 call delete('Xtest3')
674 %bw!
675endfunc
676
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100677" vim: shiftwidth=2 sts=2 expandtab