Mercurial > vim
comparison src/testdir/test_writefile.vim @ 24620:9f48b262539b v8.2.2849
patch 8.2.2849: bufwrite not sufficiently tested
Commit: https://github.com/vim/vim/commit/36f96a515109dc1fad279571a645c0f0d65f2de4
Author: Yegappan Lakshmanan <yegappan@yahoo.com>
Date: Thu May 13 18:33:16 2021 +0200
patch 8.2.2849: bufwrite not sufficiently tested
Problem: Bufwrite not sufficiently tested.
Solution: Add a few more tests. (Yegappan Lakshmanan, closes https://github.com/vim/vim/issues/8192)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Thu, 13 May 2021 18:45:02 +0200 |
parents | 2259c73cc1d9 |
children | d3c4ecf67604 |
comparison
equal
deleted
inserted
replaced
24619:53939914f672 | 24620:9f48b262539b |
---|---|
56 edit Xfile | 56 edit Xfile |
57 call setline(1, ["first line", "cannot convert \u010b", "third line"]) | 57 call setline(1, ["first line", "cannot convert \u010b", "third line"]) |
58 call assert_fails('write ++enc=cp932', 'E513:') | 58 call assert_fails('write ++enc=cp932', 'E513:') |
59 call assert_equal(contents, readfile('Xfile')) | 59 call assert_equal(contents, readfile('Xfile')) |
60 | 60 |
61 call delete('Xfile') | 61 " With 'backupcopy' set, if there is a conversion error, the backup file is |
62 " still created. | |
63 set backupcopy=yes writebackup& backup& | |
64 call delete('Xfile' .. &backupext) | |
65 call assert_fails('write ++enc=cp932', 'E513:') | |
66 call assert_equal(contents, readfile('Xfile')) | |
67 call assert_equal(contents, readfile('Xfile' .. &backupext)) | |
68 set backupcopy& | |
69 %bw! | |
70 | |
71 " Conversion error during write | |
72 new | |
73 call setline(1, ["\U10000000"]) | |
74 let output = execute('write! ++enc=utf-16 Xfile') | |
75 call assert_match('CONVERSION ERROR', output) | |
76 let output = execute('write! ++enc=ucs-2 Xfile') | |
77 call assert_match('CONVERSION ERROR', output) | |
78 call delete('Xfilz~') | |
79 call delete('Xfily~') | |
80 %bw! | |
81 | |
82 call delete('Xfile') | |
83 call delete('Xfile' .. &backupext) | |
62 bwipe! | 84 bwipe! |
63 set backup& writebackup& backupdir&vim backupskip&vim | 85 set backup& writebackup& backupdir&vim backupskip&vim |
64 endfunc | 86 endfunc |
65 | 87 |
66 func Test_writefile_fails_conversion2() | 88 func Test_writefile_fails_conversion2() |
255 | 277 |
256 " very long file name | 278 " very long file name |
257 let long_fname = repeat('n', 5000) | 279 let long_fname = repeat('n', 5000) |
258 call assert_fails('exe "w " .. long_fname', 'E75:') | 280 call assert_fails('exe "w " .. long_fname', 'E75:') |
259 call assert_fails('call writefile([], long_fname)', 'E482:') | 281 call assert_fails('call writefile([], long_fname)', 'E482:') |
282 | |
283 " Test for writing to a block device on Unix-like systems | |
284 if has('unix') && getfperm('/dev/loop0') != '' | |
285 \ && getftype('/dev/loop0') == 'bdev' && !IsRoot() | |
286 new | |
287 edit /dev/loop0 | |
288 call assert_fails('write', 'E505: ') | |
289 call assert_fails('write!', 'E503: ') | |
290 close! | |
291 endif | |
260 endfunc | 292 endfunc |
261 | 293 |
262 " Test for writing to a file which is modified after Vim read it | 294 " Test for writing to a file which is modified after Vim read it |
263 func Test_write_file_mtime() | 295 func Test_write_file_mtime() |
264 CheckEnglish | 296 CheckEnglish |
381 call assert_fails('write!', 'E504:') | 413 call assert_fails('write!', 'E504:') |
382 let &cpo = save_cpo | 414 let &cpo = save_cpo |
383 call setline(1, ['line1']) | 415 call setline(1, ['line1']) |
384 write! | 416 write! |
385 call assert_equal(['line1'], readfile('Xfile')) | 417 call assert_equal(['line1'], readfile('Xfile')) |
418 | |
419 " Auto-saving a readonly file should fail with 'autowriteall' | |
420 %bw! | |
421 e Xfile | |
422 set noreadonly autowriteall | |
423 call setline(1, ['aaaa']) | |
424 call assert_fails('n', 'E505:') | |
425 set cpo+=W | |
426 call assert_fails('n', 'E504:') | |
427 set cpo-=W | |
428 set autowriteall& | |
429 | |
386 set backupskip& | 430 set backupskip& |
387 call delete('Xfile') | 431 call delete('Xfile') |
432 %bw! | |
388 endfunc | 433 endfunc |
389 | 434 |
390 " Test for 'patchmode' | 435 " Test for 'patchmode' |
391 func Test_patchmode() | 436 func Test_patchmode() |
392 call writefile(['one'], 'Xfile') | 437 call writefile(['one'], 'Xfile') |
398 call assert_equal(['one'], readfile('Xfile.orig')) | 443 call assert_equal(['one'], readfile('Xfile.orig')) |
399 call setline(1, 'three') | 444 call setline(1, 'three') |
400 " subsequent writes should not create/modify the .orig file | 445 " subsequent writes should not create/modify the .orig file |
401 write | 446 write |
402 call assert_equal(['one'], readfile('Xfile.orig')) | 447 call assert_equal(['one'], readfile('Xfile.orig')) |
448 | |
449 " use 'patchmode' with 'nobackup' and 'nowritebackup' to create an empty | |
450 " original file | |
451 call delete('Xfile') | |
452 call delete('Xfile.orig') | |
453 %bw! | |
454 set patchmode=.orig nobackup nowritebackup | |
455 edit Xfile | |
456 call setline(1, ['xxx']) | |
457 write | |
458 call assert_equal(['xxx'], readfile('Xfile')) | |
459 call assert_equal([], readfile('Xfile.orig')) | |
460 | |
403 set patchmode& backup& backupskip& writebackup& | 461 set patchmode& backup& backupskip& writebackup& |
404 call delete('Xfile') | 462 call delete('Xfile') |
405 call delete('Xfile.orig') | 463 call delete('Xfile.orig') |
406 endfunc | 464 endfunc |
407 | 465 |
698 call delete('XNoEolSetEol') | 756 call delete('XNoEolSetEol') |
699 set ff& | 757 set ff& |
700 bwipe! XNoEolSetEol | 758 bwipe! XNoEolSetEol |
701 endfunc | 759 endfunc |
702 | 760 |
761 " Test for the 'backupcopy' option when writing files | |
762 func Test_backupcopy() | |
763 CheckUnix | |
764 set backupskip= | |
765 " With the default 'backupcopy' setting, saving a symbolic link file | |
766 " should not break the link. | |
767 set backupcopy& | |
768 call writefile(['1111'], 'Xfile1') | |
769 silent !ln -s Xfile1 Xfile2 | |
770 new Xfile2 | |
771 call setline(1, ['2222']) | |
772 write | |
773 close | |
774 call assert_equal(['2222'], readfile('Xfile1')) | |
775 call assert_equal('Xfile1', resolve('Xfile2')) | |
776 call assert_equal('link', getftype('Xfile2')) | |
777 call delete('Xfile1') | |
778 call delete('Xfile2') | |
779 | |
780 " With the 'backupcopy' set to 'breaksymlink', saving a symbolic link file | |
781 " should break the link. | |
782 set backupcopy=yes,breaksymlink | |
783 call writefile(['1111'], 'Xfile1') | |
784 silent !ln -s Xfile1 Xfile2 | |
785 new Xfile2 | |
786 call setline(1, ['2222']) | |
787 write | |
788 close | |
789 call assert_equal(['1111'], readfile('Xfile1')) | |
790 call assert_equal(['2222'], readfile('Xfile2')) | |
791 call assert_equal('Xfile2', resolve('Xfile2')) | |
792 call assert_equal('file', getftype('Xfile2')) | |
793 call delete('Xfile1') | |
794 call delete('Xfile2') | |
795 set backupcopy& | |
796 | |
797 " With the default 'backupcopy' setting, saving a hard link file | |
798 " should not break the link. | |
799 set backupcopy& | |
800 call writefile(['1111'], 'Xfile1') | |
801 silent !ln Xfile1 Xfile2 | |
802 new Xfile2 | |
803 call setline(1, ['2222']) | |
804 write | |
805 close | |
806 call assert_equal(['2222'], readfile('Xfile1')) | |
807 call delete('Xfile1') | |
808 call delete('Xfile2') | |
809 | |
810 " With the 'backupcopy' set to 'breaksymlink', saving a hard link file | |
811 " should break the link. | |
812 set backupcopy=yes,breakhardlink | |
813 call writefile(['1111'], 'Xfile1') | |
814 silent !ln Xfile1 Xfile2 | |
815 new Xfile2 | |
816 call setline(1, ['2222']) | |
817 write | |
818 call assert_equal(['1111'], readfile('Xfile1')) | |
819 call assert_equal(['2222'], readfile('Xfile2')) | |
820 call delete('Xfile1') | |
821 call delete('Xfile2') | |
822 | |
823 " If a backup file is already present, then a slightly modified filename | |
824 " should be used as the backup file. Try with 'backupcopy' set to 'yes' and | |
825 " 'no'. | |
826 %bw | |
827 call writefile(['aaaa'], 'Xfile') | |
828 call writefile(['bbbb'], 'Xfile.bak') | |
829 set backupcopy=yes backupext=.bak | |
830 new Xfile | |
831 call setline(1, ['cccc']) | |
832 write | |
833 close | |
834 call assert_equal(['cccc'], readfile('Xfile')) | |
835 call assert_equal(['bbbb'], readfile('Xfile.bak')) | |
836 set backupcopy=no backupext=.bak | |
837 new Xfile | |
838 call setline(1, ['dddd']) | |
839 write | |
840 close | |
841 call assert_equal(['dddd'], readfile('Xfile')) | |
842 call assert_equal(['bbbb'], readfile('Xfile.bak')) | |
843 call delete('Xfile') | |
844 call delete('Xfile.bak') | |
845 | |
846 " Write to a device file (in Unix-like systems) which cannot be backed up. | |
847 if has('unix') | |
848 set writebackup backupcopy=yes nobackup | |
849 new | |
850 call setline(1, ['aaaa']) | |
851 let output = execute('write! /dev/null') | |
852 call assert_match('"/dev/null" \[Device]', output) | |
853 close | |
854 set writebackup backupcopy=no nobackup | |
855 new | |
856 call setline(1, ['aaaa']) | |
857 let output = execute('write! /dev/null') | |
858 call assert_match('"/dev/null" \[Device]', output) | |
859 close | |
860 set backup writebackup& backupcopy& | |
861 new | |
862 call setline(1, ['aaaa']) | |
863 let output = execute('write! /dev/null') | |
864 call assert_match('"/dev/null" \[Device]', output) | |
865 close | |
866 endif | |
867 | |
868 set backupcopy& backupskip& backupext& backup& | |
869 endfunc | |
870 | |
871 " Test for writing a file with 'encoding' set to 'utf-16' | |
872 func Test_write_utf16() | |
873 new | |
874 call setline(1, ["\U00010001"]) | |
875 write ++enc=utf-16 Xfile | |
876 bw! | |
877 call assert_equal(0zD800DC01, readfile('Xfile', 'B')[0:3]) | |
878 call delete('Xfile') | |
879 endfunc | |
880 | |
881 " Test for trying to save a backup file when the backup file is a symbolic | |
882 " link to the original file. The backup file should not be modified. | |
883 func Test_write_backup_symlink() | |
884 CheckUnix | |
885 call writefile(['1111'], 'Xfile') | |
886 silent !ln -s Xfile Xfile.bak | |
887 | |
888 new Xfile | |
889 set backup backupcopy=yes backupext=.bak | |
890 write | |
891 call assert_equal('link', getftype('Xfile.bak')) | |
892 call assert_equal('Xfile', resolve('Xfile.bak')) | |
893 set backup& backupcopy& backupext& | |
894 close | |
895 | |
896 call delete('Xfile') | |
897 call delete('Xfile.bak') | |
898 endfunc | |
899 | |
703 " vim: shiftwidth=2 sts=2 expandtab | 900 " vim: shiftwidth=2 sts=2 expandtab |