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