robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
11.7 KiB326 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4
5local describe, it, before_each = t.describe, t.it, t.before_each
6local clear = n.clear
7local eq = t.eq
8local eval = n.eval
9local command = n.command
10local pcall_err = t.pcall_err
11local feed = n.feed
12local poke_eventloop = n.poke_eventloop
13local is_os = t.is_os
14local api = n.api
15local async_meths = n.async_meths
16local testprg = n.testprg
17local assert_alive = n.assert_alive
18
19describe('terminal channel is closed and later released if', function()
20  local screen
21
22  before_each(function()
23    clear()
24    screen = Screen.new()
25  end)
26
27  it('opened by nvim_open_term() and deleted by :bdelete!', function()
28    command([[let id = nvim_open_term(0, {})]])
29    local chans = eval('len(nvim_list_chans())')
30    -- Channel hasn't been released yet.
31    eq(
32      "Vim(call):Can't send data to closed stream",
33      pcall_err(command, [[bdelete! | call chansend(id, 'test')]])
34    )
35    -- Channel has been released after processing free_channel_event().
36    eq(chans - 1, eval('len(nvim_list_chans())'))
37
38    command('autocmd BufWipeout * ++once let id2 = nvim_open_term(str2nr(expand("<abuf>")), {})')
39    -- Channel hasn't been released yet.
40    eq(
41      "Vim(call):Can't send data to closed stream",
42      pcall_err(command, [[bdelete! | call chansend(id2, 'test')]])
43    )
44    -- Channel has been released after processing free_channel_event().
45    eq(chans - 1, eval('len(nvim_list_chans())'))
46  end)
47
48  it('opened by nvim_open_term(), closed by chanclose(), and deleted by pressing a key', function()
49    command('let id = nvim_open_term(0, {})')
50    local chans = eval('len(nvim_list_chans())')
51    -- Channel has been closed but not released.
52    eq(
53      "Vim(call):Can't send data to closed stream",
54      pcall_err(command, [[call chanclose(id) | call chansend(id, 'test')]])
55    )
56    screen:expect({ any = '%[Terminal closed%]' })
57    eq(chans, eval('len(nvim_list_chans())'))
58    feed('i<CR>') -- Delete terminal.
59    poke_eventloop() -- Process pending input.
60    -- Channel has been released after processing free_channel_event().
61    eq(chans - 1, eval('len(nvim_list_chans())'))
62  end)
63
64  it('opened by nvim_open_term(), closed by chanclose(), and deleted by :bdelete', function()
65    command('let id = nvim_open_term(0, {})')
66    local chans = eval('len(nvim_list_chans())')
67    -- Channel has been closed but not released.
68    eq(
69      "Vim(call):Can't send data to closed stream",
70      pcall_err(command, [[call chanclose(id) | call chansend(id, 'test')]])
71    )
72    screen:expect({ any = '%[Terminal closed%]' })
73    eq(chans, eval('len(nvim_list_chans())'))
74    -- Channel still hasn't been released yet.
75    eq(
76      "Vim(call):Can't send data to closed stream",
77      pcall_err(command, [[bdelete | call chansend(id, 'test')]])
78    )
79    -- Channel has been released after processing free_channel_event().
80    eq(chans - 1, eval('len(nvim_list_chans())'))
81  end)
82
83  it('opened by jobstart(…,{term=true}), exited, and deleted by pressing a key', function()
84    command([[let id = jobstart('echo',{'term':v:true})]])
85    local chans = eval('len(nvim_list_chans())')
86    -- Wait for process to exit.
87    screen:expect({ any = '%[Process exited 0%]' })
88    -- Process has exited but channel has't been released.
89    eq(
90      "Vim(call):Can't send data to closed stream",
91      pcall_err(command, [[call chansend(id, 'test')]])
92    )
93    eq(chans, eval('len(nvim_list_chans())'))
94    feed('i<CR>') -- Delete terminal.
95    poke_eventloop() -- Process pending input.
96    poke_eventloop() -- Process term_delayed_free().
97    -- Channel has been released after processing free_channel_event().
98    eq(chans - 1, eval('len(nvim_list_chans())'))
99  end)
100
101  -- This indirectly covers #16264
102  it('opened by jobstart(…,{term=true}), exited, and deleted by :bdelete', function()
103    command([[let id = jobstart('echo', {'term':v:true})]])
104    local chans = eval('len(nvim_list_chans())')
105    -- Wait for process to exit.
106    screen:expect({ any = '%[Process exited 0%]' })
107    -- Process has exited but channel hasn't been released.
108    eq(
109      "Vim(call):Can't send data to closed stream",
110      pcall_err(command, [[call chansend(id, 'test')]])
111    )
112    eq(chans, eval('len(nvim_list_chans())'))
113    -- Channel still hasn't been released yet.
114    eq(
115      "Vim(call):Can't send data to closed stream",
116      pcall_err(command, [[bdelete | call chansend(id, 'test')]])
117    )
118    poke_eventloop() -- Process term_delayed_free().
119    -- Channel has been released after processing free_channel_event().
120    eq(chans - 1, eval('len(nvim_list_chans())'))
121  end)
122end)
123
124it('chansend sends lines to terminal channel in proper order', function()
125  clear({ args = { '--cmd', 'set laststatus=2' } })
126  local screen = Screen.new(100, 20)
127  screen._default_attr_ids = nil
128  local shells = is_os('win') and { 'cmd.exe', 'pwsh.exe -nop' } or { 'sh' }
129  -- Prompt which indicates the shell is ready to read.
130  local prompt = is_os('win') and '>' or '%$ '
131  for _, sh in ipairs(shells) do
132    command([[let id = jobstart(']] .. sh .. [[', {'term':v:true})]])
133    screen:expect({ any = prompt, attr_ids = {} })
134    command([[call chansend(id, ['echo "hello"', 'echo "world"', ''])]])
135    -- With PowerShell the command may be highlighted, so specify attr_ids = {}.
136    screen:expect({ any = [[echo "hello".*echo "world"]], attr_ids = {} })
137    command('bdelete!')
138    screen:expect({ any = '%[No Name%]' })
139  end
140end)
141
142--- @param event string
143--- @param extra_tests fun(table, table)?
144local function test_autocmd_no_crash(event, extra_tests)
145  local env = {}
146  -- Use REPFAST for immediately output after start.
147  local term_args = { testprg('shell-test'), 'REPFAST', '50', 'TEST' }
148
149  before_each(function()
150    clear()
151    env.screen = Screen.new(60, 4)
152    command([[file Xoldbuf | call setline(1, 'OLDBUF') | enew]])
153    -- Wait before :bwipe to avoid closing PTY master before the child calls setsid(),
154    -- as that will cause SIGHUP to be also sent to the parent.
155    -- Use vim.uv.sleep() which blocks the event loop.
156    n.exec([[
157      func Wipe()
158        lua vim.uv.sleep(5)
159        bwipe!
160      endfunc
161    ]])
162  end)
163
164  local input_prompt_screen = [[
165                                                                |
166    {1:~                                                           }|*2
167    ^                                                            |
168  ]]
169  local oldbuf_screen = [[
170    ^OLDBUF                                                      |
171    {1:~                                                           }|*2
172                                                                |
173  ]]
174
175  it('processes job exit event when using jobstart(…,{term=true})', function()
176    api.nvim_create_autocmd(event, { command = "call input('')" })
177    async_meths.nvim_call_function('jobstart', { term_args, { term = true } })
178    env.screen:expect(input_prompt_screen)
179    vim.uv.sleep(20)
180    feed('<CR>')
181    env.screen:expect([[
182      ^0: TEST                                                     |
183      1: TEST                                                     |
184      2: TEST                                                     |
185                                                                  |
186    ]])
187    feed('i')
188    env.screen:expect([[
189      48: TEST                                                    |
190      49: TEST                                                    |
191      ^[Process exited 0]                                          |
192      {5:-- TERMINAL --}                                              |
193    ]])
194    feed('<CR>')
195    env.screen:expect(oldbuf_screen)
196    assert_alive()
197  end)
198
199  it('wipes buffer and processes events when using jobstart(…,{term=true})', function()
200    api.nvim_create_autocmd(event, { command = "call Wipe() | call input('')" })
201    async_meths.nvim_call_function('jobstart', { term_args, { term = true } })
202    env.screen:expect(input_prompt_screen)
203    vim.uv.sleep(20)
204    feed('<CR>')
205    env.screen:expect(oldbuf_screen)
206    assert_alive()
207    eq('Xoldbuf', eval('bufname()'))
208    eq(0, eval([[exists('b:term_title')]]))
209  end)
210
211  it('processes :bwipe from TermClose when using jobstart(…,{term=true})', function()
212    local term_buf = api.nvim_get_current_buf()
213    api.nvim_create_autocmd('TermClose', { command = ('bwipe! %d'):format(term_buf) })
214    api.nvim_create_autocmd(event, { command = "call input('')", nested = true })
215    async_meths.nvim_call_function('jobstart', { term_args, { term = true } })
216    env.screen:expect(input_prompt_screen)
217    vim.uv.sleep(20)
218    feed('<CR>')
219    env.screen:expect(oldbuf_screen)
220    assert_alive()
221    eq('Xoldbuf', eval('bufname()'))
222    eq(0, eval([[exists('b:term_title')]]))
223  end)
224
225  it('only wipes buffer when using jobstart(…,{term=true})', function()
226    api.nvim_create_autocmd(event, { command = 'call Wipe()' })
227    async_meths.nvim_call_function('jobstart', { term_args, { term = true } })
228    env.screen:expect(oldbuf_screen)
229    assert_alive()
230    eq('Xoldbuf', eval('bufname()'))
231    eq(0, eval([[exists('b:term_title')]]))
232  end)
233
234  if extra_tests then
235    extra_tests(env, term_args)
236  end
237end
238
239describe('no crash when TermOpen autocommand', function()
240  test_autocmd_no_crash('TermOpen', function(env, term_args)
241    it('wipes buffer and processes events when using nvim_open_term()', function()
242      api.nvim_create_autocmd('TermOpen', { command = "call Wipe() | call input('')" })
243      async_meths.nvim_open_term(0, {})
244      env.screen:expect([[
245                                                                    |
246        {1:~                                                           }|*2
247        ^                                                            |
248      ]])
249      feed('<CR>')
250      env.screen:expect([[
251        ^OLDBUF                                                      |
252        {1:~                                                           }|*2
253                                                                    |
254      ]])
255      assert_alive()
256    end)
257
258    it('wipes buffer when using jobstart(…,{term=true}) during Nvim exit', function()
259      n.expect_exit(n.exec_lua, function()
260        vim.schedule(function()
261          vim.fn.jobstart(term_args, { term = true })
262        end)
263        vim.api.nvim_create_autocmd('TermOpen', { command = 'call Wipe()' })
264        vim.cmd('qall!')
265      end)
266    end)
267  end)
268end)
269
270describe('no crash when BufFilePre autocommand', function()
271  test_autocmd_no_crash('BufFilePre')
272end)
273
274describe('no crash when BufFilePost autocommand', function()
275  test_autocmd_no_crash('BufFilePost')
276end)
277
278describe('nvim_open_term', function()
279  local screen
280
281  before_each(function()
282    clear()
283    screen = Screen.new(8, 10)
284  end)
285
286  it('with force_crlf=true converts newlines', function()
287    local win = api.nvim_get_current_win()
288    local buf = api.nvim_create_buf(false, true)
289    local term = api.nvim_open_term(buf, { force_crlf = true })
290    api.nvim_win_set_buf(win, buf)
291    api.nvim_chan_send(term, 'here\nthere\nfoo\r\nbar\n\ntest')
292    screen:expect([[
293      ^here        |
294      there       |
295      foo         |
296      bar         |
297                  |
298      test        |
299                  |*4
300    ]])
301    api.nvim_chan_send(term, '\nfirst')
302    screen:expect([[
303      ^here        |
304      there       |
305      foo         |
306      bar         |
307                  |
308      test        |
309      first       |
310                  |*3
311    ]])
312  end)
313
314  it('with force_crlf=false does not convert newlines', function()
315    local win = api.nvim_get_current_win()
316    local buf = api.nvim_create_buf(false, true)
317    local term = api.nvim_open_term(buf, { force_crlf = false })
318    api.nvim_win_set_buf(win, buf)
319    api.nvim_chan_send(term, 'here\nthere')
320    screen:expect([[
321      ^here        |
322          there   |
323                  |*8
324    ]])
325  end)
326end)