Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 1 | " Tests for the writefile() function. |
Bram Moolenaar | 19a1669 | 2016-09-01 22:19:47 +0200 | [diff] [blame] | 2 | |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 3 | func Test_writefile() |
Bram Moolenaar | 19a1669 | 2016-09-01 22:19:47 +0200 | [diff] [blame] | 4 | let f = tempname() |
| 5 | call writefile(["over","written"], f, "b") |
| 6 | call writefile(["hello","world"], f, "b") |
| 7 | call writefile(["!", "good"], f, "a") |
| 8 | call writefile(["morning"], f, "ab") |
| 9 | call writefile(["", "vimmers"], f, "ab") |
| 10 | let l = readfile(f) |
| 11 | call assert_equal("hello", l[0]) |
| 12 | call assert_equal("world!", l[1]) |
| 13 | call assert_equal("good", l[2]) |
| 14 | call assert_equal("morning", l[3]) |
| 15 | call assert_equal("vimmers", l[4]) |
| 16 | call delete(f) |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 17 | endfunc |
| 18 | |
| 19 | func Test_writefile_fails_gently() |
| 20 | call assert_fails('call writefile(["test"], "Xfile", [])', 'E730:') |
| 21 | call assert_false(filereadable("Xfile")) |
| 22 | call delete("Xfile") |
| 23 | |
| 24 | call assert_fails('call writefile(["test", [], [], [], "tset"], "Xfile")', 'E730:') |
| 25 | call assert_false(filereadable("Xfile")) |
| 26 | call delete("Xfile") |
| 27 | |
| 28 | call assert_fails('call writefile([], "Xfile", [])', 'E730:') |
| 29 | call assert_false(filereadable("Xfile")) |
| 30 | call delete("Xfile") |
| 31 | |
| 32 | call assert_fails('call writefile([], [])', 'E730:') |
| 33 | endfunc |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 34 | |
| 35 | func Test_writefile_fails_conversion() |
Bram Moolenaar | 39536dd | 2019-01-29 22:58:21 +0100 | [diff] [blame] | 36 | if !has('iconv') || has('sun') |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 37 | return |
| 38 | endif |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 39 | " Without a backup file the write won't happen if there is a conversion |
| 40 | " error. |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 41 | set nobackup nowritebackup |
| 42 | new |
| 43 | let contents = ["line one", "line two"] |
| 44 | call writefile(contents, 'Xfile') |
| 45 | edit Xfile |
| 46 | call setline(1, ["first line", "cannot convert \u010b", "third line"]) |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 47 | call assert_fails('write ++enc=cp932', 'E513:') |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 48 | call assert_equal(contents, readfile('Xfile')) |
| 49 | |
| 50 | call delete('Xfile') |
| 51 | bwipe! |
| 52 | set backup& writebackup& |
| 53 | endfunc |
Bram Moolenaar | 2c33d7b | 2017-10-14 16:06:20 +0200 | [diff] [blame] | 54 | |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 55 | func Test_writefile_fails_conversion2() |
| 56 | if !has('iconv') || has('sun') |
| 57 | return |
| 58 | endif |
| 59 | " With a backup file the write happens even if there is a conversion error, |
| 60 | " but then the backup file must remain |
| 61 | set nobackup writebackup |
| 62 | let contents = ["line one", "line two"] |
| 63 | call writefile(contents, 'Xfile_conversion_err') |
| 64 | edit Xfile_conversion_err |
| 65 | call setline(1, ["first line", "cannot convert \u010b", "third line"]) |
| 66 | set fileencoding=latin1 |
| 67 | let output = execute('write') |
| 68 | call assert_match('CONVERSION ERROR', output) |
| 69 | call assert_equal(contents, readfile('Xfile_conversion_err~')) |
| 70 | |
| 71 | call delete('Xfile_conversion_err') |
| 72 | call delete('Xfile_conversion_err~') |
| 73 | bwipe! |
| 74 | endfunc |
| 75 | |
Bram Moolenaar | 2c33d7b | 2017-10-14 16:06:20 +0200 | [diff] [blame] | 76 | func SetFlag(timer) |
| 77 | let g:flag = 1 |
| 78 | endfunc |
| 79 | |
| 80 | func Test_write_quit_split() |
| 81 | " Prevent exiting by splitting window on file write. |
| 82 | augroup testgroup |
| 83 | autocmd BufWritePre * split |
| 84 | augroup END |
| 85 | e! Xfile |
| 86 | call setline(1, 'nothing') |
| 87 | wq |
| 88 | |
| 89 | if has('timers') |
| 90 | " timer will not run if "exiting" is still set |
| 91 | let g:flag = 0 |
| 92 | call timer_start(1, 'SetFlag') |
| 93 | sleep 50m |
| 94 | call assert_equal(1, g:flag) |
| 95 | unlet g:flag |
| 96 | endif |
| 97 | au! testgroup |
| 98 | bwipe Xfile |
| 99 | call delete('Xfile') |
| 100 | endfunc |
| 101 | |
| 102 | func Test_nowrite_quit_split() |
| 103 | " Prevent exiting by opening a help window. |
| 104 | e! Xfile |
| 105 | help |
| 106 | wincmd w |
| 107 | exe winnr() . 'q' |
| 108 | |
| 109 | if has('timers') |
| 110 | " timer will not run if "exiting" is still set |
| 111 | let g:flag = 0 |
| 112 | call timer_start(1, 'SetFlag') |
| 113 | sleep 50m |
| 114 | call assert_equal(1, g:flag) |
| 115 | unlet g:flag |
| 116 | endif |
| 117 | bwipe Xfile |
| 118 | endfunc |
Bram Moolenaar | 7567d0b | 2017-11-16 23:04:15 +0100 | [diff] [blame] | 119 | |
| 120 | func Test_writefile_sync_arg() |
| 121 | " This doesn't check if fsync() works, only that the argument is accepted. |
| 122 | call writefile(['one'], 'Xtest', 's') |
| 123 | call writefile(['two'], 'Xtest', 'S') |
| 124 | call delete('Xtest') |
| 125 | endfunc |
Bram Moolenaar | 83799a7 | 2017-11-25 17:24:09 +0100 | [diff] [blame] | 126 | |
| 127 | func Test_writefile_sync_dev_stdout() |
| 128 | if !has('unix') |
| 129 | return |
| 130 | endif |
Bram Moolenaar | 9980b37 | 2018-04-21 20:12:35 +0200 | [diff] [blame] | 131 | if filewritable('/dev/stdout') |
| 132 | " Just check that this doesn't cause an error. |
| 133 | call writefile(['one'], '/dev/stdout') |
| 134 | else |
| 135 | throw 'Skipped: /dev/stdout is not writable' |
| 136 | endif |
Bram Moolenaar | 83799a7 | 2017-11-25 17:24:09 +0100 | [diff] [blame] | 137 | endfunc |
Bram Moolenaar | 8c9e7b0 | 2018-08-30 13:07:17 +0200 | [diff] [blame] | 138 | |
| 139 | func Test_writefile_autowrite() |
| 140 | set autowrite |
| 141 | new |
| 142 | next Xa Xb Xc |
| 143 | call setline(1, 'aaa') |
| 144 | next |
| 145 | call assert_equal(['aaa'], readfile('Xa')) |
| 146 | call setline(1, 'bbb') |
| 147 | call assert_fails('edit XX') |
| 148 | call assert_false(filereadable('Xb')) |
| 149 | |
| 150 | set autowriteall |
| 151 | edit XX |
| 152 | call assert_equal(['bbb'], readfile('Xb')) |
| 153 | |
| 154 | bwipe! |
| 155 | call delete('Xa') |
| 156 | call delete('Xb') |
| 157 | set noautowrite |
| 158 | endfunc |
| 159 | |
| 160 | func Test_writefile_autowrite_nowrite() |
| 161 | set autowrite |
| 162 | new |
| 163 | next Xa Xb Xc |
| 164 | set buftype=nowrite |
| 165 | call setline(1, 'aaa') |
| 166 | let buf = bufnr('%') |
| 167 | " buffer contents silently lost |
| 168 | edit XX |
| 169 | call assert_false(filereadable('Xa')) |
| 170 | rewind |
| 171 | call assert_equal('', getline(1)) |
| 172 | |
| 173 | bwipe! |
| 174 | set noautowrite |
| 175 | endfunc |