aboutsummaryrefslogtreecommitdiff
path: root/test/functional/shada/testutil.lua
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2024-05-24 19:18:11 +0000
committerJosh Rahm <joshuarahm@gmail.com>2024-05-24 19:18:11 +0000
commitff7ed8f586589d620a806c3758fac4a47a8e7e15 (patch)
tree729bbcb92231538fa61dab6c3d890b025484b7f5 /test/functional/shada/testutil.lua
parent376914f419eb08fdf4c1a63a77e1f035898a0f10 (diff)
parent28c04948a1c887a1cc0cb64de79fa32631700466 (diff)
downloadrneovim-ff7ed8f586589d620a806c3758fac4a47a8e7e15.tar.gz
rneovim-ff7ed8f586589d620a806c3758fac4a47a8e7e15.tar.bz2
rneovim-ff7ed8f586589d620a806c3758fac4a47a8e7e15.zip
Merge remote-tracking branch 'upstream/master' into mix_20240309
Diffstat (limited to 'test/functional/shada/testutil.lua')
-rw-r--r--test/functional/shada/testutil.lua89
1 files changed, 89 insertions, 0 deletions
diff --git a/test/functional/shada/testutil.lua b/test/functional/shada/testutil.lua
new file mode 100644
index 0000000000..d9252af5f0
--- /dev/null
+++ b/test/functional/shada/testutil.lua
@@ -0,0 +1,89 @@
+local t = require('test.testutil')
+local n = require('test.functional.testnvim')()
+
+local api = n.api
+local write_file = t.write_file
+local concat_tables = t.concat_tables
+
+local tmpname = t.tmpname()
+
+-- o={
+-- args=…,
+-- args_rm=…,
+-- shadafile=…,
+-- }
+local function reset(o)
+ assert(o == nil or type(o) == 'table' or type(o) == 'string')
+ o = o and o or {}
+ local args_rm = o.args_rm or {}
+ table.insert(args_rm, '-i')
+ local args = {
+ '-i',
+ o.shadafile or tmpname,
+ }
+ if type(o) == 'string' then
+ args = concat_tables(args, { '--cmd', o })
+ elseif o.args then
+ args = concat_tables(args, o.args)
+ end
+ n.clear {
+ args_rm = args_rm,
+ args = args,
+ }
+ api.nvim_set_var('tmpname', tmpname)
+end
+
+local clear = function()
+ n.expect_exit(n.command, 'qall!')
+ os.remove(tmpname)
+end
+
+local get_shada_rw = function(fname)
+ local wshada = function(text)
+ write_file(fname, text, true)
+ end
+ local sdrcmd = function(bang)
+ return 'rshada' .. (bang and '!' or '') .. ' ' .. fname
+ end
+ local clean = function()
+ os.remove(fname)
+ local i = ('a'):byte()
+ while i <= ('z'):byte() do
+ if not os.remove(fname .. ('.tmp.%c'):format(i)) then
+ break
+ end
+ i = i + 1
+ end
+ end
+ return wshada, sdrcmd, fname, clean
+end
+
+local mpack_keys = { 'type', 'timestamp', 'length', 'value' }
+
+local read_shada_file = function(fname)
+ local fd = io.open(fname, 'r')
+ local mstring = fd:read('*a')
+ fd:close()
+ local unpack = vim.mpack.Unpacker()
+ local ret = {}
+ local cur, val
+ local i = 0
+ local off = 1
+ while off <= #mstring do
+ val, off = unpack(mstring, off)
+ if i % 4 == 0 then
+ cur = {}
+ ret[#ret + 1] = cur
+ end
+ cur[mpack_keys[(i % 4) + 1]] = val
+ i = i + 1
+ end
+ return ret
+end
+
+return {
+ reset = reset,
+ clear = clear,
+ get_shada_rw = get_shada_rw,
+ read_shada_file = read_shada_file,
+}