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