blob: 5237edcfb4c4a1e101248a59d221e4b2157cd76e [file] [log] [blame]
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001" Tests for the swap feature
2
Bram Moolenaard36ef572020-03-24 21:44:51 +01003source check.vim
Bram Moolenaar07282f02019-10-10 16:46:17 +02004source shared.vim
Bram Moolenaarb6541032020-02-22 21:21:27 +01005source term_util.vim
Bram Moolenaar07282f02019-10-10 16:46:17 +02006
Bram Moolenaar110bd602018-09-16 18:46:59 +02007func s:swapname()
8 return trim(execute('swapname'))
9endfunc
10
Bram Moolenaarffe010f2017-11-04 22:30:40 +010011" Tests for 'directory' option.
12func Test_swap_directory()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020013 CheckUnix
14
Bram Moolenaarffe010f2017-11-04 22:30:40 +010015 let content = ['start of testfile',
16 \ 'line 2 Abcdefghij',
17 \ 'line 3 Abcdefghij',
18 \ 'end of testfile']
Bram Moolenaar56564962022-10-10 22:39:42 +010019 call writefile(content, 'Xtest1', 'D')
Bram Moolenaarffe010f2017-11-04 22:30:40 +010020
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 Moolenaar110bd602018-09-16 18:46:59 +020027 let swfname = s:swapname()
Bram Moolenaarffe010f2017-11-04 22:30:40 +010028 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 Moolenaar56564962022-10-10 22:39:42 +010033 call mkdir("Xtest2", 'R')
Bram Moolenaarffe010f2017-11-04 22:30:40 +010034 edit Xtest1
35 call assert_equal([], glob(swfname, 1, 1, 1))
36 let swfname = "Xtest2/Xtest1.swp"
Bram Moolenaar110bd602018-09-16 18:46:59 +020037 call assert_equal(swfname, s:swapname())
Bram Moolenaarffe010f2017-11-04 22:30:40 +010038 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 Moolenaar56564962022-10-10 22:39:42 +010043 call mkdir("Xtest.je", 'R')
Bram Moolenaarffe010f2017-11-04 22:30:40 +010044 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 Moolenaar110bd602018-09-16 18:46:59 +020048 call assert_equal(swfname, s:swapname())
Bram Moolenaarffe010f2017-11-04 22:30:40 +010049 call assert_equal([swfname], glob("Xtest.je/*", 1, 1, 1))
50
51 set dir&
Bram Moolenaarffe010f2017-11-04 22:30:40 +010052endfunc
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +010053
54func Test_swap_group()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020055 CheckUnix
56
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +010057 let groups = split(system('groups'))
58 if len(groups) <= 1
Bram Moolenaarad7dac82017-11-04 22:21:21 +010059 throw 'Skipped: need at least two groups, got ' . string(groups)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +010060 endif
Bram Moolenaar430dc5d2017-11-02 21:04:47 +010061
Bram Moolenaar5842a742017-11-04 22:36:53 +010062 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 Moolenaar5a73e0c2017-11-04 21:35:01 +010069 else
Bram Moolenaar5842a742017-11-04 22:36:53 +010070 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 Moolenaar110bd602018-09-16 18:46:59 +020076 let swapname = s:swapname()
Bram Moolenaar5842a742017-11-04 22:36:53 +010077 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 Moolenaar430dc5d2017-11-02 21:04:47 +010080
Bram Moolenaar5842a742017-11-04 22:36:53 +010081 bwipe!
82 endif
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +010083 endif
Bram Moolenaar5842a742017-11-04 22:36:53 +010084 finally
85 call delete('Xtest')
86 endtry
Bram Moolenaar430dc5d2017-11-02 21:04:47 +010087endfunc
Bram Moolenaar8c3169c2018-05-12 17:04:12 +020088
89func 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 Moolenaare3d06542019-01-27 14:29:24 +0100100 " 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 Moolenaar8c3169c2018-05-12 17:04:12 +0200103 set directory&
104 call delete('Xswapdir', 'rf')
105endfunc
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200106
107func Test_swapinfo()
108 new Xswapinfo
109 call setline(1, ['one', 'two', 'three'])
110 w
Bram Moolenaar110bd602018-09-16 18:46:59 +0200111 let fname = s:swapname()
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200112 call assert_match('Xswapinfo', fname)
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200113 let info = fname->swapinfo()
Bram Moolenaar4c5765b2018-08-22 11:28:01 +0200114
115 let ver = printf('VIM %d.%d', v:version / 100, v:version % 100)
116 call assert_equal(ver, info.version)
117
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200118 call assert_match('\w', info.user)
Bram Moolenaar4c5765b2018-08-22 11:28:01 +0200119 " host name is truncated to 39 bytes in the swap file
120 call assert_equal(hostname()[:38], info.host)
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200121 call assert_match('Xswapinfo', info.fname)
Bram Moolenaar47ad5652018-08-21 21:09:07 +0200122 call assert_match(0, info.dirty)
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200123 call assert_equal(getpid(), info.pid)
124 call assert_match('^\d*$', info.mtime)
125 if has_key(info, 'inode')
126 call assert_match('\d', info.inode)
127 endif
128 bwipe!
129 call delete(fname)
130 call delete('Xswapinfo')
131
132 let info = swapinfo('doesnotexist')
133 call assert_equal('Cannot open file', info.error)
134
Bram Moolenaar56564962022-10-10 22:39:42 +0100135 call writefile(['burp'], 'Xnotaswapfile', 'D')
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200136 let info = swapinfo('Xnotaswapfile')
137 call assert_equal('Cannot read file', info.error)
138 call delete('Xnotaswapfile')
139
140 call writefile([repeat('x', 10000)], 'Xnotaswapfile')
141 let info = swapinfo('Xnotaswapfile')
Bram Moolenaar47ad5652018-08-21 21:09:07 +0200142 call assert_equal('Not a swap file', info.error)
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200143endfunc
Bram Moolenaar110bd602018-09-16 18:46:59 +0200144
145func Test_swapname()
146 edit Xtest1
147 let expected = s:swapname()
148 call assert_equal(expected, swapname('%'))
149
150 new Xtest2
151 let buf = bufnr('%')
152 let expected = s:swapname()
153 wincmd p
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200154 call assert_equal(expected, buf->swapname())
Bram Moolenaar110bd602018-09-16 18:46:59 +0200155
156 new Xtest3
157 setlocal noswapfile
158 call assert_equal('', swapname('%'))
159
160 bwipe!
161 call delete('Xtest1')
162 call delete('Xtest2')
163 call delete('Xtest3')
164endfunc
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200165
166func Test_swapfile_delete()
167 autocmd! SwapExists
168 function s:swap_exists()
169 let v:swapchoice = s:swap_choice
170 let s:swapname = v:swapname
171 let s:filename = expand('<afile>')
172 endfunc
173 augroup test_swapfile_delete
174 autocmd!
175 autocmd SwapExists * call s:swap_exists()
176 augroup END
177
178
179 " Create a valid swapfile by editing a file.
180 split XswapfileText
181 call setline(1, ['one', 'two', 'three'])
182 write " file is written, not modified
183 " read the swapfile as a Blob
184 let swapfile_name = swapname('%')
185 let swapfile_bytes = readfile(swapfile_name, 'B')
186
187 " Close the file and recreate the swap file.
188 " Now editing the file will run into the process still existing
189 quit
Bram Moolenaar56564962022-10-10 22:39:42 +0100190 call writefile(swapfile_bytes, swapfile_name, 'D')
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200191 let s:swap_choice = 'e'
192 let s:swapname = ''
193 split XswapfileText
194 quit
Bram Moolenaar701df4e2019-04-28 23:07:18 +0200195 call assert_equal(fnamemodify(swapfile_name, ':t'), fnamemodify(s:swapname, ':t'))
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200196
Bram Moolenaar07282f02019-10-10 16:46:17 +0200197 " This test won't work as root because root can successfully run kill(1, 0)
198 if !IsRoot()
199 " Write the swapfile with a modified PID, now it will be automatically
Bram Moolenaar6738fd22021-06-23 21:44:06 +0200200 " deleted. Process 0x3fffffff most likely does not exist.
201 let swapfile_bytes[24:27] = 0zffffff3f
Bram Moolenaar07282f02019-10-10 16:46:17 +0200202 call writefile(swapfile_bytes, swapfile_name)
203 let s:swapname = ''
204 split XswapfileText
205 quit
206 call assert_equal('', s:swapname)
207 endif
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200208
209 " Now set the modified flag, the swap file will not be deleted
210 let swapfile_bytes[28 + 80 + 899] = 0x55
211 call writefile(swapfile_bytes, swapfile_name)
212 let s:swapname = ''
213 split XswapfileText
214 quit
Bram Moolenaar701df4e2019-04-28 23:07:18 +0200215 call assert_equal(fnamemodify(swapfile_name, ':t'), fnamemodify(s:swapname, ':t'))
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200216
217 call delete('XswapfileText')
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200218 augroup test_swapfile_delete
219 autocmd!
220 augroup END
221 augroup! test_swapfile_delete
222endfunc
Bram Moolenaar99499b12019-05-23 21:35:48 +0200223
224func Test_swap_recover()
225 autocmd! SwapExists
226 augroup test_swap_recover
227 autocmd!
228 autocmd SwapExists * let v:swapchoice = 'r'
229 augroup END
230
Bram Moolenaar56564962022-10-10 22:39:42 +0100231 call mkdir('Xswap', 'R')
Bram Moolenaar99499b12019-05-23 21:35:48 +0200232 let $Xswap = 'foo' " Check for issue #4369.
233 set dir=Xswap//
234 " Create a valid swapfile by editing a file.
235 split Xswap/text
236 call setline(1, ['one', 'two', 'three'])
237 write " file is written, not modified
238 " read the swapfile as a Blob
239 let swapfile_name = swapname('%')
240 let swapfile_bytes = readfile(swapfile_name, 'B')
241
242 " Close the file and recreate the swap file.
243 quit
Bram Moolenaar56564962022-10-10 22:39:42 +0100244 call writefile(swapfile_bytes, swapfile_name, 'D')
Bram Moolenaar99499b12019-05-23 21:35:48 +0200245 " Edit the file again. This triggers recovery.
246 try
247 split Xswap/text
248 catch
249 " E308 should be caught, not E305.
250 call assert_exception('E308:') " Original file may have been changed
251 endtry
252 " The file should be recovered.
253 call assert_equal(['one', 'two', 'three'], getline(1, 3))
254 quit!
255
Bram Moolenaar99499b12019-05-23 21:35:48 +0200256 unlet $Xswap
257 set dir&
258 augroup test_swap_recover
259 autocmd!
260 augroup END
261 augroup! test_swap_recover
262endfunc
263
264func Test_swap_recover_ext()
265 autocmd! SwapExists
266 augroup test_swap_recover_ext
267 autocmd!
268 autocmd SwapExists * let v:swapchoice = 'r'
269 augroup END
270
Bram Moolenaar99499b12019-05-23 21:35:48 +0200271 " Create a valid swapfile by editing a file with a special extension.
272 split Xtest.scr
273 call setline(1, ['one', 'two', 'three'])
274 write " file is written, not modified
275 write " write again to make sure the swapfile is created
276 " read the swapfile as a Blob
277 let swapfile_name = swapname('%')
278 let swapfile_bytes = readfile(swapfile_name, 'B')
279
280 " Close and delete the file and recreate the swap file.
281 quit
282 call delete('Xtest.scr')
Bram Moolenaar56564962022-10-10 22:39:42 +0100283 call writefile(swapfile_bytes, swapfile_name, 'D')
Bram Moolenaar99499b12019-05-23 21:35:48 +0200284 " Edit the file again. This triggers recovery.
285 try
286 split Xtest.scr
287 catch
288 " E308 should be caught, not E306.
289 call assert_exception('E308:') " Original file may have been changed
290 endtry
291 " The file should be recovered.
292 call assert_equal(['one', 'two', 'three'], getline(1, 3))
293 quit!
294
295 call delete('Xtest.scr')
Bram Moolenaar99499b12019-05-23 21:35:48 +0200296 augroup test_swap_recover_ext
297 autocmd!
298 augroup END
299 augroup! test_swap_recover_ext
300endfunc
Bram Moolenaar406cd902020-02-18 21:54:41 +0100301
302" Test for closing a split window automatically when a swap file is detected
303" and 'Q' is selected in the confirmation prompt.
304func Test_swap_split_win()
305 autocmd! SwapExists
306 augroup test_swap_splitwin
307 autocmd!
308 autocmd SwapExists * let v:swapchoice = 'q'
309 augroup END
310
311 " Create a valid swapfile by editing a file with a special extension.
312 split Xtest.scr
313 call setline(1, ['one', 'two', 'three'])
314 write " file is written, not modified
315 write " write again to make sure the swapfile is created
316 " read the swapfile as a Blob
317 let swapfile_name = swapname('%')
318 let swapfile_bytes = readfile(swapfile_name, 'B')
319
320 " Close and delete the file and recreate the swap file.
321 quit
322 call delete('Xtest.scr')
Bram Moolenaar56564962022-10-10 22:39:42 +0100323 call writefile(swapfile_bytes, swapfile_name, 'D')
Bram Moolenaar406cd902020-02-18 21:54:41 +0100324 " Split edit the file again. This should fail to open the window
325 try
326 split Xtest.scr
327 catch
328 " E308 should be caught, not E306.
329 call assert_exception('E308:') " Original file may have been changed
330 endtry
331 call assert_equal(1, winnr('$'))
332
333 call delete('Xtest.scr')
Bram Moolenaar406cd902020-02-18 21:54:41 +0100334
335 augroup test_swap_splitwin
336 autocmd!
337 augroup END
338 augroup! test_swap_splitwin
339endfunc
340
Bram Moolenaarb6541032020-02-22 21:21:27 +0100341" Test for selecting 'q' in the attention prompt
342func Test_swap_prompt_splitwin()
Bram Moolenaard36ef572020-03-24 21:44:51 +0100343 CheckRunVimInTerminal
344
Bram Moolenaar56564962022-10-10 22:39:42 +0100345 call writefile(['foo bar'], 'Xfile1', 'D')
Bram Moolenaarb6541032020-02-22 21:21:27 +0100346 edit Xfile1
Bram Moolenaard36ef572020-03-24 21:44:51 +0100347 preserve " should help to make sure the swap file exists
348
Bram Moolenaarb6541032020-02-22 21:21:27 +0100349 let buf = RunVimInTerminal('', {'rows': 20})
350 call term_sendkeys(buf, ":set nomore\n")
351 call term_sendkeys(buf, ":set noruler\n")
Bram Moolenaar1d97efc2021-07-04 13:27:11 +0200352
Bram Moolenaarb6541032020-02-22 21:21:27 +0100353 call term_sendkeys(buf, ":split Xfile1\n")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200354 call TermWait(buf)
Bram Moolenaarb6541032020-02-22 21:21:27 +0100355 call WaitForAssert({-> assert_match('^\[O\]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort: $', term_getline(buf, 20))})
356 call term_sendkeys(buf, "q")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200357 call TermWait(buf)
Bram Moolenaard36ef572020-03-24 21:44:51 +0100358 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb6541032020-02-22 21:21:27 +0100359 call WaitForAssert({-> assert_match('^:$', term_getline(buf, 20))})
Bram Moolenaard36ef572020-03-24 21:44:51 +0100360 call term_sendkeys(buf, ":echomsg winnr('$')\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200361 call TermWait(buf)
Bram Moolenaarb6541032020-02-22 21:21:27 +0100362 call WaitForAssert({-> assert_match('^1$', term_getline(buf, 20))})
363 call StopVimInTerminal(buf)
Bram Moolenaar1d97efc2021-07-04 13:27:11 +0200364
Bram Moolenaar3777d6e2021-07-04 17:23:25 +0200365 " This caused Vim to crash when typing "q" at the swap file prompt.
366 let buf = RunVimInTerminal('-c "au bufadd * let foo_w = wincol()"', {'rows': 18})
367 call term_sendkeys(buf, ":e Xfile1\<CR>")
368 call WaitForAssert({-> assert_match('More', term_getline(buf, 18))})
369 call term_sendkeys(buf, " ")
370 call WaitForAssert({-> assert_match('^\[O\]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort:', term_getline(buf, 18))})
Bram Moolenaar1d97efc2021-07-04 13:27:11 +0200371 call term_sendkeys(buf, "q")
Bram Moolenaar3777d6e2021-07-04 17:23:25 +0200372 call TermWait(buf)
373 " check that Vim is still running
374 call term_sendkeys(buf, ":echo 'hello'\<CR>")
375 call WaitForAssert({-> assert_match('^hello', term_getline(buf, 18))})
376 call term_sendkeys(buf, ":%bwipe!\<CR>")
377 call StopVimInTerminal(buf)
Bram Moolenaar1d97efc2021-07-04 13:27:11 +0200378
Bram Moolenaarb6541032020-02-22 21:21:27 +0100379 %bwipe!
Bram Moolenaarb6541032020-02-22 21:21:27 +0100380endfunc
381
Bram Moolenaar5966ea12020-07-15 15:30:05 +0200382func Test_swap_symlink()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200383 CheckUnix
Bram Moolenaar5966ea12020-07-15 15:30:05 +0200384
Bram Moolenaar56564962022-10-10 22:39:42 +0100385 call writefile(['text'], 'Xtestfile', 'D')
Bram Moolenaar5966ea12020-07-15 15:30:05 +0200386 silent !ln -s -f Xtestfile Xtestlink
387
388 set dir=.
389
390 " Test that swap file uses the name of the file when editing through a
391 " symbolic link (so that editing the file twice is detected)
392 edit Xtestlink
393 call assert_match('Xtestfile\.swp$', s:swapname())
394 bwipe!
395
Bram Moolenaar56564962022-10-10 22:39:42 +0100396 call mkdir('Xswapdir', 'R')
Bram Moolenaar5966ea12020-07-15 15:30:05 +0200397 exe 'set dir=' . getcwd() . '/Xswapdir//'
398
399 " Check that this also works when 'directory' ends with '//'
400 edit Xtestlink
Bram Moolenaar8b0e62c2021-10-19 22:12:25 +0100401 call assert_match('Xswapdir[/\\]%.*testdir%Xtestfile\.swp$', s:swapname())
Bram Moolenaar5966ea12020-07-15 15:30:05 +0200402 bwipe!
403
404 set dir&
Bram Moolenaar5966ea12020-07-15 15:30:05 +0200405 call delete('Xtestlink')
Bram Moolenaar5966ea12020-07-15 15:30:05 +0200406endfunc
407
Bram Moolenaar5ee09812020-11-25 12:43:28 +0100408func s:get_unused_pid(base)
409 if has('job')
410 " Execute 'echo' as a temporary job, and return its pid as an unused pid.
411 if has('win32')
412 let cmd = 'cmd /c echo'
413 else
414 let cmd = 'echo'
415 endif
416 let j = job_start(cmd)
417 while job_status(j) ==# 'run'
418 sleep 10m
419 endwhile
420 if job_status(j) ==# 'dead'
421 return job_info(j).process
422 endif
423 endif
424 " Must add four for MS-Windows to see it as a different one.
425 return a:base + 4
426endfunc
427
428func s:blob_to_pid(b)
429 return a:b[3] * 16777216 + a:b[2] * 65536 + a:b[1] * 256 + a:b[0]
430endfunc
431
432func s:pid_to_blob(i)
433 let b = 0z
434 let b[0] = and(a:i, 0xff)
435 let b[1] = and(a:i / 256, 0xff)
436 let b[2] = and(a:i / 65536, 0xff)
437 let b[3] = and(a:i / 16777216, 0xff)
438 return b
439endfunc
440
Bram Moolenaarf8835082020-11-09 21:04:17 +0100441func Test_swap_auto_delete()
442 " Create a valid swapfile by editing a file with a special extension.
443 split Xtest.scr
444 call setline(1, ['one', 'two', 'three'])
445 write " file is written, not modified
446 write " write again to make sure the swapfile is created
447 " read the swapfile as a Blob
448 let swapfile_name = swapname('%')
449 let swapfile_bytes = readfile(swapfile_name, 'B')
450
451 " Forget about the file, recreate the swap file, then edit it again. The
452 " swap file should be automatically deleted.
453 bwipe!
Bram Moolenaar5ee09812020-11-25 12:43:28 +0100454 " Change the process ID to avoid the "still running" warning.
455 let swapfile_bytes[24:27] = s:pid_to_blob(s:get_unused_pid(
456 \ s:blob_to_pid(swapfile_bytes[24:27])))
Bram Moolenaar56564962022-10-10 22:39:42 +0100457 call writefile(swapfile_bytes, swapfile_name, 'D')
Bram Moolenaarf8835082020-11-09 21:04:17 +0100458 edit Xtest.scr
459 " will end up using the same swap file after deleting the existing one
460 call assert_equal(swapfile_name, swapname('%'))
461 bwipe!
462
463 " create the swap file again, but change the host name so that it won't be
464 " deleted
465 autocmd! SwapExists
466 augroup test_swap_recover_ext
467 autocmd!
468 autocmd SwapExists * let v:swapchoice = 'e'
469 augroup END
470
471 " change the host name
Bram Moolenaarc6ca9f32020-11-19 18:57:23 +0100472 let swapfile_bytes[28 + 40] = swapfile_bytes[28 + 40] + 2
Bram Moolenaarf8835082020-11-09 21:04:17 +0100473 call writefile(swapfile_bytes, swapfile_name)
474 edit Xtest.scr
475 call assert_equal(1, filereadable(swapfile_name))
476 " will use another same swap file name
477 call assert_notequal(swapfile_name, swapname('%'))
478 bwipe!
479
480 call delete('Xtest.scr')
Bram Moolenaarf8835082020-11-09 21:04:17 +0100481 augroup test_swap_recover_ext
482 autocmd!
483 augroup END
484 augroup! test_swap_recover_ext
485endfunc
486
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200487" Test for renaming a buffer when the swap file is deleted out-of-band
488func Test_missing_swap_file()
489 CheckUnix
Bram Moolenaarf33cae62021-07-04 17:36:54 +0200490 new Xfile2
Yegappan Lakshmanan99285552021-06-06 17:12:46 +0200491 call delete(swapname(''))
Bram Moolenaarf33cae62021-07-04 17:36:54 +0200492 call assert_fails('file Xfile3', 'E301:')
493 call assert_equal('Xfile3', bufname())
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200494 call assert_true(bufexists('Xfile2'))
Bram Moolenaarf33cae62021-07-04 17:36:54 +0200495 call assert_true(bufexists('Xfile3'))
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200496 %bw!
497endfunc
498
499" Test for :preserve command
500func Test_preserve()
Bram Moolenaarf33cae62021-07-04 17:36:54 +0200501 new Xfile4
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200502 setlocal noswapfile
503 call assert_fails('preserve', 'E313:')
504 bw!
505endfunc
506
507" Test for the v:swapchoice variable
508func Test_swapchoice()
Bram Moolenaar56564962022-10-10 22:39:42 +0100509 call writefile(['aaa', 'bbb'], 'Xfile5', 'D')
Bram Moolenaarf33cae62021-07-04 17:36:54 +0200510 edit Xfile5
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200511 preserve
512 let swapfname = swapname('')
513 let b = readblob(swapfname)
514 bw!
Bram Moolenaar56564962022-10-10 22:39:42 +0100515 call writefile(b, swapfname, 'D')
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200516
517 autocmd! SwapExists
518
519 " Test for v:swapchoice = 'o' (readonly)
520 augroup test_swapchoice
521 autocmd!
522 autocmd SwapExists * let v:swapchoice = 'o'
523 augroup END
Bram Moolenaarf33cae62021-07-04 17:36:54 +0200524 edit Xfile5
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200525 call assert_true(&readonly)
526 call assert_equal(['aaa', 'bbb'], getline(1, '$'))
527 %bw!
528 call assert_true(filereadable(swapfname))
529
530 " Test for v:swapchoice = 'a' (abort)
531 augroup test_swapchoice
532 autocmd!
533 autocmd SwapExists * let v:swapchoice = 'a'
534 augroup END
535 try
Bram Moolenaarf33cae62021-07-04 17:36:54 +0200536 edit Xfile5
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200537 catch /^Vim:Interrupt$/
538 endtry
539 call assert_equal('', @%)
Bram Moolenaarf33cae62021-07-04 17:36:54 +0200540 call assert_true(bufexists('Xfile5'))
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200541 %bw!
542 call assert_true(filereadable(swapfname))
543
544 " Test for v:swapchoice = 'd' (delete)
545 augroup test_swapchoice
546 autocmd!
547 autocmd SwapExists * let v:swapchoice = 'd'
548 augroup END
Bram Moolenaarf33cae62021-07-04 17:36:54 +0200549 edit Xfile5
550 call assert_equal('Xfile5', @%)
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200551 %bw!
552 call assert_false(filereadable(swapfname))
553
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200554 call delete(swapfname)
555 augroup test_swapchoice
556 autocmd!
557 augroup END
558 augroup! test_swapchoice
559endfunc
560
Dominique Pellefe3418a2021-07-10 17:59:48 +0200561func Test_no_swap_file()
562 call assert_equal("\nNo swap file", execute('swapname'))
563endfunc
564
Bram Moolenaar406cd902020-02-18 21:54:41 +0100565" vim: shiftwidth=2 sts=2 expandtab