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 |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 4 | source term_util.vim |
Bram Moolenaar | ea3db91 | 2020-02-02 15:32:13 +0100 | [diff] [blame] | 5 | |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 6 | func Test_writefile() |
Bram Moolenaar | 19a1669 | 2016-09-01 22:19:47 +0200 | [diff] [blame] | 7 | let f = tempname() |
| 8 | call writefile(["over","written"], f, "b") |
| 9 | call writefile(["hello","world"], f, "b") |
| 10 | call writefile(["!", "good"], f, "a") |
| 11 | call writefile(["morning"], f, "ab") |
| 12 | call writefile(["", "vimmers"], f, "ab") |
| 13 | let l = readfile(f) |
| 14 | call assert_equal("hello", l[0]) |
| 15 | call assert_equal("world!", l[1]) |
| 16 | call assert_equal("good", l[2]) |
| 17 | call assert_equal("morning", l[3]) |
| 18 | call assert_equal("vimmers", l[4]) |
| 19 | call delete(f) |
Bram Moolenaar | 18a2b87 | 2020-03-19 13:08:45 +0100 | [diff] [blame] | 20 | |
| 21 | call assert_fails('call writefile("text", "Xfile")', 'E475: Invalid argument: writefile() first argument must be a List or a Blob') |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 22 | endfunc |
| 23 | |
Bram Moolenaar | b40c257 | 2019-10-19 21:01:05 +0200 | [diff] [blame] | 24 | func Test_writefile_ignore_regexp_error() |
| 25 | write Xt[z-a]est.txt |
| 26 | call delete('Xt[z-a]est.txt') |
| 27 | endfunc |
| 28 | |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 29 | func Test_writefile_fails_gently() |
| 30 | call assert_fails('call writefile(["test"], "Xfile", [])', 'E730:') |
| 31 | call assert_false(filereadable("Xfile")) |
| 32 | call delete("Xfile") |
| 33 | |
| 34 | call assert_fails('call writefile(["test", [], [], [], "tset"], "Xfile")', 'E730:') |
| 35 | call assert_false(filereadable("Xfile")) |
| 36 | call delete("Xfile") |
| 37 | |
| 38 | call assert_fails('call writefile([], "Xfile", [])', 'E730:') |
| 39 | call assert_false(filereadable("Xfile")) |
| 40 | call delete("Xfile") |
| 41 | |
| 42 | call assert_fails('call writefile([], [])', 'E730:') |
| 43 | endfunc |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 44 | |
| 45 | func Test_writefile_fails_conversion() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 46 | CheckFeature iconv |
| 47 | if has('sun') |
| 48 | throw 'Skipped: does not work on SunOS' |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 49 | endif |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 50 | " Without a backup file the write won't happen if there is a conversion |
| 51 | " error. |
Bram Moolenaar | c28cb5b | 2019-05-31 20:42:09 +0200 | [diff] [blame] | 52 | set nobackup nowritebackup backupdir=. backupskip= |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 53 | new |
| 54 | let contents = ["line one", "line two"] |
| 55 | call writefile(contents, 'Xfile') |
| 56 | edit Xfile |
| 57 | call setline(1, ["first line", "cannot convert \u010b", "third line"]) |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 58 | call assert_fails('write ++enc=cp932', 'E513:') |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 59 | call assert_equal(contents, readfile('Xfile')) |
| 60 | |
| 61 | call delete('Xfile') |
| 62 | bwipe! |
Bram Moolenaar | c28cb5b | 2019-05-31 20:42:09 +0200 | [diff] [blame] | 63 | set backup& writebackup& backupdir&vim backupskip&vim |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 64 | endfunc |
Bram Moolenaar | 2c33d7b | 2017-10-14 16:06:20 +0200 | [diff] [blame] | 65 | |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 66 | func Test_writefile_fails_conversion2() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 67 | CheckFeature iconv |
| 68 | if has('sun') |
| 69 | throw 'Skipped: does not work on SunOS' |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 70 | endif |
| 71 | " With a backup file the write happens even if there is a conversion error, |
| 72 | " but then the backup file must remain |
Bram Moolenaar | c28cb5b | 2019-05-31 20:42:09 +0200 | [diff] [blame] | 73 | set nobackup writebackup backupdir=. backupskip= |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 74 | let contents = ["line one", "line two"] |
| 75 | call writefile(contents, 'Xfile_conversion_err') |
| 76 | edit Xfile_conversion_err |
| 77 | call setline(1, ["first line", "cannot convert \u010b", "third line"]) |
| 78 | set fileencoding=latin1 |
| 79 | let output = execute('write') |
| 80 | call assert_match('CONVERSION ERROR', output) |
| 81 | call assert_equal(contents, readfile('Xfile_conversion_err~')) |
| 82 | |
| 83 | call delete('Xfile_conversion_err') |
| 84 | call delete('Xfile_conversion_err~') |
| 85 | bwipe! |
Bram Moolenaar | c28cb5b | 2019-05-31 20:42:09 +0200 | [diff] [blame] | 86 | set backup& writebackup& backupdir&vim backupskip&vim |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 87 | endfunc |
| 88 | |
Bram Moolenaar | 2c33d7b | 2017-10-14 16:06:20 +0200 | [diff] [blame] | 89 | func SetFlag(timer) |
| 90 | let g:flag = 1 |
| 91 | endfunc |
| 92 | |
| 93 | func Test_write_quit_split() |
| 94 | " Prevent exiting by splitting window on file write. |
| 95 | augroup testgroup |
| 96 | autocmd BufWritePre * split |
| 97 | augroup END |
| 98 | e! Xfile |
| 99 | call setline(1, 'nothing') |
| 100 | wq |
| 101 | |
| 102 | if has('timers') |
| 103 | " timer will not run if "exiting" is still set |
| 104 | let g:flag = 0 |
| 105 | call timer_start(1, 'SetFlag') |
| 106 | sleep 50m |
| 107 | call assert_equal(1, g:flag) |
| 108 | unlet g:flag |
| 109 | endif |
| 110 | au! testgroup |
| 111 | bwipe Xfile |
| 112 | call delete('Xfile') |
| 113 | endfunc |
| 114 | |
| 115 | func Test_nowrite_quit_split() |
| 116 | " Prevent exiting by opening a help window. |
| 117 | e! Xfile |
| 118 | help |
| 119 | wincmd w |
| 120 | exe winnr() . 'q' |
| 121 | |
| 122 | if has('timers') |
| 123 | " timer will not run if "exiting" is still set |
| 124 | let g:flag = 0 |
| 125 | call timer_start(1, 'SetFlag') |
| 126 | sleep 50m |
| 127 | call assert_equal(1, g:flag) |
| 128 | unlet g:flag |
| 129 | endif |
| 130 | bwipe Xfile |
| 131 | endfunc |
Bram Moolenaar | 7567d0b | 2017-11-16 23:04:15 +0100 | [diff] [blame] | 132 | |
| 133 | func Test_writefile_sync_arg() |
| 134 | " This doesn't check if fsync() works, only that the argument is accepted. |
| 135 | call writefile(['one'], 'Xtest', 's') |
| 136 | call writefile(['two'], 'Xtest', 'S') |
| 137 | call delete('Xtest') |
| 138 | endfunc |
Bram Moolenaar | 83799a7 | 2017-11-25 17:24:09 +0100 | [diff] [blame] | 139 | |
| 140 | func Test_writefile_sync_dev_stdout() |
Bram Moolenaar | b86abad | 2020-08-01 16:08:19 +0200 | [diff] [blame] | 141 | CheckUnix |
Bram Moolenaar | 9980b37 | 2018-04-21 20:12:35 +0200 | [diff] [blame] | 142 | if filewritable('/dev/stdout') |
| 143 | " Just check that this doesn't cause an error. |
| 144 | call writefile(['one'], '/dev/stdout') |
| 145 | else |
| 146 | throw 'Skipped: /dev/stdout is not writable' |
| 147 | endif |
Bram Moolenaar | 83799a7 | 2017-11-25 17:24:09 +0100 | [diff] [blame] | 148 | endfunc |
Bram Moolenaar | 8c9e7b0 | 2018-08-30 13:07:17 +0200 | [diff] [blame] | 149 | |
| 150 | func Test_writefile_autowrite() |
| 151 | set autowrite |
| 152 | new |
| 153 | next Xa Xb Xc |
| 154 | call setline(1, 'aaa') |
| 155 | next |
| 156 | call assert_equal(['aaa'], readfile('Xa')) |
| 157 | call setline(1, 'bbb') |
| 158 | call assert_fails('edit XX') |
| 159 | call assert_false(filereadable('Xb')) |
| 160 | |
| 161 | set autowriteall |
| 162 | edit XX |
| 163 | call assert_equal(['bbb'], readfile('Xb')) |
| 164 | |
| 165 | bwipe! |
| 166 | call delete('Xa') |
| 167 | call delete('Xb') |
| 168 | set noautowrite |
| 169 | endfunc |
| 170 | |
| 171 | func Test_writefile_autowrite_nowrite() |
| 172 | set autowrite |
| 173 | new |
| 174 | next Xa Xb Xc |
| 175 | set buftype=nowrite |
| 176 | call setline(1, 'aaa') |
| 177 | let buf = bufnr('%') |
| 178 | " buffer contents silently lost |
| 179 | edit XX |
| 180 | call assert_false(filereadable('Xa')) |
| 181 | rewind |
| 182 | call assert_equal('', getline(1)) |
| 183 | |
| 184 | bwipe! |
| 185 | set noautowrite |
| 186 | endfunc |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 187 | |
| 188 | " Test for ':w !<cmd>' to pipe lines from the current buffer to an external |
| 189 | " command. |
| 190 | func Test_write_pipe_to_cmd() |
Bram Moolenaar | ea3db91 | 2020-02-02 15:32:13 +0100 | [diff] [blame] | 191 | CheckUnix |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 192 | new |
| 193 | call setline(1, ['L1', 'L2', 'L3', 'L4']) |
| 194 | 2,3w !cat > Xfile |
| 195 | call assert_equal(['L2', 'L3'], readfile('Xfile')) |
| 196 | close! |
| 197 | call delete('Xfile') |
| 198 | endfunc |
| 199 | |
| 200 | " Test for :saveas |
| 201 | func Test_saveas() |
| 202 | call assert_fails('saveas', 'E471:') |
| 203 | call writefile(['L1'], 'Xfile') |
| 204 | new Xfile |
| 205 | new |
| 206 | call setline(1, ['L1']) |
| 207 | call assert_fails('saveas Xfile', 'E139:') |
| 208 | close! |
| 209 | enew | only |
| 210 | call delete('Xfile') |
| 211 | endfunc |
| 212 | |
| 213 | func Test_write_errors() |
| 214 | " Test for writing partial buffer |
| 215 | call writefile(['L1', 'L2', 'L3'], 'Xfile') |
| 216 | new Xfile |
| 217 | call assert_fails('1,2write', 'E140:') |
| 218 | close! |
| 219 | |
Bram Moolenaar | 4f5776c | 2020-02-12 22:15:19 +0100 | [diff] [blame] | 220 | call assert_fails('w > Xtest', 'E494:') |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 221 | |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 222 | " Try to overwrite a directory |
| 223 | if has('unix') |
| 224 | call mkdir('Xdir1') |
| 225 | call assert_fails('write Xdir1', 'E17:') |
| 226 | call delete('Xdir1', 'd') |
| 227 | endif |
| 228 | |
| 229 | " Test for :wall for a buffer with no name |
| 230 | enew | only |
| 231 | call setline(1, ['L1']) |
| 232 | call assert_fails('wall', 'E141:') |
| 233 | enew! |
| 234 | |
| 235 | " Test for writing a 'readonly' file |
| 236 | new Xfile |
| 237 | set readonly |
| 238 | call assert_fails('write', 'E45:') |
| 239 | close |
| 240 | |
| 241 | " Test for writing to a read-only file |
| 242 | new Xfile |
| 243 | call setfperm('Xfile', 'r--r--r--') |
| 244 | call assert_fails('write', 'E505:') |
| 245 | call setfperm('Xfile', 'rw-rw-rw-') |
| 246 | close |
| 247 | |
| 248 | call delete('Xfile') |
Bram Moolenaar | 99fa721 | 2020-04-26 15:59:55 +0200 | [diff] [blame] | 249 | |
| 250 | call writefile(test_null_list(), 'Xfile') |
| 251 | call assert_false(filereadable('Xfile')) |
| 252 | call writefile(test_null_blob(), 'Xfile') |
| 253 | call assert_false(filereadable('Xfile')) |
| 254 | call assert_fails('call writefile([], "")', 'E482:') |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 255 | |
| 256 | " very long file name |
| 257 | let long_fname = repeat('n', 5000) |
| 258 | call assert_fails('exe "w " .. long_fname', 'E75:') |
| 259 | call assert_fails('call writefile([], long_fname)', 'E482:') |
| 260 | endfunc |
| 261 | |
| 262 | " Test for writing to a file which is modified after Vim read it |
| 263 | func Test_write_file_mtime() |
| 264 | CheckEnglish |
| 265 | CheckRunVimInTerminal |
| 266 | |
| 267 | " First read the file into a buffer |
| 268 | call writefile(["Line1", "Line2"], 'Xfile') |
| 269 | let old_ftime = getftime('Xfile') |
| 270 | let buf = RunVimInTerminal('Xfile', #{rows : 10}) |
Bram Moolenaar | 733d259 | 2020-08-20 18:59:06 +0200 | [diff] [blame] | 271 | call TermWait(buf) |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 272 | call term_sendkeys(buf, ":set noswapfile\<CR>") |
Bram Moolenaar | 733d259 | 2020-08-20 18:59:06 +0200 | [diff] [blame] | 273 | call TermWait(buf) |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 274 | |
| 275 | " Modify the file directly. Make sure the file modification time is |
| 276 | " different. Note that on Linux/Unix, the file is considered modified |
| 277 | " outside, only if the difference is 2 seconds or more |
| 278 | sleep 1 |
| 279 | call writefile(["Line3", "Line4"], 'Xfile') |
| 280 | let new_ftime = getftime('Xfile') |
| 281 | while new_ftime - old_ftime < 2 |
| 282 | sleep 100m |
| 283 | call writefile(["Line3", "Line4"], 'Xfile') |
| 284 | let new_ftime = getftime('Xfile') |
| 285 | endwhile |
| 286 | |
| 287 | " Try to overwrite the file and check for the prompt |
| 288 | call term_sendkeys(buf, ":w\<CR>") |
Bram Moolenaar | 733d259 | 2020-08-20 18:59:06 +0200 | [diff] [blame] | 289 | call TermWait(buf) |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 290 | call WaitForAssert({-> assert_equal("WARNING: The file has been changed since reading it!!!", term_getline(buf, 9))}) |
| 291 | call assert_equal("Do you really want to write to it (y/n)?", |
| 292 | \ term_getline(buf, 10)) |
| 293 | call term_sendkeys(buf, "n\<CR>") |
Bram Moolenaar | 733d259 | 2020-08-20 18:59:06 +0200 | [diff] [blame] | 294 | call TermWait(buf) |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 295 | call assert_equal(new_ftime, getftime('Xfile')) |
| 296 | call term_sendkeys(buf, ":w\<CR>") |
Bram Moolenaar | 733d259 | 2020-08-20 18:59:06 +0200 | [diff] [blame] | 297 | call TermWait(buf) |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 298 | call term_sendkeys(buf, "y\<CR>") |
Bram Moolenaar | 733d259 | 2020-08-20 18:59:06 +0200 | [diff] [blame] | 299 | call TermWait(buf) |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 300 | call WaitForAssert({-> assert_equal('Line2', readfile('Xfile')[1])}) |
| 301 | |
| 302 | " clean up |
| 303 | call StopVimInTerminal(buf) |
| 304 | call delete('Xfile') |
| 305 | endfunc |
| 306 | |
| 307 | " Test for an autocmd unloading a buffer during a write command |
| 308 | func Test_write_autocmd_unloadbuf_lockmark() |
| 309 | augroup WriteTest |
| 310 | autocmd BufWritePre Xfile enew | write |
| 311 | augroup END |
| 312 | e Xfile |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 313 | call assert_fails('lockmarks write', ['E32:', 'E203:']) |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 314 | augroup WriteTest |
| 315 | au! |
| 316 | augroup END |
| 317 | augroup! WriteTest |
| 318 | endfunc |
| 319 | |
| 320 | " Test for writing a buffer with 'acwrite' but without autocmds |
| 321 | func Test_write_acwrite_error() |
| 322 | new Xfile |
| 323 | call setline(1, ['line1', 'line2', 'line3']) |
| 324 | set buftype=acwrite |
| 325 | call assert_fails('write', 'E676:') |
| 326 | call assert_fails('1,2write!', 'E676:') |
| 327 | call assert_fails('w >>', 'E676:') |
| 328 | close! |
| 329 | endfunc |
| 330 | |
| 331 | " Test for adding and removing lines from an autocmd when writing a buffer |
| 332 | func Test_write_autocmd_add_remove_lines() |
| 333 | new Xfile |
| 334 | call setline(1, ['aaa', 'bbb', 'ccc', 'ddd']) |
| 335 | |
| 336 | " Autocmd deleting lines from the file when writing a partial file |
| 337 | augroup WriteTest2 |
| 338 | au! |
| 339 | autocmd FileWritePre Xfile 1,2d |
| 340 | augroup END |
| 341 | call assert_fails('2,3w!', 'E204:') |
| 342 | |
| 343 | " Autocmd adding lines to a file when writing a partial file |
| 344 | augroup WriteTest2 |
| 345 | au! |
| 346 | autocmd FileWritePre Xfile call append(0, ['xxx', 'yyy']) |
| 347 | augroup END |
| 348 | %d |
| 349 | call setline(1, ['aaa', 'bbb', 'ccc', 'ddd']) |
| 350 | 1,2w! |
| 351 | call assert_equal(['xxx', 'yyy', 'aaa', 'bbb'], readfile('Xfile')) |
| 352 | |
| 353 | " Autocmd deleting lines from the file when writing the whole file |
| 354 | augroup WriteTest2 |
| 355 | au! |
| 356 | autocmd BufWritePre Xfile 1,2d |
| 357 | augroup END |
| 358 | %d |
| 359 | call setline(1, ['aaa', 'bbb', 'ccc', 'ddd']) |
| 360 | w |
| 361 | call assert_equal(['ccc', 'ddd'], readfile('Xfile')) |
| 362 | |
| 363 | augroup WriteTest2 |
| 364 | au! |
| 365 | augroup END |
| 366 | augroup! WriteTest2 |
| 367 | |
| 368 | close! |
| 369 | call delete('Xfile') |
| 370 | endfunc |
| 371 | |
| 372 | " Test for writing to a readonly file |
| 373 | func Test_write_readonly() |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 374 | call writefile([], 'Xfile') |
| 375 | call setfperm('Xfile', "r--------") |
| 376 | edit Xfile |
Bram Moolenaar | b86abad | 2020-08-01 16:08:19 +0200 | [diff] [blame] | 377 | set noreadonly backupskip= |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 378 | call assert_fails('write', 'E505:') |
| 379 | let save_cpo = &cpo |
| 380 | set cpo+=W |
| 381 | call assert_fails('write!', 'E504:') |
| 382 | let &cpo = save_cpo |
Bram Moolenaar | 1de5f7c | 2020-06-11 19:22:43 +0200 | [diff] [blame] | 383 | call setline(1, ['line1']) |
| 384 | write! |
| 385 | call assert_equal(['line1'], readfile('Xfile')) |
Bram Moolenaar | b86abad | 2020-08-01 16:08:19 +0200 | [diff] [blame] | 386 | set backupskip& |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 387 | call delete('Xfile') |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 388 | endfunc |
| 389 | |
Bram Moolenaar | 1de5f7c | 2020-06-11 19:22:43 +0200 | [diff] [blame] | 390 | " Test for 'patchmode' |
| 391 | func Test_patchmode() |
Bram Moolenaar | 1de5f7c | 2020-06-11 19:22:43 +0200 | [diff] [blame] | 392 | call writefile(['one'], 'Xfile') |
Bram Moolenaar | b86abad | 2020-08-01 16:08:19 +0200 | [diff] [blame] | 393 | set patchmode=.orig nobackup backupskip= writebackup |
Bram Moolenaar | 1de5f7c | 2020-06-11 19:22:43 +0200 | [diff] [blame] | 394 | new Xfile |
| 395 | call setline(1, 'two') |
| 396 | " first write should create the .orig file |
| 397 | write |
Bram Moolenaar | 1de5f7c | 2020-06-11 19:22:43 +0200 | [diff] [blame] | 398 | call assert_equal(['one'], readfile('Xfile.orig')) |
| 399 | call setline(1, 'three') |
| 400 | " subsequent writes should not create/modify the .orig file |
| 401 | write |
| 402 | call assert_equal(['one'], readfile('Xfile.orig')) |
Bram Moolenaar | b86abad | 2020-08-01 16:08:19 +0200 | [diff] [blame] | 403 | set patchmode& backup& backupskip& writebackup& |
Bram Moolenaar | 1de5f7c | 2020-06-11 19:22:43 +0200 | [diff] [blame] | 404 | call delete('Xfile') |
| 405 | call delete('Xfile.orig') |
| 406 | endfunc |
| 407 | |
| 408 | " Test for writing to a file in a readonly directory |
| 409 | func Test_write_readonly_dir() |
Bram Moolenaar | b86abad | 2020-08-01 16:08:19 +0200 | [diff] [blame] | 410 | " On MS-Windows, modifying files in a read-only directory is allowed. |
| 411 | CheckUnix |
Bram Moolenaar | 1de5f7c | 2020-06-11 19:22:43 +0200 | [diff] [blame] | 412 | call mkdir('Xdir') |
| 413 | call writefile(['one'], 'Xdir/Xfile1') |
| 414 | call setfperm('Xdir', 'r-xr--r--') |
| 415 | " try to create a new file in the directory |
| 416 | new Xdir/Xfile2 |
| 417 | call setline(1, 'two') |
| 418 | call assert_fails('write', 'E212:') |
| 419 | " try to create a backup file in the directory |
| 420 | edit! Xdir/Xfile1 |
Bram Moolenaar | b86abad | 2020-08-01 16:08:19 +0200 | [diff] [blame] | 421 | set backupdir=./Xdir backupskip= |
Bram Moolenaar | 1de5f7c | 2020-06-11 19:22:43 +0200 | [diff] [blame] | 422 | set patchmode=.orig |
| 423 | call assert_fails('write', 'E509:') |
| 424 | call setfperm('Xdir', 'rwxr--r--') |
| 425 | call delete('Xdir', 'rf') |
Bram Moolenaar | b86abad | 2020-08-01 16:08:19 +0200 | [diff] [blame] | 426 | set backupdir& backupskip& patchmode& |
Bram Moolenaar | 1de5f7c | 2020-06-11 19:22:43 +0200 | [diff] [blame] | 427 | endfunc |
| 428 | |
Bram Moolenaar | b340bae | 2020-06-15 19:51:56 +0200 | [diff] [blame] | 429 | " Test for writing a file using invalid file encoding |
| 430 | func Test_write_invalid_encoding() |
| 431 | new |
| 432 | call setline(1, 'abc') |
| 433 | call assert_fails('write ++enc=axbyc Xfile', 'E213:') |
| 434 | close! |
| 435 | endfunc |
| 436 | |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 437 | " Tests for reading and writing files with conversion for Win32. |
| 438 | func Test_write_file_encoding() |
| 439 | CheckMSWindows |
| 440 | let save_encoding = &encoding |
| 441 | let save_fileencodings = &fileencodings |
| 442 | set encoding& fileencodings& |
| 443 | let text =<< trim END |
| 444 | 1 utf-8 text: ÐÐ»Ñ Vim version 6.2. ÐоÑледнее изменение: 1970 Jan 01 |
| 445 | 2 cp1251 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01 |
| 446 | 3 cp866 text: «ï Vim version 6.2. ®á«¥¤¥¥ ¨§¬¥¥¨¥: 1970 Jan 01 |
| 447 | END |
| 448 | call writefile(text, 'Xfile') |
| 449 | edit Xfile |
| 450 | |
| 451 | " write tests: |
| 452 | " combine three values for 'encoding' with three values for 'fileencoding' |
| 453 | " also write files for read tests |
| 454 | call cursor(1, 1) |
| 455 | set encoding=utf-8 |
| 456 | .w! ++enc=utf-8 Xtest |
| 457 | .w ++enc=cp1251 >> Xtest |
| 458 | .w ++enc=cp866 >> Xtest |
| 459 | .w! ++enc=utf-8 Xutf8 |
| 460 | let expected =<< trim END |
| 461 | 1 utf-8 text: ÐÐ»Ñ Vim version 6.2. ÐоÑледнее изменение: 1970 Jan 01 |
| 462 | 1 utf-8 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01 |
| 463 | 1 utf-8 text: «ï Vim version 6.2. ®á«¥¤¥¥ ¨§¬¥¥¨¥: 1970 Jan 01 |
| 464 | END |
| 465 | call assert_equal(expected, readfile('Xtest')) |
| 466 | |
| 467 | call cursor(2, 1) |
| 468 | set encoding=cp1251 |
| 469 | .w! ++enc=utf-8 Xtest |
| 470 | .w ++enc=cp1251 >> Xtest |
| 471 | .w ++enc=cp866 >> Xtest |
| 472 | .w! ++enc=cp1251 Xcp1251 |
| 473 | let expected =<< trim END |
| 474 | 2 cp1251 text: ÐÐ»Ñ Vim version 6.2. ÐоÑледнее изменение: 1970 Jan 01 |
| 475 | 2 cp1251 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01 |
| 476 | 2 cp1251 text: «ï Vim version 6.2. ®á«¥¤¥¥ ¨§¬¥¥¨¥: 1970 Jan 01 |
| 477 | END |
| 478 | call assert_equal(expected, readfile('Xtest')) |
| 479 | |
| 480 | call cursor(3, 1) |
| 481 | set encoding=cp866 |
| 482 | .w! ++enc=utf-8 Xtest |
| 483 | .w ++enc=cp1251 >> Xtest |
| 484 | .w ++enc=cp866 >> Xtest |
| 485 | .w! ++enc=cp866 Xcp866 |
| 486 | let expected =<< trim END |
| 487 | 3 cp866 text: ÐÐ»Ñ Vim version 6.2. ÐоÑледнее изменение: 1970 Jan 01 |
| 488 | 3 cp866 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01 |
| 489 | 3 cp866 text: «ï Vim version 6.2. ®á«¥¤¥¥ ¨§¬¥¥¨¥: 1970 Jan 01 |
| 490 | END |
| 491 | call assert_equal(expected, readfile('Xtest')) |
| 492 | |
| 493 | " read three 'fileencoding's with utf-8 'encoding' |
| 494 | set encoding=utf-8 fencs=utf-8,cp1251 |
| 495 | e Xutf8 |
| 496 | .w! ++enc=utf-8 Xtest |
| 497 | e Xcp1251 |
| 498 | .w ++enc=utf-8 >> Xtest |
| 499 | set fencs=utf-8,cp866 |
| 500 | e Xcp866 |
| 501 | .w ++enc=utf-8 >> Xtest |
| 502 | let expected =<< trim END |
| 503 | 1 utf-8 text: ÐÐ»Ñ Vim version 6.2. ÐоÑледнее изменение: 1970 Jan 01 |
| 504 | 2 cp1251 text: ÐÐ»Ñ Vim version 6.2. ÐоÑледнее изменение: 1970 Jan 01 |
| 505 | 3 cp866 text: ÐÐ»Ñ Vim version 6.2. ÐоÑледнее изменение: 1970 Jan 01 |
| 506 | END |
| 507 | call assert_equal(expected, readfile('Xtest')) |
| 508 | |
| 509 | " read three 'fileencoding's with cp1251 'encoding' |
| 510 | set encoding=utf-8 fencs=utf-8,cp1251 |
| 511 | e Xutf8 |
| 512 | .w! ++enc=cp1251 Xtest |
| 513 | e Xcp1251 |
| 514 | .w ++enc=cp1251 >> Xtest |
| 515 | set fencs=utf-8,cp866 |
| 516 | e Xcp866 |
| 517 | .w ++enc=cp1251 >> Xtest |
| 518 | let expected =<< trim END |
| 519 | 1 utf-8 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01 |
| 520 | 2 cp1251 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01 |
| 521 | 3 cp866 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01 |
| 522 | END |
| 523 | call assert_equal(expected, readfile('Xtest')) |
| 524 | |
| 525 | " read three 'fileencoding's with cp866 'encoding' |
| 526 | set encoding=cp866 fencs=utf-8,cp1251 |
| 527 | e Xutf8 |
| 528 | .w! ++enc=cp866 Xtest |
| 529 | e Xcp1251 |
| 530 | .w ++enc=cp866 >> Xtest |
| 531 | set fencs=utf-8,cp866 |
| 532 | e Xcp866 |
| 533 | .w ++enc=cp866 >> Xtest |
| 534 | let expected =<< trim END |
| 535 | 1 utf-8 text: «ï Vim version 6.2. ®á«¥¤¥¥ ¨§¬¥¥¨¥: 1970 Jan 01 |
| 536 | 2 cp1251 text: «ï Vim version 6.2. ®á«¥¤¥¥ ¨§¬¥¥¨¥: 1970 Jan 01 |
| 537 | 3 cp866 text: «ï Vim version 6.2. ®á«¥¤¥¥ ¨§¬¥¥¨¥: 1970 Jan 01 |
| 538 | END |
| 539 | call assert_equal(expected, readfile('Xtest')) |
| 540 | |
| 541 | call delete('Xfile') |
| 542 | call delete('Xtest') |
| 543 | call delete('Xutf8') |
| 544 | call delete('Xcp1251') |
| 545 | call delete('Xcp866') |
| 546 | let &encoding = save_encoding |
| 547 | let &fileencodings = save_fileencodings |
| 548 | %bw! |
| 549 | endfunc |
| 550 | |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 551 | " Test for writing and reading a file starting with a BOM. |
| 552 | " Byte Order Mark (BOM) character for various encodings is below: |
| 553 | " UTF-8 : EF BB BF |
| 554 | " UTF-16 (BE): FE FF |
| 555 | " UTF-16 (LE): FF FE |
| 556 | " UTF-32 (BE): 00 00 FE FF |
| 557 | " UTF-32 (LE): FF FE 00 00 |
| 558 | func Test_readwrite_file_with_bom() |
| 559 | let utf8_bom = "\xEF\xBB\xBF" |
| 560 | let utf16be_bom = "\xFE\xFF" |
| 561 | let utf16le_bom = "\xFF\xFE" |
| 562 | let utf32be_bom = "\n\n\xFE\xFF" |
| 563 | let utf32le_bom = "\xFF\xFE\n\n" |
| 564 | let save_fileencoding = &fileencoding |
| 565 | set cpoptions+=S |
| 566 | |
| 567 | " Check that editing a latin1 file doesn't see a BOM |
| 568 | call writefile(["\xFE\xFElatin-1"], 'Xtest1') |
| 569 | edit Xtest1 |
| 570 | call assert_equal('latin1', &fileencoding) |
| 571 | call assert_equal(0, &bomb) |
| 572 | set fenc=latin1 |
| 573 | write Xfile2 |
| 574 | call assert_equal(["\xFE\xFElatin-1", ''], readfile('Xfile2', 'b')) |
| 575 | set bomb fenc=latin1 |
| 576 | write Xtest3 |
| 577 | call assert_equal(["\xFE\xFElatin-1", ''], readfile('Xtest3', 'b')) |
| 578 | set bomb& |
| 579 | |
| 580 | " Check utf-8 BOM |
| 581 | %bw! |
| 582 | call writefile([utf8_bom .. "utf-8"], 'Xtest1') |
| 583 | edit! Xtest1 |
| 584 | call assert_equal('utf-8', &fileencoding) |
| 585 | call assert_equal(1, &bomb) |
| 586 | call assert_equal('utf-8', getline(1)) |
| 587 | set fenc=latin1 |
| 588 | write! Xfile2 |
| 589 | call assert_equal(['utf-8', ''], readfile('Xfile2', 'b')) |
| 590 | set fenc=utf-8 |
| 591 | w! Xtest3 |
| 592 | call assert_equal([utf8_bom .. "utf-8", ''], readfile('Xtest3', 'b')) |
| 593 | |
| 594 | " Check utf-8 with an error (will fall back to latin-1) |
| 595 | %bw! |
| 596 | call writefile([utf8_bom .. "utf-8\x80err"], 'Xtest1') |
| 597 | edit! Xtest1 |
| 598 | call assert_equal('latin1', &fileencoding) |
| 599 | call assert_equal(0, &bomb) |
| 600 | call assert_equal("\xC3\xAF\xC2\xBB\xC2\xBFutf-8\xC2\x80err", getline(1)) |
| 601 | set fenc=latin1 |
| 602 | write! Xfile2 |
| 603 | call assert_equal([utf8_bom .. "utf-8\x80err", ''], readfile('Xfile2', 'b')) |
| 604 | set fenc=utf-8 |
| 605 | w! Xtest3 |
| 606 | call assert_equal(["\xC3\xAF\xC2\xBB\xC2\xBFutf-8\xC2\x80err", ''], |
| 607 | \ readfile('Xtest3', 'b')) |
| 608 | |
| 609 | " Check ucs-2 BOM |
| 610 | %bw! |
| 611 | call writefile([utf16be_bom .. "\nu\nc\ns\n-\n2\n"], 'Xtest1') |
| 612 | edit! Xtest1 |
| 613 | call assert_equal('utf-16', &fileencoding) |
| 614 | call assert_equal(1, &bomb) |
| 615 | call assert_equal('ucs-2', getline(1)) |
| 616 | set fenc=latin1 |
| 617 | write! Xfile2 |
| 618 | call assert_equal(["ucs-2", ''], readfile('Xfile2', 'b')) |
| 619 | set fenc=ucs-2 |
| 620 | w! Xtest3 |
| 621 | call assert_equal([utf16be_bom .. "\nu\nc\ns\n-\n2\n", ''], |
| 622 | \ readfile('Xtest3', 'b')) |
| 623 | |
| 624 | " Check ucs-2le BOM |
| 625 | %bw! |
| 626 | call writefile([utf16le_bom .. "u\nc\ns\n-\n2\nl\ne\n"], 'Xtest1') |
| 627 | " Need to add a NUL byte after the NL byte |
| 628 | call writefile(0z00, 'Xtest1', 'a') |
| 629 | edit! Xtest1 |
| 630 | call assert_equal('utf-16le', &fileencoding) |
| 631 | call assert_equal(1, &bomb) |
| 632 | call assert_equal('ucs-2le', getline(1)) |
| 633 | set fenc=latin1 |
| 634 | write! Xfile2 |
| 635 | call assert_equal(["ucs-2le", ''], readfile('Xfile2', 'b')) |
| 636 | set fenc=ucs-2le |
| 637 | w! Xtest3 |
| 638 | call assert_equal([utf16le_bom .. "u\nc\ns\n-\n2\nl\ne\n", "\n"], |
| 639 | \ readfile('Xtest3', 'b')) |
| 640 | |
| 641 | " Check ucs-4 BOM |
| 642 | %bw! |
| 643 | call writefile([utf32be_bom .. "\n\n\nu\n\n\nc\n\n\ns\n\n\n-\n\n\n4\n\n\n"], 'Xtest1') |
| 644 | edit! Xtest1 |
| 645 | call assert_equal('ucs-4', &fileencoding) |
| 646 | call assert_equal(1, &bomb) |
| 647 | call assert_equal('ucs-4', getline(1)) |
| 648 | set fenc=latin1 |
| 649 | write! Xfile2 |
| 650 | call assert_equal(["ucs-4", ''], readfile('Xfile2', 'b')) |
| 651 | set fenc=ucs-4 |
| 652 | w! Xtest3 |
| 653 | call 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')) |
| 654 | |
| 655 | " Check ucs-4le BOM |
| 656 | %bw! |
| 657 | call 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') |
| 658 | " Need to add three NUL bytes after the NL byte |
| 659 | call writefile(0z000000, 'Xtest1', 'a') |
| 660 | edit! Xtest1 |
| 661 | call assert_equal('ucs-4le', &fileencoding) |
| 662 | call assert_equal(1, &bomb) |
| 663 | call assert_equal('ucs-4le', getline(1)) |
| 664 | set fenc=latin1 |
| 665 | write! Xfile2 |
| 666 | call assert_equal(["ucs-4le", ''], readfile('Xfile2', 'b')) |
| 667 | set fenc=ucs-4le |
| 668 | w! Xtest3 |
| 669 | 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('Xtest3', 'b')) |
| 670 | |
| 671 | set cpoptions-=S |
| 672 | let &fileencoding = save_fileencoding |
| 673 | call delete('Xtest1') |
Bram Moolenaar | 733d259 | 2020-08-20 18:59:06 +0200 | [diff] [blame] | 674 | call delete('Xfile2') |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 675 | call delete('Xtest3') |
| 676 | %bw! |
| 677 | endfunc |
| 678 | |
Bram Moolenaar | b3c8b1d | 2020-12-23 18:54:57 +0100 | [diff] [blame] | 679 | func Test_read_write_bin() |
| 680 | " write file missing EOL |
| 681 | call writefile(['noeol'], "XNoEolSetEol", 'bS') |
| 682 | call assert_equal(0z6E6F656F6C, readfile('XNoEolSetEol', 'B')) |
| 683 | |
| 684 | " when file is read 'eol' is off |
Bram Moolenaar | 1620496 | 2020-12-23 22:40:11 +0100 | [diff] [blame] | 685 | set nofixeol |
| 686 | e! ++ff=unix XNoEolSetEol |
Bram Moolenaar | b3c8b1d | 2020-12-23 18:54:57 +0100 | [diff] [blame] | 687 | call assert_equal(0, &eol) |
| 688 | |
| 689 | " writing with 'eol' set adds the newline |
| 690 | setlocal eol |
| 691 | w |
| 692 | call assert_equal(0z6E6F656F6C0A, readfile('XNoEolSetEol', 'B')) |
| 693 | |
| 694 | call delete('XNoEolSetEol') |
Bram Moolenaar | bd31855 | 2020-12-23 20:55:15 +0100 | [diff] [blame] | 695 | set ff& |
| 696 | bwipe! XNoEolSetEol |
Bram Moolenaar | b3c8b1d | 2020-12-23 18:54:57 +0100 | [diff] [blame] | 697 | endfunc |
| 698 | |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 699 | " vim: shiftwidth=2 sts=2 expandtab |