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