blob: b599bf76ee5201642f7e7ab2cc3e7ae38cdf7be0 [file] [log] [blame]
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001" Tests for the swap feature
2
Bram Moolenaar110bd602018-09-16 18:46:59 +02003func s:swapname()
4 return trim(execute('swapname'))
5endfunc
6
Bram Moolenaarffe010f2017-11-04 22:30:40 +01007" Tests for 'directory' option.
8func Test_swap_directory()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02009 CheckUnix
10
Bram Moolenaarffe010f2017-11-04 22:30:40 +010011 let content = ['start of testfile',
12 \ 'line 2 Abcdefghij',
13 \ 'line 3 Abcdefghij',
14 \ 'end of testfile']
Bram Moolenaar56564962022-10-10 22:39:42 +010015 call writefile(content, 'Xtest1', 'D')
Bram Moolenaarffe010f2017-11-04 22:30:40 +010016
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 Moolenaar110bd602018-09-16 18:46:59 +020023 let swfname = s:swapname()
Bram Moolenaarffe010f2017-11-04 22:30:40 +010024 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 Moolenaar56564962022-10-10 22:39:42 +010029 call mkdir("Xtest2", 'R')
Bram Moolenaarffe010f2017-11-04 22:30:40 +010030 edit Xtest1
31 call assert_equal([], glob(swfname, 1, 1, 1))
32 let swfname = "Xtest2/Xtest1.swp"
Bram Moolenaar110bd602018-09-16 18:46:59 +020033 call assert_equal(swfname, s:swapname())
Bram Moolenaarffe010f2017-11-04 22:30:40 +010034 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 Moolenaar56564962022-10-10 22:39:42 +010039 call mkdir("Xtest.je", 'R')
Bram Moolenaarffe010f2017-11-04 22:30:40 +010040 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 Moolenaar110bd602018-09-16 18:46:59 +020044 call assert_equal(swfname, s:swapname())
Bram Moolenaarffe010f2017-11-04 22:30:40 +010045 call assert_equal([swfname], glob("Xtest.je/*", 1, 1, 1))
46
47 set dir&
Bram Moolenaarffe010f2017-11-04 22:30:40 +010048endfunc
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +010049
50func Test_swap_group()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020051 CheckUnix
52
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +010053 let groups = split(system('groups'))
54 if len(groups) <= 1
Bram Moolenaarad7dac82017-11-04 22:21:21 +010055 throw 'Skipped: need at least two groups, got ' . string(groups)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +010056 endif
Bram Moolenaar430dc5d2017-11-02 21:04:47 +010057
Bram Moolenaar5842a742017-11-04 22:36:53 +010058 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 Moolenaar5a73e0c2017-11-04 21:35:01 +010065 else
Bram Moolenaar5842a742017-11-04 22:36:53 +010066 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 Moolenaar110bd602018-09-16 18:46:59 +020072 let swapname = s:swapname()
Bram Moolenaar5842a742017-11-04 22:36:53 +010073 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 Moolenaar430dc5d2017-11-02 21:04:47 +010076
Bram Moolenaar5842a742017-11-04 22:36:53 +010077 bwipe!
78 endif
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +010079 endif
Bram Moolenaar5842a742017-11-04 22:36:53 +010080 finally
81 call delete('Xtest')
82 endtry
Bram Moolenaar430dc5d2017-11-02 21:04:47 +010083endfunc
Bram Moolenaar8c3169c2018-05-12 17:04:12 +020084
85func 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 Moolenaare3d06542019-01-27 14:29:24 +010096 " 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 Moolenaar8c3169c2018-05-12 17:04:12 +020099 set directory&
100 call delete('Xswapdir', 'rf')
101endfunc
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200102
103func Test_swapinfo()
104 new Xswapinfo
105 call setline(1, ['one', 'two', 'three'])
106 w
Bram Moolenaar110bd602018-09-16 18:46:59 +0200107 let fname = s:swapname()
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200108 call assert_match('Xswapinfo', fname)
Bram Moolenaarc216a7a2022-12-05 13:50:55 +0000109
Bram Moolenaar6cf31512022-12-05 15:01:05 +0000110 " Check the tail appears in the list from swapfilelist(). The path depends
111 " on the system.
112 let tail = fnamemodify(fname, ":t")->fnameescape()
Bram Moolenaarc216a7a2022-12-05 13:50:55 +0000113 let nr = 0
114 for name in swapfilelist()
Bram Moolenaar6cf31512022-12-05 15:01:05 +0000115 if name =~ tail .. '$'
Bram Moolenaarc216a7a2022-12-05 13:50:55 +0000116 let nr += 1
117 endif
118 endfor
Bram Moolenaar6cf31512022-12-05 15:01:05 +0000119 call assert_equal(1, nr, 'not found in ' .. string(swapfilelist()))
Bram Moolenaarc216a7a2022-12-05 13:50:55 +0000120
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200121 let info = fname->swapinfo()
Bram Moolenaar4c5765b2018-08-22 11:28:01 +0200122 let ver = printf('VIM %d.%d', v:version / 100, v:version % 100)
123 call assert_equal(ver, info.version)
124
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200125 call assert_match('\w', info.user)
Bram Moolenaar4c5765b2018-08-22 11:28:01 +0200126 " host name is truncated to 39 bytes in the swap file
127 call assert_equal(hostname()[:38], info.host)
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200128 call assert_match('Xswapinfo', info.fname)
Bram Moolenaar47ad5652018-08-21 21:09:07 +0200129 call assert_match(0, info.dirty)
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200130 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 Moolenaar56564962022-10-10 22:39:42 +0100142 call writefile(['burp'], 'Xnotaswapfile', 'D')
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200143 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 Moolenaar47ad5652018-08-21 21:09:07 +0200149 call assert_equal('Not a swap file', info.error)
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200150endfunc
Bram Moolenaar110bd602018-09-16 18:46:59 +0200151
152func 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 Moolenaarf6ed61e2019-09-07 19:05:09 +0200161 call assert_equal(expected, buf->swapname())
Bram Moolenaar110bd602018-09-16 18:46:59 +0200162
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')
171endfunc
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200172
173func 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 Moolenaar56564962022-10-10 22:39:42 +0100197 call writefile(swapfile_bytes, swapfile_name, 'D')
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200198 let s:swap_choice = 'e'
199 let s:swapname = ''
200 split XswapfileText
201 quit
Bram Moolenaar701df4e2019-04-28 23:07:18 +0200202 call assert_equal(fnamemodify(swapfile_name, ':t'), fnamemodify(s:swapname, ':t'))
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200203
Bram Moolenaar07282f02019-10-10 16:46:17 +0200204 " 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 Moolenaar6738fd22021-06-23 21:44:06 +0200207 " deleted. Process 0x3fffffff most likely does not exist.
208 let swapfile_bytes[24:27] = 0zffffff3f
Bram Moolenaar07282f02019-10-10 16:46:17 +0200209 call writefile(swapfile_bytes, swapfile_name)
210 let s:swapname = ''
211 split XswapfileText
212 quit
213 call assert_equal('', s:swapname)
214 endif
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200215
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 Moolenaar701df4e2019-04-28 23:07:18 +0200222 call assert_equal(fnamemodify(swapfile_name, ':t'), fnamemodify(s:swapname, ':t'))
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200223
224 call delete('XswapfileText')
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200225 augroup test_swapfile_delete
226 autocmd!
227 augroup END
228 augroup! test_swapfile_delete
229endfunc
Bram Moolenaar99499b12019-05-23 21:35:48 +0200230
231func 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 Moolenaar56564962022-10-10 22:39:42 +0100238 call mkdir('Xswap', 'R')
Bram Moolenaar99499b12019-05-23 21:35:48 +0200239 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 Moolenaar56564962022-10-10 22:39:42 +0100251 call writefile(swapfile_bytes, swapfile_name, 'D')
Bram Moolenaar99499b12019-05-23 21:35:48 +0200252 " 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 Moolenaar99499b12019-05-23 21:35:48 +0200263 unlet $Xswap
264 set dir&
265 augroup test_swap_recover
266 autocmd!
267 augroup END
268 augroup! test_swap_recover
269endfunc
270
271func 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 Moolenaar99499b12019-05-23 21:35:48 +0200278 " 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 Moolenaar56564962022-10-10 22:39:42 +0100290 call writefile(swapfile_bytes, swapfile_name, 'D')
Bram Moolenaar99499b12019-05-23 21:35:48 +0200291 " 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 Moolenaar99499b12019-05-23 21:35:48 +0200303 augroup test_swap_recover_ext
304 autocmd!
305 augroup END
306 augroup! test_swap_recover_ext
307endfunc
Bram Moolenaar406cd902020-02-18 21:54:41 +0100308
309" Test for closing a split window automatically when a swap file is detected
310" and 'Q' is selected in the confirmation prompt.
311func 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 Moolenaar56564962022-10-10 22:39:42 +0100330 call writefile(swapfile_bytes, swapfile_name, 'D')
Bram Moolenaar406cd902020-02-18 21:54:41 +0100331 " 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 Moolenaar406cd902020-02-18 21:54:41 +0100341
342 augroup test_swap_splitwin
343 autocmd!
344 augroup END
345 augroup! test_swap_splitwin
346endfunc
347
Bram Moolenaarb6541032020-02-22 21:21:27 +0100348" Test for selecting 'q' in the attention prompt
349func Test_swap_prompt_splitwin()
Bram Moolenaard36ef572020-03-24 21:44:51 +0100350 CheckRunVimInTerminal
351
Bram Moolenaar56564962022-10-10 22:39:42 +0100352 call writefile(['foo bar'], 'Xfile1', 'D')
Bram Moolenaarb6541032020-02-22 21:21:27 +0100353 edit Xfile1
Bram Moolenaard36ef572020-03-24 21:44:51 +0100354 preserve " should help to make sure the swap file exists
355
Bram Moolenaarb6541032020-02-22 21:21:27 +0100356 let buf = RunVimInTerminal('', {'rows': 20})
357 call term_sendkeys(buf, ":set nomore\n")
358 call term_sendkeys(buf, ":set noruler\n")
Bram Moolenaar1d97efc2021-07-04 13:27:11 +0200359
Bram Moolenaarb6541032020-02-22 21:21:27 +0100360 call term_sendkeys(buf, ":split Xfile1\n")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200361 call TermWait(buf)
Bram Moolenaarb6541032020-02-22 21:21:27 +0100362 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 Moolenaar6a2c5a72020-04-08 21:50:25 +0200364 call TermWait(buf)
Bram Moolenaard36ef572020-03-24 21:44:51 +0100365 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb6541032020-02-22 21:21:27 +0100366 call WaitForAssert({-> assert_match('^:$', term_getline(buf, 20))})
Bram Moolenaard36ef572020-03-24 21:44:51 +0100367 call term_sendkeys(buf, ":echomsg winnr('$')\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200368 call TermWait(buf)
Bram Moolenaarb6541032020-02-22 21:21:27 +0100369 call WaitForAssert({-> assert_match('^1$', term_getline(buf, 20))})
370 call StopVimInTerminal(buf)
Bram Moolenaar1d97efc2021-07-04 13:27:11 +0200371
Bram Moolenaar3777d6e2021-07-04 17:23:25 +0200372 " 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 Moolenaar1d97efc2021-07-04 13:27:11 +0200378 call term_sendkeys(buf, "q")
Bram Moolenaar3777d6e2021-07-04 17:23:25 +0200379 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 Moolenaar1d97efc2021-07-04 13:27:11 +0200385
Bram Moolenaarb6541032020-02-22 21:21:27 +0100386 %bwipe!
Bram Moolenaarb6541032020-02-22 21:21:27 +0100387endfunc
388
Bram Moolenaar5966ea12020-07-15 15:30:05 +0200389func Test_swap_symlink()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200390 CheckUnix
Bram Moolenaar5966ea12020-07-15 15:30:05 +0200391
Bram Moolenaar56564962022-10-10 22:39:42 +0100392 call writefile(['text'], 'Xtestfile', 'D')
Bram Moolenaar5966ea12020-07-15 15:30:05 +0200393 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 Moolenaar56564962022-10-10 22:39:42 +0100403 call mkdir('Xswapdir', 'R')
Bram Moolenaar5966ea12020-07-15 15:30:05 +0200404 exe 'set dir=' . getcwd() . '/Xswapdir//'
405
406 " Check that this also works when 'directory' ends with '//'
407 edit Xtestlink
Bram Moolenaar8b0e62c2021-10-19 22:12:25 +0100408 call assert_match('Xswapdir[/\\]%.*testdir%Xtestfile\.swp$', s:swapname())
Bram Moolenaar5966ea12020-07-15 15:30:05 +0200409 bwipe!
410
411 set dir&
Bram Moolenaar5966ea12020-07-15 15:30:05 +0200412 call delete('Xtestlink')
Bram Moolenaar5966ea12020-07-15 15:30:05 +0200413endfunc
414
Bram Moolenaar5ee09812020-11-25 12:43:28 +0100415func 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')
Milly4f5681d2024-10-20 11:06:00 +0200419 let cmd = 'cmd /D /c echo'
Bram Moolenaar5ee09812020-11-25 12:43:28 +0100420 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
433endfunc
434
435func s:blob_to_pid(b)
436 return a:b[3] * 16777216 + a:b[2] * 65536 + a:b[1] * 256 + a:b[0]
437endfunc
438
439func 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
446endfunc
447
Bram Moolenaarf8835082020-11-09 21:04:17 +0100448func 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 Moolenaar5ee09812020-11-25 12:43:28 +0100461 " 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 Moolenaar56564962022-10-10 22:39:42 +0100464 call writefile(swapfile_bytes, swapfile_name, 'D')
Bram Moolenaarf8835082020-11-09 21:04:17 +0100465 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 Moolenaarc6ca9f32020-11-19 18:57:23 +0100479 let swapfile_bytes[28 + 40] = swapfile_bytes[28 + 40] + 2
Bram Moolenaarf8835082020-11-09 21:04:17 +0100480 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 Moolenaarf8835082020-11-09 21:04:17 +0100488 augroup test_swap_recover_ext
489 autocmd!
490 augroup END
491 augroup! test_swap_recover_ext
492endfunc
493
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200494" Test for renaming a buffer when the swap file is deleted out-of-band
495func Test_missing_swap_file()
496 CheckUnix
Bram Moolenaarf33cae62021-07-04 17:36:54 +0200497 new Xfile2
Yegappan Lakshmanan99285552021-06-06 17:12:46 +0200498 call delete(swapname(''))
Bram Moolenaarf33cae62021-07-04 17:36:54 +0200499 call assert_fails('file Xfile3', 'E301:')
500 call assert_equal('Xfile3', bufname())
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200501 call assert_true(bufexists('Xfile2'))
Bram Moolenaarf33cae62021-07-04 17:36:54 +0200502 call assert_true(bufexists('Xfile3'))
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200503 %bw!
504endfunc
505
506" Test for :preserve command
507func Test_preserve()
Bram Moolenaarf33cae62021-07-04 17:36:54 +0200508 new Xfile4
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200509 setlocal noswapfile
510 call assert_fails('preserve', 'E313:')
511 bw!
512endfunc
513
514" Test for the v:swapchoice variable
515func Test_swapchoice()
Bram Moolenaar56564962022-10-10 22:39:42 +0100516 call writefile(['aaa', 'bbb'], 'Xfile5', 'D')
Bram Moolenaarf33cae62021-07-04 17:36:54 +0200517 edit Xfile5
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200518 preserve
519 let swapfname = swapname('')
520 let b = readblob(swapfname)
521 bw!
Bram Moolenaar56564962022-10-10 22:39:42 +0100522 call writefile(b, swapfname, 'D')
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200523
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 Moolenaarf33cae62021-07-04 17:36:54 +0200531 edit Xfile5
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200532 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 Moolenaarf33cae62021-07-04 17:36:54 +0200543 edit Xfile5
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200544 catch /^Vim:Interrupt$/
545 endtry
546 call assert_equal('', @%)
Bram Moolenaarf33cae62021-07-04 17:36:54 +0200547 call assert_true(bufexists('Xfile5'))
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200548 %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 Moolenaarf33cae62021-07-04 17:36:54 +0200556 edit Xfile5
557 call assert_equal('Xfile5', @%)
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200558 %bw!
559 call assert_false(filereadable(swapfname))
560
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200561 call delete(swapfname)
562 augroup test_swapchoice
563 autocmd!
564 augroup END
565 augroup! test_swapchoice
566endfunc
567
Dominique Pellefe3418a2021-07-10 17:59:48 +0200568func Test_no_swap_file()
569 call assert_equal("\nNo swap file", execute('swapname'))
570endfunc
571
Bram Moolenaar406cd902020-02-18 21:54:41 +0100572" vim: shiftwidth=2 sts=2 expandtab