test_swap.vim 16 KB

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