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() |
Bram Moolenaar | db4c947 | 2022-10-15 22:06:06 +0100 | [diff] [blame] | 8 | call writefile(["over", "written"], f, "bD") |
| 9 | call writefile(["hello", "world"], f, "b") |
Bram Moolenaar | 19a1669 | 2016-09-01 22:19:47 +0200 | [diff] [blame] | 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]) |
Bram Moolenaar | 18a2b87 | 2020-03-19 13:08:45 +0100 | [diff] [blame] | 19 | |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 20 | call assert_fails('call writefile("text", "Xwffile")', '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] | 21 | endfunc |
| 22 | |
Bram Moolenaar | b40c257 | 2019-10-19 21:01:05 +0200 | [diff] [blame] | 23 | func Test_writefile_ignore_regexp_error() |
| 24 | write Xt[z-a]est.txt |
| 25 | call delete('Xt[z-a]est.txt') |
| 26 | endfunc |
| 27 | |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 28 | func Test_writefile_fails_gently() |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 29 | call assert_fails('call writefile(["test"], "Xwffile", [])', 'E730:') |
| 30 | call assert_false(filereadable("Xwffile")) |
| 31 | call delete("Xwffile") |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 32 | |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 33 | call assert_fails('call writefile(["test", [], [], [], "tset"], "Xwffile")', 'E730:') |
| 34 | call assert_false(filereadable("Xwffile")) |
| 35 | call delete("Xwffile") |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 36 | |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 37 | call assert_fails('call writefile([], "Xwffile", [])', 'E730:') |
| 38 | call assert_false(filereadable("Xwffile")) |
| 39 | call delete("Xwffile") |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 40 | |
| 41 | call assert_fails('call writefile([], [])', 'E730:') |
| 42 | endfunc |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 43 | |
| 44 | func Test_writefile_fails_conversion() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 45 | CheckFeature iconv |
| 46 | if has('sun') |
| 47 | throw 'Skipped: does not work on SunOS' |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 48 | endif |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 49 | " Without a backup file the write won't happen if there is a conversion |
| 50 | " error. |
Bram Moolenaar | c28cb5b | 2019-05-31 20:42:09 +0200 | [diff] [blame] | 51 | set nobackup nowritebackup backupdir=. backupskip= |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 52 | new |
| 53 | let contents = ["line one", "line two"] |
Bram Moolenaar | db4c947 | 2022-10-15 22:06:06 +0100 | [diff] [blame] | 54 | call writefile(contents, 'Xwfcfile', 'D') |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 55 | edit Xwfcfile |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 56 | call setline(1, ["first line", "cannot convert \u010b", "third line"]) |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 57 | call assert_fails('write ++enc=cp932', 'E513:') |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 58 | call assert_equal(contents, readfile('Xwfcfile')) |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 59 | |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 60 | " With 'backupcopy' set, if there is a conversion error, the backup file is |
| 61 | " still created. |
| 62 | set backupcopy=yes writebackup& backup& |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 63 | call delete('Xwfcfile' .. &backupext) |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 64 | call assert_fails('write ++enc=cp932', 'E513:') |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 65 | call assert_equal(contents, readfile('Xwfcfile')) |
| 66 | call assert_equal(contents, readfile('Xwfcfile' .. &backupext)) |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 67 | set backupcopy& |
| 68 | %bw! |
| 69 | |
| 70 | " Conversion error during write |
| 71 | new |
| 72 | call setline(1, ["\U10000000"]) |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 73 | let output = execute('write! ++enc=utf-16 Xwfcfile') |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 74 | call assert_match('CONVERSION ERROR', output) |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 75 | let output = execute('write! ++enc=ucs-2 Xwfcfile') |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 76 | call assert_match('CONVERSION ERROR', output) |
Dominique Pelle | b40ad4f | 2022-09-04 21:29:46 +0100 | [diff] [blame] | 77 | call delete('Xwfcfilz~') |
| 78 | call delete('Xwfcfily~') |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 79 | %bw! |
| 80 | |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 81 | call delete('Xwfcfile' .. &backupext) |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 82 | bwipe! |
Bram Moolenaar | c28cb5b | 2019-05-31 20:42:09 +0200 | [diff] [blame] | 83 | set backup& writebackup& backupdir&vim backupskip&vim |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 84 | endfunc |
Bram Moolenaar | 2c33d7b | 2017-10-14 16:06:20 +0200 | [diff] [blame] | 85 | |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 86 | func Test_writefile_fails_conversion2() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 87 | CheckFeature iconv |
| 88 | if has('sun') |
| 89 | throw 'Skipped: does not work on SunOS' |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 90 | endif |
| 91 | " With a backup file the write happens even if there is a conversion error, |
| 92 | " but then the backup file must remain |
Bram Moolenaar | c28cb5b | 2019-05-31 20:42:09 +0200 | [diff] [blame] | 93 | set nobackup writebackup backupdir=. backupskip= |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 94 | let contents = ["line one", "line two"] |
Bram Moolenaar | db4c947 | 2022-10-15 22:06:06 +0100 | [diff] [blame] | 95 | call writefile(contents, 'Xwf2file_conversion_err', 'D') |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 96 | edit Xwf2file_conversion_err |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 97 | call setline(1, ["first line", "cannot convert \u010b", "third line"]) |
| 98 | set fileencoding=latin1 |
| 99 | let output = execute('write') |
| 100 | call assert_match('CONVERSION ERROR', output) |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 101 | call assert_equal(contents, readfile('Xwf2file_conversion_err~')) |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 102 | |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 103 | call delete('Xwf2file_conversion_err~') |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 104 | bwipe! |
Bram Moolenaar | c28cb5b | 2019-05-31 20:42:09 +0200 | [diff] [blame] | 105 | set backup& writebackup& backupdir&vim backupskip&vim |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 106 | endfunc |
| 107 | |
Bram Moolenaar | 2c33d7b | 2017-10-14 16:06:20 +0200 | [diff] [blame] | 108 | func SetFlag(timer) |
| 109 | let g:flag = 1 |
| 110 | endfunc |
| 111 | |
| 112 | func Test_write_quit_split() |
| 113 | " Prevent exiting by splitting window on file write. |
| 114 | augroup testgroup |
| 115 | autocmd BufWritePre * split |
| 116 | augroup END |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 117 | e! Xwqsfile |
Bram Moolenaar | 2c33d7b | 2017-10-14 16:06:20 +0200 | [diff] [blame] | 118 | call setline(1, 'nothing') |
| 119 | wq |
| 120 | |
| 121 | if has('timers') |
| 122 | " timer will not run if "exiting" is still set |
| 123 | let g:flag = 0 |
| 124 | call timer_start(1, 'SetFlag') |
| 125 | sleep 50m |
| 126 | call assert_equal(1, g:flag) |
| 127 | unlet g:flag |
| 128 | endif |
| 129 | au! testgroup |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 130 | bwipe Xwqsfile |
| 131 | call delete('Xwqsfile') |
Bram Moolenaar | 2c33d7b | 2017-10-14 16:06:20 +0200 | [diff] [blame] | 132 | endfunc |
| 133 | |
| 134 | func Test_nowrite_quit_split() |
| 135 | " Prevent exiting by opening a help window. |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 136 | e! Xnqsfile |
Bram Moolenaar | 2c33d7b | 2017-10-14 16:06:20 +0200 | [diff] [blame] | 137 | help |
| 138 | wincmd w |
| 139 | exe winnr() . 'q' |
| 140 | |
| 141 | if has('timers') |
| 142 | " timer will not run if "exiting" is still set |
| 143 | let g:flag = 0 |
| 144 | call timer_start(1, 'SetFlag') |
| 145 | sleep 50m |
| 146 | call assert_equal(1, g:flag) |
| 147 | unlet g:flag |
| 148 | endif |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 149 | bwipe Xnqsfile |
Bram Moolenaar | 2c33d7b | 2017-10-14 16:06:20 +0200 | [diff] [blame] | 150 | endfunc |
Bram Moolenaar | 7567d0b | 2017-11-16 23:04:15 +0100 | [diff] [blame] | 151 | |
| 152 | func Test_writefile_sync_arg() |
| 153 | " This doesn't check if fsync() works, only that the argument is accepted. |
Bram Moolenaar | db4c947 | 2022-10-15 22:06:06 +0100 | [diff] [blame] | 154 | call writefile(['one'], 'Xtest', 'sD') |
Bram Moolenaar | 7567d0b | 2017-11-16 23:04:15 +0100 | [diff] [blame] | 155 | call writefile(['two'], 'Xtest', 'S') |
Bram Moolenaar | 7567d0b | 2017-11-16 23:04:15 +0100 | [diff] [blame] | 156 | endfunc |
Bram Moolenaar | 83799a7 | 2017-11-25 17:24:09 +0100 | [diff] [blame] | 157 | |
| 158 | func Test_writefile_sync_dev_stdout() |
Bram Moolenaar | b86abad | 2020-08-01 16:08:19 +0200 | [diff] [blame] | 159 | CheckUnix |
Bram Moolenaar | 9980b37 | 2018-04-21 20:12:35 +0200 | [diff] [blame] | 160 | if filewritable('/dev/stdout') |
| 161 | " Just check that this doesn't cause an error. |
| 162 | call writefile(['one'], '/dev/stdout') |
| 163 | else |
| 164 | throw 'Skipped: /dev/stdout is not writable' |
| 165 | endif |
Bram Moolenaar | 83799a7 | 2017-11-25 17:24:09 +0100 | [diff] [blame] | 166 | endfunc |
Bram Moolenaar | 8c9e7b0 | 2018-08-30 13:07:17 +0200 | [diff] [blame] | 167 | |
| 168 | func Test_writefile_autowrite() |
| 169 | set autowrite |
| 170 | new |
| 171 | next Xa Xb Xc |
| 172 | call setline(1, 'aaa') |
| 173 | next |
| 174 | call assert_equal(['aaa'], readfile('Xa')) |
| 175 | call setline(1, 'bbb') |
| 176 | call assert_fails('edit XX') |
| 177 | call assert_false(filereadable('Xb')) |
| 178 | |
| 179 | set autowriteall |
| 180 | edit XX |
| 181 | call assert_equal(['bbb'], readfile('Xb')) |
| 182 | |
| 183 | bwipe! |
| 184 | call delete('Xa') |
| 185 | call delete('Xb') |
| 186 | set noautowrite |
| 187 | endfunc |
| 188 | |
| 189 | func Test_writefile_autowrite_nowrite() |
| 190 | set autowrite |
| 191 | new |
| 192 | next Xa Xb Xc |
| 193 | set buftype=nowrite |
| 194 | call setline(1, 'aaa') |
| 195 | let buf = bufnr('%') |
| 196 | " buffer contents silently lost |
| 197 | edit XX |
| 198 | call assert_false(filereadable('Xa')) |
| 199 | rewind |
| 200 | call assert_equal('', getline(1)) |
| 201 | |
| 202 | bwipe! |
| 203 | set noautowrite |
| 204 | endfunc |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 205 | |
| 206 | " Test for ':w !<cmd>' to pipe lines from the current buffer to an external |
| 207 | " command. |
| 208 | func Test_write_pipe_to_cmd() |
Bram Moolenaar | ea3db91 | 2020-02-02 15:32:13 +0100 | [diff] [blame] | 209 | CheckUnix |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 210 | new |
| 211 | call setline(1, ['L1', 'L2', 'L3', 'L4']) |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 212 | 2,3w !cat > Xptfile |
| 213 | call assert_equal(['L2', 'L3'], readfile('Xptfile')) |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 214 | close! |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 215 | call delete('Xptfile') |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 216 | endfunc |
| 217 | |
| 218 | " Test for :saveas |
| 219 | func Test_saveas() |
| 220 | call assert_fails('saveas', 'E471:') |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 221 | call writefile(['L1'], 'Xsafile') |
| 222 | new Xsafile |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 223 | new |
| 224 | call setline(1, ['L1']) |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 225 | call assert_fails('saveas Xsafile', 'E139:') |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 226 | close! |
| 227 | enew | only |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 228 | call delete('Xsafile') |
Dominique Pelle | bd9e796 | 2021-08-09 21:04:44 +0200 | [diff] [blame] | 229 | |
| 230 | " :saveas should detect and set the file type. |
| 231 | syntax on |
| 232 | saveas! Xsaveas.pl |
| 233 | call assert_equal('perl', &filetype) |
| 234 | syntax off |
| 235 | %bw! |
| 236 | call delete('Xsaveas.pl') |
Bram Moolenaar | 500a1f9 | 2022-09-20 11:49:10 +0100 | [diff] [blame] | 237 | |
| 238 | " :saveas fails for "nofile" buffer |
| 239 | set buftype=nofile |
| 240 | call assert_fails('saveas Xsafile', 'E676: No matching autocommands for buftype=nofile buffer') |
| 241 | |
| 242 | bwipe! |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 243 | endfunc |
| 244 | |
| 245 | func Test_write_errors() |
| 246 | " Test for writing partial buffer |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 247 | call writefile(['L1', 'L2', 'L3'], 'Xwefile') |
| 248 | new Xwefile |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 249 | call assert_fails('1,2write', 'E140:') |
| 250 | close! |
| 251 | |
Bram Moolenaar | 4f5776c | 2020-02-12 22:15:19 +0100 | [diff] [blame] | 252 | call assert_fails('w > Xtest', 'E494:') |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 253 | |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 254 | " Try to overwrite a directory |
| 255 | if has('unix') |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 256 | call mkdir('Xwerdir1') |
| 257 | call assert_fails('write Xwerdir1', 'E17:') |
| 258 | call delete('Xwerdir1', 'd') |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 259 | endif |
| 260 | |
| 261 | " Test for :wall for a buffer with no name |
| 262 | enew | only |
| 263 | call setline(1, ['L1']) |
| 264 | call assert_fails('wall', 'E141:') |
| 265 | enew! |
| 266 | |
| 267 | " Test for writing a 'readonly' file |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 268 | new Xwefile |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 269 | set readonly |
| 270 | call assert_fails('write', 'E45:') |
| 271 | close |
| 272 | |
| 273 | " Test for writing to a read-only file |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 274 | new Xwefile |
| 275 | call setfperm('Xwefile', 'r--r--r--') |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 276 | call assert_fails('write', 'E505:') |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 277 | call setfperm('Xwefile', 'rw-rw-rw-') |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 278 | close |
| 279 | |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 280 | call delete('Xwefile') |
Bram Moolenaar | 99fa721 | 2020-04-26 15:59:55 +0200 | [diff] [blame] | 281 | |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 282 | call writefile(test_null_list(), 'Xwefile') |
| 283 | call assert_false(filereadable('Xwefile')) |
| 284 | call writefile(test_null_blob(), 'Xwefile') |
| 285 | call assert_false(filereadable('Xwefile')) |
Bram Moolenaar | 99fa721 | 2020-04-26 15:59:55 +0200 | [diff] [blame] | 286 | call assert_fails('call writefile([], "")', 'E482:') |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 287 | |
| 288 | " very long file name |
| 289 | let long_fname = repeat('n', 5000) |
| 290 | call assert_fails('exe "w " .. long_fname', 'E75:') |
| 291 | call assert_fails('call writefile([], long_fname)', 'E482:') |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 292 | |
| 293 | " Test for writing to a block device on Unix-like systems |
| 294 | if has('unix') && getfperm('/dev/loop0') != '' |
| 295 | \ && getftype('/dev/loop0') == 'bdev' && !IsRoot() |
| 296 | new |
| 297 | edit /dev/loop0 |
Bram Moolenaar | 50157ef | 2021-05-15 23:21:05 +0200 | [diff] [blame] | 298 | call assert_fails('write', 'E503: ') |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 299 | call assert_fails('write!', 'E503: ') |
| 300 | close! |
| 301 | endif |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 302 | endfunc |
| 303 | |
| 304 | " Test for writing to a file which is modified after Vim read it |
| 305 | func Test_write_file_mtime() |
| 306 | CheckEnglish |
| 307 | CheckRunVimInTerminal |
| 308 | |
| 309 | " First read the file into a buffer |
Bram Moolenaar | db4c947 | 2022-10-15 22:06:06 +0100 | [diff] [blame] | 310 | call writefile(["Line1", "Line2"], 'Xwfmfile', 'D') |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 311 | let old_ftime = getftime('Xwfmfile') |
| 312 | let buf = RunVimInTerminal('Xwfmfile', #{rows : 10}) |
Bram Moolenaar | 733d259 | 2020-08-20 18:59:06 +0200 | [diff] [blame] | 313 | call TermWait(buf) |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 314 | call term_sendkeys(buf, ":set noswapfile\<CR>") |
Bram Moolenaar | 733d259 | 2020-08-20 18:59:06 +0200 | [diff] [blame] | 315 | call TermWait(buf) |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 316 | |
| 317 | " Modify the file directly. Make sure the file modification time is |
| 318 | " different. Note that on Linux/Unix, the file is considered modified |
| 319 | " outside, only if the difference is 2 seconds or more |
| 320 | sleep 1 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 321 | call writefile(["Line3", "Line4"], 'Xwfmfile') |
| 322 | let new_ftime = getftime('Xwfmfile') |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 323 | while new_ftime - old_ftime < 2 |
| 324 | sleep 100m |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 325 | call writefile(["Line3", "Line4"], 'Xwfmfile') |
| 326 | let new_ftime = getftime('Xwfmfile') |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 327 | endwhile |
| 328 | |
| 329 | " Try to overwrite the file and check for the prompt |
| 330 | call term_sendkeys(buf, ":w\<CR>") |
Bram Moolenaar | 733d259 | 2020-08-20 18:59:06 +0200 | [diff] [blame] | 331 | call TermWait(buf) |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 332 | call WaitForAssert({-> assert_equal("WARNING: The file has been changed since reading it!!!", term_getline(buf, 9))}) |
| 333 | call assert_equal("Do you really want to write to it (y/n)?", |
| 334 | \ term_getline(buf, 10)) |
| 335 | call term_sendkeys(buf, "n\<CR>") |
Bram Moolenaar | 733d259 | 2020-08-20 18:59:06 +0200 | [diff] [blame] | 336 | call TermWait(buf) |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 337 | call assert_equal(new_ftime, getftime('Xwfmfile')) |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 338 | call term_sendkeys(buf, ":w\<CR>") |
Bram Moolenaar | 733d259 | 2020-08-20 18:59:06 +0200 | [diff] [blame] | 339 | call TermWait(buf) |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 340 | call term_sendkeys(buf, "y\<CR>") |
Bram Moolenaar | 733d259 | 2020-08-20 18:59:06 +0200 | [diff] [blame] | 341 | call TermWait(buf) |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 342 | call WaitForAssert({-> assert_equal('Line2', readfile('Xwfmfile')[1])}) |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 343 | |
| 344 | " clean up |
| 345 | call StopVimInTerminal(buf) |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 346 | endfunc |
| 347 | |
| 348 | " Test for an autocmd unloading a buffer during a write command |
| 349 | func Test_write_autocmd_unloadbuf_lockmark() |
| 350 | augroup WriteTest |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 351 | autocmd BufWritePre Xwaufile enew | write |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 352 | augroup END |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 353 | e Xwaufile |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 354 | call assert_fails('lockmarks write', ['E32:', 'E203:']) |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 355 | augroup WriteTest |
| 356 | au! |
| 357 | augroup END |
| 358 | augroup! WriteTest |
| 359 | endfunc |
| 360 | |
| 361 | " Test for writing a buffer with 'acwrite' but without autocmds |
| 362 | func Test_write_acwrite_error() |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 363 | new Xwaefile |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 364 | call setline(1, ['line1', 'line2', 'line3']) |
| 365 | set buftype=acwrite |
| 366 | call assert_fails('write', 'E676:') |
| 367 | call assert_fails('1,2write!', 'E676:') |
| 368 | call assert_fails('w >>', 'E676:') |
| 369 | close! |
| 370 | endfunc |
| 371 | |
| 372 | " Test for adding and removing lines from an autocmd when writing a buffer |
| 373 | func Test_write_autocmd_add_remove_lines() |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 374 | new Xwaafile |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 375 | call setline(1, ['aaa', 'bbb', 'ccc', 'ddd']) |
| 376 | |
| 377 | " Autocmd deleting lines from the file when writing a partial file |
| 378 | augroup WriteTest2 |
| 379 | au! |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 380 | autocmd FileWritePre Xwaafile 1,2d |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 381 | augroup END |
| 382 | call assert_fails('2,3w!', 'E204:') |
| 383 | |
| 384 | " Autocmd adding lines to a file when writing a partial file |
| 385 | augroup WriteTest2 |
| 386 | au! |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 387 | autocmd FileWritePre Xwaafile call append(0, ['xxx', 'yyy']) |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 388 | augroup END |
| 389 | %d |
| 390 | call setline(1, ['aaa', 'bbb', 'ccc', 'ddd']) |
| 391 | 1,2w! |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 392 | call assert_equal(['xxx', 'yyy', 'aaa', 'bbb'], readfile('Xwaafile')) |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 393 | |
| 394 | " Autocmd deleting lines from the file when writing the whole file |
| 395 | augroup WriteTest2 |
| 396 | au! |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 397 | autocmd BufWritePre Xwaafile 1,2d |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 398 | augroup END |
| 399 | %d |
| 400 | call setline(1, ['aaa', 'bbb', 'ccc', 'ddd']) |
| 401 | w |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 402 | call assert_equal(['ccc', 'ddd'], readfile('Xwaafile')) |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 403 | |
| 404 | augroup WriteTest2 |
| 405 | au! |
| 406 | augroup END |
| 407 | augroup! WriteTest2 |
| 408 | |
| 409 | close! |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 410 | call delete('Xwaafile') |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 411 | endfunc |
| 412 | |
| 413 | " Test for writing to a readonly file |
| 414 | func Test_write_readonly() |
Bram Moolenaar | db4c947 | 2022-10-15 22:06:06 +0100 | [diff] [blame] | 415 | call writefile([], 'Xwrofile', 'D') |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 416 | call setfperm('Xwrofile', "r--------") |
| 417 | edit Xwrofile |
Bram Moolenaar | b86abad | 2020-08-01 16:08:19 +0200 | [diff] [blame] | 418 | set noreadonly backupskip= |
Bram Moolenaar | 494e906 | 2020-05-31 21:28:02 +0200 | [diff] [blame] | 419 | call assert_fails('write', 'E505:') |
| 420 | let save_cpo = &cpo |
| 421 | set cpo+=W |
| 422 | call assert_fails('write!', 'E504:') |
| 423 | let &cpo = save_cpo |
Bram Moolenaar | 1de5f7c | 2020-06-11 19:22:43 +0200 | [diff] [blame] | 424 | call setline(1, ['line1']) |
| 425 | write! |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 426 | call assert_equal(['line1'], readfile('Xwrofile')) |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 427 | |
| 428 | " Auto-saving a readonly file should fail with 'autowriteall' |
| 429 | %bw! |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 430 | e Xwrofile |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 431 | set noreadonly autowriteall |
| 432 | call setline(1, ['aaaa']) |
| 433 | call assert_fails('n', 'E505:') |
| 434 | set cpo+=W |
| 435 | call assert_fails('n', 'E504:') |
| 436 | set cpo-=W |
| 437 | set autowriteall& |
| 438 | |
Bram Moolenaar | b86abad | 2020-08-01 16:08:19 +0200 | [diff] [blame] | 439 | set backupskip& |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 440 | %bw! |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 441 | endfunc |
| 442 | |
Bram Moolenaar | 1de5f7c | 2020-06-11 19:22:43 +0200 | [diff] [blame] | 443 | " Test for 'patchmode' |
| 444 | func Test_patchmode() |
Bram Moolenaar | db4c947 | 2022-10-15 22:06:06 +0100 | [diff] [blame] | 445 | call writefile(['one'], 'Xpafile', 'D') |
Bram Moolenaar | b86abad | 2020-08-01 16:08:19 +0200 | [diff] [blame] | 446 | set patchmode=.orig nobackup backupskip= writebackup |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 447 | new Xpafile |
Bram Moolenaar | 1de5f7c | 2020-06-11 19:22:43 +0200 | [diff] [blame] | 448 | call setline(1, 'two') |
| 449 | " first write should create the .orig file |
| 450 | write |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 451 | call assert_equal(['one'], readfile('Xpafile.orig')) |
Bram Moolenaar | 1de5f7c | 2020-06-11 19:22:43 +0200 | [diff] [blame] | 452 | call setline(1, 'three') |
| 453 | " subsequent writes should not create/modify the .orig file |
| 454 | write |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 455 | call assert_equal(['one'], readfile('Xpafile.orig')) |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 456 | |
| 457 | " use 'patchmode' with 'nobackup' and 'nowritebackup' to create an empty |
| 458 | " original file |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 459 | call delete('Xpafile') |
| 460 | call delete('Xpafile.orig') |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 461 | %bw! |
| 462 | set patchmode=.orig nobackup nowritebackup |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 463 | edit Xpafile |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 464 | call setline(1, ['xxx']) |
| 465 | write |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 466 | call assert_equal(['xxx'], readfile('Xpafile')) |
| 467 | call assert_equal([], readfile('Xpafile.orig')) |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 468 | |
Bram Moolenaar | b86abad | 2020-08-01 16:08:19 +0200 | [diff] [blame] | 469 | set patchmode& backup& backupskip& writebackup& |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 470 | call delete('Xpafile.orig') |
Bram Moolenaar | 1de5f7c | 2020-06-11 19:22:43 +0200 | [diff] [blame] | 471 | endfunc |
| 472 | |
| 473 | " Test for writing to a file in a readonly directory |
Bram Moolenaar | f9a6550 | 2021-03-05 20:47:44 +0100 | [diff] [blame] | 474 | " NOTE: if you run tests as root this will fail. Don't run tests as root! |
Bram Moolenaar | 1de5f7c | 2020-06-11 19:22:43 +0200 | [diff] [blame] | 475 | func Test_write_readonly_dir() |
Bram Moolenaar | b86abad | 2020-08-01 16:08:19 +0200 | [diff] [blame] | 476 | " On MS-Windows, modifying files in a read-only directory is allowed. |
| 477 | CheckUnix |
Bram Moolenaar | 17709e2 | 2021-03-19 14:38:12 +0100 | [diff] [blame] | 478 | " Root can do it too. |
| 479 | CheckNotRoot |
| 480 | |
Bram Moolenaar | db4c947 | 2022-10-15 22:06:06 +0100 | [diff] [blame] | 481 | call mkdir('Xrodir/', 'R') |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 482 | call writefile(['one'], 'Xrodir/Xfile1') |
| 483 | call setfperm('Xrodir', 'r-xr--r--') |
Bram Moolenaar | 1de5f7c | 2020-06-11 19:22:43 +0200 | [diff] [blame] | 484 | " try to create a new file in the directory |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 485 | new Xrodir/Xfile2 |
Bram Moolenaar | 1de5f7c | 2020-06-11 19:22:43 +0200 | [diff] [blame] | 486 | call setline(1, 'two') |
| 487 | call assert_fails('write', 'E212:') |
| 488 | " try to create a backup file in the directory |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 489 | edit! Xrodir/Xfile1 |
| 490 | set backupdir=./Xrodir backupskip= |
Bram Moolenaar | 1de5f7c | 2020-06-11 19:22:43 +0200 | [diff] [blame] | 491 | set patchmode=.orig |
| 492 | call assert_fails('write', 'E509:') |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 493 | call setfperm('Xrodir', 'rwxr--r--') |
Bram Moolenaar | b86abad | 2020-08-01 16:08:19 +0200 | [diff] [blame] | 494 | set backupdir& backupskip& patchmode& |
Bram Moolenaar | 1de5f7c | 2020-06-11 19:22:43 +0200 | [diff] [blame] | 495 | endfunc |
| 496 | |
Bram Moolenaar | b340bae | 2020-06-15 19:51:56 +0200 | [diff] [blame] | 497 | " Test for writing a file using invalid file encoding |
| 498 | func Test_write_invalid_encoding() |
| 499 | new |
| 500 | call setline(1, 'abc') |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 501 | call assert_fails('write ++enc=axbyc Xiefile', 'E213:') |
Bram Moolenaar | b340bae | 2020-06-15 19:51:56 +0200 | [diff] [blame] | 502 | close! |
| 503 | endfunc |
| 504 | |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 505 | " Tests for reading and writing files with conversion for Win32. |
| 506 | func Test_write_file_encoding() |
| 507 | CheckMSWindows |
| 508 | let save_encoding = &encoding |
| 509 | let save_fileencodings = &fileencodings |
K.Takata | f883d90 | 2021-05-30 18:04:19 +0200 | [diff] [blame] | 510 | set encoding=latin1 fileencodings& |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 511 | let text =<< trim END |
| 512 | 1 utf-8 text: ÐÐ»Ñ Vim version 6.2. ÐоÑледнее изменение: 1970 Jan 01 |
| 513 | 2 cp1251 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01 |
| 514 | 3 cp866 text: «ï Vim version 6.2. ®á«¥¤¥¥ ¨§¬¥¥¨¥: 1970 Jan 01 |
| 515 | END |
Bram Moolenaar | db4c947 | 2022-10-15 22:06:06 +0100 | [diff] [blame] | 516 | call writefile(text, 'Xwfefile', 'D') |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 517 | edit Xwfefile |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 518 | |
| 519 | " write tests: |
| 520 | " combine three values for 'encoding' with three values for 'fileencoding' |
| 521 | " also write files for read tests |
| 522 | call cursor(1, 1) |
| 523 | set encoding=utf-8 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 524 | .w! ++enc=utf-8 Xwfetest |
| 525 | .w ++enc=cp1251 >> Xwfetest |
| 526 | .w ++enc=cp866 >> Xwfetest |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 527 | .w! ++enc=utf-8 Xutf8 |
| 528 | let expected =<< trim END |
| 529 | 1 utf-8 text: ÐÐ»Ñ Vim version 6.2. ÐоÑледнее изменение: 1970 Jan 01 |
| 530 | 1 utf-8 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01 |
| 531 | 1 utf-8 text: «ï Vim version 6.2. ®á«¥¤¥¥ ¨§¬¥¥¨¥: 1970 Jan 01 |
| 532 | END |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 533 | call assert_equal(expected, readfile('Xwfetest')) |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 534 | |
| 535 | call cursor(2, 1) |
| 536 | set encoding=cp1251 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 537 | .w! ++enc=utf-8 Xwfetest |
| 538 | .w ++enc=cp1251 >> Xwfetest |
| 539 | .w ++enc=cp866 >> Xwfetest |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 540 | .w! ++enc=cp1251 Xcp1251 |
| 541 | let expected =<< trim END |
| 542 | 2 cp1251 text: ÐÐ»Ñ Vim version 6.2. ÐоÑледнее изменение: 1970 Jan 01 |
| 543 | 2 cp1251 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01 |
| 544 | 2 cp1251 text: «ï Vim version 6.2. ®á«¥¤¥¥ ¨§¬¥¥¨¥: 1970 Jan 01 |
| 545 | END |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 546 | call assert_equal(expected, readfile('Xwfetest')) |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 547 | |
| 548 | call cursor(3, 1) |
| 549 | set encoding=cp866 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 550 | .w! ++enc=utf-8 Xwfetest |
| 551 | .w ++enc=cp1251 >> Xwfetest |
| 552 | .w ++enc=cp866 >> Xwfetest |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 553 | .w! ++enc=cp866 Xcp866 |
| 554 | let expected =<< trim END |
| 555 | 3 cp866 text: ÐÐ»Ñ Vim version 6.2. ÐоÑледнее изменение: 1970 Jan 01 |
| 556 | 3 cp866 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01 |
| 557 | 3 cp866 text: «ï Vim version 6.2. ®á«¥¤¥¥ ¨§¬¥¥¨¥: 1970 Jan 01 |
| 558 | END |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 559 | call assert_equal(expected, readfile('Xwfetest')) |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 560 | |
| 561 | " read three 'fileencoding's with utf-8 'encoding' |
| 562 | set encoding=utf-8 fencs=utf-8,cp1251 |
| 563 | e Xutf8 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 564 | .w! ++enc=utf-8 Xwfetest |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 565 | e Xcp1251 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 566 | .w ++enc=utf-8 >> Xwfetest |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 567 | set fencs=utf-8,cp866 |
| 568 | e Xcp866 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 569 | .w ++enc=utf-8 >> Xwfetest |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 570 | let expected =<< trim END |
| 571 | 1 utf-8 text: ÐÐ»Ñ Vim version 6.2. ÐоÑледнее изменение: 1970 Jan 01 |
| 572 | 2 cp1251 text: ÐÐ»Ñ Vim version 6.2. ÐоÑледнее изменение: 1970 Jan 01 |
| 573 | 3 cp866 text: ÐÐ»Ñ Vim version 6.2. ÐоÑледнее изменение: 1970 Jan 01 |
| 574 | END |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 575 | call assert_equal(expected, readfile('Xwfetest')) |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 576 | |
| 577 | " read three 'fileencoding's with cp1251 'encoding' |
| 578 | set encoding=utf-8 fencs=utf-8,cp1251 |
| 579 | e Xutf8 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 580 | .w! ++enc=cp1251 Xwfetest |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 581 | e Xcp1251 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 582 | .w ++enc=cp1251 >> Xwfetest |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 583 | set fencs=utf-8,cp866 |
| 584 | e Xcp866 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 585 | .w ++enc=cp1251 >> Xwfetest |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 586 | let expected =<< trim END |
| 587 | 1 utf-8 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01 |
| 588 | 2 cp1251 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01 |
| 589 | 3 cp866 text: Äëÿ Vim version 6.2. Ïîñëåäíåå èçìåíåíèå: 1970 Jan 01 |
| 590 | END |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 591 | call assert_equal(expected, readfile('Xwfetest')) |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 592 | |
| 593 | " read three 'fileencoding's with cp866 'encoding' |
| 594 | set encoding=cp866 fencs=utf-8,cp1251 |
| 595 | e Xutf8 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 596 | .w! ++enc=cp866 Xwfetest |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 597 | e Xcp1251 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 598 | .w ++enc=cp866 >> Xwfetest |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 599 | set fencs=utf-8,cp866 |
| 600 | e Xcp866 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 601 | .w ++enc=cp866 >> Xwfetest |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 602 | let expected =<< trim END |
| 603 | 1 utf-8 text: «ï Vim version 6.2. ®á«¥¤¥¥ ¨§¬¥¥¨¥: 1970 Jan 01 |
| 604 | 2 cp1251 text: «ï Vim version 6.2. ®á«¥¤¥¥ ¨§¬¥¥¨¥: 1970 Jan 01 |
| 605 | 3 cp866 text: «ï Vim version 6.2. ®á«¥¤¥¥ ¨§¬¥¥¨¥: 1970 Jan 01 |
| 606 | END |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 607 | call assert_equal(expected, readfile('Xwfetest')) |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 608 | |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 609 | call delete('Xwfetest') |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 610 | call delete('Xutf8') |
| 611 | call delete('Xcp1251') |
| 612 | call delete('Xcp866') |
| 613 | let &encoding = save_encoding |
| 614 | let &fileencodings = save_fileencodings |
| 615 | %bw! |
| 616 | endfunc |
| 617 | |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 618 | " Test for writing and reading a file starting with a BOM. |
| 619 | " Byte Order Mark (BOM) character for various encodings is below: |
| 620 | " UTF-8 : EF BB BF |
| 621 | " UTF-16 (BE): FE FF |
| 622 | " UTF-16 (LE): FF FE |
| 623 | " UTF-32 (BE): 00 00 FE FF |
| 624 | " UTF-32 (LE): FF FE 00 00 |
| 625 | func Test_readwrite_file_with_bom() |
| 626 | let utf8_bom = "\xEF\xBB\xBF" |
| 627 | let utf16be_bom = "\xFE\xFF" |
| 628 | let utf16le_bom = "\xFF\xFE" |
| 629 | let utf32be_bom = "\n\n\xFE\xFF" |
| 630 | let utf32le_bom = "\xFF\xFE\n\n" |
| 631 | let save_fileencoding = &fileencoding |
| 632 | set cpoptions+=S |
| 633 | |
| 634 | " Check that editing a latin1 file doesn't see a BOM |
Bram Moolenaar | db4c947 | 2022-10-15 22:06:06 +0100 | [diff] [blame] | 635 | call writefile(["\xFE\xFElatin-1"], 'Xrwtest1', 'D') |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 636 | edit Xrwtest1 |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 637 | call assert_equal('latin1', &fileencoding) |
| 638 | call assert_equal(0, &bomb) |
| 639 | set fenc=latin1 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 640 | write Xrwfile2 |
| 641 | call assert_equal(["\xFE\xFElatin-1", ''], readfile('Xrwfile2', 'b')) |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 642 | set bomb fenc=latin1 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 643 | write Xrwtest3 |
| 644 | call assert_equal(["\xFE\xFElatin-1", ''], readfile('Xrwtest3', 'b')) |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 645 | set bomb& |
| 646 | |
| 647 | " Check utf-8 BOM |
| 648 | %bw! |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 649 | call writefile([utf8_bom .. "utf-8"], 'Xrwtest1') |
| 650 | edit! Xrwtest1 |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 651 | call assert_equal('utf-8', &fileencoding) |
| 652 | call assert_equal(1, &bomb) |
| 653 | call assert_equal('utf-8', getline(1)) |
| 654 | set fenc=latin1 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 655 | write! Xrwfile2 |
| 656 | call assert_equal(['utf-8', ''], readfile('Xrwfile2', 'b')) |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 657 | set fenc=utf-8 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 658 | w! Xrwtest3 |
| 659 | call assert_equal([utf8_bom .. "utf-8", ''], readfile('Xrwtest3', 'b')) |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 660 | |
| 661 | " Check utf-8 with an error (will fall back to latin-1) |
| 662 | %bw! |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 663 | call writefile([utf8_bom .. "utf-8\x80err"], 'Xrwtest1') |
| 664 | edit! Xrwtest1 |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 665 | call assert_equal('latin1', &fileencoding) |
| 666 | call assert_equal(0, &bomb) |
| 667 | call assert_equal("\xC3\xAF\xC2\xBB\xC2\xBFutf-8\xC2\x80err", getline(1)) |
| 668 | set fenc=latin1 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 669 | write! Xrwfile2 |
| 670 | call assert_equal([utf8_bom .. "utf-8\x80err", ''], readfile('Xrwfile2', 'b')) |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 671 | set fenc=utf-8 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 672 | w! Xrwtest3 |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 673 | call assert_equal(["\xC3\xAF\xC2\xBB\xC2\xBFutf-8\xC2\x80err", ''], |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 674 | \ readfile('Xrwtest3', 'b')) |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 675 | |
| 676 | " Check ucs-2 BOM |
| 677 | %bw! |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 678 | call writefile([utf16be_bom .. "\nu\nc\ns\n-\n2\n"], 'Xrwtest1') |
| 679 | edit! Xrwtest1 |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 680 | call assert_equal('utf-16', &fileencoding) |
| 681 | call assert_equal(1, &bomb) |
| 682 | call assert_equal('ucs-2', getline(1)) |
| 683 | set fenc=latin1 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 684 | write! Xrwfile2 |
| 685 | call assert_equal(["ucs-2", ''], readfile('Xrwfile2', 'b')) |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 686 | set fenc=ucs-2 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 687 | w! Xrwtest3 |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 688 | call assert_equal([utf16be_bom .. "\nu\nc\ns\n-\n2\n", ''], |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 689 | \ readfile('Xrwtest3', 'b')) |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 690 | |
| 691 | " Check ucs-2le BOM |
| 692 | %bw! |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 693 | call writefile([utf16le_bom .. "u\nc\ns\n-\n2\nl\ne\n"], 'Xrwtest1') |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 694 | " Need to add a NUL byte after the NL byte |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 695 | call writefile(0z00, 'Xrwtest1', 'a') |
| 696 | edit! Xrwtest1 |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 697 | call assert_equal('utf-16le', &fileencoding) |
| 698 | call assert_equal(1, &bomb) |
| 699 | call assert_equal('ucs-2le', getline(1)) |
| 700 | set fenc=latin1 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 701 | write! Xrwfile2 |
| 702 | call assert_equal(["ucs-2le", ''], readfile('Xrwfile2', 'b')) |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 703 | set fenc=ucs-2le |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 704 | w! Xrwtest3 |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 705 | call assert_equal([utf16le_bom .. "u\nc\ns\n-\n2\nl\ne\n", "\n"], |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 706 | \ readfile('Xrwtest3', 'b')) |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 707 | |
| 708 | " Check ucs-4 BOM |
| 709 | %bw! |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 710 | call writefile([utf32be_bom .. "\n\n\nu\n\n\nc\n\n\ns\n\n\n-\n\n\n4\n\n\n"], 'Xrwtest1') |
| 711 | edit! Xrwtest1 |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 712 | call assert_equal('ucs-4', &fileencoding) |
| 713 | call assert_equal(1, &bomb) |
| 714 | call assert_equal('ucs-4', getline(1)) |
| 715 | set fenc=latin1 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 716 | write! Xrwfile2 |
| 717 | call assert_equal(["ucs-4", ''], readfile('Xrwfile2', 'b')) |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 718 | set fenc=ucs-4 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 719 | w! Xrwtest3 |
| 720 | call assert_equal([utf32be_bom .. "\n\n\nu\n\n\nc\n\n\ns\n\n\n-\n\n\n4\n\n\n", ''], readfile('Xrwtest3', 'b')) |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 721 | |
| 722 | " Check ucs-4le BOM |
| 723 | %bw! |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 724 | 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"], 'Xrwtest1') |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 725 | " Need to add three NUL bytes after the NL byte |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 726 | call writefile(0z000000, 'Xrwtest1', 'a') |
| 727 | edit! Xrwtest1 |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 728 | call assert_equal('ucs-4le', &fileencoding) |
| 729 | call assert_equal(1, &bomb) |
| 730 | call assert_equal('ucs-4le', getline(1)) |
| 731 | set fenc=latin1 |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 732 | write! Xrwfile2 |
| 733 | call assert_equal(["ucs-4le", ''], readfile('Xrwfile2', 'b')) |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 734 | set fenc=ucs-4le |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 735 | w! Xrwtest3 |
| 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')) |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 737 | |
| 738 | set cpoptions-=S |
| 739 | let &fileencoding = save_fileencoding |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 740 | call delete('Xrwfile2') |
| 741 | call delete('Xrwtest3') |
Bram Moolenaar | b61ef01 | 2020-07-29 16:08:21 +0200 | [diff] [blame] | 742 | %bw! |
| 743 | endfunc |
| 744 | |
Bram Moolenaar | b3c8b1d | 2020-12-23 18:54:57 +0100 | [diff] [blame] | 745 | func Test_read_write_bin() |
| 746 | " write file missing EOL |
Bram Moolenaar | db4c947 | 2022-10-15 22:06:06 +0100 | [diff] [blame] | 747 | call writefile(['noeol'], "XNoEolSetEol", 'bSD') |
Bram Moolenaar | b3c8b1d | 2020-12-23 18:54:57 +0100 | [diff] [blame] | 748 | call assert_equal(0z6E6F656F6C, readfile('XNoEolSetEol', 'B')) |
| 749 | |
| 750 | " when file is read 'eol' is off |
Bram Moolenaar | 1620496 | 2020-12-23 22:40:11 +0100 | [diff] [blame] | 751 | set nofixeol |
| 752 | e! ++ff=unix XNoEolSetEol |
Bram Moolenaar | b3c8b1d | 2020-12-23 18:54:57 +0100 | [diff] [blame] | 753 | call assert_equal(0, &eol) |
| 754 | |
| 755 | " writing with 'eol' set adds the newline |
| 756 | setlocal eol |
| 757 | w |
| 758 | call assert_equal(0z6E6F656F6C0A, readfile('XNoEolSetEol', 'B')) |
| 759 | |
Yegappan Lakshmanan | 46aa6f9 | 2021-05-19 17:15:04 +0200 | [diff] [blame] | 760 | set ff& fixeol& |
Bram Moolenaar | bd31855 | 2020-12-23 20:55:15 +0100 | [diff] [blame] | 761 | bwipe! XNoEolSetEol |
Bram Moolenaar | b3c8b1d | 2020-12-23 18:54:57 +0100 | [diff] [blame] | 762 | endfunc |
| 763 | |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 764 | " Test for the 'backupcopy' option when writing files |
| 765 | func Test_backupcopy() |
| 766 | CheckUnix |
| 767 | set backupskip= |
| 768 | " With the default 'backupcopy' setting, saving a symbolic link file |
| 769 | " should not break the link. |
| 770 | set backupcopy& |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 771 | call writefile(['1111'], 'Xbcfile1') |
| 772 | silent !ln -s Xbcfile1 Xbcfile2 |
| 773 | new Xbcfile2 |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 774 | call setline(1, ['2222']) |
| 775 | write |
| 776 | close |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 777 | call assert_equal(['2222'], readfile('Xbcfile1')) |
| 778 | call assert_equal('Xbcfile1', resolve('Xbcfile2')) |
| 779 | call assert_equal('link', getftype('Xbcfile2')) |
| 780 | call delete('Xbcfile1') |
| 781 | call delete('Xbcfile2') |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 782 | |
| 783 | " With the 'backupcopy' set to 'breaksymlink', saving a symbolic link file |
| 784 | " should break the link. |
| 785 | set backupcopy=yes,breaksymlink |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 786 | call writefile(['1111'], 'Xbcfile1') |
| 787 | silent !ln -s Xbcfile1 Xbcfile2 |
| 788 | new Xbcfile2 |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 789 | call setline(1, ['2222']) |
| 790 | write |
| 791 | close |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 792 | call assert_equal(['1111'], readfile('Xbcfile1')) |
| 793 | call assert_equal(['2222'], readfile('Xbcfile2')) |
| 794 | call assert_equal('Xbcfile2', resolve('Xbcfile2')) |
| 795 | call assert_equal('file', getftype('Xbcfile2')) |
| 796 | call delete('Xbcfile1') |
| 797 | call delete('Xbcfile2') |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 798 | set backupcopy& |
| 799 | |
| 800 | " With the default 'backupcopy' setting, saving a hard link file |
| 801 | " should not break the link. |
| 802 | set backupcopy& |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 803 | call writefile(['1111'], 'Xbcfile1') |
| 804 | silent !ln Xbcfile1 Xbcfile2 |
| 805 | new Xbcfile2 |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 806 | call setline(1, ['2222']) |
| 807 | write |
| 808 | close |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 809 | call assert_equal(['2222'], readfile('Xbcfile1')) |
| 810 | call delete('Xbcfile1') |
| 811 | call delete('Xbcfile2') |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 812 | |
| 813 | " With the 'backupcopy' set to 'breaksymlink', saving a hard link file |
| 814 | " should break the link. |
| 815 | set backupcopy=yes,breakhardlink |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 816 | call writefile(['1111'], 'Xbcfile1') |
| 817 | silent !ln Xbcfile1 Xbcfile2 |
| 818 | new Xbcfile2 |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 819 | call setline(1, ['2222']) |
| 820 | write |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 821 | call assert_equal(['1111'], readfile('Xbcfile1')) |
| 822 | call assert_equal(['2222'], readfile('Xbcfile2')) |
| 823 | call delete('Xbcfile1') |
| 824 | call delete('Xbcfile2') |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 825 | |
| 826 | " If a backup file is already present, then a slightly modified filename |
| 827 | " should be used as the backup file. Try with 'backupcopy' set to 'yes' and |
| 828 | " 'no'. |
| 829 | %bw |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 830 | call writefile(['aaaa'], 'Xbcfile') |
| 831 | call writefile(['bbbb'], 'Xbcfile.bak') |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 832 | set backupcopy=yes backupext=.bak |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 833 | new Xbcfile |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 834 | call setline(1, ['cccc']) |
| 835 | write |
| 836 | close |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 837 | call assert_equal(['cccc'], readfile('Xbcfile')) |
| 838 | call assert_equal(['bbbb'], readfile('Xbcfile.bak')) |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 839 | set backupcopy=no backupext=.bak |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 840 | new Xbcfile |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 841 | call setline(1, ['dddd']) |
| 842 | write |
| 843 | close |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 844 | call assert_equal(['dddd'], readfile('Xbcfile')) |
| 845 | call assert_equal(['bbbb'], readfile('Xbcfile.bak')) |
| 846 | call delete('Xbcfile') |
| 847 | call delete('Xbcfile.bak') |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 848 | |
| 849 | " Write to a device file (in Unix-like systems) which cannot be backed up. |
| 850 | if has('unix') |
| 851 | set writebackup backupcopy=yes nobackup |
| 852 | new |
| 853 | call setline(1, ['aaaa']) |
| 854 | let output = execute('write! /dev/null') |
| 855 | call assert_match('"/dev/null" \[Device]', output) |
| 856 | close |
| 857 | set writebackup backupcopy=no nobackup |
| 858 | new |
| 859 | call setline(1, ['aaaa']) |
| 860 | let output = execute('write! /dev/null') |
| 861 | call assert_match('"/dev/null" \[Device]', output) |
| 862 | close |
| 863 | set backup writebackup& backupcopy& |
| 864 | new |
| 865 | call setline(1, ['aaaa']) |
| 866 | let output = execute('write! /dev/null') |
| 867 | call assert_match('"/dev/null" \[Device]', output) |
| 868 | close |
| 869 | endif |
| 870 | |
| 871 | set backupcopy& backupskip& backupext& backup& |
| 872 | endfunc |
| 873 | |
| 874 | " Test for writing a file with 'encoding' set to 'utf-16' |
| 875 | func Test_write_utf16() |
| 876 | new |
| 877 | call setline(1, ["\U00010001"]) |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 878 | write ++enc=utf-16 Xw16file |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 879 | bw! |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 880 | call assert_equal(0zD800DC01, readfile('Xw16file', 'B')[0:3]) |
| 881 | call delete('Xw16file') |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 882 | endfunc |
| 883 | |
| 884 | " Test for trying to save a backup file when the backup file is a symbolic |
| 885 | " link to the original file. The backup file should not be modified. |
| 886 | func Test_write_backup_symlink() |
| 887 | CheckUnix |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 888 | call mkdir('Xbackup') |
| 889 | let save_backupdir = &backupdir |
| 890 | set backupdir=.,./Xbackup |
Bram Moolenaar | db4c947 | 2022-10-15 22:06:06 +0100 | [diff] [blame] | 891 | call writefile(['1111'], 'Xwbsfile', 'D') |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 892 | silent !ln -s Xwbsfile Xwbsfile.bak |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 893 | |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 894 | new Xwbsfile |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 895 | set backup backupcopy=yes backupext=.bak |
| 896 | write |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 897 | call assert_equal('link', getftype('Xwbsfile.bak')) |
| 898 | call assert_equal('Xwbsfile', resolve('Xwbsfile.bak')) |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 899 | " backup file should be created in the 'backup' directory |
| 900 | if !has('bsd') |
| 901 | " This check fails on FreeBSD |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 902 | call assert_true(filereadable('./Xbackup/Xwbsfile.bak')) |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 903 | endif |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 904 | set backup& backupcopy& backupext& |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 905 | %bw |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 906 | |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 907 | call delete('Xwbsfile.bak') |
Bram Moolenaar | 6e2e2cc | 2022-03-14 19:24:46 +0000 | [diff] [blame] | 908 | call delete('Xbackup', 'rf') |
| 909 | let &backupdir = save_backupdir |
Yegappan Lakshmanan | 36f96a5 | 2021-05-13 18:33:16 +0200 | [diff] [blame] | 910 | endfunc |
| 911 | |
Yegappan Lakshmanan | 46aa6f9 | 2021-05-19 17:15:04 +0200 | [diff] [blame] | 912 | " Test for ':write ++bin' and ':write ++nobin' |
| 913 | func Test_write_binary_file() |
| 914 | " create a file without an eol/eof character |
Bram Moolenaar | db4c947 | 2022-10-15 22:06:06 +0100 | [diff] [blame] | 915 | call writefile(0z616161, 'Xwbfile1', 'bD') |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 916 | new Xwbfile1 |
| 917 | write ++bin Xwbfile2 |
| 918 | write ++nobin Xwbfile3 |
| 919 | call assert_equal(0z616161, readblob('Xwbfile2')) |
Yegappan Lakshmanan | 46aa6f9 | 2021-05-19 17:15:04 +0200 | [diff] [blame] | 920 | if has('win32') |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 921 | call assert_equal(0z6161610D.0A, readblob('Xwbfile3')) |
Yegappan Lakshmanan | 46aa6f9 | 2021-05-19 17:15:04 +0200 | [diff] [blame] | 922 | else |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 923 | call assert_equal(0z6161610A, readblob('Xwbfile3')) |
Yegappan Lakshmanan | 46aa6f9 | 2021-05-19 17:15:04 +0200 | [diff] [blame] | 924 | endif |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 925 | call delete('Xwbfile2') |
| 926 | call delete('Xwbfile3') |
Yegappan Lakshmanan | 46aa6f9 | 2021-05-19 17:15:04 +0200 | [diff] [blame] | 927 | endfunc |
| 928 | |
Bram Moolenaar | 806a273 | 2022-09-04 15:40:36 +0100 | [diff] [blame] | 929 | func DoWriteDefer() |
| 930 | call writefile(['some text'], 'XdeferDelete', 'D') |
| 931 | call assert_equal(['some text'], readfile('XdeferDelete')) |
| 932 | endfunc |
| 933 | |
| 934 | def DefWriteDefer() |
| 935 | writefile(['some text'], 'XdefdeferDelete', 'D') |
| 936 | assert_equal(['some text'], readfile('XdefdeferDelete')) |
| 937 | enddef |
| 938 | |
| 939 | func Test_write_with_deferred_delete() |
| 940 | call DoWriteDefer() |
| 941 | call assert_equal('', glob('XdeferDelete')) |
| 942 | call DefWriteDefer() |
| 943 | call assert_equal('', glob('XdefdeferDelete')) |
| 944 | endfunc |
| 945 | |
Bram Moolenaar | 6f14da1 | 2022-09-07 21:30:44 +0100 | [diff] [blame] | 946 | func DoWriteFile() |
| 947 | call writefile(['text'], 'Xthefile', 'D') |
| 948 | cd .. |
| 949 | endfunc |
| 950 | |
| 951 | func Test_write_defer_delete_chdir() |
| 952 | let dir = getcwd() |
| 953 | call DoWriteFile() |
| 954 | call assert_notequal(dir, getcwd()) |
| 955 | call chdir(dir) |
| 956 | call assert_equal('', glob('Xthefile')) |
| 957 | endfunc |
| 958 | |
Bram Moolenaar | 1174b01 | 2021-05-29 14:30:43 +0200 | [diff] [blame] | 959 | " Check that buffer is written before triggering QuitPre |
| 960 | func Test_wq_quitpre_autocommand() |
| 961 | edit Xsomefile |
| 962 | call setline(1, 'hello') |
| 963 | split |
| 964 | let g:seq = [] |
| 965 | augroup Testing |
| 966 | au QuitPre * call add(g:seq, 'QuitPre - ' .. (&modified ? 'modified' : 'not modified')) |
| 967 | au BufWritePost * call add(g:seq, 'written') |
| 968 | augroup END |
| 969 | wq |
| 970 | call assert_equal(['written', 'QuitPre - not modified'], g:seq) |
| 971 | |
| 972 | augroup Testing |
| 973 | au! |
| 974 | augroup END |
| 975 | bwipe! |
| 976 | unlet g:seq |
| 977 | call delete('Xsomefile') |
| 978 | endfunc |
| 979 | |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 980 | " vim: shiftwidth=2 sts=2 expandtab |