robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1" Tests for the writefile() function and some :write commands. 2 3source check.vim 4source term_util.vim 5 6func Test_writefile () 7let f = tempname () 8call writefile ([ "over" , "written" ], f , "b" ) 9call writefile ([ "hello" , "world" ], f , "b" ) 10call writefile ([ "!" , "good" ], f , "a" ) 11call writefile ([ "morning" ], f , "ab" ) 12call writefile ([ "" , "vimmers" ], f , "ab" ) 13let l = readfile ( f ) 14call assert_equal ( "hello" , l [ 0 ]) 15call assert_equal ( "world!" , l [ 1 ]) 16call assert_equal ( "good" , l [ 2 ]) 17call assert_equal ( "morning" , l [ 3 ]) 18call assert_equal ( "vimmers" , l [ 4 ]) 19call delete ( f ) 20 21call assert_fails ( 'call writefile("text", "Xfile")' , 'E475: Invalid argument: writefile() first argument must be a List or a Blob' ) 22endfunc 23 24func Test_writefile_ignore_regexp_error () 25write Xt[z-a]est.txt 26call delete ( 'Xt[z-a]est.txt' ) 27endfunc 28 29func Test_writefile_fails_gently () 30call assert_fails ( 'call writefile(["test"], "Xfile", [])' , 'E730:' ) 31call assert_false ( filereadable ( "Xfile" )) 32call delete ( "Xfile" ) 33 34call assert_fails ( 'call writefile(["test", [], [], [], "tset"], "Xfile")' , 'E745:' ) 35call assert_false ( filereadable ( "Xfile" )) 36call delete ( "Xfile" ) 37 38call assert_fails ( 'call writefile([], "Xfile", [])' , 'E730:' ) 39call assert_false ( filereadable ( "Xfile" )) 40call delete ( "Xfile" ) 41 42call assert_fails ( 'call writefile([], [])' , 'E730:' ) 43endfunc 44 45func Test_writefile_fails_conversion () 46if ! has ( 'iconv' ) || has ( 'sun' ) 47return 48endif 49" Without a backup file the write won't happen if there is a conversion 50" error. 51set nobackup nowritebackup backupdir = .backupskip = 52new 53let contents = [ "line one" , "line two" ] 54call writefile ( contents , 'Xfile' ) 55edit Xfile 56call setline ( 1 , [ "first line" , "cannot convert \u010b" , "third line" ]) 57call assert_fails ( 'write ++enc=cp932' , 'E513:' ) 58call assert_equal ( contents , readfile ( 'Xfile' )) 59 60" With 'backupcopy' set, if there is a conversion error, the backup file is 61" still created. 62set backupcopy = yeswritebackup & backup & 63call delete ( 'Xfile' .. & backupext ) 64call assert_fails ( 'write ++enc=cp932' , 'E513:' ) 65call assert_equal ( contents , readfile ( 'Xfile' )) 66call assert_equal ( contents , readfile ( 'Xfile' .. & backupext )) 67set backupcopy & 68 %bw ! 69 70" Conversion error during write 71new 72call setline ( 1 , [ "\U10000000" ]) 73let output = execute ( 'write! ++enc=utf-16 Xfile' ) 74call assert_match ( 'CONVERSION ERROR' , output ) 75let output = execute ( 'write! ++enc=ucs-2 Xfile' ) 76call assert_match ( 'CONVERSION ERROR' , output ) 77call delete ( 'Xfilz~' ) 78call delete ( 'Xfily~' ) 79 %bw ! 80 81call delete ( 'Xfile' ) 82call delete ( 'Xfile' .. & backupext ) 83bwipe ! 84set backup & writebackup & backupdir & vim backupskip & vim 85endfunc 86 87func Test_writefile_fails_conversion2 () 88if ! has ( 'iconv' ) || has ( 'sun' ) 89return 90endif 91" With a backup file the write happens even if there is a conversion error, 92" but then the backup file must remain 93set nobackup writebackup backupdir = .backupskip = 94let contents = [ "line one" , "line two" ] 95call writefile ( contents , 'Xfile_conversion_err' ) 96edit Xfile_conversion_err 97call setline ( 1 , [ "first line" , "cannot convert \u010b" , "third line" ]) 98set fileencoding = latin1 99let output = execute ( 'write' ) 100call assert_match ( 'CONVERSION ERROR' , output ) 101call assert_equal ( contents , readfile ( 'Xfile_conversion_err~' )) 102 103call delete ( 'Xfile_conversion_err' ) 104call delete ( 'Xfile_conversion_err~' ) 105bwipe ! 106set backup & writebackup & backupdir & vim backupskip & vim 107endfunc 108 109func SetFlag ( timer ) 110let g: flag = 1 111endfunc 112 113func Test_write_quit_split () 114" Prevent exiting by splitting window on file write. 115augroup testgroup 116autocmd BufWritePre * split 117augroup END 118e ! Xfile 119call setline ( 1 , 'nothing' ) 120wq 121 122if has ( 'timers' ) 123" timer will not run if "exiting" is still set 124let g: flag = 0 125call timer_start ( 1 , 'SetFlag' ) 126sleep 50m 127call assert_equal ( 1 , g: flag ) 128unlet g: flag 129endif 130au ! testgroup 131bwipe Xfile 132call delete ( 'Xfile' ) 133endfunc 134 135func Test_nowrite_quit_split () 136" Prevent exiting by opening a help window. 137e ! Xfile 138help 139wincmd w 140exe winnr () . 'q' 141 142if has ( 'timers' ) 143" timer will not run if "exiting" is still set 144let g: flag = 0 145call timer_start ( 1 , 'SetFlag' ) 146sleep 50m 147call assert_equal ( 1 , g: flag ) 148unlet g: flag 149endif 150bwipe Xfile 151endfunc 152 153func Test_writefile_sync_arg () 154" This doesn't check if fsync() works, only that the argument is accepted. 155call writefile ([ 'one' ], 'Xtest' , 's' ) 156call writefile ([ 'two' ], 'Xtest' , 'S' ) 157call delete ( 'Xtest' ) 158endfunc 159 160func Test_writefile_sync_dev_stdout () 161CheckUnix 162if filewritable ( '/dev/stdout' ) 163" Just check that this doesn't cause an error. 164call writefile ([ 'one' ], '/dev/stdout' , 's' ) 165else 166throw 'Skipped: /dev/stdout is not writable' 167endif 168endfunc 169 170func Test_writefile_autowrite () 171set autowrite 172new 173next Xa Xb Xc 174call setline ( 1 , 'aaa' ) 175next 176call assert_equal ([ 'aaa' ], readfile ( 'Xa' )) 177call setline ( 1 , 'bbb' ) 178call assert_fails ( 'edit XX' , 'E37: No write since last change (add ! to override)' ) 179call assert_false ( filereadable ( 'Xb' )) 180 181set autowriteall 182edit XX 183call assert_equal ([ 'bbb' ], readfile ( 'Xb' )) 184 185bwipe ! 186call delete ( 'Xa' ) 187call delete ( 'Xb' ) 188set noautowrite 189endfunc 190 191func Test_writefile_autowrite_nowrite () 192set autowrite 193new 194next Xa Xb Xc 195set buftype = nowrite 196call setline ( 1 , 'aaa' ) 197let buf = bufnr ( '%' ) 198" buffer contents silently lost 199edit XX 200call assert_false ( filereadable ( 'Xa' )) 201rewind 202call assert_equal ( '' , getline ( 1 )) 203 204bwipe ! 205set noautowrite 206endfunc 207 208" Test for ':w !<cmd>' to pipe lines from the current buffer to an external 209" command. 210func Test_write_pipe_to_cmd () 211CheckUnix 212new 213call setline ( 1 , [ 'L1' , 'L2' , 'L3' , 'L4' ]) 2142 , 3 w !cat > Xfile 215call assert_equal ([ 'L2' , 'L3' ], readfile ( 'Xfile' )) 216close ! 217call delete ( 'Xfile' ) 218endfunc 219 220" Test for :saveas 221func Test_saveas () 222call assert_fails ( 'saveas' , 'E471:' ) 223call writefile ([ 'L1' ], 'Xfile' ) 224new Xfile 225new 226call setline ( 1 , [ 'L1' ]) 227call assert_fails ( 'saveas Xfile' , 'E139:' ) 228close ! 229enew |only 230call delete ( 'Xfile' ) 231 232" :saveas should detect and set the file type. 233syntax on 234saveas ! Xsaveas.pl 235call assert_equal ( 'perl' , & filetype ) 236syntax off 237 %bw ! 238call delete ( 'Xsaveas.pl' ) 239 240" :saveas fails for "nofile" buffer 241set buftype = nofile 242call assert_fails ( 'saveas Xsafile' , 'E676: No matching autocommands for buftype=nofile buffer' ) 243 244bwipe ! 245endfunc 246 247func Test_write_errors () 248" Test for writing partial buffer 249call writefile ([ 'L1' , 'L2' , 'L3' ], 'Xfile' ) 250new Xfile 251call assert_fails ( '1,2write' , 'E140:' ) 252close ! 253 254call assert_fails ( 'w > Xtest' , 'E494:' ) 255 256" Try to overwrite a directory 257if has ( 'unix' ) 258call mkdir ( 'Xdir1' ) 259call assert_fails ( 'write Xdir1' , 'E17:' ) 260call delete ( 'Xdir1' , 'd' ) 261endif 262 263" Test for :wall for a buffer with no name 264enew |only 265call setline ( 1 , [ 'L1' ]) 266call assert_fails ( 'wall' , 'E141:' ) 267enew ! 268 269" Test for writing a 'readonly' file 270new Xfile 271set readonly 272call assert_fails ( 'write' , 'E45:' ) 273close 274 275" Test for writing to a read-only file 276new Xfile 277call setfperm ( 'Xfile' , 'r--r--r--' ) 278call assert_fails ( 'write' , 'E505:' ) 279call setfperm ( 'Xfile' , 'rw-rw-rw-' ) 280close 281 282call delete ( 'Xfile' ) 283 284" Nvim treats NULL list/blob more like empty list/blob 285" call writefile(v:_null_list, 'Xfile') 286" call assert_false(filereadable('Xfile')) 287" call writefile(v:_null_blob, 'Xfile') 288" call assert_false(filereadable('Xfile')) 289call assert_fails ( 'call writefile([], "")' , 'E482:' ) 290 291" very long file name 292let long_fname = repeat ( 'n' , 5000 ) 293call assert_fails ( 'exe "w " .. long_fname' , 'E75:' ) 294call assert_fails ( 'call writefile([], long_fname)' , 'E482:' ) 295 296" Test for writing to a block device on Unix-like systems 297if has ( 'unix' ) && getfperm ( '/dev/loop0' ) != '' 298 \&& getftype ( '/dev/loop0' ) == 'bdev' && ! IsRoot () 299new 300edit /dev/loop0 301call assert_fails ( 'write' , 'E503: ' ) 302call assert_fails ( 'write!' , 'E503: ' ) 303close ! 304endif 305endfunc 306 307" Test for writing to a file which is modified after Vim read it 308func Test_write_file_mtime () 309CheckEnglish 310CheckRunVimInTerminal 311 312" First read the file into a buffer 313call writefile ([ "Line1" , "Line2" ], 'Xfile' ) 314let old_ftime = getftime ( 'Xfile' ) 315let buf = RunVimInTerminal ( 'Xfile' , #{ rows : 10 }) 316call TermWait ( buf ) 317call term_sendkeys ( buf , ":set noswapfile\<CR>" ) 318call TermWait ( buf ) 319 320" Modify the file directly. Make sure the file modification time is 321" different. Note that on Linux/Unix, the file is considered modified 322" outside, only if the difference is 2 seconds or more 323sleep 1 324call writefile ([ "Line3" , "Line4" ], 'Xfile' ) 325let new_ftime = getftime ( 'Xfile' ) 326while new_ftime - old_ftime < 2 327sleep 100m 328call writefile ([ "Line3" , "Line4" ], 'Xfile' ) 329let new_ftime = getftime ( 'Xfile' ) 330endwhile 331 332" Try to overwrite the file and check for the prompt 333call term_sendkeys ( buf , ":w\<CR>" ) 334call TermWait ( buf ) 335call WaitForAssert ({ ->assert_equal ( "WARNING: The file has been changed since reading it!!!" , term_getline ( buf , 9 ))}) 336call assert_equal ( "Do you really want to write to it (y/n)?" , 337 \term_getline ( buf , 10 )) 338call term_sendkeys ( buf , "n\<CR>" ) 339call TermWait ( buf ) 340call assert_equal ( new_ftime , getftime ( 'Xfile' )) 341call term_sendkeys ( buf , ":w\<CR>" ) 342call TermWait ( buf ) 343call term_sendkeys ( buf , "y\<CR>" ) 344call TermWait ( buf ) 345call WaitForAssert ({ ->assert_equal ( 'Line2' , readfile ( 'Xfile' )[ 1 ])}) 346 347" clean up 348call StopVimInTerminal ( buf ) 349call delete ( 'Xfile' ) 350endfunc 351 352" Test for an autocmd unloading a buffer during a write command 353func Test_write_autocmd_unloadbuf_lockmark () 354augroup WriteTest 355autocmd BufWritePre Xfile enew |write 356augroup END 357e Xfile 358call assert_fails ( 'lockmarks write' , [ 'E32' , 'E203:' ]) 359augroup WriteTest 360au ! 361augroup END 362augroup ! WriteTest 363endfunc 364 365" Test for writing a buffer with 'acwrite' but without autocmds 366func Test_write_acwrite_error () 367new Xfile 368call setline ( 1 , [ 'line1' , 'line2' , 'line3' ]) 369set buftype = acwrite 370call assert_fails ( 'write' , 'E676:' ) 371call assert_fails ( '1,2write!' , 'E676:' ) 372call assert_fails ( 'w >>' , 'E676:' ) 373close ! 374endfunc 375 376" Test for adding and removing lines from an autocmd when writing a buffer 377func Test_write_autocmd_add_remove_lines () 378new Xfile 379call setline ( 1 , [ 'aaa' , 'bbb' , 'ccc' , 'ddd' ]) 380 381" Autocmd deleting lines from the file when writing a partial file 382augroup WriteTest2 383au ! 384autocmd FileWritePre Xfile 1 , 2 d 385augroup END 386call assert_fails ( '2,3w!' , 'E204:' ) 387 388" Autocmd adding lines to a file when writing a partial file 389augroup WriteTest2 390au ! 391autocmd FileWritePre Xfile call append ( 0 , [ 'xxx' , 'yyy' ]) 392augroup END 393 %d 394call setline ( 1 , [ 'aaa' , 'bbb' , 'ccc' , 'ddd' ]) 3951 , 2 w ! 396call assert_equal ([ 'xxx' , 'yyy' , 'aaa' , 'bbb' ], readfile ( 'Xfile' )) 397 398" Autocmd deleting lines from the file when writing the whole file 399augroup WriteTest2 400au ! 401autocmd BufWritePre Xfile 1 , 2 d 402augroup END 403 %d 404call setline ( 1 , [ 'aaa' , 'bbb' , 'ccc' , 'ddd' ]) 405w 406call assert_equal ([ 'ccc' , 'ddd' ], readfile ( 'Xfile' )) 407 408augroup WriteTest2 409au ! 410augroup END 411augroup ! WriteTest2 412 413close ! 414call delete ( 'Xfile' ) 415endfunc 416 417" Test for writing to a readonly file 418func Test_write_readonly () 419call writefile ([], 'Xfile' ) 420call setfperm ( 'Xfile' , "r--------" ) 421edit Xfile 422set noreadonly backupskip = 423call assert_fails ( 'write' , 'E505:' ) 424let save_cpo = & cpo 425set cpo += W 426call assert_fails ( 'write!' , 'E504:' ) 427let & cpo = save_cpo 428call setline ( 1 , [ 'line1' ]) 429write ! 430call assert_equal ([ 'line1' ], readfile ( 'Xfile' )) 431 432" Auto-saving a readonly file should fail with 'autowriteall' 433 %bw ! 434e Xfile 435set noreadonly autowriteall 436call setline ( 1 , [ 'aaaa' ]) 437call assert_fails ( 'n' , 'E505:' ) 438set cpo += W 439call assert_fails ( 'n' , 'E504:' ) 440set cpo -= W 441set autowriteall & 442 443set backupskip & 444call delete ( 'Xfile' ) 445 %bw ! 446endfunc 447 448" Test for 'patchmode' 449func Test_patchmode () 450call writefile ([ 'one' ], 'Xfile' ) 451set patchmode = .orignobackup backupskip = writebackup 452new Xfile 453call setline ( 1 , 'two' ) 454" first write should create the .orig file 455write 456call assert_equal ([ 'one' ], readfile ( 'Xfile.orig' )) 457call setline ( 1 , 'three' ) 458" subsequent writes should not create/modify the .orig file 459write 460call assert_equal ([ 'one' ], readfile ( 'Xfile.orig' )) 461 462" use 'patchmode' with 'nobackup' and 'nowritebackup' to create an empty 463" original file 464call delete ( 'Xfile' ) 465call delete ( 'Xfile.orig' ) 466 %bw ! 467set patchmode = .orignobackup nowritebackup 468edit Xfile 469call setline ( 1 , [ 'xxx' ]) 470write 471call assert_equal ([ 'xxx' ], readfile ( 'Xfile' )) 472call assert_equal ([], readfile ( 'Xfile.orig' )) 473 474set patchmode & backup & backupskip & writebackup & 475call delete ( 'Xfile' ) 476call delete ( 'Xfile.orig' ) 477endfunc 478 479" Test for writing to a file in a readonly directory 480" NOTE: if you run tests as root this will fail. Don't run tests as root! 481func Test_write_readonly_dir () 482" On MS-Windows, modifying files in a read-only directory is allowed. 483CheckUnix 484" Root can do it too. 485CheckNotRoot 486 487call mkdir ( 'Xdir/' ) 488call writefile ([ 'one' ], 'Xdir/Xfile1' ) 489call setfperm ( 'Xdir' , 'r-xr--r--' ) 490" try to create a new file in the directory 491new Xdir/Xfile2 492call setline ( 1 , 'two' ) 493call assert_fails ( 'write' , 'E212:' ) 494" try to create a backup file in the directory 495edit ! Xdir/Xfile1 496set backupdir = ./Xdirbackupskip = 497set patchmode = .orig 498call assert_fails ( 'write' , 'E509:' ) 499call setfperm ( 'Xdir' , 'rwxr--r--' ) 500call delete ( 'Xdir' , 'rf' ) 501set backupdir & backupskip & patchmode & 502endfunc 503 504" Test for writing a file using invalid file encoding 505func Test_write_invalid_encoding () 506new 507call setline ( 1 , 'abc' ) 508call assert_fails ( 'write ++enc=axbyc Xfile' , 'E213:' ) 509close ! 510endfunc 511 512" Tests for reading and writing files with conversion for Win32. 513func Test_write_file_encoding () 514throw 'Skipped: Nvim does not support encoding=latin1' 515CheckMSWindows 516let save_encoding = & encoding 517let save_fileencodings = & fileencodings 518set encoding = latin1fileencodings & 519let text =<< trim END 5201 utf-8 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01 5212 cp1251 text: ��� Vim version 6.2. ��������� ���������: 1970 Jan 01 5223 cp866 text: ��� Vim version 6.2. ������ ���������: 1970 Jan 01 523END 524call writefile ( text , 'Xfile' ) 525edit Xfile 526 527" write tests: 528" combine three values for 'encoding' with three values for 'fileencoding' 529" also write files for read tests 530call cursor ( 1 , 1 ) 531set encoding = utf-8 532 .w ! ++enc=utf-8 Xtest 533 .w ++enc=cp1251 >> Xtest 534 .w ++enc=cp866 >> Xtest 535 .w ! ++enc=utf-8 Xutf8 536let expected =<< trim END 5371 utf-8 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01 5381 utf-8 text: ��� Vim version 6.2. ��������� ���������: 1970 Jan 01 5391 utf-8 text: ��� Vim version 6.2. ������ ���������: 1970 Jan 01 540END 541call assert_equal ( expected , readfile ( 'Xtest' )) 542 543call cursor ( 2 , 1 ) 544set encoding = cp1251 545 .w ! ++enc=utf-8 Xtest 546 .w ++enc=cp1251 >> Xtest 547 .w ++enc=cp866 >> Xtest 548 .w ! ++enc=cp1251 Xcp1251 549let expected =<< trim END 5502 cp1251 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01 5512 cp1251 text: ��� Vim version 6.2. ��������� ���������: 1970 Jan 01 5522 cp1251 text: ��� Vim version 6.2. ������ ���������: 1970 Jan 01 553END 554call assert_equal ( expected , readfile ( 'Xtest' )) 555 556call cursor ( 3 , 1 ) 557set encoding = cp866 558 .w ! ++enc=utf-8 Xtest 559 .w ++enc=cp1251 >> Xtest 560 .w ++enc=cp866 >> Xtest 561 .w ! ++enc=cp866 Xcp866 562let expected =<< trim END 5633 cp866 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01 5643 cp866 text: ��� Vim version 6.2. ��������� ���������: 1970 Jan 01 5653 cp866 text: ��� Vim version 6.2. ������ ���������: 1970 Jan 01 566END 567call assert_equal ( expected , readfile ( 'Xtest' )) 568 569" read three 'fileencoding's with utf-8 'encoding' 570set encoding = utf-8fencs = utf-8,cp1251 571e Xutf8 572 .w ! ++enc=utf-8 Xtest 573e Xcp1251 574 .w ++enc=utf-8 >> Xtest 575set fencs = utf-8,cp866 576e Xcp866 577 .w ++enc=utf-8 >> Xtest 578let expected =<< trim END 5791 utf-8 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01 5802 cp1251 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01 5813 cp866 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01 582END 583call assert_equal ( expected , readfile ( 'Xtest' )) 584 585" read three 'fileencoding's with cp1251 'encoding' 586set encoding = utf-8fencs = utf-8,cp1251 587e Xutf8 588 .w ! ++enc=cp1251 Xtest 589e Xcp1251 590 .w ++enc=cp1251 >> Xtest 591set fencs = utf-8,cp866 592e Xcp866 593 .w ++enc=cp1251 >> Xtest 594let expected =<< trim END 5951 utf-8 text: ��� Vim version 6.2. ��������� ���������: 1970 Jan 01 5962 cp1251 text: ��� Vim version 6.2. ��������� ���������: 1970 Jan 01 5973 cp866 text: ��� Vim version 6.2. ��������� ���������: 1970 Jan 01 598END 599call assert_equal ( expected , readfile ( 'Xtest' )) 600 601" read three 'fileencoding's with cp866 'encoding' 602set encoding = cp866fencs = utf-8,cp1251 603e Xutf8 604 .w ! ++enc=cp866 Xtest 605e Xcp1251 606 .w ++enc=cp866 >> Xtest 607set fencs = utf-8,cp866 608e Xcp866 609 .w ++enc=cp866 >> Xtest 610let expected =<< trim END 6111 utf-8 text: ��� Vim version 6.2. ������ ���������: 1970 Jan 01 6122 cp1251 text: ��� Vim version 6.2. ������ ���������: 1970 Jan 01 6133 cp866 text: ��� Vim version 6.2. ������ ���������: 1970 Jan 01 614END 615call assert_equal ( expected , readfile ( 'Xtest' )) 616 617call delete ( 'Xfile' ) 618call delete ( 'Xtest' ) 619call delete ( 'Xutf8' ) 620call delete ( 'Xcp1251' ) 621call delete ( 'Xcp866' ) 622let & encoding = save_encoding 623let & fileencodings = save_fileencodings 624 %bw ! 625endfunc 626 627" Test for writing and reading a file starting with a BOM. 628" Byte Order Mark (BOM) character for various encodings is below: 629" UTF-8 : EF BB BF 630" UTF-16 (BE): FE FF 631" UTF-16 (LE): FF FE 632" UTF-32 (BE): 00 00 FE FF 633" UTF-32 (LE): FF FE 00 00 634func Test_readwrite_file_with_bom () 635let utf8_bom = "\xEF\xBB\xBF" 636let utf16be_bom = "\xFE\xFF" 637let utf16le_bom = "\xFF\xFE" 638let utf32be_bom = "\n\n\xFE\xFF" 639let utf32le_bom = "\xFF\xFE\n\n" 640let save_fileencoding = & fileencoding 641set cpoptions += S 642 643" Check that editing a latin1 file doesn't see a BOM 644call writefile ([ "\xFE\xFElatin-1" ], 'Xtest1' ) 645edit Xtest1 646call assert_equal ( 'latin1' , & fileencoding ) 647call assert_equal ( 0 , & bomb ) 648set fenc = latin1 649write Xfile2 650call assert_equal ([ "\xFE\xFElatin-1" , '' ], readfile ( 'Xfile2' , 'b' )) 651set bomb fenc = latin1 652write Xtest3 653call assert_equal ([ "\xFE\xFElatin-1" , '' ], readfile ( 'Xtest3' , 'b' )) 654set bomb & 655 656" Check utf-8 BOM 657 %bw ! 658call writefile ([ utf8_bom .. "utf-8" ], 'Xtest1' ) 659edit ! Xtest1 660call assert_equal ( 'utf-8' , & fileencoding ) 661call assert_equal ( 1 , & bomb ) 662call assert_equal ( 'utf-8' , getline ( 1 )) 663set fenc = latin1 664write ! Xfile2 665call assert_equal ([ 'utf-8' , '' ], readfile ( 'Xfile2' , 'b' )) 666set fenc = utf-8 667w ! Xtest3 668call assert_equal ([ utf8_bom .. "utf-8" , '' ], readfile ( 'Xtest3' , 'b' )) 669 670" Check utf-8 with an error (will fall back to latin-1) 671 %bw ! 672call writefile ([ utf8_bom .. "utf-8\x80err" ], 'Xtest1' ) 673edit ! Xtest1 674call assert_equal ( 'latin1' , & fileencoding ) 675call assert_equal ( 0 , & bomb ) 676call assert_equal ( "\xC3\xAF\xC2\xBB\xC2\xBFutf-8\xC2\x80err" , getline ( 1 )) 677set fenc = latin1 678write ! Xfile2 679call assert_equal ([ utf8_bom .. "utf-8\x80err" , '' ], readfile ( 'Xfile2' , 'b' )) 680set fenc = utf-8 681w ! Xtest3 682call assert_equal ([ "\xC3\xAF\xC2\xBB\xC2\xBFutf-8\xC2\x80err" , '' ], 683 \readfile ( 'Xtest3' , 'b' )) 684 685" Check ucs-2 BOM 686 %bw ! 687call writefile ([ utf16be_bom .. "\nu\nc\ns\n-\n2\n" ], 'Xtest1' ) 688edit ! Xtest1 689call assert_equal ( 'utf-16' , & fileencoding ) 690call assert_equal ( 1 , & bomb ) 691call assert_equal ( 'ucs-2' , getline ( 1 )) 692set fenc = latin1 693write ! Xfile2 694call assert_equal ([ "ucs-2" , '' ], readfile ( 'Xfile2' , 'b' )) 695set fenc = ucs-2 696w ! Xtest3 697call assert_equal ([ utf16be_bom .. "\nu\nc\ns\n-\n2\n" , '' ], 698 \readfile ( 'Xtest3' , 'b' )) 699 700" Check ucs-2le BOM 701 %bw ! 702call writefile ([ utf16le_bom .. "u\nc\ns\n-\n2\nl\ne\n" ], 'Xtest1' ) 703" Need to add a NUL byte after the NL byte 704call writefile ( 0 z00, 'Xtest1', 'a') 705edit ! Xtest1 706call assert_equal ( 'utf-16le' , & fileencoding ) 707call assert_equal ( 1 , & bomb ) 708call assert_equal ( 'ucs-2le' , getline ( 1 )) 709set fenc = latin1 710write ! Xfile2 711call assert_equal ([ "ucs-2le" , '' ], readfile ( 'Xfile2' , 'b' )) 712set fenc = ucs-2le 713w ! Xtest3 714call assert_equal ([ utf16le_bom .. "u\nc\ns\n-\n2\nl\ne\n" , "\n" ], 715 \readfile ( 'Xtest3' , 'b' )) 716 717" Check ucs-4 BOM 718 %bw ! 719call writefile ([ utf32be_bom .. "\n\n\nu\n\n\nc\n\n\ns\n\n\n-\n\n\n4\n\n\n" ], 'Xtest1' ) 720edit ! Xtest1 721call assert_equal ( 'ucs-4' , & fileencoding ) 722call assert_equal ( 1 , & bomb ) 723call assert_equal ( 'ucs-4' , getline ( 1 )) 724set fenc = latin1 725write ! Xfile2 726call assert_equal ([ "ucs-4" , '' ], readfile ( 'Xfile2' , 'b' )) 727set fenc = ucs-4 728w ! Xtest3 729call 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' )) 730 731" Check ucs-4le BOM 732 %bw ! 733call 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' ) 734" Need to add three NUL bytes after the NL byte 735call writefile ( 0 z000000, 'Xtest1', 'a') 736edit ! Xtest1 737call assert_equal ( 'ucs-4le' , & fileencoding ) 738call assert_equal ( 1 , & bomb ) 739call assert_equal ( 'ucs-4le' , getline ( 1 )) 740set fenc = latin1 741write ! Xfile2 742call assert_equal ([ "ucs-4le" , '' ], readfile ( 'Xfile2' , 'b' )) 743set fenc = ucs-4le 744w ! Xtest3 745call 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' )) 746 747set cpoptions -= S 748let & fileencoding = save_fileencoding 749call delete ( 'Xtest1' ) 750call delete ( 'Xfile2' ) 751call delete ( 'Xtest3' ) 752 %bw ! 753endfunc 754 755func Test_read_write_bin () 756" write file missing EOL 757call writefile ([ 'noeol' ], "XNoEolSetEol" , 'bS' ) 758call assert_equal ( 0 z6E6F656F6C, readfile('XNoEolSetEol', 'B')) 759 760" when file is read 'eol' is off 761set nofixeol 762e ! ++ff = unix XNoEolSetEol 763call assert_equal ( 0 , & eol ) 764 765" writing with 'eol' set adds the newline 766setlocal eol 767w 768call assert_equal ( 0 z6E6F656F6C0A, readfile('XNoEolSetEol', 'B')) 769 770call delete ( 'XNoEolSetEol' ) 771set ff & fixeol & 772bwipe ! XNoEolSetEol 773endfunc 774 775" Test for the 'backupcopy' option when writing files 776func Test_backupcopy () 777CheckUnix 778set backupskip = 779" With the default 'backupcopy' setting, saving a symbolic link file 780" should not break the link. 781set backupcopy & 782call writefile ([ '1111' ], 'Xfile1' ) 783silent !ln -sXfile1 Xfile2 784new Xfile2 785call setline ( 1 , [ '2222' ]) 786write 787close 788call assert_equal ([ '2222' ], readfile ( 'Xfile1' )) 789call assert_equal ( 'Xfile1' , resolve ( 'Xfile2' )) 790call assert_equal ( 'link' , getftype ( 'Xfile2' )) 791call delete ( 'Xfile1' ) 792call delete ( 'Xfile2' ) 793 794" With the 'backupcopy' set to 'breaksymlink', saving a symbolic link file 795" should break the link. 796set backupcopy = yes,breaksymlink 797call writefile ([ '1111' ], 'Xfile1' ) 798silent !ln -sXfile1 Xfile2 799new Xfile2 800call setline ( 1 , [ '2222' ]) 801write 802close 803call assert_equal ([ '1111' ], readfile ( 'Xfile1' )) 804call assert_equal ([ '2222' ], readfile ( 'Xfile2' )) 805call assert_equal ( 'Xfile2' , resolve ( 'Xfile2' )) 806call assert_equal ( 'file' , getftype ( 'Xfile2' )) 807call delete ( 'Xfile1' ) 808call delete ( 'Xfile2' ) 809set backupcopy & 810 811" With the default 'backupcopy' setting, saving a hard link file 812" should not break the link. 813set backupcopy & 814call writefile ([ '1111' ], 'Xfile1' ) 815silent !lnXfile1 Xfile2 816new Xfile2 817call setline ( 1 , [ '2222' ]) 818write 819close 820call assert_equal ([ '2222' ], readfile ( 'Xfile1' )) 821call delete ( 'Xfile1' ) 822call delete ( 'Xfile2' ) 823 824" With the 'backupcopy' set to 'breaksymlink', saving a hard link file 825" should break the link. 826set backupcopy = yes,breakhardlink 827call writefile ([ '1111' ], 'Xfile1' ) 828silent !lnXfile1 Xfile2 829new Xfile2 830call setline ( 1 , [ '2222' ]) 831write 832call assert_equal ([ '1111' ], readfile ( 'Xfile1' )) 833call assert_equal ([ '2222' ], readfile ( 'Xfile2' )) 834call delete ( 'Xfile1' ) 835call delete ( 'Xfile2' ) 836 837" If a backup file is already present, then a slightly modified filename 838" should be used as the backup file. Try with 'backupcopy' set to 'yes' and 839" 'no'. 840 %bw 841call writefile ([ 'aaaa' ], 'Xfile' ) 842call writefile ([ 'bbbb' ], 'Xfile.bak' ) 843set backupcopy = yesbackupext = .bak 844new Xfile 845call setline ( 1 , [ 'cccc' ]) 846write 847close 848call assert_equal ([ 'cccc' ], readfile ( 'Xfile' )) 849call assert_equal ([ 'bbbb' ], readfile ( 'Xfile.bak' )) 850set backupcopy = nobackupext = .bak 851new Xfile 852call setline ( 1 , [ 'dddd' ]) 853write 854close 855call assert_equal ([ 'dddd' ], readfile ( 'Xfile' )) 856call assert_equal ([ 'bbbb' ], readfile ( 'Xfile.bak' )) 857call delete ( 'Xfile' ) 858call delete ( 'Xfile.bak' ) 859 860" Write to a device file (in Unix-like systems) which cannot be backed up. 861if has ( 'unix' ) 862set writebackup backupcopy = yesnobackup 863new 864call setline ( 1 , [ 'aaaa' ]) 865let output = execute ( 'write! /dev/null' ) 866call assert_match ( '"/dev/null" \[Device]' , output ) 867close 868set writebackup backupcopy = nonobackup 869new 870call setline ( 1 , [ 'aaaa' ]) 871let output = execute ( 'write! /dev/null' ) 872call assert_match ( '"/dev/null" \[Device]' , output ) 873close 874set backup writebackup & backupcopy & 875new 876call setline ( 1 , [ 'aaaa' ]) 877let output = execute ( 'write! /dev/null' ) 878call assert_match ( '"/dev/null" \[Device]' , output ) 879close 880endif 881 882set backupcopy & backupskip & backupext & backup & 883endfunc 884 885" Test for writing a file with 'encoding' set to 'utf-16' 886func Test_write_utf16 () 887new 888call setline ( 1 , [ "\U00010001" ]) 889write ++enc=utf-16 Xfile 890bw ! 891call assert_equal ( 0 zD800DC01, readfile('Xfile', 'B')[0:3]) 892call delete ( 'Xfile' ) 893endfunc 894 895" Test for trying to save a backup file when the backup file is a symbolic 896" link to the original file. The backup file should not be modified. 897func Test_write_backup_symlink () 898CheckUnix 899call mkdir ( 'Xbackup' ) 900let save_backupdir = & backupdir 901set backupdir = .,./Xbackup 902call writefile ([ '1111' ], 'Xfile' ) 903silent !ln -sXfile Xfile.bak 904 905new Xfile 906set backup backupcopy = yesbackupext = .bak 907write 908call assert_equal ( 'link' , getftype ( 'Xfile.bak' )) 909call assert_equal ( 'Xfile' , resolve ( 'Xfile.bak' )) 910" backup file should be created in the 'backup' directory 911if ! has ( 'bsd' ) 912" This check fails on FreeBSD 913call assert_true ( filereadable ( './Xbackup/Xfile.bak' )) 914endif 915set backup & backupcopy & backupext & 916 %bw 917 918call delete ( 'Xfile' ) 919call delete ( 'Xfile.bak' ) 920call delete ( 'Xbackup' , 'rf' ) 921let & backupdir = save_backupdir 922endfunc 923 924" Test for ':write ++bin' and ':write ++nobin' 925func Test_write_binary_file () 926" create a file without an eol/eof character 927call writefile ( 0 z616161, 'Xwbfile1', 'b') 928new Xwbfile1 929write ++bin Xwbfile2 930write ++nobin Xwbfile3 931call assert_equal ( 0 z616161, readblob('Xwbfile2')) 932if has ( 'win32' ) 933call assert_equal ( 0 z6161610D.0A, readblob('Xwbfile3')) 934else 935call assert_equal ( 0 z6161610A, readblob('Xwbfile3')) 936endif 937call delete ( 'Xwbfile1' ) 938call delete ( 'Xwbfile2' ) 939call delete ( 'Xwbfile3' ) 940endfunc 941 942func DoWriteDefer () 943call writefile ([ 'some text' ], 'XdeferDelete' , 'D' ) 944call assert_equal ([ 'some text' ], readfile ( 'XdeferDelete' )) 945endfunc 946 947" def DefWriteDefer() 948" writefile(['some text'], 'XdefdeferDelete', 'D') 949" assert_equal(['some text'], readfile('XdefdeferDelete')) 950" enddef 951 952func Test_write_with_deferred_delete () 953call DoWriteDefer () 954call assert_equal ( '' , glob ( 'XdeferDelete' )) 955" call DefWriteDefer() 956" call assert_equal('', glob('XdefdeferDelete')) 957endfunc 958 959func DoWriteFile () 960call writefile ([ 'text' ], 'Xthefile' , 'D' ) 961cd .. 962endfunc 963 964func Test_write_defer_delete_chdir () 965let dir = getcwd () 966call DoWriteFile () 967call assert_notequal ( dir , getcwd ()) 968call chdir ( dir ) 969call assert_equal ( '' , glob ( 'Xthefile' )) 970endfunc 971 972" Check that buffer is written before triggering QuitPre 973func Test_wq_quitpre_autocommand () 974edit Xsomefile 975call setline ( 1 , 'hello' ) 976split 977let g: seq = [] 978augroup Testing 979au QuitPre * call add ( g: seq , 'QuitPre - ' .. ( & modified ? 'modified' : 'not modified' )) 980au BufWritePost * call add ( g: seq , 'written' ) 981augroup END 982wq 983call assert_equal ([ 'written' , 'QuitPre - not modified' ], g: seq ) 984 985augroup Testing 986au ! 987augroup END 988bwipe ! 989unlet g: seq 990call delete ( 'Xsomefile' ) 991endfunc 992 993func Test_write_with_xattr_support () 994CheckLinux 995CheckFeature xattr 996CheckExecutable setfattr 997 998let contents = [ "file with xattrs" , "line two" ] 999call writefile ( contents , 'Xwattr.txt' , 'D' ) 1000" write a couple of xattr 1001call system ( 'setfattr -n user.cookie -v chocolate Xwattr.txt' ) 1002call system ( 'setfattr -n user.frieda -v bar Xwattr.txt' ) 1003call system ( 'setfattr -n user.empty Xwattr.txt' ) 1004 1005set backupcopy = nowritebackup & backup & 1006sp Xwattr.txt 1007w 1008$ r ! getfattr -d % 1009let expected = [ 'file with xattrs' , 'line two' , '# file: Xwattr.txt' , 'user.cookie="chocolate"' , 'user.empty=""' , 'user.frieda="bar"' , '' ] 1010call assert_equal ( expected , getline ( 1 , '$' )) 1011 1012set backupcopy & 1013bw ! 1014endfunc 1015 1016func Test_backupcopy_auto_restrictive_umask () 1017CheckUnix 1018call writefile ([ 'FOO' ], 'Xumaskfile' , 'D' ) 1019call setfperm ( 'Xumaskfile' , 'rw-r--r--' ) 1020let inode_before = systemlist ( 'ls -i Xumaskfile' )[ 0 ] ->matchstr ( '^\s*\zs\d\+' ) 1021call writefile ([ 1022 \'set backupcopy=auto writebackup nobackup backupskip=' , 1023 \'edit Xumaskfile' , 1024 \'call setline(1, ["BAR"])' , 1025 \'write' , 1026 \'qall!' 1027 \], 'Xumaskscript' , 'D' ) 1028call system ( 'umask 0077; ' .. GetVimCommand () .. ' -i NONE -n -S Xumaskscript' ) 1029call assert_equal ( 0 , v: shell_error ) 1030call assert_equal ([ 'BAR' ], readfile ( 'Xumaskfile' )) 1031call assert_equal ( 'rw-r--r--' , getfperm ( 'Xumaskfile' )) 1032call assert_notequal ( inode_before , 1033 \systemlist ( 'ls -i Xumaskfile' )[ 0 ] ->matchstr ( '^\s*\zs\d\+' )) 1034endfunc 1035 1036" Test for :write when a BufWritePre autocmd changes to a directory that does 1037" not contain the file: the short file name is dropped and the full name must 1038" be used. 1039func Test_write_BufWritePre_cd_away () 1040call mkdir ( 'Xcdaway/other' , 'pR' ) 1041let save_cwd = getcwd () 1042defer chdir(save_cwd) 1043cd Xcdaway 1044new file.txt 1045call setline ( 1 , 'hello' ) 1046autocmd BufWritePre <buffer> cd other 1047write 1048call assert_equal ([ 'hello' ], readfile ( '../file.txt' )) 1049bwipe ! 1050endfunc 1051 1052" vim: shiftwidth=2 sts=2 expandtab