Bram Moolenaar | 5e66b42 | 2019-01-24 21:58:10 +0100 | [diff] [blame] | 1 | " Tests for when a file was changed outside of Vim. |
| 2 | |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 3 | source check.vim |
| 4 | |
Bram Moolenaar | 5e66b42 | 2019-01-24 21:58:10 +0100 | [diff] [blame] | 5 | func Test_FileChangedShell_reload() |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 6 | CheckUnix |
| 7 | |
Bram Moolenaar | 5e66b42 | 2019-01-24 21:58:10 +0100 | [diff] [blame] | 8 | augroup testreload |
| 9 | au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'reload' |
| 10 | augroup END |
| 11 | new Xchanged_r |
| 12 | call setline(1, 'reload this') |
| 13 | write |
zeertzjq | 83cd2c7 | 2024-04-05 20:05:11 +0200 | [diff] [blame] | 14 | " Need to wait until the timestamp would change. |
| 15 | if has('nanotime') |
| 16 | sleep 10m |
| 17 | else |
| 18 | sleep 2 |
| 19 | endif |
Bram Moolenaar | 5e66b42 | 2019-01-24 21:58:10 +0100 | [diff] [blame] | 20 | silent !echo 'extra line' >>Xchanged_r |
| 21 | checktime |
| 22 | call assert_equal('changed', g:reason) |
| 23 | call assert_equal(2, line('$')) |
| 24 | call assert_equal('extra line', getline(2)) |
| 25 | |
| 26 | " Only triggers once |
| 27 | let g:reason = '' |
| 28 | checktime |
| 29 | call assert_equal('', g:reason) |
| 30 | |
| 31 | " When deleted buffer is not reloaded |
| 32 | silent !rm Xchanged_r |
| 33 | let g:reason = '' |
| 34 | checktime |
| 35 | call assert_equal('deleted', g:reason) |
| 36 | call assert_equal(2, line('$')) |
| 37 | call assert_equal('extra line', getline(2)) |
| 38 | |
| 39 | " When recreated buffer is reloaded |
| 40 | call setline(1, 'buffer is changed') |
| 41 | silent !echo 'new line' >>Xchanged_r |
| 42 | let g:reason = '' |
| 43 | checktime |
| 44 | call assert_equal('conflict', g:reason) |
| 45 | call assert_equal(1, line('$')) |
| 46 | call assert_equal('new line', getline(1)) |
| 47 | |
| 48 | " Only mode changed |
| 49 | silent !chmod +x Xchanged_r |
| 50 | let g:reason = '' |
| 51 | checktime |
| 52 | call assert_equal('mode', g:reason) |
| 53 | call assert_equal(1, line('$')) |
| 54 | call assert_equal('new line', getline(1)) |
| 55 | |
| 56 | " Only time changed |
zeertzjq | 83cd2c7 | 2024-04-05 20:05:11 +0200 | [diff] [blame] | 57 | if has('nanotime') |
| 58 | sleep 10m |
| 59 | else |
| 60 | sleep 2 |
| 61 | endif |
Bram Moolenaar | 5e66b42 | 2019-01-24 21:58:10 +0100 | [diff] [blame] | 62 | silent !touch Xchanged_r |
| 63 | let g:reason = '' |
| 64 | checktime |
| 65 | call assert_equal('time', g:reason) |
| 66 | call assert_equal(1, line('$')) |
| 67 | call assert_equal('new line', getline(1)) |
| 68 | |
| 69 | if has('persistent_undo') |
| 70 | " With an undo file the reload can be undone and a change before the |
| 71 | " reload. |
| 72 | set undofile |
| 73 | call setline(2, 'before write') |
| 74 | write |
| 75 | call setline(2, 'after write') |
zeertzjq | 83cd2c7 | 2024-04-05 20:05:11 +0200 | [diff] [blame] | 76 | if has('nanotime') |
| 77 | sleep 10m |
| 78 | else |
| 79 | sleep 2 |
| 80 | endif |
Bram Moolenaar | 5e66b42 | 2019-01-24 21:58:10 +0100 | [diff] [blame] | 81 | silent !echo 'different line' >>Xchanged_r |
| 82 | let g:reason = '' |
| 83 | checktime |
| 84 | call assert_equal('conflict', g:reason) |
| 85 | call assert_equal(3, line('$')) |
| 86 | call assert_equal('before write', getline(2)) |
| 87 | call assert_equal('different line', getline(3)) |
| 88 | " undo the reload |
| 89 | undo |
| 90 | call assert_equal(2, line('$')) |
| 91 | call assert_equal('after write', getline(2)) |
| 92 | " undo the change before reload |
| 93 | undo |
| 94 | call assert_equal(2, line('$')) |
| 95 | call assert_equal('before write', getline(2)) |
| 96 | |
| 97 | set noundofile |
| 98 | endif |
| 99 | |
| 100 | au! testreload |
| 101 | bwipe! |
Bram Moolenaar | 137c14b | 2019-04-18 20:30:55 +0200 | [diff] [blame] | 102 | call delete(undofile('Xchanged_r')) |
Bram Moolenaar | 5e66b42 | 2019-01-24 21:58:10 +0100 | [diff] [blame] | 103 | call delete('Xchanged_r') |
| 104 | endfunc |
| 105 | |
Rob Pilling | 8196e94 | 2022-02-11 15:12:10 +0000 | [diff] [blame] | 106 | func Test_FileChangedShell_edit() |
| 107 | CheckUnix |
| 108 | |
| 109 | new Xchanged_r |
| 110 | call setline(1, 'reload this') |
| 111 | set fileformat=unix |
| 112 | write |
| 113 | |
| 114 | " File format changed, reload (content only, no 'ff' etc) |
| 115 | augroup testreload |
| 116 | au! |
| 117 | au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'reload' |
| 118 | augroup END |
| 119 | call assert_equal(&fileformat, 'unix') |
Bram Moolenaar | 70e6725 | 2022-09-27 19:34:35 +0100 | [diff] [blame] | 120 | call writefile(["line1\r", "line2\r"], 'Xchanged_r', 'D') |
Rob Pilling | 8196e94 | 2022-02-11 15:12:10 +0000 | [diff] [blame] | 121 | let g:reason = '' |
| 122 | checktime |
| 123 | call assert_equal('changed', g:reason) |
| 124 | call assert_equal(&fileformat, 'unix') |
| 125 | call assert_equal("line1\r", getline(1)) |
| 126 | call assert_equal("line2\r", getline(2)) |
| 127 | %s/\r |
| 128 | write |
| 129 | |
| 130 | " File format changed, reload with 'ff', etc |
| 131 | augroup testreload |
| 132 | au! |
| 133 | au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'edit' |
| 134 | augroup END |
| 135 | call assert_equal(&fileformat, 'unix') |
| 136 | call writefile(["line1\r", "line2\r"], 'Xchanged_r') |
| 137 | let g:reason = '' |
| 138 | checktime |
| 139 | call assert_equal('changed', g:reason) |
| 140 | call assert_equal(&fileformat, 'dos') |
| 141 | call assert_equal('line1', getline(1)) |
| 142 | call assert_equal('line2', getline(2)) |
| 143 | set fileformat=unix |
| 144 | write |
| 145 | |
| 146 | au! testreload |
| 147 | bwipe! |
| 148 | call delete(undofile('Xchanged_r')) |
Rob Pilling | 8196e94 | 2022-02-11 15:12:10 +0000 | [diff] [blame] | 149 | endfunc |
| 150 | |
| 151 | func Test_FileChangedShell_edit_dialog() |
| 152 | CheckNotGui |
Bram Moolenaar | b4ad3b0 | 2022-03-30 10:57:45 +0100 | [diff] [blame] | 153 | CheckUnix " Using low level feedkeys() does not work on MS-Windows. |
Rob Pilling | 8196e94 | 2022-02-11 15:12:10 +0000 | [diff] [blame] | 154 | |
| 155 | new Xchanged_r |
| 156 | call setline(1, 'reload this') |
| 157 | set fileformat=unix |
| 158 | write |
| 159 | |
| 160 | " File format changed, reload (content only) via prompt |
| 161 | augroup testreload |
| 162 | au! |
| 163 | au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'ask' |
| 164 | augroup END |
| 165 | call assert_equal(&fileformat, 'unix') |
Bram Moolenaar | 70e6725 | 2022-09-27 19:34:35 +0100 | [diff] [blame] | 166 | call writefile(["line1\r", "line2\r"], 'Xchanged_r', 'D') |
Rob Pilling | 8196e94 | 2022-02-11 15:12:10 +0000 | [diff] [blame] | 167 | let g:reason = '' |
| 168 | call feedkeys('L', 'L') " load file content only |
| 169 | checktime |
| 170 | call assert_equal('changed', g:reason) |
| 171 | call assert_equal(&fileformat, 'unix') |
| 172 | call assert_equal("line1\r", getline(1)) |
| 173 | call assert_equal("line2\r", getline(2)) |
| 174 | %s/\r |
| 175 | write |
| 176 | |
| 177 | " File format changed, reload (file and options) via prompt |
| 178 | augroup testreload |
| 179 | au! |
| 180 | au FileChangedShell Xchanged_r let g:reason = v:fcs_reason | let v:fcs_choice = 'ask' |
| 181 | augroup END |
| 182 | call assert_equal(&fileformat, 'unix') |
| 183 | call writefile(["line1\r", "line2\r"], 'Xchanged_r') |
| 184 | let g:reason = '' |
| 185 | call feedkeys('a', 'L') " load file content and options |
| 186 | checktime |
| 187 | call assert_equal('changed', g:reason) |
| 188 | call assert_equal(&fileformat, 'dos') |
| 189 | call assert_equal("line1", getline(1)) |
| 190 | call assert_equal("line2", getline(2)) |
| 191 | set fileformat=unix |
| 192 | write |
| 193 | |
| 194 | au! testreload |
| 195 | bwipe! |
| 196 | call delete(undofile('Xchanged_r')) |
Rob Pilling | 8196e94 | 2022-02-11 15:12:10 +0000 | [diff] [blame] | 197 | endfunc |
| 198 | |
Bram Moolenaar | 5e66b42 | 2019-01-24 21:58:10 +0100 | [diff] [blame] | 199 | func Test_file_changed_dialog() |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 200 | CheckUnix |
| 201 | CheckNotGui |
Bram Moolenaar | 5e66b42 | 2019-01-24 21:58:10 +0100 | [diff] [blame] | 202 | au! FileChangedShell |
| 203 | |
| 204 | new Xchanged_d |
| 205 | call setline(1, 'reload this') |
| 206 | write |
zeertzjq | 83cd2c7 | 2024-04-05 20:05:11 +0200 | [diff] [blame] | 207 | " Need to wait until the timestamp would change. |
| 208 | if has('nanotime') |
| 209 | sleep 10m |
| 210 | else |
| 211 | sleep 2 |
| 212 | endif |
Bram Moolenaar | 5e66b42 | 2019-01-24 21:58:10 +0100 | [diff] [blame] | 213 | silent !echo 'extra line' >>Xchanged_d |
| 214 | call feedkeys('L', 'L') |
| 215 | checktime |
| 216 | call assert_match('W11:', v:warningmsg) |
| 217 | call assert_equal(2, line('$')) |
| 218 | call assert_equal('reload this', getline(1)) |
| 219 | call assert_equal('extra line', getline(2)) |
| 220 | |
| 221 | " delete buffer, only shows an error, no prompt |
| 222 | silent !rm Xchanged_d |
| 223 | checktime |
| 224 | call assert_match('E211:', v:warningmsg) |
| 225 | call assert_equal(2, line('$')) |
| 226 | call assert_equal('extra line', getline(2)) |
Bram Moolenaar | 8239c62 | 2019-05-24 16:46:01 +0200 | [diff] [blame] | 227 | let v:warningmsg = 'empty' |
Bram Moolenaar | 5e66b42 | 2019-01-24 21:58:10 +0100 | [diff] [blame] | 228 | |
Bram Moolenaar | 8239c62 | 2019-05-24 16:46:01 +0200 | [diff] [blame] | 229 | " change buffer, recreate the file and reload |
Bram Moolenaar | 5e66b42 | 2019-01-24 21:58:10 +0100 | [diff] [blame] | 230 | call setline(1, 'buffer is changed') |
| 231 | silent !echo 'new line' >Xchanged_d |
| 232 | call feedkeys('L', 'L') |
| 233 | checktime |
| 234 | call assert_match('W12:', v:warningmsg) |
| 235 | call assert_equal(1, line('$')) |
| 236 | call assert_equal('new line', getline(1)) |
| 237 | |
| 238 | " Only mode changed, reload |
| 239 | silent !chmod +x Xchanged_d |
| 240 | call feedkeys('L', 'L') |
| 241 | checktime |
| 242 | call assert_match('W16:', v:warningmsg) |
| 243 | call assert_equal(1, line('$')) |
| 244 | call assert_equal('new line', getline(1)) |
| 245 | |
| 246 | " Only time changed, no prompt |
zeertzjq | 83cd2c7 | 2024-04-05 20:05:11 +0200 | [diff] [blame] | 247 | if has('nanotime') |
| 248 | sleep 10m |
| 249 | else |
| 250 | sleep 2 |
| 251 | endif |
Bram Moolenaar | 5e66b42 | 2019-01-24 21:58:10 +0100 | [diff] [blame] | 252 | silent !touch Xchanged_d |
| 253 | let v:warningmsg = '' |
Bram Moolenaar | e20b9ec | 2020-02-03 21:40:04 +0100 | [diff] [blame] | 254 | checktime Xchanged_d |
Bram Moolenaar | 5e66b42 | 2019-01-24 21:58:10 +0100 | [diff] [blame] | 255 | call assert_equal('', v:warningmsg) |
| 256 | call assert_equal(1, line('$')) |
| 257 | call assert_equal('new line', getline(1)) |
| 258 | |
Bram Moolenaar | b340bae | 2020-06-15 19:51:56 +0200 | [diff] [blame] | 259 | " File created after starting to edit it |
| 260 | call delete('Xchanged_d') |
| 261 | new Xchanged_d |
Bram Moolenaar | 70e6725 | 2022-09-27 19:34:35 +0100 | [diff] [blame] | 262 | call writefile(['one'], 'Xchanged_d', 'D') |
Bram Moolenaar | b340bae | 2020-06-15 19:51:56 +0200 | [diff] [blame] | 263 | call feedkeys('L', 'L') |
| 264 | checktime Xchanged_d |
| 265 | call assert_equal(['one'], getline(1, '$')) |
| 266 | close! |
| 267 | |
Bram Moolenaar | 5e66b42 | 2019-01-24 21:58:10 +0100 | [diff] [blame] | 268 | bwipe! |
Bram Moolenaar | 5e66b42 | 2019-01-24 21:58:10 +0100 | [diff] [blame] | 269 | endfunc |
Bram Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 270 | |
| 271 | " Test for editing a new buffer from a FileChangedShell autocmd |
| 272 | func Test_FileChangedShell_newbuf() |
Bram Moolenaar | 70e6725 | 2022-09-27 19:34:35 +0100 | [diff] [blame] | 273 | call writefile(['one', 'two'], 'Xchfile', 'D') |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 274 | new Xchfile |
Bram Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 275 | augroup testnewbuf |
| 276 | autocmd FileChangedShell * enew |
| 277 | augroup END |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 278 | call writefile(['red'], 'Xchfile') |
Bram Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 279 | call assert_fails('checktime', 'E811:') |
Bram Moolenaar | 70e6725 | 2022-09-27 19:34:35 +0100 | [diff] [blame] | 280 | |
Bram Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 281 | au! testnewbuf |
Bram Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 282 | endfunc |
| 283 | |
| 284 | " vim: shiftwidth=2 sts=2 expandtab |