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