testutil.lua (1916B)
1 local t = require('test.testutil') 2 local n = require('test.functional.testnvim')() 3 4 local api = n.api 5 local write_file = t.write_file 6 local concat_tables = t.concat_tables 7 8 local tmpname = t.tmpname() 9 10 -- o={ 11 -- args=…, 12 -- args_rm=…, 13 -- shadafile=…, 14 -- } 15 local function reset(o) 16 assert(o == nil or type(o) == 'table' or type(o) == 'string') 17 o = o and o or {} 18 local args_rm = o.args_rm or {} 19 table.insert(args_rm, '-i') 20 local args = { 21 '-i', 22 o.shadafile or tmpname, 23 } 24 if type(o) == 'string' then 25 args = concat_tables(args, { '--cmd', o }) 26 elseif o.args then 27 args = concat_tables(args, o.args) 28 end 29 n.clear { 30 args_rm = args_rm, 31 args = args, 32 } 33 api.nvim_set_var('tmpname', tmpname) 34 end 35 36 local clear = function() 37 n.expect_exit(n.command, 'qall!') 38 os.remove(tmpname) 39 end 40 41 local get_shada_rw = function(fname) 42 local wshada = function(text) 43 write_file(fname, text, true) 44 end 45 local sdrcmd = function(bang) 46 return 'rshada' .. (bang and '!' or '') .. ' ' .. fname 47 end 48 local clean = function() 49 os.remove(fname) 50 local i = ('a'):byte() 51 while i <= ('z'):byte() do 52 if not os.remove(fname .. ('.tmp.%c'):format(i)) then 53 break 54 end 55 i = i + 1 56 end 57 end 58 return wshada, sdrcmd, fname, clean 59 end 60 61 local mpack_keys = { 'type', 'timestamp', 'length', 'value' } 62 63 local read_shada_file = function(fname) 64 local fd = io.open(fname, 'r') 65 local mstring = fd:read('*a') 66 fd:close() 67 local unpack = vim.mpack.Unpacker() 68 local ret = {} 69 local cur, val 70 local i = 0 71 local off = 1 72 while off <= #mstring do 73 val, off = unpack(mstring, off) 74 if i % 4 == 0 then 75 cur = {} 76 ret[#ret + 1] = cur 77 end 78 cur[mpack_keys[(i % 4) + 1]] = val 79 i = i + 1 80 end 81 return ret 82 end 83 84 return { 85 reset = reset, 86 clear = clear, 87 get_shada_rw = get_shada_rw, 88 read_shada_file = read_shada_file, 89 }