aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/functional/ex_cmds/recover_spec.lua71
-rw-r--r--test/functional/helpers.lua11
-rw-r--r--test/functional/legacy/078_swapfile_recover_spec.lua80
3 files changed, 161 insertions, 1 deletions
diff --git a/test/functional/ex_cmds/recover_spec.lua b/test/functional/ex_cmds/recover_spec.lua
new file mode 100644
index 0000000000..d4c9477133
--- /dev/null
+++ b/test/functional/ex_cmds/recover_spec.lua
@@ -0,0 +1,71 @@
+-- Tests for :recover
+
+local helpers = require('test.functional.helpers')
+local execute, eq, clear, eval, feed, expect, source =
+ helpers.execute, helpers.eq, helpers.clear, helpers.eval, helpers.feed,
+ helpers.expect, helpers.source
+
+describe(':recover', function()
+ before_each(clear)
+
+ it('fails if given a non-existent swapfile', function()
+ local swapname = 'bogus-swapfile'
+ execute('recover '..swapname) -- This should not segfault. #2117
+ eq('E305: No swap file found for '..swapname, eval('v:errmsg'))
+ end)
+
+end)
+
+describe(':preserve', function()
+ local swapdir = lfs.currentdir()..'/testdir_recover_spec'
+ before_each(function()
+ clear()
+ os.remove(swapdir)
+ lfs.mkdir(swapdir)
+ end)
+ after_each(function()
+ os.remove(swapdir)
+ end)
+
+ it("saves to custom 'directory' and (R)ecovers (issue #1836)", function()
+ local testfile = 'testfile_recover_spec'
+ local init = [[
+ set swapfile fileformat=unix undolevels=-1
+ set directory^=]]..swapdir..[[//
+ ]]
+
+ source(init)
+ execute('set swapfile fileformat=unix undolevels=-1')
+ -- Put swapdir at the start of the 'directory' list. #1836
+ execute('set directory^='..swapdir..'//')
+ execute('edit '..testfile)
+ feed('isometext<esc>')
+ execute('preserve')
+ source('redir => g:swapname | swapname | redir END')
+
+ local swappath1 = eval('g:swapname')
+
+ --TODO(justinmk): this is an ugly hack to force `helpers` to support
+ --multiple sessions.
+ local nvim2 = helpers.spawn({helpers.nvim_prog, '-u', 'NONE', '--embed'})
+ helpers.set_session(nvim2)
+
+ source(init)
+
+ -- Use the "SwapExists" event to choose the (R)ecover choice at the dialog.
+ execute('autocmd SwapExists * let v:swapchoice = "r"')
+ execute('silent edit '..testfile)
+ source('redir => g:swapname | swapname | redir END')
+
+ local swappath2 = eval('g:swapname')
+
+ -- swapfile from session 1 should end in .swp
+ assert(testfile..'.swp' == string.match(swappath1, '[^%%]+$'))
+
+ -- swapfile from session 2 should end in .swo
+ assert(testfile..'.swo' == string.match(swappath2, '[^%%]+$'))
+
+ expect('sometext')
+ end)
+
+end)
diff --git a/test/functional/helpers.lua b/test/functional/helpers.lua
index 393b42dda5..27c94c34a8 100644
--- a/test/functional/helpers.lua
+++ b/test/functional/helpers.lua
@@ -18,6 +18,10 @@ if nvim_dir == nvim_prog then
nvim_dir = "."
end
+-- Nvim "Unit Under Test" http://en.wikipedia.org/wiki/Device_under_test
+local NvimUUT = {}
+NvimUUT.__index = NvimUUT
+
local prepend_argv
if os.getenv('VALGRIND') then
@@ -49,6 +53,10 @@ end
local session, loop_running, loop_stopped, last_error
+local function set_session(s)
+ session = s
+end
+
local function request(method, ...)
local status, rv = session:request(method, ...)
if not status then
@@ -305,5 +313,6 @@ return {
curwin = curwin,
curtab = curtab,
curbuf_contents = curbuf_contents,
- wait = wait
+ wait = wait,
+ set_session = set_session
}
diff --git a/test/functional/legacy/078_swapfile_recover_spec.lua b/test/functional/legacy/078_swapfile_recover_spec.lua
new file mode 100644
index 0000000000..e250c91441
--- /dev/null
+++ b/test/functional/legacy/078_swapfile_recover_spec.lua
@@ -0,0 +1,80 @@
+-- Inserts 10000 lines with text to fill the swap file with two levels of
+-- pointer blocks. Then recovers from the swap file and checks all text is
+-- restored. We need about 10000 lines of 100 characters to get two levels of
+-- pointer blocks.
+
+local helpers = require('test.functional.helpers')
+local feed, insert, source = helpers.feed, helpers.insert, helpers.source
+local clear, execute, expect, source = helpers.clear, helpers.execute, helpers.expect, helpers.source
+local eval = helpers.eval
+
+describe('78', function()
+ setup(clear)
+ teardown(function()
+ os.remove(".Xtest.swp")
+ os.remove(".Xtest.swo")
+ end)
+
+ it('is working', function()
+ source([=[
+ set swapfile fileformat=unix undolevels=-1
+ e! Xtest
+ let text = "\tabcdefghijklmnoparstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnoparstuvwxyz0123456789"
+ let i = 1
+ let linecount = 10000
+ while i <= linecount | call append(i - 1, i . text) | let i += 1 | endwhile
+ preserve
+
+ " Get the name of the swap file, and clean up the :redir capture.
+ redir => g:swapname | swapname | redir END
+ let g:swapname = substitute(g:swapname, '[[:blank:][:cntrl:]]*\(.\{-}\)[[:blank:][:cntrl:]]*$', '\1', 'g')
+ let g:swapname = fnameescape(g:swapname)
+
+ " Make a copy of the swap file in Xswap
+ set bin
+ exe 'sp ' . g:swapname
+ w! Xswap
+
+ set nobin
+ new
+ only!
+ bwipe! Xtest
+ call rename('Xswap', g:swapname)
+
+ "TODO(jkeyes): without 'silent', this hangs the test " at message:
+ " 'Recovery completed. You should check if everything is OK.'
+ silent recover Xtest
+
+ call delete(g:swapname)
+ new
+ call append(0, 'recovery start')
+ wincmd w
+
+ let g:linedollar = line('$')
+ if g:linedollar < linecount
+ wincmd w
+ call append(line('$'), "expected " . linecount
+ \ . " lines but found only " . g:linedollar)
+ wincmd w
+ let linecount = g:linedollar
+ endif
+
+ let i = 1
+ while i <= linecount
+ if getline(i) != i . text
+ exe 'wincmd w'
+ call append(line('$'), i . ' differs')
+ exe 'wincmd w'
+ endif
+ let i += 1
+ endwhile
+ q!
+ call append(line('$'), 'recovery end')
+ ]=])
+
+ expect([[
+ recovery start
+
+ recovery end]])
+ end)
+end)