helpers.lua 1.8 KB

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