blob: 7256c65847096af2289b1a8a115f395f7a0d8e0d [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 Moolenaar6d91bcb2020-08-12 18:50:36 +020046 CheckFeature iconv
47 if has('sun')
48 throw 'Skipped: does not work on SunOS'
Bram Moolenaare6bf6552017-06-27 22:11:51 +020049 endif
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020050 " Without a backup file the write won't happen if there is a conversion
51 " error.
Bram Moolenaarc28cb5b2019-05-31 20:42:09 +020052 set nobackup nowritebackup backupdir=. backupskip=
Bram Moolenaare6bf6552017-06-27 22:11:51 +020053 new
54 let contents = ["line one", "line two"]
55 call writefile(contents, 'Xfile')
56 edit Xfile
57 call setline(1, ["first line", "cannot convert \u010b", "third line"])
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020058 call assert_fails('write ++enc=cp932', 'E513:')
Bram Moolenaare6bf6552017-06-27 22:11:51 +020059 call assert_equal(contents, readfile('Xfile'))
60
61 call delete('Xfile')
62 bwipe!
Bram Moolenaarc28cb5b2019-05-31 20:42:09 +020063 set backup& writebackup& backupdir&vim backupskip&vim
Bram Moolenaare6bf6552017-06-27 22:11:51 +020064endfunc
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +020065
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020066func Test_writefile_fails_conversion2()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020067 CheckFeature iconv
68 if has('sun')
69 throw 'Skipped: does not work on SunOS'
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020070 endif
71 " With a backup file the write happens even if there is a conversion error,
72 " but then the backup file must remain
Bram Moolenaarc28cb5b2019-05-31 20:42:09 +020073 set nobackup writebackup backupdir=. backupskip=
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020074 let contents = ["line one", "line two"]
75 call writefile(contents, 'Xfile_conversion_err')
76 edit Xfile_conversion_err
77 call setline(1, ["first line", "cannot convert \u010b", "third line"])
78 set fileencoding=latin1
79 let output = execute('write')
80 call assert_match('CONVERSION ERROR', output)
81 call assert_equal(contents, readfile('Xfile_conversion_err~'))
82
83 call delete('Xfile_conversion_err')
84 call delete('Xfile_conversion_err~')
85 bwipe!
Bram Moolenaarc28cb5b2019-05-31 20:42:09 +020086 set backup& writebackup& backupdir&vim backupskip&vim
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020087endfunc
88
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +020089func SetFlag(timer)
90 let g:flag = 1
91endfunc
92
93func Test_write_quit_split()
94 " Prevent exiting by splitting window on file write.
95 augroup testgroup
96 autocmd BufWritePre * split
97 augroup END
98 e! Xfile
99 call setline(1, 'nothing')
100 wq
101
102 if has('timers')
103 " timer will not run if "exiting" is still set
104 let g:flag = 0
105 call timer_start(1, 'SetFlag')
106 sleep 50m
107 call assert_equal(1, g:flag)
108 unlet g:flag
109 endif
110 au! testgroup
111 bwipe Xfile
112 call delete('Xfile')
113endfunc
114
115func Test_nowrite_quit_split()
116 " Prevent exiting by opening a help window.
117 e! Xfile
118 help
119 wincmd w
120 exe winnr() . 'q'
121
122 if has('timers')
123 " timer will not run if "exiting" is still set
124 let g:flag = 0
125 call timer_start(1, 'SetFlag')
126 sleep 50m
127 call assert_equal(1, g:flag)
128 unlet g:flag
129 endif
130 bwipe Xfile
131endfunc
Bram Moolenaar7567d0b2017-11-16 23:04:15 +0100132
133func Test_writefile_sync_arg()
134 " This doesn't check if fsync() works, only that the argument is accepted.
135 call writefile(['one'], 'Xtest', 's')
136 call writefile(['two'], 'Xtest', 'S')
137 call delete('Xtest')
138endfunc
Bram Moolenaar83799a72017-11-25 17:24:09 +0100139
140func Test_writefile_sync_dev_stdout()
Bram Moolenaarb86abad2020-08-01 16:08:19 +0200141 CheckUnix
Bram Moolenaar9980b372018-04-21 20:12:35 +0200142 if filewritable('/dev/stdout')
143 " Just check that this doesn't cause an error.
144 call writefile(['one'], '/dev/stdout')
145 else
146 throw 'Skipped: /dev/stdout is not writable'
147 endif
Bram Moolenaar83799a72017-11-25 17:24:09 +0100148endfunc
Bram Moolenaar8c9e7b02018-08-30 13:07:17 +0200149
150func Test_writefile_autowrite()
151 set autowrite
152 new
153 next Xa Xb Xc
154 call setline(1, 'aaa')
155 next
156 call assert_equal(['aaa'], readfile('Xa'))
157 call setline(1, 'bbb')
158 call assert_fails('edit XX')
159 call assert_false(filereadable('Xb'))
160
161 set autowriteall
162 edit XX
163 call assert_equal(['bbb'], readfile('Xb'))
164
165 bwipe!
166 call delete('Xa')
167 call delete('Xb')
168 set noautowrite
169endfunc
170
171func Test_writefile_autowrite_nowrite()
172 set autowrite
173 new
174 next Xa Xb Xc
175 set buftype=nowrite
176 call setline(1, 'aaa')
177 let buf = bufnr('%')
178 " buffer contents silently lost
179 edit XX
180 call assert_false(filereadable('Xa'))
181 rewind
182 call assert_equal('', getline(1))
183
184 bwipe!
185 set noautowrite
186endfunc
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100187
188" Test for ':w !<cmd>' to pipe lines from the current buffer to an external
189" command.
190func Test_write_pipe_to_cmd()
Bram Moolenaarea3db912020-02-02 15:32:13 +0100191 CheckUnix
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100192 new
193 call setline(1, ['L1', 'L2', 'L3', 'L4'])
194 2,3w !cat > Xfile
195 call assert_equal(['L2', 'L3'], readfile('Xfile'))
196 close!
197 call delete('Xfile')
198endfunc
199
200" Test for :saveas
201func Test_saveas()
202 call assert_fails('saveas', 'E471:')
203 call writefile(['L1'], 'Xfile')
204 new Xfile
205 new
206 call setline(1, ['L1'])
207 call assert_fails('saveas Xfile', 'E139:')
208 close!
209 enew | only
210 call delete('Xfile')
211endfunc
212
213func Test_write_errors()
214 " Test for writing partial buffer
215 call writefile(['L1', 'L2', 'L3'], 'Xfile')
216 new Xfile
217 call assert_fails('1,2write', 'E140:')
218 close!
219
Bram Moolenaar4f5776c2020-02-12 22:15:19 +0100220 call assert_fails('w > Xtest', 'E494:')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100221
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100222 " Try to overwrite a directory
223 if has('unix')
224 call mkdir('Xdir1')
225 call assert_fails('write Xdir1', 'E17:')
226 call delete('Xdir1', 'd')
227 endif
228
229 " Test for :wall for a buffer with no name
230 enew | only
231 call setline(1, ['L1'])
232 call assert_fails('wall', 'E141:')
233 enew!
234
235 " Test for writing a 'readonly' file
236 new Xfile
237 set readonly
238 call assert_fails('write', 'E45:')
239 close
240
241 " Test for writing to a read-only file
242 new Xfile
243 call setfperm('Xfile', 'r--r--r--')
244 call assert_fails('write', 'E505:')
245 call setfperm('Xfile', 'rw-rw-rw-')
246 close
247
248 call delete('Xfile')
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200249
250 call writefile(test_null_list(), 'Xfile')
251 call assert_false(filereadable('Xfile'))
252 call writefile(test_null_blob(), 'Xfile')
253 call assert_false(filereadable('Xfile'))
254 call assert_fails('call writefile([], "")', 'E482:')
Bram Moolenaar494e9062020-05-31 21:28:02 +0200255
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:')
260endfunc
261
262" Test for writing to a file which is modified after Vim read it
263func 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})
Bram Moolenaar733d2592020-08-20 18:59:06 +0200271 call TermWait(buf)
Bram Moolenaar494e9062020-05-31 21:28:02 +0200272 call term_sendkeys(buf, ":set noswapfile\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200273 call TermWait(buf)
Bram Moolenaar494e9062020-05-31 21:28:02 +0200274
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>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200289 call TermWait(buf)
Bram Moolenaar494e9062020-05-31 21:28:02 +0200290 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>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200294 call TermWait(buf)
Bram Moolenaar494e9062020-05-31 21:28:02 +0200295 call assert_equal(new_ftime, getftime('Xfile'))
296 call term_sendkeys(buf, ":w\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200297 call TermWait(buf)
Bram Moolenaar494e9062020-05-31 21:28:02 +0200298 call term_sendkeys(buf, "y\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200299 call TermWait(buf)
Bram Moolenaar494e9062020-05-31 21:28:02 +0200300 call WaitForAssert({-> assert_equal('Line2', readfile('Xfile')[1])})
301
302 " clean up
303 call StopVimInTerminal(buf)
304 call delete('Xfile')
305endfunc
306
307" Test for an autocmd unloading a buffer during a write command
308func Test_write_autocmd_unloadbuf_lockmark()
309 augroup WriteTest
310 autocmd BufWritePre Xfile enew | write
311 augroup END
312 e Xfile
Bram Moolenaare2e40752020-09-04 21:18:46 +0200313 call assert_fails('lockmarks write', ['E32:', 'E203:'])
Bram Moolenaar494e9062020-05-31 21:28:02 +0200314 augroup WriteTest
315 au!
316 augroup END
317 augroup! WriteTest
318endfunc
319
320" Test for writing a buffer with 'acwrite' but without autocmds
321func 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!
329endfunc
330
331" Test for adding and removing lines from an autocmd when writing a buffer
332func 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')
370endfunc
371
372" Test for writing to a readonly file
373func Test_write_readonly()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200374 call writefile([], 'Xfile')
375 call setfperm('Xfile', "r--------")
376 edit Xfile
Bram Moolenaarb86abad2020-08-01 16:08:19 +0200377 set noreadonly backupskip=
Bram Moolenaar494e9062020-05-31 21:28:02 +0200378 call assert_fails('write', 'E505:')
379 let save_cpo = &cpo
380 set cpo+=W
381 call assert_fails('write!', 'E504:')
382 let &cpo = save_cpo
Bram Moolenaar1de5f7c2020-06-11 19:22:43 +0200383 call setline(1, ['line1'])
384 write!
385 call assert_equal(['line1'], readfile('Xfile'))
Bram Moolenaarb86abad2020-08-01 16:08:19 +0200386 set backupskip&
Bram Moolenaar494e9062020-05-31 21:28:02 +0200387 call delete('Xfile')
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100388endfunc
389
Bram Moolenaar1de5f7c2020-06-11 19:22:43 +0200390" Test for 'patchmode'
391func Test_patchmode()
Bram Moolenaar1de5f7c2020-06-11 19:22:43 +0200392 call writefile(['one'], 'Xfile')
Bram Moolenaarb86abad2020-08-01 16:08:19 +0200393 set patchmode=.orig nobackup backupskip= writebackup
Bram Moolenaar1de5f7c2020-06-11 19:22:43 +0200394 new Xfile
395 call setline(1, 'two')
396 " first write should create the .orig file
397 write
Bram Moolenaar1de5f7c2020-06-11 19:22:43 +0200398 call assert_equal(['one'], readfile('Xfile.orig'))
399 call setline(1, 'three')
400 " subsequent writes should not create/modify the .orig file
401 write
402 call assert_equal(['one'], readfile('Xfile.orig'))
Bram Moolenaarb86abad2020-08-01 16:08:19 +0200403 set patchmode& backup& backupskip& writebackup&
Bram Moolenaar1de5f7c2020-06-11 19:22:43 +0200404 call delete('Xfile')
405 call delete('Xfile.orig')
406endfunc
407
408" Test for writing to a file in a readonly directory
Bram Moolenaarf9a65502021-03-05 20:47:44 +0100409" NOTE: if you run tests as root this will fail. Don't run tests as root!
Bram Moolenaar1de5f7c2020-06-11 19:22:43 +0200410func Test_write_readonly_dir()
Bram Moolenaarb86abad2020-08-01 16:08:19 +0200411 " On MS-Windows, modifying files in a read-only directory is allowed.
412 CheckUnix
Bram Moolenaar17709e22021-03-19 14:38:12 +0100413 " Root can do it too.
414 CheckNotRoot
415
Bram Moolenaar1de5f7c2020-06-11 19:22:43 +0200416 call mkdir('Xdir')
417 call writefile(['one'], 'Xdir/Xfile1')
418 call setfperm('Xdir', 'r-xr--r--')
419 " try to create a new file in the directory
420 new Xdir/Xfile2
421 call setline(1, 'two')
422 call assert_fails('write', 'E212:')
423 " try to create a backup file in the directory
424 edit! Xdir/Xfile1
Bram Moolenaarb86abad2020-08-01 16:08:19 +0200425 set backupdir=./Xdir backupskip=
Bram Moolenaar1de5f7c2020-06-11 19:22:43 +0200426 set patchmode=.orig
427 call assert_fails('write', 'E509:')
428 call setfperm('Xdir', 'rwxr--r--')
429 call delete('Xdir', 'rf')
Bram Moolenaarb86abad2020-08-01 16:08:19 +0200430 set backupdir& backupskip& patchmode&
Bram Moolenaar1de5f7c2020-06-11 19:22:43 +0200431endfunc
432
Bram Moolenaarb340bae2020-06-15 19:51:56 +0200433" Test for writing a file using invalid file encoding
434func Test_write_invalid_encoding()
435 new
436 call setline(1, 'abc')
437 call assert_fails('write ++enc=axbyc Xfile', 'E213:')
438 close!
439endfunc
440
Bram Moolenaar622b3562020-07-27 20:02:41 +0200441" Tests for reading and writing files with conversion for Win32.
442func Test_write_file_encoding()
443 CheckMSWindows
444 let save_encoding = &encoding
445 let save_fileencodings = &fileencodings
446 set encoding& fileencodings&
447 let text =<< trim END
448 1 utf-8 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01
449 2 cp1251 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01
450 3 cp866 text: „«ï Vim version 6.2. ®á«¥¤­¥¥ ¨§¬¥­¥­¨¥: 1970 Jan 01
451 END
452 call writefile(text, 'Xfile')
453 edit Xfile
454
455 " write tests:
456 " combine three values for 'encoding' with three values for 'fileencoding'
457 " also write files for read tests
458 call cursor(1, 1)
459 set encoding=utf-8
460 .w! ++enc=utf-8 Xtest
461 .w ++enc=cp1251 >> Xtest
462 .w ++enc=cp866 >> Xtest
463 .w! ++enc=utf-8 Xutf8
464 let expected =<< trim END
465 1 utf-8 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01
466 1 utf-8 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01
467 1 utf-8 text: „«ï Vim version 6.2. ®á«¥¤­¥¥ ¨§¬¥­¥­¨¥: 1970 Jan 01
468 END
469 call assert_equal(expected, readfile('Xtest'))
470
471 call cursor(2, 1)
472 set encoding=cp1251
473 .w! ++enc=utf-8 Xtest
474 .w ++enc=cp1251 >> Xtest
475 .w ++enc=cp866 >> Xtest
476 .w! ++enc=cp1251 Xcp1251
477 let expected =<< trim END
478 2 cp1251 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01
479 2 cp1251 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01
480 2 cp1251 text: „«ï Vim version 6.2. ®á«¥¤­¥¥ ¨§¬¥­¥­¨¥: 1970 Jan 01
481 END
482 call assert_equal(expected, readfile('Xtest'))
483
484 call cursor(3, 1)
485 set encoding=cp866
486 .w! ++enc=utf-8 Xtest
487 .w ++enc=cp1251 >> Xtest
488 .w ++enc=cp866 >> Xtest
489 .w! ++enc=cp866 Xcp866
490 let expected =<< trim END
491 3 cp866 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01
492 3 cp866 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01
493 3 cp866 text: „«ï Vim version 6.2. ®á«¥¤­¥¥ ¨§¬¥­¥­¨¥: 1970 Jan 01
494 END
495 call assert_equal(expected, readfile('Xtest'))
496
497 " read three 'fileencoding's with utf-8 'encoding'
498 set encoding=utf-8 fencs=utf-8,cp1251
499 e Xutf8
500 .w! ++enc=utf-8 Xtest
501 e Xcp1251
502 .w ++enc=utf-8 >> Xtest
503 set fencs=utf-8,cp866
504 e Xcp866
505 .w ++enc=utf-8 >> Xtest
506 let expected =<< trim END
507 1 utf-8 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01
508 2 cp1251 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01
509 3 cp866 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01
510 END
511 call assert_equal(expected, readfile('Xtest'))
512
513 " read three 'fileencoding's with cp1251 'encoding'
514 set encoding=utf-8 fencs=utf-8,cp1251
515 e Xutf8
516 .w! ++enc=cp1251 Xtest
517 e Xcp1251
518 .w ++enc=cp1251 >> Xtest
519 set fencs=utf-8,cp866
520 e Xcp866
521 .w ++enc=cp1251 >> Xtest
522 let expected =<< trim END
523 1 utf-8 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01
524 2 cp1251 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01
525 3 cp866 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01
526 END
527 call assert_equal(expected, readfile('Xtest'))
528
529 " read three 'fileencoding's with cp866 'encoding'
530 set encoding=cp866 fencs=utf-8,cp1251
531 e Xutf8
532 .w! ++enc=cp866 Xtest
533 e Xcp1251
534 .w ++enc=cp866 >> Xtest
535 set fencs=utf-8,cp866
536 e Xcp866
537 .w ++enc=cp866 >> Xtest
538 let expected =<< trim END
539 1 utf-8 text: „«ï Vim version 6.2. ®á«¥¤­¥¥ ¨§¬¥­¥­¨¥: 1970 Jan 01
540 2 cp1251 text: „«ï Vim version 6.2. ®á«¥¤­¥¥ ¨§¬¥­¥­¨¥: 1970 Jan 01
541 3 cp866 text: „«ï Vim version 6.2. ®á«¥¤­¥¥ ¨§¬¥­¥­¨¥: 1970 Jan 01
542 END
543 call assert_equal(expected, readfile('Xtest'))
544
545 call delete('Xfile')
546 call delete('Xtest')
547 call delete('Xutf8')
548 call delete('Xcp1251')
549 call delete('Xcp866')
550 let &encoding = save_encoding
551 let &fileencodings = save_fileencodings
552 %bw!
553endfunc
554
Bram Moolenaarb61ef012020-07-29 16:08:21 +0200555" Test for writing and reading a file starting with a BOM.
556" Byte Order Mark (BOM) character for various encodings is below:
557" UTF-8 : EF BB BF
558" UTF-16 (BE): FE FF
559" UTF-16 (LE): FF FE
560" UTF-32 (BE): 00 00 FE FF
561" UTF-32 (LE): FF FE 00 00
562func Test_readwrite_file_with_bom()
563 let utf8_bom = "\xEF\xBB\xBF"
564 let utf16be_bom = "\xFE\xFF"
565 let utf16le_bom = "\xFF\xFE"
566 let utf32be_bom = "\n\n\xFE\xFF"
567 let utf32le_bom = "\xFF\xFE\n\n"
568 let save_fileencoding = &fileencoding
569 set cpoptions+=S
570
571 " Check that editing a latin1 file doesn't see a BOM
572 call writefile(["\xFE\xFElatin-1"], 'Xtest1')
573 edit Xtest1
574 call assert_equal('latin1', &fileencoding)
575 call assert_equal(0, &bomb)
576 set fenc=latin1
577 write Xfile2
578 call assert_equal(["\xFE\xFElatin-1", ''], readfile('Xfile2', 'b'))
579 set bomb fenc=latin1
580 write Xtest3
581 call assert_equal(["\xFE\xFElatin-1", ''], readfile('Xtest3', 'b'))
582 set bomb&
583
584 " Check utf-8 BOM
585 %bw!
586 call writefile([utf8_bom .. "utf-8"], 'Xtest1')
587 edit! Xtest1
588 call assert_equal('utf-8', &fileencoding)
589 call assert_equal(1, &bomb)
590 call assert_equal('utf-8', getline(1))
591 set fenc=latin1
592 write! Xfile2
593 call assert_equal(['utf-8', ''], readfile('Xfile2', 'b'))
594 set fenc=utf-8
595 w! Xtest3
596 call assert_equal([utf8_bom .. "utf-8", ''], readfile('Xtest3', 'b'))
597
598 " Check utf-8 with an error (will fall back to latin-1)
599 %bw!
600 call writefile([utf8_bom .. "utf-8\x80err"], 'Xtest1')
601 edit! Xtest1
602 call assert_equal('latin1', &fileencoding)
603 call assert_equal(0, &bomb)
604 call assert_equal("\xC3\xAF\xC2\xBB\xC2\xBFutf-8\xC2\x80err", getline(1))
605 set fenc=latin1
606 write! Xfile2
607 call assert_equal([utf8_bom .. "utf-8\x80err", ''], readfile('Xfile2', 'b'))
608 set fenc=utf-8
609 w! Xtest3
610 call assert_equal(["\xC3\xAF\xC2\xBB\xC2\xBFutf-8\xC2\x80err", ''],
611 \ readfile('Xtest3', 'b'))
612
613 " Check ucs-2 BOM
614 %bw!
615 call writefile([utf16be_bom .. "\nu\nc\ns\n-\n2\n"], 'Xtest1')
616 edit! Xtest1
617 call assert_equal('utf-16', &fileencoding)
618 call assert_equal(1, &bomb)
619 call assert_equal('ucs-2', getline(1))
620 set fenc=latin1
621 write! Xfile2
622 call assert_equal(["ucs-2", ''], readfile('Xfile2', 'b'))
623 set fenc=ucs-2
624 w! Xtest3
625 call assert_equal([utf16be_bom .. "\nu\nc\ns\n-\n2\n", ''],
626 \ readfile('Xtest3', 'b'))
627
628 " Check ucs-2le BOM
629 %bw!
630 call writefile([utf16le_bom .. "u\nc\ns\n-\n2\nl\ne\n"], 'Xtest1')
631 " Need to add a NUL byte after the NL byte
632 call writefile(0z00, 'Xtest1', 'a')
633 edit! Xtest1
634 call assert_equal('utf-16le', &fileencoding)
635 call assert_equal(1, &bomb)
636 call assert_equal('ucs-2le', getline(1))
637 set fenc=latin1
638 write! Xfile2
639 call assert_equal(["ucs-2le", ''], readfile('Xfile2', 'b'))
640 set fenc=ucs-2le
641 w! Xtest3
642 call assert_equal([utf16le_bom .. "u\nc\ns\n-\n2\nl\ne\n", "\n"],
643 \ readfile('Xtest3', 'b'))
644
645 " Check ucs-4 BOM
646 %bw!
647 call writefile([utf32be_bom .. "\n\n\nu\n\n\nc\n\n\ns\n\n\n-\n\n\n4\n\n\n"], 'Xtest1')
648 edit! Xtest1
649 call assert_equal('ucs-4', &fileencoding)
650 call assert_equal(1, &bomb)
651 call assert_equal('ucs-4', getline(1))
652 set fenc=latin1
653 write! Xfile2
654 call assert_equal(["ucs-4", ''], readfile('Xfile2', 'b'))
655 set fenc=ucs-4
656 w! Xtest3
657 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'))
658
659 " Check ucs-4le BOM
660 %bw!
661 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')
662 " Need to add three NUL bytes after the NL byte
663 call writefile(0z000000, 'Xtest1', 'a')
664 edit! Xtest1
665 call assert_equal('ucs-4le', &fileencoding)
666 call assert_equal(1, &bomb)
667 call assert_equal('ucs-4le', getline(1))
668 set fenc=latin1
669 write! Xfile2
670 call assert_equal(["ucs-4le", ''], readfile('Xfile2', 'b'))
671 set fenc=ucs-4le
672 w! Xtest3
673 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'))
674
675 set cpoptions-=S
676 let &fileencoding = save_fileencoding
677 call delete('Xtest1')
Bram Moolenaar733d2592020-08-20 18:59:06 +0200678 call delete('Xfile2')
Bram Moolenaarb61ef012020-07-29 16:08:21 +0200679 call delete('Xtest3')
680 %bw!
681endfunc
682
Bram Moolenaarb3c8b1d2020-12-23 18:54:57 +0100683func Test_read_write_bin()
684 " write file missing EOL
685 call writefile(['noeol'], "XNoEolSetEol", 'bS')
686 call assert_equal(0z6E6F656F6C, readfile('XNoEolSetEol', 'B'))
687
688 " when file is read 'eol' is off
Bram Moolenaar16204962020-12-23 22:40:11 +0100689 set nofixeol
690 e! ++ff=unix XNoEolSetEol
Bram Moolenaarb3c8b1d2020-12-23 18:54:57 +0100691 call assert_equal(0, &eol)
692
693 " writing with 'eol' set adds the newline
694 setlocal eol
695 w
696 call assert_equal(0z6E6F656F6C0A, readfile('XNoEolSetEol', 'B'))
697
698 call delete('XNoEolSetEol')
Bram Moolenaarbd318552020-12-23 20:55:15 +0100699 set ff&
700 bwipe! XNoEolSetEol
Bram Moolenaarb3c8b1d2020-12-23 18:54:57 +0100701endfunc
702
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100703" vim: shiftwidth=2 sts=2 expandtab