aboutsummaryrefslogtreecommitdiff
path: root/test/functional/shada/helpers.lua
blob: 180b6f8c443dfcbc5d2b35cbf4b92a2f530d6e7b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
local helpers = require('test.functional.helpers')
local spawn, set_session, nvim, nvim_prog, nvim_command, nvim_eval =
  helpers.spawn, helpers.set_session, helpers.nvim, helpers.nvim_prog,
  helpers.command, helpers.eval
local write_file = helpers.write_file

local tmpname = os.tmpname()
local additional_cmd = ''

local function nvim_argv()
  local ret
  local nvim_argv = {nvim_prog, '-u', 'NONE', '-i', tmpname, '-N',
                     '--cmd', 'set shortmess+=I background=light noswapfile',
                     '--cmd', additional_cmd,
                     '--embed'}
  if helpers.prepend_argv then
    ret = {}
    for i, v in ipairs(helpers.prepend_argv) do
      ret[i] = v
    end
    local shift = #ret
    for i, v in ipairs(nvim_argv) do
      ret[i + shift] = v
    end
  else
    ret = nvim_argv
  end
  return ret
end

local session = nil

local reset = function()
  if session then
    session:exit(0)
  end
  session = spawn(nvim_argv())
  set_session(session)
  nvim('set_var', 'tmpname', tmpname)
end

local set_additional_cmd = function(s)
  additional_cmd = s
end

local clear = function()
  os.remove(tmpname)
  set_additional_cmd('')
end

local exc_exec = function(cmd)
  nvim_command(([[
    try
      execute "%s"
    catch
      let g:__exception = v:exception
    endtry
  ]]):format(cmd:gsub('\n', '\\n'):gsub('[\\"]', '\\%0')))
  local ret = nvim_eval('get(g:, "__exception", 0)')
  nvim_command('unlet! g:__exception')
  return ret
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
  return wshada, sdrcmd, fname
end

return {
  reset=reset,
  set_additional_cmd=set_additional_cmd,
  clear=clear,
  exc_exec=exc_exec,
  get_shada_rw=get_shada_rw,
}