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() |
| 5 | call writefile(["over","written"], f, "b") |
| 6 | call writefile(["hello","world"], f, "b") |
| 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]) |
| 16 | call delete(f) |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 17 | endfunc |
| 18 | |
Bram Moolenaar | b40c257 | 2019-10-19 21:01:05 +0200 | [diff] [blame] | 19 | func Test_writefile_ignore_regexp_error() |
| 20 | write Xt[z-a]est.txt |
| 21 | call delete('Xt[z-a]est.txt') |
| 22 | endfunc |
| 23 | |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 24 | func Test_writefile_fails_gently() |
| 25 | call assert_fails('call writefile(["test"], "Xfile", [])', 'E730:') |
| 26 | call assert_false(filereadable("Xfile")) |
| 27 | call delete("Xfile") |
| 28 | |
| 29 | call assert_fails('call writefile(["test", [], [], [], "tset"], "Xfile")', 'E730:') |
| 30 | call assert_false(filereadable("Xfile")) |
| 31 | call delete("Xfile") |
| 32 | |
| 33 | call assert_fails('call writefile([], "Xfile", [])', 'E730:') |
| 34 | call assert_false(filereadable("Xfile")) |
| 35 | call delete("Xfile") |
| 36 | |
| 37 | call assert_fails('call writefile([], [])', 'E730:') |
| 38 | endfunc |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 39 | |
| 40 | func Test_writefile_fails_conversion() |
Bram Moolenaar | 39536dd | 2019-01-29 22:58:21 +0100 | [diff] [blame] | 41 | if !has('iconv') || has('sun') |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 42 | return |
| 43 | endif |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 44 | " Without a backup file the write won't happen if there is a conversion |
| 45 | " error. |
Bram Moolenaar | c28cb5b | 2019-05-31 20:42:09 +0200 | [diff] [blame] | 46 | set nobackup nowritebackup backupdir=. backupskip= |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 47 | new |
| 48 | let contents = ["line one", "line two"] |
| 49 | call writefile(contents, 'Xfile') |
| 50 | edit Xfile |
| 51 | call setline(1, ["first line", "cannot convert \u010b", "third line"]) |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 52 | call assert_fails('write ++enc=cp932', 'E513:') |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 53 | call assert_equal(contents, readfile('Xfile')) |
| 54 | |
| 55 | call delete('Xfile') |
| 56 | bwipe! |
Bram Moolenaar | c28cb5b | 2019-05-31 20:42:09 +0200 | [diff] [blame] | 57 | set backup& writebackup& backupdir&vim backupskip&vim |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 58 | endfunc |
Bram Moolenaar | 2c33d7b | 2017-10-14 16:06:20 +0200 | [diff] [blame] | 59 | |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 60 | func Test_writefile_fails_conversion2() |
| 61 | if !has('iconv') || has('sun') |
| 62 | return |
| 63 | endif |
| 64 | " With a backup file the write happens even if there is a conversion error, |
| 65 | " but then the backup file must remain |
Bram Moolenaar | c28cb5b | 2019-05-31 20:42:09 +0200 | [diff] [blame] | 66 | set nobackup writebackup backupdir=. backupskip= |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 67 | let contents = ["line one", "line two"] |
| 68 | call writefile(contents, 'Xfile_conversion_err') |
| 69 | edit Xfile_conversion_err |
| 70 | call setline(1, ["first line", "cannot convert \u010b", "third line"]) |
| 71 | set fileencoding=latin1 |
| 72 | let output = execute('write') |
| 73 | call assert_match('CONVERSION ERROR', output) |
| 74 | call assert_equal(contents, readfile('Xfile_conversion_err~')) |
| 75 | |
| 76 | call delete('Xfile_conversion_err') |
| 77 | call delete('Xfile_conversion_err~') |
| 78 | bwipe! |
Bram Moolenaar | c28cb5b | 2019-05-31 20:42:09 +0200 | [diff] [blame] | 79 | set backup& writebackup& backupdir&vim backupskip&vim |
Bram Moolenaar | cf0bfd9 | 2019-05-18 18:52:04 +0200 | [diff] [blame] | 80 | endfunc |
| 81 | |
Bram Moolenaar | 2c33d7b | 2017-10-14 16:06:20 +0200 | [diff] [blame] | 82 | func SetFlag(timer) |
| 83 | let g:flag = 1 |
| 84 | endfunc |
| 85 | |
| 86 | func Test_write_quit_split() |
| 87 | " Prevent exiting by splitting window on file write. |
| 88 | augroup testgroup |
| 89 | autocmd BufWritePre * split |
| 90 | augroup END |
| 91 | e! Xfile |
| 92 | call setline(1, 'nothing') |
| 93 | wq |
| 94 | |
| 95 | if has('timers') |
| 96 | " timer will not run if "exiting" is still set |
| 97 | let g:flag = 0 |
| 98 | call timer_start(1, 'SetFlag') |
| 99 | sleep 50m |
| 100 | call assert_equal(1, g:flag) |
| 101 | unlet g:flag |
| 102 | endif |
| 103 | au! testgroup |
| 104 | bwipe Xfile |
| 105 | call delete('Xfile') |
| 106 | endfunc |
| 107 | |
| 108 | func Test_nowrite_quit_split() |
| 109 | " Prevent exiting by opening a help window. |
| 110 | e! Xfile |
| 111 | help |
| 112 | wincmd w |
| 113 | exe winnr() . 'q' |
| 114 | |
| 115 | if has('timers') |
| 116 | " timer will not run if "exiting" is still set |
| 117 | let g:flag = 0 |
| 118 | call timer_start(1, 'SetFlag') |
| 119 | sleep 50m |
| 120 | call assert_equal(1, g:flag) |
| 121 | unlet g:flag |
| 122 | endif |
| 123 | bwipe Xfile |
| 124 | endfunc |
Bram Moolenaar | 7567d0b | 2017-11-16 23:04:15 +0100 | [diff] [blame] | 125 | |
| 126 | func Test_writefile_sync_arg() |
| 127 | " This doesn't check if fsync() works, only that the argument is accepted. |
| 128 | call writefile(['one'], 'Xtest', 's') |
| 129 | call writefile(['two'], 'Xtest', 'S') |
| 130 | call delete('Xtest') |
| 131 | endfunc |
Bram Moolenaar | 83799a7 | 2017-11-25 17:24:09 +0100 | [diff] [blame] | 132 | |
| 133 | func Test_writefile_sync_dev_stdout() |
| 134 | if !has('unix') |
| 135 | return |
| 136 | endif |
Bram Moolenaar | 9980b37 | 2018-04-21 20:12:35 +0200 | [diff] [blame] | 137 | if filewritable('/dev/stdout') |
| 138 | " Just check that this doesn't cause an error. |
| 139 | call writefile(['one'], '/dev/stdout') |
| 140 | else |
| 141 | throw 'Skipped: /dev/stdout is not writable' |
| 142 | endif |
Bram Moolenaar | 83799a7 | 2017-11-25 17:24:09 +0100 | [diff] [blame] | 143 | endfunc |
Bram Moolenaar | 8c9e7b0 | 2018-08-30 13:07:17 +0200 | [diff] [blame] | 144 | |
| 145 | func Test_writefile_autowrite() |
| 146 | set autowrite |
| 147 | new |
| 148 | next Xa Xb Xc |
| 149 | call setline(1, 'aaa') |
| 150 | next |
| 151 | call assert_equal(['aaa'], readfile('Xa')) |
| 152 | call setline(1, 'bbb') |
| 153 | call assert_fails('edit XX') |
| 154 | call assert_false(filereadable('Xb')) |
| 155 | |
| 156 | set autowriteall |
| 157 | edit XX |
| 158 | call assert_equal(['bbb'], readfile('Xb')) |
| 159 | |
| 160 | bwipe! |
| 161 | call delete('Xa') |
| 162 | call delete('Xb') |
| 163 | set noautowrite |
| 164 | endfunc |
| 165 | |
| 166 | func Test_writefile_autowrite_nowrite() |
| 167 | set autowrite |
| 168 | new |
| 169 | next Xa Xb Xc |
| 170 | set buftype=nowrite |
| 171 | call setline(1, 'aaa') |
| 172 | let buf = bufnr('%') |
| 173 | " buffer contents silently lost |
| 174 | edit XX |
| 175 | call assert_false(filereadable('Xa')) |
| 176 | rewind |
| 177 | call assert_equal('', getline(1)) |
| 178 | |
| 179 | bwipe! |
| 180 | set noautowrite |
| 181 | endfunc |