Mercurial > vim
comparison src/testdir/test_writefile.vim @ 30869:bff3fa5f4c74 v9.0.0769
patch 9.0.0769: too many delete() calls in tests
Commit: https://github.com/vim/vim/commit/db4c94788ad70118fa1ccc5fbc821757350ac771
Author: Bram Moolenaar <Bram@vim.org>
Date: Sat Oct 15 22:06:06 2022 +0100
patch 9.0.0769: too many delete() calls in tests
Problem: Too many delete() calls in tests.
Solution: Use deferred delete where possible.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sat, 15 Oct 2022 23:15:04 +0200 |
parents | ea2935ec4435 |
children | dcfbfe57141c |
comparison
equal
deleted
inserted
replaced
30868:ae65ad930e86 | 30869:bff3fa5f4c74 |
---|---|
3 source check.vim | 3 source check.vim |
4 source term_util.vim | 4 source term_util.vim |
5 | 5 |
6 func Test_writefile() | 6 func Test_writefile() |
7 let f = tempname() | 7 let f = tempname() |
8 call writefile(["over","written"], f, "b") | 8 call writefile(["over", "written"], f, "bD") |
9 call writefile(["hello","world"], f, "b") | 9 call writefile(["hello", "world"], f, "b") |
10 call writefile(["!", "good"], f, "a") | 10 call writefile(["!", "good"], f, "a") |
11 call writefile(["morning"], f, "ab") | 11 call writefile(["morning"], f, "ab") |
12 call writefile(["", "vimmers"], f, "ab") | 12 call writefile(["", "vimmers"], f, "ab") |
13 let l = readfile(f) | 13 let l = readfile(f) |
14 call assert_equal("hello", l[0]) | 14 call assert_equal("hello", l[0]) |
15 call assert_equal("world!", l[1]) | 15 call assert_equal("world!", l[1]) |
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) | |
20 | 19 |
21 call assert_fails('call writefile("text", "Xwffile")', 'E475: Invalid argument: writefile() first argument must be a List or a Blob') | 20 call assert_fails('call writefile("text", "Xwffile")', 'E475: Invalid argument: writefile() first argument must be a List or a Blob') |
22 endfunc | 21 endfunc |
23 | 22 |
24 func Test_writefile_ignore_regexp_error() | 23 func Test_writefile_ignore_regexp_error() |
50 " Without a backup file the write won't happen if there is a conversion | 49 " Without a backup file the write won't happen if there is a conversion |
51 " error. | 50 " error. |
52 set nobackup nowritebackup backupdir=. backupskip= | 51 set nobackup nowritebackup backupdir=. backupskip= |
53 new | 52 new |
54 let contents = ["line one", "line two"] | 53 let contents = ["line one", "line two"] |
55 call writefile(contents, 'Xwfcfile') | 54 call writefile(contents, 'Xwfcfile', 'D') |
56 edit Xwfcfile | 55 edit Xwfcfile |
57 call setline(1, ["first line", "cannot convert \u010b", "third line"]) | 56 call setline(1, ["first line", "cannot convert \u010b", "third line"]) |
58 call assert_fails('write ++enc=cp932', 'E513:') | 57 call assert_fails('write ++enc=cp932', 'E513:') |
59 call assert_equal(contents, readfile('Xwfcfile')) | 58 call assert_equal(contents, readfile('Xwfcfile')) |
60 | 59 |
77 call assert_match('CONVERSION ERROR', output) | 76 call assert_match('CONVERSION ERROR', output) |
78 call delete('Xwfcfilz~') | 77 call delete('Xwfcfilz~') |
79 call delete('Xwfcfily~') | 78 call delete('Xwfcfily~') |
80 %bw! | 79 %bw! |
81 | 80 |
82 call delete('Xwfcfile') | |
83 call delete('Xwfcfile' .. &backupext) | 81 call delete('Xwfcfile' .. &backupext) |
84 bwipe! | 82 bwipe! |
85 set backup& writebackup& backupdir&vim backupskip&vim | 83 set backup& writebackup& backupdir&vim backupskip&vim |
86 endfunc | 84 endfunc |
87 | 85 |
92 endif | 90 endif |
93 " With a backup file the write happens even if there is a conversion error, | 91 " With a backup file the write happens even if there is a conversion error, |
94 " but then the backup file must remain | 92 " but then the backup file must remain |
95 set nobackup writebackup backupdir=. backupskip= | 93 set nobackup writebackup backupdir=. backupskip= |
96 let contents = ["line one", "line two"] | 94 let contents = ["line one", "line two"] |
97 call writefile(contents, 'Xwf2file_conversion_err') | 95 call writefile(contents, 'Xwf2file_conversion_err', 'D') |
98 edit Xwf2file_conversion_err | 96 edit Xwf2file_conversion_err |
99 call setline(1, ["first line", "cannot convert \u010b", "third line"]) | 97 call setline(1, ["first line", "cannot convert \u010b", "third line"]) |
100 set fileencoding=latin1 | 98 set fileencoding=latin1 |
101 let output = execute('write') | 99 let output = execute('write') |
102 call assert_match('CONVERSION ERROR', output) | 100 call assert_match('CONVERSION ERROR', output) |
103 call assert_equal(contents, readfile('Xwf2file_conversion_err~')) | 101 call assert_equal(contents, readfile('Xwf2file_conversion_err~')) |
104 | 102 |
105 call delete('Xwf2file_conversion_err') | |
106 call delete('Xwf2file_conversion_err~') | 103 call delete('Xwf2file_conversion_err~') |
107 bwipe! | 104 bwipe! |
108 set backup& writebackup& backupdir&vim backupskip&vim | 105 set backup& writebackup& backupdir&vim backupskip&vim |
109 endfunc | 106 endfunc |
110 | 107 |
152 bwipe Xnqsfile | 149 bwipe Xnqsfile |
153 endfunc | 150 endfunc |
154 | 151 |
155 func Test_writefile_sync_arg() | 152 func Test_writefile_sync_arg() |
156 " This doesn't check if fsync() works, only that the argument is accepted. | 153 " This doesn't check if fsync() works, only that the argument is accepted. |
157 call writefile(['one'], 'Xtest', 's') | 154 call writefile(['one'], 'Xtest', 'sD') |
158 call writefile(['two'], 'Xtest', 'S') | 155 call writefile(['two'], 'Xtest', 'S') |
159 call delete('Xtest') | |
160 endfunc | 156 endfunc |
161 | 157 |
162 func Test_writefile_sync_dev_stdout() | 158 func Test_writefile_sync_dev_stdout() |
163 CheckUnix | 159 CheckUnix |
164 if filewritable('/dev/stdout') | 160 if filewritable('/dev/stdout') |
309 func Test_write_file_mtime() | 305 func Test_write_file_mtime() |
310 CheckEnglish | 306 CheckEnglish |
311 CheckRunVimInTerminal | 307 CheckRunVimInTerminal |
312 | 308 |
313 " First read the file into a buffer | 309 " First read the file into a buffer |
314 call writefile(["Line1", "Line2"], 'Xwfmfile') | 310 call writefile(["Line1", "Line2"], 'Xwfmfile', 'D') |
315 let old_ftime = getftime('Xwfmfile') | 311 let old_ftime = getftime('Xwfmfile') |
316 let buf = RunVimInTerminal('Xwfmfile', #{rows : 10}) | 312 let buf = RunVimInTerminal('Xwfmfile', #{rows : 10}) |
317 call TermWait(buf) | 313 call TermWait(buf) |
318 call term_sendkeys(buf, ":set noswapfile\<CR>") | 314 call term_sendkeys(buf, ":set noswapfile\<CR>") |
319 call TermWait(buf) | 315 call TermWait(buf) |
345 call TermWait(buf) | 341 call TermWait(buf) |
346 call WaitForAssert({-> assert_equal('Line2', readfile('Xwfmfile')[1])}) | 342 call WaitForAssert({-> assert_equal('Line2', readfile('Xwfmfile')[1])}) |
347 | 343 |
348 " clean up | 344 " clean up |
349 call StopVimInTerminal(buf) | 345 call StopVimInTerminal(buf) |
350 call delete('Xwfmfile') | |
351 endfunc | 346 endfunc |
352 | 347 |
353 " Test for an autocmd unloading a buffer during a write command | 348 " Test for an autocmd unloading a buffer during a write command |
354 func Test_write_autocmd_unloadbuf_lockmark() | 349 func Test_write_autocmd_unloadbuf_lockmark() |
355 augroup WriteTest | 350 augroup WriteTest |
415 call delete('Xwaafile') | 410 call delete('Xwaafile') |
416 endfunc | 411 endfunc |
417 | 412 |
418 " Test for writing to a readonly file | 413 " Test for writing to a readonly file |
419 func Test_write_readonly() | 414 func Test_write_readonly() |
420 call writefile([], 'Xwrofile') | 415 call writefile([], 'Xwrofile', 'D') |
421 call setfperm('Xwrofile', "r--------") | 416 call setfperm('Xwrofile', "r--------") |
422 edit Xwrofile | 417 edit Xwrofile |
423 set noreadonly backupskip= | 418 set noreadonly backupskip= |
424 call assert_fails('write', 'E505:') | 419 call assert_fails('write', 'E505:') |
425 let save_cpo = &cpo | 420 let save_cpo = &cpo |
440 call assert_fails('n', 'E504:') | 435 call assert_fails('n', 'E504:') |
441 set cpo-=W | 436 set cpo-=W |
442 set autowriteall& | 437 set autowriteall& |
443 | 438 |
444 set backupskip& | 439 set backupskip& |
445 call delete('Xwrofile') | |
446 %bw! | 440 %bw! |
447 endfunc | 441 endfunc |
448 | 442 |
449 " Test for 'patchmode' | 443 " Test for 'patchmode' |
450 func Test_patchmode() | 444 func Test_patchmode() |
451 call writefile(['one'], 'Xpafile') | 445 call writefile(['one'], 'Xpafile', 'D') |
452 set patchmode=.orig nobackup backupskip= writebackup | 446 set patchmode=.orig nobackup backupskip= writebackup |
453 new Xpafile | 447 new Xpafile |
454 call setline(1, 'two') | 448 call setline(1, 'two') |
455 " first write should create the .orig file | 449 " first write should create the .orig file |
456 write | 450 write |
471 write | 465 write |
472 call assert_equal(['xxx'], readfile('Xpafile')) | 466 call assert_equal(['xxx'], readfile('Xpafile')) |
473 call assert_equal([], readfile('Xpafile.orig')) | 467 call assert_equal([], readfile('Xpafile.orig')) |
474 | 468 |
475 set patchmode& backup& backupskip& writebackup& | 469 set patchmode& backup& backupskip& writebackup& |
476 call delete('Xpafile') | |
477 call delete('Xpafile.orig') | 470 call delete('Xpafile.orig') |
478 endfunc | 471 endfunc |
479 | 472 |
480 " Test for writing to a file in a readonly directory | 473 " Test for writing to a file in a readonly directory |
481 " NOTE: if you run tests as root this will fail. Don't run tests as root! | 474 " NOTE: if you run tests as root this will fail. Don't run tests as root! |
483 " On MS-Windows, modifying files in a read-only directory is allowed. | 476 " On MS-Windows, modifying files in a read-only directory is allowed. |
484 CheckUnix | 477 CheckUnix |
485 " Root can do it too. | 478 " Root can do it too. |
486 CheckNotRoot | 479 CheckNotRoot |
487 | 480 |
488 call mkdir('Xrodir/') | 481 call mkdir('Xrodir/', 'R') |
489 call writefile(['one'], 'Xrodir/Xfile1') | 482 call writefile(['one'], 'Xrodir/Xfile1') |
490 call setfperm('Xrodir', 'r-xr--r--') | 483 call setfperm('Xrodir', 'r-xr--r--') |
491 " try to create a new file in the directory | 484 " try to create a new file in the directory |
492 new Xrodir/Xfile2 | 485 new Xrodir/Xfile2 |
493 call setline(1, 'two') | 486 call setline(1, 'two') |
496 edit! Xrodir/Xfile1 | 489 edit! Xrodir/Xfile1 |
497 set backupdir=./Xrodir backupskip= | 490 set backupdir=./Xrodir backupskip= |
498 set patchmode=.orig | 491 set patchmode=.orig |
499 call assert_fails('write', 'E509:') | 492 call assert_fails('write', 'E509:') |
500 call setfperm('Xrodir', 'rwxr--r--') | 493 call setfperm('Xrodir', 'rwxr--r--') |
501 call delete('Xrodir', 'rf') | |
502 set backupdir& backupskip& patchmode& | 494 set backupdir& backupskip& patchmode& |
503 endfunc | 495 endfunc |
504 | 496 |
505 " Test for writing a file using invalid file encoding | 497 " Test for writing a file using invalid file encoding |
506 func Test_write_invalid_encoding() | 498 func Test_write_invalid_encoding() |
519 let text =<< trim END | 511 let text =<< trim END |
520 1 utf-8 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01 | 512 1 utf-8 text: Для Vim version 6.2. Последнее изменение: 1970 Jan 01 |
521 2 cp1251 text: Vim version 6.2. : 1970 Jan 01 | 513 2 cp1251 text: Vim version 6.2. : 1970 Jan 01 |
522 3 cp866 text: Vim version 6.2. : 1970 Jan 01 | 514 3 cp866 text: Vim version 6.2. : 1970 Jan 01 |
523 END | 515 END |
524 call writefile(text, 'Xwfefile') | 516 call writefile(text, 'Xwfefile', 'D') |
525 edit Xwfefile | 517 edit Xwfefile |
526 | 518 |
527 " write tests: | 519 " write tests: |
528 " combine three values for 'encoding' with three values for 'fileencoding' | 520 " combine three values for 'encoding' with three values for 'fileencoding' |
529 " also write files for read tests | 521 " also write files for read tests |
612 2 cp1251 text: Vim version 6.2. : 1970 Jan 01 | 604 2 cp1251 text: Vim version 6.2. : 1970 Jan 01 |
613 3 cp866 text: Vim version 6.2. : 1970 Jan 01 | 605 3 cp866 text: Vim version 6.2. : 1970 Jan 01 |
614 END | 606 END |
615 call assert_equal(expected, readfile('Xwfetest')) | 607 call assert_equal(expected, readfile('Xwfetest')) |
616 | 608 |
617 call delete('Xwfefile') | |
618 call delete('Xwfetest') | 609 call delete('Xwfetest') |
619 call delete('Xutf8') | 610 call delete('Xutf8') |
620 call delete('Xcp1251') | 611 call delete('Xcp1251') |
621 call delete('Xcp866') | 612 call delete('Xcp866') |
622 let &encoding = save_encoding | 613 let &encoding = save_encoding |
639 let utf32le_bom = "\xFF\xFE\n\n" | 630 let utf32le_bom = "\xFF\xFE\n\n" |
640 let save_fileencoding = &fileencoding | 631 let save_fileencoding = &fileencoding |
641 set cpoptions+=S | 632 set cpoptions+=S |
642 | 633 |
643 " Check that editing a latin1 file doesn't see a BOM | 634 " Check that editing a latin1 file doesn't see a BOM |
644 call writefile(["\xFE\xFElatin-1"], 'Xrwtest1') | 635 call writefile(["\xFE\xFElatin-1"], 'Xrwtest1', 'D') |
645 edit Xrwtest1 | 636 edit Xrwtest1 |
646 call assert_equal('latin1', &fileencoding) | 637 call assert_equal('latin1', &fileencoding) |
647 call assert_equal(0, &bomb) | 638 call assert_equal(0, &bomb) |
648 set fenc=latin1 | 639 set fenc=latin1 |
649 write Xrwfile2 | 640 write Xrwfile2 |
744 w! Xrwtest3 | 735 w! Xrwtest3 |
745 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')) | 736 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')) |
746 | 737 |
747 set cpoptions-=S | 738 set cpoptions-=S |
748 let &fileencoding = save_fileencoding | 739 let &fileencoding = save_fileencoding |
749 call delete('Xrwtest1') | |
750 call delete('Xrwfile2') | 740 call delete('Xrwfile2') |
751 call delete('Xrwtest3') | 741 call delete('Xrwtest3') |
752 %bw! | 742 %bw! |
753 endfunc | 743 endfunc |
754 | 744 |
755 func Test_read_write_bin() | 745 func Test_read_write_bin() |
756 " write file missing EOL | 746 " write file missing EOL |
757 call writefile(['noeol'], "XNoEolSetEol", 'bS') | 747 call writefile(['noeol'], "XNoEolSetEol", 'bSD') |
758 call assert_equal(0z6E6F656F6C, readfile('XNoEolSetEol', 'B')) | 748 call assert_equal(0z6E6F656F6C, readfile('XNoEolSetEol', 'B')) |
759 | 749 |
760 " when file is read 'eol' is off | 750 " when file is read 'eol' is off |
761 set nofixeol | 751 set nofixeol |
762 e! ++ff=unix XNoEolSetEol | 752 e! ++ff=unix XNoEolSetEol |
765 " writing with 'eol' set adds the newline | 755 " writing with 'eol' set adds the newline |
766 setlocal eol | 756 setlocal eol |
767 w | 757 w |
768 call assert_equal(0z6E6F656F6C0A, readfile('XNoEolSetEol', 'B')) | 758 call assert_equal(0z6E6F656F6C0A, readfile('XNoEolSetEol', 'B')) |
769 | 759 |
770 call delete('XNoEolSetEol') | |
771 set ff& fixeol& | 760 set ff& fixeol& |
772 bwipe! XNoEolSetEol | 761 bwipe! XNoEolSetEol |
773 endfunc | 762 endfunc |
774 | 763 |
775 " Test for the 'backupcopy' option when writing files | 764 " Test for the 'backupcopy' option when writing files |
897 func Test_write_backup_symlink() | 886 func Test_write_backup_symlink() |
898 CheckUnix | 887 CheckUnix |
899 call mkdir('Xbackup') | 888 call mkdir('Xbackup') |
900 let save_backupdir = &backupdir | 889 let save_backupdir = &backupdir |
901 set backupdir=.,./Xbackup | 890 set backupdir=.,./Xbackup |
902 call writefile(['1111'], 'Xwbsfile') | 891 call writefile(['1111'], 'Xwbsfile', 'D') |
903 silent !ln -s Xwbsfile Xwbsfile.bak | 892 silent !ln -s Xwbsfile Xwbsfile.bak |
904 | 893 |
905 new Xwbsfile | 894 new Xwbsfile |
906 set backup backupcopy=yes backupext=.bak | 895 set backup backupcopy=yes backupext=.bak |
907 write | 896 write |
913 call assert_true(filereadable('./Xbackup/Xwbsfile.bak')) | 902 call assert_true(filereadable('./Xbackup/Xwbsfile.bak')) |
914 endif | 903 endif |
915 set backup& backupcopy& backupext& | 904 set backup& backupcopy& backupext& |
916 %bw | 905 %bw |
917 | 906 |
918 call delete('Xwbsfile') | |
919 call delete('Xwbsfile.bak') | 907 call delete('Xwbsfile.bak') |
920 call delete('Xbackup', 'rf') | 908 call delete('Xbackup', 'rf') |
921 let &backupdir = save_backupdir | 909 let &backupdir = save_backupdir |
922 endfunc | 910 endfunc |
923 | 911 |
924 " Test for ':write ++bin' and ':write ++nobin' | 912 " Test for ':write ++bin' and ':write ++nobin' |
925 func Test_write_binary_file() | 913 func Test_write_binary_file() |
926 " create a file without an eol/eof character | 914 " create a file without an eol/eof character |
927 call writefile(0z616161, 'Xwbfile1', 'b') | 915 call writefile(0z616161, 'Xwbfile1', 'bD') |
928 new Xwbfile1 | 916 new Xwbfile1 |
929 write ++bin Xwbfile2 | 917 write ++bin Xwbfile2 |
930 write ++nobin Xwbfile3 | 918 write ++nobin Xwbfile3 |
931 call assert_equal(0z616161, readblob('Xwbfile2')) | 919 call assert_equal(0z616161, readblob('Xwbfile2')) |
932 if has('win32') | 920 if has('win32') |
933 call assert_equal(0z6161610D.0A, readblob('Xwbfile3')) | 921 call assert_equal(0z6161610D.0A, readblob('Xwbfile3')) |
934 else | 922 else |
935 call assert_equal(0z6161610A, readblob('Xwbfile3')) | 923 call assert_equal(0z6161610A, readblob('Xwbfile3')) |
936 endif | 924 endif |
937 call delete('Xwbfile1') | |
938 call delete('Xwbfile2') | 925 call delete('Xwbfile2') |
939 call delete('Xwbfile3') | 926 call delete('Xwbfile3') |
940 endfunc | 927 endfunc |
941 | 928 |
942 func DoWriteDefer() | 929 func DoWriteDefer() |