Bram Moolenaar | b40c257 | 2019-10-19 21:01:05 +0200 | [diff] [blame] | 1 | " Tests for the writefile() function and some :write commands. |
Bram Moolenaar | 19a1669 | 2016-09-01 22:19:47 +0200 | [diff] [blame] | 2 | |
Bram Moolenaar | ea3db91 | 2020-02-02 15:32:13 +0100 | [diff] [blame] | 3 | source check.vim |
| 4 | |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 5 | func Test_writefile() |
Bram Moolenaar | 19a1669 | 2016-09-01 22:19:47 +0200 | [diff] [blame] | 6 | let f = tempname() |
| 7 | call writefile(["over","written"], f, "b") |
| 8 | call writefile(["hello","world"], f, "b") |
| 9 | call writefile(["!", "good"], f, "a") |
| 10 | call writefile(["morning"], f, "ab") |
| 11 | call writefile(["", "vimmers"], f, "ab") |
| 12 | let l = readfile(f) |
| 13 | call assert_equal("hello", l[0]) |
| 14 | call assert_equal("world!", l[1]) |
| 15 | call assert_equal("good", l[2]) |
| 16 | call assert_equal("morning", l[3]) |
| 17 | call assert_equal("vimmers", l[4]) |
| 18 | call delete(f) |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 19 | endfunc |
| 20 | |
Bram Moolenaar | b40c257 | 2019-10-19 21:01:05 +0200 | [diff] [blame] | 21 | func Test_writefile_ignore_regexp_error() |
| 22 | write Xt[z-a]est.txt |
| 23 | call delete('Xt[z-a]est.txt') |
| 24 | endfunc |
| 25 | |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 26 | func Test_writefile_fails_gently() |
| 27 | call assert_fails('call writefile(["test"], "Xfile", [])', 'E730:') |
| 28 | call assert_false(filereadable("Xfile")) |
| 29 | call delete("Xfile") |
| 30 | |
| 31 | call assert_fails('call writefile(["test", [], [], [], "tset"], "Xfile")', 'E730:') |
| 32 | call assert_false(filereadable("Xfile")) |
| 33 | call delete("Xfile") |
| 34 | |
| 35 | call assert_fails('call writefile([], "Xfile", [])', 'E730:') |
| 36 | call assert_false(filereadable("Xfile")) |
| 37 | call delete("Xfile") |
| 38 | |
| 39 | call assert_fails('call writefile([], [])', 'E730:') |
| 40 | endfunc |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 41 | |
| 42 | func Test_writefile_fails_conversion() |
Bram Moolenaar | 39536dd | 2019-01-29 22:58:21 +0100 | [diff] [blame] | 43 | if !has('iconv') || has('sun') |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 44 | return |
| 45 | endif |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 46 | " Without a backup file the write won't happen if there is a conversion |
| 47 | " error. |
Bram Moolenaar | c28cb5b | 2019-05-31 20:42:09 +0200 | [diff] [blame] | 48 | set nobackup nowritebackup backupdir=. backupskip= |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 49 | new |
| 50 | let contents = ["line one", "line two"] |
| 51 | call writefile(contents, 'Xfile') |
| 52 | edit Xfile |
| 53 | call setline(1, ["first line", "cannot convert \u010b", "third line"]) |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 54 | call assert_fails('write ++enc=cp932', 'E513:') |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 55 | call assert_equal(contents, readfile('Xfile')) |
| 56 | |
| 57 | call delete('Xfile') |
| 58 | bwipe! |
Bram Moolenaar | c28cb5b | 2019-05-31 20:42:09 +0200 | [diff] [blame] | 59 | set backup& writebackup& backupdir&vim backupskip&vim |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 60 | endfunc |
Bram Moolenaar | 2c33d7b | 2017-10-14 16:06:20 +0200 | [diff] [blame] | 61 | |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 62 | func Test_writefile_fails_conversion2() |
| 63 | if !has('iconv') || has('sun') |
| 64 | return |
| 65 | endif |
| 66 | " With a backup file the write happens even if there is a conversion error, |
| 67 | " but then the backup file must remain |
Bram Moolenaar | c28cb5b | 2019-05-31 20:42:09 +0200 | [diff] [blame] | 68 | set nobackup writebackup backupdir=. backupskip= |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 69 | let contents = ["line one", "line two"] |
| 70 | call writefile(contents, 'Xfile_conversion_err') |
| 71 | edit Xfile_conversion_err |
| 72 | call setline(1, ["first line", "cannot convert \u010b", "third line"]) |
| 73 | set fileencoding=latin1 |
| 74 | let output = execute('write') |
| 75 | call assert_match('CONVERSION ERROR', output) |
| 76 | call assert_equal(contents, readfile('Xfile_conversion_err~')) |
| 77 | |
| 78 | call delete('Xfile_conversion_err') |
| 79 | call delete('Xfile_conversion_err~') |
| 80 | bwipe! |
Bram Moolenaar | c28cb5b | 2019-05-31 20:42:09 +0200 | [diff] [blame] | 81 | set backup& writebackup& backupdir&vim backupskip&vim |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 82 | endfunc |
| 83 | |
Bram Moolenaar | 2c33d7b | 2017-10-14 16:06:20 +0200 | [diff] [blame] | 84 | func SetFlag(timer) |
| 85 | let g:flag = 1 |
| 86 | endfunc |
| 87 | |
| 88 | func Test_write_quit_split() |
| 89 | " Prevent exiting by splitting window on file write. |
| 90 | augroup testgroup |
| 91 | autocmd BufWritePre * split |
| 92 | augroup END |
| 93 | e! Xfile |
| 94 | call setline(1, 'nothing') |
| 95 | wq |
| 96 | |
| 97 | if has('timers') |
| 98 | " timer will not run if "exiting" is still set |
| 99 | let g:flag = 0 |
| 100 | call timer_start(1, 'SetFlag') |
| 101 | sleep 50m |
| 102 | call assert_equal(1, g:flag) |
| 103 | unlet g:flag |
| 104 | endif |
| 105 | au! testgroup |
| 106 | bwipe Xfile |
| 107 | call delete('Xfile') |
| 108 | endfunc |
| 109 | |
| 110 | func Test_nowrite_quit_split() |
| 111 | " Prevent exiting by opening a help window. |
| 112 | e! Xfile |
| 113 | help |
| 114 | wincmd w |
| 115 | exe winnr() . 'q' |
| 116 | |
| 117 | if has('timers') |
| 118 | " timer will not run if "exiting" is still set |
| 119 | let g:flag = 0 |
| 120 | call timer_start(1, 'SetFlag') |
| 121 | sleep 50m |
| 122 | call assert_equal(1, g:flag) |
| 123 | unlet g:flag |
| 124 | endif |
| 125 | bwipe Xfile |
| 126 | endfunc |
Bram Moolenaar | 7567d0b | 2017-11-16 23:04:15 +0100 | [diff] [blame] | 127 | |
| 128 | func Test_writefile_sync_arg() |
| 129 | " This doesn't check if fsync() works, only that the argument is accepted. |
| 130 | call writefile(['one'], 'Xtest', 's') |
| 131 | call writefile(['two'], 'Xtest', 'S') |
| 132 | call delete('Xtest') |
| 133 | endfunc |
Bram Moolenaar | 83799a7 | 2017-11-25 17:24:09 +0100 | [diff] [blame] | 134 | |
| 135 | func Test_writefile_sync_dev_stdout() |
| 136 | if !has('unix') |
| 137 | return |
| 138 | endif |
Bram Moolenaar | 9980b37 | 2018-04-21 20:12:35 +0200 | [diff] [blame] | 139 | if filewritable('/dev/stdout') |
| 140 | " Just check that this doesn't cause an error. |
| 141 | call writefile(['one'], '/dev/stdout') |
| 142 | else |
| 143 | throw 'Skipped: /dev/stdout is not writable' |
| 144 | endif |
Bram Moolenaar | 83799a7 | 2017-11-25 17:24:09 +0100 | [diff] [blame] | 145 | endfunc |
Bram Moolenaar | 8c9e7b0 | 2018-08-30 13:07:17 +0200 | [diff] [blame] | 146 | |
| 147 | func Test_writefile_autowrite() |
| 148 | set autowrite |
| 149 | new |
| 150 | next Xa Xb Xc |
| 151 | call setline(1, 'aaa') |
| 152 | next |
| 153 | call assert_equal(['aaa'], readfile('Xa')) |
| 154 | call setline(1, 'bbb') |
| 155 | call assert_fails('edit XX') |
| 156 | call assert_false(filereadable('Xb')) |
| 157 | |
| 158 | set autowriteall |
| 159 | edit XX |
| 160 | call assert_equal(['bbb'], readfile('Xb')) |
| 161 | |
| 162 | bwipe! |
| 163 | call delete('Xa') |
| 164 | call delete('Xb') |
| 165 | set noautowrite |
| 166 | endfunc |
| 167 | |
| 168 | func Test_writefile_autowrite_nowrite() |
| 169 | set autowrite |
| 170 | new |
| 171 | next Xa Xb Xc |
| 172 | set buftype=nowrite |
| 173 | call setline(1, 'aaa') |
| 174 | let buf = bufnr('%') |
| 175 | " buffer contents silently lost |
| 176 | edit XX |
| 177 | call assert_false(filereadable('Xa')) |
| 178 | rewind |
| 179 | call assert_equal('', getline(1)) |
| 180 | |
| 181 | bwipe! |
| 182 | set noautowrite |
| 183 | endfunc |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 184 | |
| 185 | " Test for ':w !<cmd>' to pipe lines from the current buffer to an external |
| 186 | " command. |
| 187 | func Test_write_pipe_to_cmd() |
Bram Moolenaar | ea3db91 | 2020-02-02 15:32:13 +0100 | [diff] [blame] | 188 | CheckUnix |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 189 | new |
| 190 | call setline(1, ['L1', 'L2', 'L3', 'L4']) |
| 191 | 2,3w !cat > Xfile |
| 192 | call assert_equal(['L2', 'L3'], readfile('Xfile')) |
| 193 | close! |
| 194 | call delete('Xfile') |
| 195 | endfunc |
| 196 | |
| 197 | " Test for :saveas |
| 198 | func Test_saveas() |
| 199 | call assert_fails('saveas', 'E471:') |
| 200 | call writefile(['L1'], 'Xfile') |
| 201 | new Xfile |
| 202 | new |
| 203 | call setline(1, ['L1']) |
| 204 | call assert_fails('saveas Xfile', 'E139:') |
| 205 | close! |
| 206 | enew | only |
| 207 | call delete('Xfile') |
| 208 | endfunc |
| 209 | |
| 210 | func Test_write_errors() |
| 211 | " Test for writing partial buffer |
| 212 | call writefile(['L1', 'L2', 'L3'], 'Xfile') |
| 213 | new Xfile |
| 214 | call assert_fails('1,2write', 'E140:') |
| 215 | close! |
| 216 | |
Bram Moolenaar | 4f5776c | 2020-02-12 22:15:19 +0100 | [diff] [blame] | 217 | call assert_fails('w > Xtest', 'E494:') |
| 218 | |
| 219 | call assert_fails('w > Xtest', 'E494:') |
| 220 | |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 221 | " Try to overwrite a directory |
| 222 | if has('unix') |
| 223 | call mkdir('Xdir1') |
| 224 | call assert_fails('write Xdir1', 'E17:') |
| 225 | call delete('Xdir1', 'd') |
| 226 | endif |
| 227 | |
| 228 | " Test for :wall for a buffer with no name |
| 229 | enew | only |
| 230 | call setline(1, ['L1']) |
| 231 | call assert_fails('wall', 'E141:') |
| 232 | enew! |
| 233 | |
| 234 | " Test for writing a 'readonly' file |
| 235 | new Xfile |
| 236 | set readonly |
| 237 | call assert_fails('write', 'E45:') |
| 238 | close |
| 239 | |
| 240 | " Test for writing to a read-only file |
| 241 | new Xfile |
| 242 | call setfperm('Xfile', 'r--r--r--') |
| 243 | call assert_fails('write', 'E505:') |
| 244 | call setfperm('Xfile', 'rw-rw-rw-') |
| 245 | close |
| 246 | |
| 247 | call delete('Xfile') |
| 248 | endfunc |
| 249 | |
| 250 | " vim: shiftwidth=2 sts=2 expandtab |