Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 1 | " Tests for the swap feature |
| 2 | |
Bram Moolenaar | d36ef57 | 2020-03-24 21:44:51 +0100 | [diff] [blame] | 3 | source check.vim |
Bram Moolenaar | 07282f0 | 2019-10-10 16:46:17 +0200 | [diff] [blame] | 4 | source shared.vim |
Bram Moolenaar | b654103 | 2020-02-22 21:21:27 +0100 | [diff] [blame] | 5 | source term_util.vim |
Bram Moolenaar | 07282f0 | 2019-10-10 16:46:17 +0200 | [diff] [blame] | 6 | |
Bram Moolenaar | 110bd60 | 2018-09-16 18:46:59 +0200 | [diff] [blame] | 7 | func s:swapname() |
| 8 | return trim(execute('swapname')) |
| 9 | endfunc |
| 10 | |
Bram Moolenaar | ffe010f | 2017-11-04 22:30:40 +0100 | [diff] [blame] | 11 | " Tests for 'directory' option. |
| 12 | func Test_swap_directory() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 13 | CheckUnix |
| 14 | |
Bram Moolenaar | ffe010f | 2017-11-04 22:30:40 +0100 | [diff] [blame] | 15 | let content = ['start of testfile', |
| 16 | \ 'line 2 Abcdefghij', |
| 17 | \ 'line 3 Abcdefghij', |
| 18 | \ 'end of testfile'] |
| 19 | call writefile(content, 'Xtest1') |
| 20 | |
| 21 | " '.', swap file in the same directory as file |
| 22 | set dir=.,~ |
| 23 | |
| 24 | " Verify that the swap file doesn't exist in the current directory |
| 25 | call assert_equal([], glob(".Xtest1*.swp", 1, 1, 1)) |
| 26 | edit Xtest1 |
Bram Moolenaar | 110bd60 | 2018-09-16 18:46:59 +0200 | [diff] [blame] | 27 | let swfname = s:swapname() |
Bram Moolenaar | ffe010f | 2017-11-04 22:30:40 +0100 | [diff] [blame] | 28 | call assert_equal([swfname], glob(swfname, 1, 1, 1)) |
| 29 | |
| 30 | " './dir', swap file in a directory relative to the file |
| 31 | set dir=./Xtest2,.,~ |
| 32 | |
| 33 | call mkdir("Xtest2") |
| 34 | edit Xtest1 |
| 35 | call assert_equal([], glob(swfname, 1, 1, 1)) |
| 36 | let swfname = "Xtest2/Xtest1.swp" |
Bram Moolenaar | 110bd60 | 2018-09-16 18:46:59 +0200 | [diff] [blame] | 37 | call assert_equal(swfname, s:swapname()) |
Bram Moolenaar | ffe010f | 2017-11-04 22:30:40 +0100 | [diff] [blame] | 38 | call assert_equal([swfname], glob("Xtest2/*", 1, 1, 1)) |
| 39 | |
| 40 | " 'dir', swap file in directory relative to the current dir |
| 41 | set dir=Xtest.je,~ |
| 42 | |
| 43 | call mkdir("Xtest.je") |
| 44 | call writefile(content, 'Xtest2/Xtest3') |
| 45 | edit Xtest2/Xtest3 |
| 46 | call assert_equal(["Xtest2/Xtest3"], glob("Xtest2/*", 1, 1, 1)) |
| 47 | let swfname = "Xtest.je/Xtest3.swp" |
Bram Moolenaar | 110bd60 | 2018-09-16 18:46:59 +0200 | [diff] [blame] | 48 | call assert_equal(swfname, s:swapname()) |
Bram Moolenaar | ffe010f | 2017-11-04 22:30:40 +0100 | [diff] [blame] | 49 | call assert_equal([swfname], glob("Xtest.je/*", 1, 1, 1)) |
| 50 | |
| 51 | set dir& |
| 52 | call delete("Xtest1") |
| 53 | call delete("Xtest2", "rf") |
| 54 | call delete("Xtest.je", "rf") |
| 55 | endfunc |
Bram Moolenaar | 5a73e0c | 2017-11-04 21:35:01 +0100 | [diff] [blame] | 56 | |
| 57 | func Test_swap_group() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 58 | CheckUnix |
| 59 | |
Bram Moolenaar | 5a73e0c | 2017-11-04 21:35:01 +0100 | [diff] [blame] | 60 | let groups = split(system('groups')) |
| 61 | if len(groups) <= 1 |
Bram Moolenaar | ad7dac8 | 2017-11-04 22:21:21 +0100 | [diff] [blame] | 62 | throw 'Skipped: need at least two groups, got ' . string(groups) |
Bram Moolenaar | 5a73e0c | 2017-11-04 21:35:01 +0100 | [diff] [blame] | 63 | endif |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 64 | |
Bram Moolenaar | 5842a74 | 2017-11-04 22:36:53 +0100 | [diff] [blame] | 65 | try |
| 66 | call delete('Xtest') |
| 67 | split Xtest |
| 68 | call setline(1, 'just some text') |
| 69 | wq |
| 70 | if system('ls -l Xtest') !~ ' ' . groups[0] . ' \d' |
| 71 | throw 'Skipped: test file does not have the first group' |
Bram Moolenaar | 5a73e0c | 2017-11-04 21:35:01 +0100 | [diff] [blame] | 72 | else |
Bram Moolenaar | 5842a74 | 2017-11-04 22:36:53 +0100 | [diff] [blame] | 73 | silent !chmod 640 Xtest |
| 74 | call system('chgrp ' . groups[1] . ' Xtest') |
| 75 | if system('ls -l Xtest') !~ ' ' . groups[1] . ' \d' |
| 76 | throw 'Skipped: cannot set second group on test file' |
| 77 | else |
| 78 | split Xtest |
Bram Moolenaar | 110bd60 | 2018-09-16 18:46:59 +0200 | [diff] [blame] | 79 | let swapname = s:swapname() |
Bram Moolenaar | 5842a74 | 2017-11-04 22:36:53 +0100 | [diff] [blame] | 80 | call assert_match('Xtest', swapname) |
| 81 | " Group of swapfile must now match original file. |
| 82 | call assert_match(' ' . groups[1] . ' \d', system('ls -l ' . swapname)) |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 83 | |
Bram Moolenaar | 5842a74 | 2017-11-04 22:36:53 +0100 | [diff] [blame] | 84 | bwipe! |
| 85 | endif |
Bram Moolenaar | 5a73e0c | 2017-11-04 21:35:01 +0100 | [diff] [blame] | 86 | endif |
Bram Moolenaar | 5842a74 | 2017-11-04 22:36:53 +0100 | [diff] [blame] | 87 | finally |
| 88 | call delete('Xtest') |
| 89 | endtry |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 90 | endfunc |
Bram Moolenaar | 8c3169c | 2018-05-12 17:04:12 +0200 | [diff] [blame] | 91 | |
| 92 | func Test_missing_dir() |
| 93 | call mkdir('Xswapdir') |
| 94 | exe 'set directory=' . getcwd() . '/Xswapdir' |
| 95 | |
| 96 | call assert_equal('', glob('foo')) |
| 97 | call assert_equal('', glob('bar')) |
| 98 | edit foo/x.txt |
| 99 | " This should not give a warning for an existing swap file. |
| 100 | split bar/x.txt |
| 101 | only |
| 102 | |
Bram Moolenaar | e3d0654 | 2019-01-27 14:29:24 +0100 | [diff] [blame] | 103 | " Delete the buffer so that swap file is removed before we try to delete the |
| 104 | " directory. That fails on MS-Windows. |
| 105 | %bdelete! |
Bram Moolenaar | 8c3169c | 2018-05-12 17:04:12 +0200 | [diff] [blame] | 106 | set directory& |
| 107 | call delete('Xswapdir', 'rf') |
| 108 | endfunc |
Bram Moolenaar | 00f123a | 2018-08-21 20:28:54 +0200 | [diff] [blame] | 109 | |
| 110 | func Test_swapinfo() |
| 111 | new Xswapinfo |
| 112 | call setline(1, ['one', 'two', 'three']) |
| 113 | w |
Bram Moolenaar | 110bd60 | 2018-09-16 18:46:59 +0200 | [diff] [blame] | 114 | let fname = s:swapname() |
Bram Moolenaar | 00f123a | 2018-08-21 20:28:54 +0200 | [diff] [blame] | 115 | call assert_match('Xswapinfo', fname) |
Bram Moolenaar | f6ed61e | 2019-09-07 19:05:09 +0200 | [diff] [blame] | 116 | let info = fname->swapinfo() |
Bram Moolenaar | 4c5765b | 2018-08-22 11:28:01 +0200 | [diff] [blame] | 117 | |
| 118 | let ver = printf('VIM %d.%d', v:version / 100, v:version % 100) |
| 119 | call assert_equal(ver, info.version) |
| 120 | |
Bram Moolenaar | 00f123a | 2018-08-21 20:28:54 +0200 | [diff] [blame] | 121 | call assert_match('\w', info.user) |
Bram Moolenaar | 4c5765b | 2018-08-22 11:28:01 +0200 | [diff] [blame] | 122 | " host name is truncated to 39 bytes in the swap file |
| 123 | call assert_equal(hostname()[:38], info.host) |
Bram Moolenaar | 00f123a | 2018-08-21 20:28:54 +0200 | [diff] [blame] | 124 | call assert_match('Xswapinfo', info.fname) |
Bram Moolenaar | 47ad565 | 2018-08-21 21:09:07 +0200 | [diff] [blame] | 125 | call assert_match(0, info.dirty) |
Bram Moolenaar | 00f123a | 2018-08-21 20:28:54 +0200 | [diff] [blame] | 126 | call assert_equal(getpid(), info.pid) |
| 127 | call assert_match('^\d*$', info.mtime) |
| 128 | if has_key(info, 'inode') |
| 129 | call assert_match('\d', info.inode) |
| 130 | endif |
| 131 | bwipe! |
| 132 | call delete(fname) |
| 133 | call delete('Xswapinfo') |
| 134 | |
| 135 | let info = swapinfo('doesnotexist') |
| 136 | call assert_equal('Cannot open file', info.error) |
| 137 | |
| 138 | call writefile(['burp'], 'Xnotaswapfile') |
| 139 | let info = swapinfo('Xnotaswapfile') |
| 140 | call assert_equal('Cannot read file', info.error) |
| 141 | call delete('Xnotaswapfile') |
| 142 | |
| 143 | call writefile([repeat('x', 10000)], 'Xnotaswapfile') |
| 144 | let info = swapinfo('Xnotaswapfile') |
Bram Moolenaar | 47ad565 | 2018-08-21 21:09:07 +0200 | [diff] [blame] | 145 | call assert_equal('Not a swap file', info.error) |
Bram Moolenaar | 00f123a | 2018-08-21 20:28:54 +0200 | [diff] [blame] | 146 | call delete('Xnotaswapfile') |
| 147 | endfunc |
Bram Moolenaar | 110bd60 | 2018-09-16 18:46:59 +0200 | [diff] [blame] | 148 | |
| 149 | func Test_swapname() |
| 150 | edit Xtest1 |
| 151 | let expected = s:swapname() |
| 152 | call assert_equal(expected, swapname('%')) |
| 153 | |
| 154 | new Xtest2 |
| 155 | let buf = bufnr('%') |
| 156 | let expected = s:swapname() |
| 157 | wincmd p |
Bram Moolenaar | f6ed61e | 2019-09-07 19:05:09 +0200 | [diff] [blame] | 158 | call assert_equal(expected, buf->swapname()) |
Bram Moolenaar | 110bd60 | 2018-09-16 18:46:59 +0200 | [diff] [blame] | 159 | |
| 160 | new Xtest3 |
| 161 | setlocal noswapfile |
| 162 | call assert_equal('', swapname('%')) |
| 163 | |
| 164 | bwipe! |
| 165 | call delete('Xtest1') |
| 166 | call delete('Xtest2') |
| 167 | call delete('Xtest3') |
| 168 | endfunc |
Bram Moolenaar | 67cf86b | 2019-04-28 22:25:38 +0200 | [diff] [blame] | 169 | |
| 170 | func Test_swapfile_delete() |
| 171 | autocmd! SwapExists |
| 172 | function s:swap_exists() |
| 173 | let v:swapchoice = s:swap_choice |
| 174 | let s:swapname = v:swapname |
| 175 | let s:filename = expand('<afile>') |
| 176 | endfunc |
| 177 | augroup test_swapfile_delete |
| 178 | autocmd! |
| 179 | autocmd SwapExists * call s:swap_exists() |
| 180 | augroup END |
| 181 | |
| 182 | |
| 183 | " Create a valid swapfile by editing a file. |
| 184 | split XswapfileText |
| 185 | call setline(1, ['one', 'two', 'three']) |
| 186 | write " file is written, not modified |
| 187 | " read the swapfile as a Blob |
| 188 | let swapfile_name = swapname('%') |
| 189 | let swapfile_bytes = readfile(swapfile_name, 'B') |
| 190 | |
| 191 | " Close the file and recreate the swap file. |
| 192 | " Now editing the file will run into the process still existing |
| 193 | quit |
| 194 | call writefile(swapfile_bytes, swapfile_name) |
| 195 | let s:swap_choice = 'e' |
| 196 | let s:swapname = '' |
| 197 | split XswapfileText |
| 198 | quit |
Bram Moolenaar | 701df4e | 2019-04-28 23:07:18 +0200 | [diff] [blame] | 199 | call assert_equal(fnamemodify(swapfile_name, ':t'), fnamemodify(s:swapname, ':t')) |
Bram Moolenaar | 67cf86b | 2019-04-28 22:25:38 +0200 | [diff] [blame] | 200 | |
Bram Moolenaar | 07282f0 | 2019-10-10 16:46:17 +0200 | [diff] [blame] | 201 | " This test won't work as root because root can successfully run kill(1, 0) |
| 202 | if !IsRoot() |
| 203 | " Write the swapfile with a modified PID, now it will be automatically |
Bram Moolenaar | 6738fd2 | 2021-06-23 21:44:06 +0200 | [diff] [blame] | 204 | " deleted. Process 0x3fffffff most likely does not exist. |
| 205 | let swapfile_bytes[24:27] = 0zffffff3f |
Bram Moolenaar | 07282f0 | 2019-10-10 16:46:17 +0200 | [diff] [blame] | 206 | call writefile(swapfile_bytes, swapfile_name) |
| 207 | let s:swapname = '' |
| 208 | split XswapfileText |
| 209 | quit |
| 210 | call assert_equal('', s:swapname) |
| 211 | endif |
Bram Moolenaar | 67cf86b | 2019-04-28 22:25:38 +0200 | [diff] [blame] | 212 | |
| 213 | " Now set the modified flag, the swap file will not be deleted |
| 214 | let swapfile_bytes[28 + 80 + 899] = 0x55 |
| 215 | call writefile(swapfile_bytes, swapfile_name) |
| 216 | let s:swapname = '' |
| 217 | split XswapfileText |
| 218 | quit |
Bram Moolenaar | 701df4e | 2019-04-28 23:07:18 +0200 | [diff] [blame] | 219 | call assert_equal(fnamemodify(swapfile_name, ':t'), fnamemodify(s:swapname, ':t')) |
Bram Moolenaar | 67cf86b | 2019-04-28 22:25:38 +0200 | [diff] [blame] | 220 | |
| 221 | call delete('XswapfileText') |
| 222 | call delete(swapfile_name) |
| 223 | augroup test_swapfile_delete |
| 224 | autocmd! |
| 225 | augroup END |
| 226 | augroup! test_swapfile_delete |
| 227 | endfunc |
Bram Moolenaar | 99499b1 | 2019-05-23 21:35:48 +0200 | [diff] [blame] | 228 | |
| 229 | func Test_swap_recover() |
| 230 | autocmd! SwapExists |
| 231 | augroup test_swap_recover |
| 232 | autocmd! |
| 233 | autocmd SwapExists * let v:swapchoice = 'r' |
| 234 | augroup END |
| 235 | |
Bram Moolenaar | 99499b1 | 2019-05-23 21:35:48 +0200 | [diff] [blame] | 236 | call mkdir('Xswap') |
| 237 | let $Xswap = 'foo' " Check for issue #4369. |
| 238 | set dir=Xswap// |
| 239 | " Create a valid swapfile by editing a file. |
| 240 | split Xswap/text |
| 241 | call setline(1, ['one', 'two', 'three']) |
| 242 | write " file is written, not modified |
| 243 | " read the swapfile as a Blob |
| 244 | let swapfile_name = swapname('%') |
| 245 | let swapfile_bytes = readfile(swapfile_name, 'B') |
| 246 | |
| 247 | " Close the file and recreate the swap file. |
| 248 | quit |
| 249 | call writefile(swapfile_bytes, swapfile_name) |
| 250 | " Edit the file again. This triggers recovery. |
| 251 | try |
| 252 | split Xswap/text |
| 253 | catch |
| 254 | " E308 should be caught, not E305. |
| 255 | call assert_exception('E308:') " Original file may have been changed |
| 256 | endtry |
| 257 | " The file should be recovered. |
| 258 | call assert_equal(['one', 'two', 'three'], getline(1, 3)) |
| 259 | quit! |
| 260 | |
| 261 | call delete('Xswap/text') |
| 262 | call delete(swapfile_name) |
| 263 | call delete('Xswap', 'd') |
| 264 | unlet $Xswap |
| 265 | set dir& |
| 266 | augroup test_swap_recover |
| 267 | autocmd! |
| 268 | augroup END |
| 269 | augroup! test_swap_recover |
| 270 | endfunc |
| 271 | |
| 272 | func Test_swap_recover_ext() |
| 273 | autocmd! SwapExists |
| 274 | augroup test_swap_recover_ext |
| 275 | autocmd! |
| 276 | autocmd SwapExists * let v:swapchoice = 'r' |
| 277 | augroup END |
| 278 | |
Bram Moolenaar | 99499b1 | 2019-05-23 21:35:48 +0200 | [diff] [blame] | 279 | " Create a valid swapfile by editing a file with a special extension. |
| 280 | split Xtest.scr |
| 281 | call setline(1, ['one', 'two', 'three']) |
| 282 | write " file is written, not modified |
| 283 | write " write again to make sure the swapfile is created |
| 284 | " read the swapfile as a Blob |
| 285 | let swapfile_name = swapname('%') |
| 286 | let swapfile_bytes = readfile(swapfile_name, 'B') |
| 287 | |
| 288 | " Close and delete the file and recreate the swap file. |
| 289 | quit |
| 290 | call delete('Xtest.scr') |
| 291 | call writefile(swapfile_bytes, swapfile_name) |
| 292 | " Edit the file again. This triggers recovery. |
| 293 | try |
| 294 | split Xtest.scr |
| 295 | catch |
| 296 | " E308 should be caught, not E306. |
| 297 | call assert_exception('E308:') " Original file may have been changed |
| 298 | endtry |
| 299 | " The file should be recovered. |
| 300 | call assert_equal(['one', 'two', 'three'], getline(1, 3)) |
| 301 | quit! |
| 302 | |
| 303 | call delete('Xtest.scr') |
| 304 | call delete(swapfile_name) |
| 305 | augroup test_swap_recover_ext |
| 306 | autocmd! |
| 307 | augroup END |
| 308 | augroup! test_swap_recover_ext |
| 309 | endfunc |
Bram Moolenaar | 406cd90 | 2020-02-18 21:54:41 +0100 | [diff] [blame] | 310 | |
| 311 | " Test for closing a split window automatically when a swap file is detected |
| 312 | " and 'Q' is selected in the confirmation prompt. |
| 313 | func Test_swap_split_win() |
| 314 | autocmd! SwapExists |
| 315 | augroup test_swap_splitwin |
| 316 | autocmd! |
| 317 | autocmd SwapExists * let v:swapchoice = 'q' |
| 318 | augroup END |
| 319 | |
| 320 | " Create a valid swapfile by editing a file with a special extension. |
| 321 | split Xtest.scr |
| 322 | call setline(1, ['one', 'two', 'three']) |
| 323 | write " file is written, not modified |
| 324 | write " write again to make sure the swapfile is created |
| 325 | " read the swapfile as a Blob |
| 326 | let swapfile_name = swapname('%') |
| 327 | let swapfile_bytes = readfile(swapfile_name, 'B') |
| 328 | |
| 329 | " Close and delete the file and recreate the swap file. |
| 330 | quit |
| 331 | call delete('Xtest.scr') |
| 332 | call writefile(swapfile_bytes, swapfile_name) |
| 333 | " Split edit the file again. This should fail to open the window |
| 334 | try |
| 335 | split Xtest.scr |
| 336 | catch |
| 337 | " E308 should be caught, not E306. |
| 338 | call assert_exception('E308:') " Original file may have been changed |
| 339 | endtry |
| 340 | call assert_equal(1, winnr('$')) |
| 341 | |
| 342 | call delete('Xtest.scr') |
| 343 | call delete(swapfile_name) |
| 344 | |
| 345 | augroup test_swap_splitwin |
| 346 | autocmd! |
| 347 | augroup END |
| 348 | augroup! test_swap_splitwin |
| 349 | endfunc |
| 350 | |
Bram Moolenaar | b654103 | 2020-02-22 21:21:27 +0100 | [diff] [blame] | 351 | " Test for selecting 'q' in the attention prompt |
| 352 | func Test_swap_prompt_splitwin() |
Bram Moolenaar | d36ef57 | 2020-03-24 21:44:51 +0100 | [diff] [blame] | 353 | CheckRunVimInTerminal |
| 354 | |
Bram Moolenaar | b654103 | 2020-02-22 21:21:27 +0100 | [diff] [blame] | 355 | call writefile(['foo bar'], 'Xfile1') |
| 356 | edit Xfile1 |
Bram Moolenaar | d36ef57 | 2020-03-24 21:44:51 +0100 | [diff] [blame] | 357 | preserve " should help to make sure the swap file exists |
| 358 | |
Bram Moolenaar | b654103 | 2020-02-22 21:21:27 +0100 | [diff] [blame] | 359 | let buf = RunVimInTerminal('', {'rows': 20}) |
| 360 | call term_sendkeys(buf, ":set nomore\n") |
| 361 | call term_sendkeys(buf, ":set noruler\n") |
Bram Moolenaar | 1d97efc | 2021-07-04 13:27:11 +0200 | [diff] [blame] | 362 | |
Bram Moolenaar | b654103 | 2020-02-22 21:21:27 +0100 | [diff] [blame] | 363 | call term_sendkeys(buf, ":split Xfile1\n") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 364 | call TermWait(buf) |
Bram Moolenaar | b654103 | 2020-02-22 21:21:27 +0100 | [diff] [blame] | 365 | call WaitForAssert({-> assert_match('^\[O\]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort: $', term_getline(buf, 20))}) |
| 366 | call term_sendkeys(buf, "q") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 367 | call TermWait(buf) |
Bram Moolenaar | d36ef57 | 2020-03-24 21:44:51 +0100 | [diff] [blame] | 368 | call term_sendkeys(buf, ":\<CR>") |
Bram Moolenaar | b654103 | 2020-02-22 21:21:27 +0100 | [diff] [blame] | 369 | call WaitForAssert({-> assert_match('^:$', term_getline(buf, 20))}) |
Bram Moolenaar | d36ef57 | 2020-03-24 21:44:51 +0100 | [diff] [blame] | 370 | call term_sendkeys(buf, ":echomsg winnr('$')\<CR>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 371 | call TermWait(buf) |
Bram Moolenaar | b654103 | 2020-02-22 21:21:27 +0100 | [diff] [blame] | 372 | call WaitForAssert({-> assert_match('^1$', term_getline(buf, 20))}) |
| 373 | call StopVimInTerminal(buf) |
Bram Moolenaar | 1d97efc | 2021-07-04 13:27:11 +0200 | [diff] [blame] | 374 | |
Bram Moolenaar | 3777d6e | 2021-07-04 17:23:25 +0200 | [diff] [blame] | 375 | " This caused Vim to crash when typing "q" at the swap file prompt. |
| 376 | let buf = RunVimInTerminal('-c "au bufadd * let foo_w = wincol()"', {'rows': 18}) |
| 377 | call term_sendkeys(buf, ":e Xfile1\<CR>") |
| 378 | call WaitForAssert({-> assert_match('More', term_getline(buf, 18))}) |
| 379 | call term_sendkeys(buf, " ") |
| 380 | call WaitForAssert({-> assert_match('^\[O\]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort:', term_getline(buf, 18))}) |
Bram Moolenaar | 1d97efc | 2021-07-04 13:27:11 +0200 | [diff] [blame] | 381 | call term_sendkeys(buf, "q") |
Bram Moolenaar | 3777d6e | 2021-07-04 17:23:25 +0200 | [diff] [blame] | 382 | call TermWait(buf) |
| 383 | " check that Vim is still running |
| 384 | call term_sendkeys(buf, ":echo 'hello'\<CR>") |
| 385 | call WaitForAssert({-> assert_match('^hello', term_getline(buf, 18))}) |
| 386 | call term_sendkeys(buf, ":%bwipe!\<CR>") |
| 387 | call StopVimInTerminal(buf) |
Bram Moolenaar | 1d97efc | 2021-07-04 13:27:11 +0200 | [diff] [blame] | 388 | |
Bram Moolenaar | b654103 | 2020-02-22 21:21:27 +0100 | [diff] [blame] | 389 | %bwipe! |
| 390 | call delete('Xfile1') |
| 391 | endfunc |
| 392 | |
Bram Moolenaar | 5966ea1 | 2020-07-15 15:30:05 +0200 | [diff] [blame] | 393 | func Test_swap_symlink() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 394 | CheckUnix |
Bram Moolenaar | 5966ea1 | 2020-07-15 15:30:05 +0200 | [diff] [blame] | 395 | |
| 396 | call writefile(['text'], 'Xtestfile') |
| 397 | silent !ln -s -f Xtestfile Xtestlink |
| 398 | |
| 399 | set dir=. |
| 400 | |
| 401 | " Test that swap file uses the name of the file when editing through a |
| 402 | " symbolic link (so that editing the file twice is detected) |
| 403 | edit Xtestlink |
| 404 | call assert_match('Xtestfile\.swp$', s:swapname()) |
| 405 | bwipe! |
| 406 | |
| 407 | call mkdir('Xswapdir') |
| 408 | exe 'set dir=' . getcwd() . '/Xswapdir//' |
| 409 | |
| 410 | " Check that this also works when 'directory' ends with '//' |
| 411 | edit Xtestlink |
Bram Moolenaar | 8b0e62c | 2021-10-19 22:12:25 +0100 | [diff] [blame] | 412 | call assert_match('Xswapdir[/\\]%.*testdir%Xtestfile\.swp$', s:swapname()) |
Bram Moolenaar | 5966ea1 | 2020-07-15 15:30:05 +0200 | [diff] [blame] | 413 | bwipe! |
| 414 | |
| 415 | set dir& |
| 416 | call delete('Xtestfile') |
| 417 | call delete('Xtestlink') |
| 418 | call delete('Xswapdir', 'rf') |
| 419 | endfunc |
| 420 | |
Bram Moolenaar | 5ee0981 | 2020-11-25 12:43:28 +0100 | [diff] [blame] | 421 | func s:get_unused_pid(base) |
| 422 | if has('job') |
| 423 | " Execute 'echo' as a temporary job, and return its pid as an unused pid. |
| 424 | if has('win32') |
| 425 | let cmd = 'cmd /c echo' |
| 426 | else |
| 427 | let cmd = 'echo' |
| 428 | endif |
| 429 | let j = job_start(cmd) |
| 430 | while job_status(j) ==# 'run' |
| 431 | sleep 10m |
| 432 | endwhile |
| 433 | if job_status(j) ==# 'dead' |
| 434 | return job_info(j).process |
| 435 | endif |
| 436 | endif |
| 437 | " Must add four for MS-Windows to see it as a different one. |
| 438 | return a:base + 4 |
| 439 | endfunc |
| 440 | |
| 441 | func s:blob_to_pid(b) |
| 442 | return a:b[3] * 16777216 + a:b[2] * 65536 + a:b[1] * 256 + a:b[0] |
| 443 | endfunc |
| 444 | |
| 445 | func s:pid_to_blob(i) |
| 446 | let b = 0z |
| 447 | let b[0] = and(a:i, 0xff) |
| 448 | let b[1] = and(a:i / 256, 0xff) |
| 449 | let b[2] = and(a:i / 65536, 0xff) |
| 450 | let b[3] = and(a:i / 16777216, 0xff) |
| 451 | return b |
| 452 | endfunc |
| 453 | |
Bram Moolenaar | f883508 | 2020-11-09 21:04:17 +0100 | [diff] [blame] | 454 | func Test_swap_auto_delete() |
| 455 | " Create a valid swapfile by editing a file with a special extension. |
| 456 | split Xtest.scr |
| 457 | call setline(1, ['one', 'two', 'three']) |
| 458 | write " file is written, not modified |
| 459 | write " write again to make sure the swapfile is created |
| 460 | " read the swapfile as a Blob |
| 461 | let swapfile_name = swapname('%') |
| 462 | let swapfile_bytes = readfile(swapfile_name, 'B') |
| 463 | |
| 464 | " Forget about the file, recreate the swap file, then edit it again. The |
| 465 | " swap file should be automatically deleted. |
| 466 | bwipe! |
Bram Moolenaar | 5ee0981 | 2020-11-25 12:43:28 +0100 | [diff] [blame] | 467 | " Change the process ID to avoid the "still running" warning. |
| 468 | let swapfile_bytes[24:27] = s:pid_to_blob(s:get_unused_pid( |
| 469 | \ s:blob_to_pid(swapfile_bytes[24:27]))) |
Bram Moolenaar | f883508 | 2020-11-09 21:04:17 +0100 | [diff] [blame] | 470 | call writefile(swapfile_bytes, swapfile_name) |
| 471 | edit Xtest.scr |
| 472 | " will end up using the same swap file after deleting the existing one |
| 473 | call assert_equal(swapfile_name, swapname('%')) |
| 474 | bwipe! |
| 475 | |
| 476 | " create the swap file again, but change the host name so that it won't be |
| 477 | " deleted |
| 478 | autocmd! SwapExists |
| 479 | augroup test_swap_recover_ext |
| 480 | autocmd! |
| 481 | autocmd SwapExists * let v:swapchoice = 'e' |
| 482 | augroup END |
| 483 | |
| 484 | " change the host name |
Bram Moolenaar | c6ca9f3 | 2020-11-19 18:57:23 +0100 | [diff] [blame] | 485 | let swapfile_bytes[28 + 40] = swapfile_bytes[28 + 40] + 2 |
Bram Moolenaar | f883508 | 2020-11-09 21:04:17 +0100 | [diff] [blame] | 486 | call writefile(swapfile_bytes, swapfile_name) |
| 487 | edit Xtest.scr |
| 488 | call assert_equal(1, filereadable(swapfile_name)) |
| 489 | " will use another same swap file name |
| 490 | call assert_notequal(swapfile_name, swapname('%')) |
| 491 | bwipe! |
| 492 | |
| 493 | call delete('Xtest.scr') |
| 494 | call delete(swapfile_name) |
| 495 | augroup test_swap_recover_ext |
| 496 | autocmd! |
| 497 | augroup END |
| 498 | augroup! test_swap_recover_ext |
| 499 | endfunc |
| 500 | |
Yegappan Lakshmanan | 59b2623 | 2021-06-05 20:59:22 +0200 | [diff] [blame] | 501 | " Test for renaming a buffer when the swap file is deleted out-of-band |
| 502 | func Test_missing_swap_file() |
| 503 | CheckUnix |
Bram Moolenaar | f33cae6 | 2021-07-04 17:36:54 +0200 | [diff] [blame] | 504 | new Xfile2 |
Yegappan Lakshmanan | 9928555 | 2021-06-06 17:12:46 +0200 | [diff] [blame] | 505 | call delete(swapname('')) |
Bram Moolenaar | f33cae6 | 2021-07-04 17:36:54 +0200 | [diff] [blame] | 506 | call assert_fails('file Xfile3', 'E301:') |
| 507 | call assert_equal('Xfile3', bufname()) |
Yegappan Lakshmanan | 59b2623 | 2021-06-05 20:59:22 +0200 | [diff] [blame] | 508 | call assert_true(bufexists('Xfile2')) |
Bram Moolenaar | f33cae6 | 2021-07-04 17:36:54 +0200 | [diff] [blame] | 509 | call assert_true(bufexists('Xfile3')) |
Yegappan Lakshmanan | 59b2623 | 2021-06-05 20:59:22 +0200 | [diff] [blame] | 510 | %bw! |
| 511 | endfunc |
| 512 | |
| 513 | " Test for :preserve command |
| 514 | func Test_preserve() |
Bram Moolenaar | f33cae6 | 2021-07-04 17:36:54 +0200 | [diff] [blame] | 515 | new Xfile4 |
Yegappan Lakshmanan | 59b2623 | 2021-06-05 20:59:22 +0200 | [diff] [blame] | 516 | setlocal noswapfile |
| 517 | call assert_fails('preserve', 'E313:') |
| 518 | bw! |
| 519 | endfunc |
| 520 | |
| 521 | " Test for the v:swapchoice variable |
| 522 | func Test_swapchoice() |
Bram Moolenaar | f33cae6 | 2021-07-04 17:36:54 +0200 | [diff] [blame] | 523 | call writefile(['aaa', 'bbb'], 'Xfile5') |
| 524 | edit Xfile5 |
Yegappan Lakshmanan | 59b2623 | 2021-06-05 20:59:22 +0200 | [diff] [blame] | 525 | preserve |
| 526 | let swapfname = swapname('') |
| 527 | let b = readblob(swapfname) |
| 528 | bw! |
| 529 | call writefile(b, swapfname) |
| 530 | |
| 531 | autocmd! SwapExists |
| 532 | |
| 533 | " Test for v:swapchoice = 'o' (readonly) |
| 534 | augroup test_swapchoice |
| 535 | autocmd! |
| 536 | autocmd SwapExists * let v:swapchoice = 'o' |
| 537 | augroup END |
Bram Moolenaar | f33cae6 | 2021-07-04 17:36:54 +0200 | [diff] [blame] | 538 | edit Xfile5 |
Yegappan Lakshmanan | 59b2623 | 2021-06-05 20:59:22 +0200 | [diff] [blame] | 539 | call assert_true(&readonly) |
| 540 | call assert_equal(['aaa', 'bbb'], getline(1, '$')) |
| 541 | %bw! |
| 542 | call assert_true(filereadable(swapfname)) |
| 543 | |
| 544 | " Test for v:swapchoice = 'a' (abort) |
| 545 | augroup test_swapchoice |
| 546 | autocmd! |
| 547 | autocmd SwapExists * let v:swapchoice = 'a' |
| 548 | augroup END |
| 549 | try |
Bram Moolenaar | f33cae6 | 2021-07-04 17:36:54 +0200 | [diff] [blame] | 550 | edit Xfile5 |
Yegappan Lakshmanan | 59b2623 | 2021-06-05 20:59:22 +0200 | [diff] [blame] | 551 | catch /^Vim:Interrupt$/ |
| 552 | endtry |
| 553 | call assert_equal('', @%) |
Bram Moolenaar | f33cae6 | 2021-07-04 17:36:54 +0200 | [diff] [blame] | 554 | call assert_true(bufexists('Xfile5')) |
Yegappan Lakshmanan | 59b2623 | 2021-06-05 20:59:22 +0200 | [diff] [blame] | 555 | %bw! |
| 556 | call assert_true(filereadable(swapfname)) |
| 557 | |
| 558 | " Test for v:swapchoice = 'd' (delete) |
| 559 | augroup test_swapchoice |
| 560 | autocmd! |
| 561 | autocmd SwapExists * let v:swapchoice = 'd' |
| 562 | augroup END |
Bram Moolenaar | f33cae6 | 2021-07-04 17:36:54 +0200 | [diff] [blame] | 563 | edit Xfile5 |
| 564 | call assert_equal('Xfile5', @%) |
Yegappan Lakshmanan | 59b2623 | 2021-06-05 20:59:22 +0200 | [diff] [blame] | 565 | %bw! |
| 566 | call assert_false(filereadable(swapfname)) |
| 567 | |
Bram Moolenaar | f33cae6 | 2021-07-04 17:36:54 +0200 | [diff] [blame] | 568 | call delete('Xfile5') |
Yegappan Lakshmanan | 59b2623 | 2021-06-05 20:59:22 +0200 | [diff] [blame] | 569 | call delete(swapfname) |
| 570 | augroup test_swapchoice |
| 571 | autocmd! |
| 572 | augroup END |
| 573 | augroup! test_swapchoice |
| 574 | endfunc |
| 575 | |
Dominique Pelle | fe3418a | 2021-07-10 17:59:48 +0200 | [diff] [blame] | 576 | func Test_no_swap_file() |
| 577 | call assert_equal("\nNo swap file", execute('swapname')) |
| 578 | endfunc |
| 579 | |
Bram Moolenaar | 406cd90 | 2020-02-18 21:54:41 +0100 | [diff] [blame] | 580 | " vim: shiftwidth=2 sts=2 expandtab |