robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
5.0 KiB163 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each, after_each = t.describe, t.it, t.before_each, t.after_each
5local clear = n.clear
6local command = n.command
7local eq = t.eq
8local exec_capture = n.exec_capture
9local exec_lua = n.exec_lua
10local expect = n.expect
11local fn = n.fn
12local insert = n.insert
13local nvim_prog = n.nvim_prog
14local neq = t.neq
15local set_session = n.set_session
16local tmpname = t.tmpname
17local write_file = t.write_file
18
19describe('Remote', function()
20  local fname, other_fname
21  local contents = 'The call is coming from outside the process'
22  local other_contents = "A second file's contents"
23
24  before_each(function()
25    fname = tmpname() .. ' with spaces in the filename'
26    other_fname = tmpname()
27    write_file(fname, contents)
28    write_file(other_fname, other_contents)
29  end)
30
31  describe('connect to server and', function()
32    local server
33    before_each(function()
34      server = n.clear()
35    end)
36
37    after_each(function()
38      server:close()
39    end)
40
41    -- Run a `nvim --remote*` command and return { stdout, stderr } of the process
42    local function run_remote(...)
43      set_session(server)
44      local addr = fn.serverlist()[1]
45
46      -- Create an nvim instance just to run the remote-invoking nvim. We want
47      -- to wait for the remote instance to exit and calling jobwait blocks
48      -- the event loop. If the server event loop is blocked, it can't process
49      -- our incoming --remote calls.
50      local client_starter = n.new_session(true)
51      set_session(client_starter)
52      -- Call jobstart() and jobwait() in the same RPC request to reduce flakiness.
53      eq(
54        { 0 },
55        exec_lua(
56          [[return vim.fn.jobwait({ vim.fn.jobstart({...}, {
57        stdout_buffered = true,
58        stderr_buffered = true,
59        on_stdout = function(_, data, _)
60          _G.Remote_stdout = table.concat(data, '\n')
61        end,
62        on_stderr = function(_, data, _)
63          _G.Remote_stderr = table.concat(data, '\n')
64        end,
65      }) })]],
66          nvim_prog,
67          '--clean',
68          '--headless',
69          '--server',
70          addr,
71          ...
72        )
73      )
74      local res = exec_lua([[return { _G.Remote_stdout, _G.Remote_stderr }]])
75      client_starter:close()
76      set_session(server)
77      return res
78    end
79
80    it('edit a single file', function()
81      eq({ '', '' }, run_remote('--remote', fname))
82      expect(contents)
83      eq(1, #fn.getbufinfo())
84    end)
85
86    it('tab edit a single file with a non-changed buffer', function()
87      eq({ '', '' }, run_remote('--remote-tab', fname))
88      expect(contents)
89      eq(1, #fn.gettabinfo())
90    end)
91
92    it('tab edit a single file with a changed buffer', function()
93      insert('hello')
94      eq({ '', '' }, run_remote('--remote-tab', fname))
95      expect(contents)
96      eq(2, #fn.gettabinfo())
97    end)
98
99    it('edit multiple files', function()
100      eq({ '', '' }, run_remote('--remote', fname, other_fname))
101      expect(contents)
102      command('next')
103      expect(other_contents)
104      eq(2, #fn.getbufinfo())
105    end)
106
107    it('send keys', function()
108      eq({ '', '' }, run_remote('--remote-send', ':edit ' .. fname .. '<CR><C-W>v'))
109      expect(contents)
110      eq(2, #fn.getwininfo())
111      -- Only a single buffer as we're using edit and not drop like --remote does
112      eq(1, #fn.getbufinfo())
113    end)
114
115    it('evaluate expressions', function()
116      eq({ '0', '' }, run_remote('--remote-expr', 'setline(1, "Yo")'))
117      eq({ 'Yo', '' }, run_remote('--remote-expr', 'getline(1)'))
118      expect('Yo')
119      eq({ ('k'):rep(1234), '' }, run_remote('--remote-expr', 'repeat("k", 1234)'))
120      eq({ '1.25', '' }, run_remote('--remote-expr', '1.25'))
121      eq({ 'no', '' }, run_remote('--remote-expr', '0z6E6F'))
122      eq({ '\t', '' }, run_remote('--remote-expr', '"\t"'))
123    end)
124  end)
125
126  it('creates server if not found', function()
127    clear('--remote', fname)
128    expect(contents)
129    eq(1, #fn.getbufinfo())
130    -- Since we didn't pass silent, we should get a complaint
131    neq(nil, string.find(exec_capture('messages'), 'E247:'))
132  end)
133
134  it('creates server if not found with tabs', function()
135    clear('--remote-tab-silent', fname, other_fname)
136    expect(contents)
137    eq(2, #fn.gettabinfo())
138    eq(2, #fn.getbufinfo())
139    -- We passed silent, so no message should be issued about the server not being found
140    eq(nil, string.find(exec_capture('messages'), 'E247:'))
141  end)
142
143  describe('exits with error on', function()
144    local function run_and_check_exit_code(...)
145      local p = n.spawn_wait { args = { ... } }
146      eq(2, p.status)
147    end
148    it('bogus subcommand', function()
149      run_and_check_exit_code('--remote-bogus')
150    end)
151
152    it('send without server', function()
153      run_and_check_exit_code('--remote-send', 'i')
154    end)
155
156    it('expr without server', function()
157      run_and_check_exit_code('--remote-expr', 'setline(1, "Yo")')
158    end)
159    it('wait subcommand', function()
160      run_and_check_exit_code('--remote-wait', fname)
161    end)
162  end)
163end)