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