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