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