robin/laufey

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

Justin M. KeyesMerge #41224 from justinmk/fixbuilde16d577f16

master
12.9 KiB379 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, after_each, finally =
6  t.describe, t.it, t.before_each, t.after_each, t.finally
7local assert_alive = n.assert_alive
8local clear, poke_eventloop = n.clear, n.poke_eventloop
9local testprg, source, eq, neq = n.testprg, n.source, t.eq, t.neq
10local feed = n.feed
11local eval = n.eval
12local fn = n.fn
13local api = n.api
14local exec_lua = n.exec_lua
15local retry = t.retry
16local ok = t.ok
17local command = n.command
18local skip = t.skip
19local is_os = t.is_os
20
21describe(':terminal', function()
22  local screen
23
24  before_each(function()
25    clear()
26    screen = Screen.new(50, 4, { rgb = false })
27    screen._default_attr_ids = nil
28  end)
29
30  it('does not interrupt Press-ENTER prompt #2748', function()
31    -- Ensure that :messages shows Press-ENTER.
32    source([[
33      echomsg "msg1"
34      echomsg "msg2"
35      echomsg "msg3"
36    ]])
37    -- Invoke a command that emits frequent terminal activity.
38    feed([[:terminal "]] .. testprg('shell-test') .. [[" REP 9999 !terminal_output!<cr>]])
39    feed([[<C-\><C-N>]])
40    poke_eventloop()
41    -- Wait for some terminal activity.
42    retry(nil, 4000, function()
43      ok(fn.line('$') > 6)
44    end)
45    feed(':messages<CR>')
46    screen:expect([[
47      msg1                                              |
48      msg2                                              |
49      msg3                                              |
50      Press ENTER or type command to continue^           |
51    ]])
52  end)
53
54  it('reads output buffer on terminal reporting #4151', function()
55    skip(is_os('win'))
56    if is_os('win') then
57      command(
58        [[terminal powershell -NoProfile -NoLogo -Command Write-Host -NoNewline "\"$([char]27)[6n\""; Start-Sleep -Milliseconds 500 ]]
59      )
60    else
61      command([[terminal printf '\e[6n'; sleep 0.5 ]])
62    end
63    screen:expect { any = '%^%[%[1;1R' }
64  end)
65
66  it('in normal-mode :split does not move cursor', function()
67    if is_os('win') then
68      command(
69        [[terminal for /L \\%I in (1,0,2) do ( echo foo & ping -w 100 -n 1 127.0.0.1 > nul )]]
70      )
71    else
72      command([[terminal while true; do echo foo; sleep .1; done]])
73    end
74    feed([[<C-\><C-N>M]]) -- move cursor away from last line
75    poke_eventloop()
76    eq(3, eval("line('$')")) -- window height
77    eq(2, eval("line('.')")) -- cursor is in the middle
78    feed(':vsplit<CR>')
79    eq(2, eval("line('.')")) -- cursor stays where we put it
80    feed(':split<CR>')
81    eq(2, eval("line('.')")) -- cursor stays where we put it
82  end)
83
84  it('Enter/Leave does not increment jumplist #3723', function()
85    feed(':terminal<CR>')
86    local function enter_and_leave()
87      local lines_before = fn.line('$')
88      -- Create a new line (in the shell). For a normal buffer this
89      -- increments the jumplist; for a terminal-buffer it should not. #3723
90      feed('i')
91      poke_eventloop()
92      feed('<CR><CR><CR><CR>')
93      poke_eventloop()
94      feed([[<C-\><C-N>]])
95      poke_eventloop()
96      -- Wait for >=1 lines to be created.
97      retry(nil, 4000, function()
98        ok(fn.line('$') > lines_before)
99      end)
100    end
101    enter_and_leave()
102    enter_and_leave()
103    enter_and_leave()
104    ok(fn.line('$') > 6) -- Verify assumption.
105    local jumps = fn.split(fn.execute('jumps'), '\n')
106    eq(' jump line  col file/text', jumps[1])
107    eq(3, #jumps)
108  end)
109
110  it("triggers 'autowrite'", function()
111    api.nvim_set_option_value('autowrite', true, {})
112    command('vnew Xtermautowritetestfile | setlocal fileformat=unix')
113    finally(function()
114      os.remove('Xtermautowritetestfile')
115    end)
116    fn.setline(1, 'test content')
117    command('terminal')
118    eq('test content\n', t.read_file('Xtermautowritetestfile'))
119  end)
120
121  it('nvim_get_mode() in :terminal', function()
122    command('terminal')
123    eq({ blocking = false, mode = 'nt' }, api.nvim_get_mode())
124    feed('i')
125    eq({ blocking = false, mode = 't' }, api.nvim_get_mode())
126    feed([[<C-\><C-N>]])
127    eq({ blocking = false, mode = 'nt' }, api.nvim_get_mode())
128  end)
129
130  it(':stopinsert RPC request exits terminal-mode #7807', function()
131    command('terminal')
132    feed('i[tui] insert-mode')
133    eq({ blocking = false, mode = 't' }, api.nvim_get_mode())
134    command('stopinsert')
135    feed('<Ignore>') -- Add input to separate two RPC requests
136    eq({ blocking = false, mode = 'nt' }, api.nvim_get_mode())
137  end)
138
139  it(":stopinsert in normal mode doesn't break insert mode #9889", function()
140    command('terminal')
141    eq({ blocking = false, mode = 'nt' }, api.nvim_get_mode())
142    command('stopinsert')
143    feed('<Ignore>') -- Add input to separate two RPC requests
144    eq({ blocking = false, mode = 'nt' }, api.nvim_get_mode())
145    feed('a')
146    eq({ blocking = false, mode = 't' }, api.nvim_get_mode())
147  end)
148
149  it('switching to terminal buffer in Insert mode goes to Terminal mode #7164', function()
150    command('terminal')
151    command('vnew')
152    feed('i')
153    command('let g:events = []')
154    command('autocmd InsertLeave * let g:events += ["InsertLeave"]')
155    command('autocmd TermEnter * let g:events += ["TermEnter"]')
156    command('inoremap <F2> <Cmd>wincmd p<CR>')
157    eq({ blocking = false, mode = 'i' }, api.nvim_get_mode())
158    feed('<F2>')
159    eq({ blocking = false, mode = 't' }, api.nvim_get_mode())
160    eq({ 'InsertLeave', 'TermEnter' }, eval('g:events'))
161  end)
162
163  it('switching to terminal buffer immediately after :stopinsert #27031', function()
164    command('terminal')
165    command('vnew')
166    feed('i')
167    eq({ blocking = false, mode = 'i' }, api.nvim_get_mode())
168    command('stopinsert | wincmd p')
169    feed('<Ignore>') -- Add input to separate two RPC requests
170    eq({ blocking = false, mode = 'nt' }, api.nvim_get_mode())
171  end)
172
173  it('switching to another terminal buffer in Terminal mode', function()
174    command('terminal')
175    local buf0 = api.nvim_get_current_buf()
176    command('terminal')
177    local buf1 = api.nvim_get_current_buf()
178    command('terminal')
179    local buf2 = api.nvim_get_current_buf()
180    neq(buf0, buf1)
181    neq(buf0, buf2)
182    neq(buf1, buf2)
183    feed('i')
184    eq({ blocking = false, mode = 't' }, api.nvim_get_mode())
185    api.nvim_set_current_buf(buf1)
186    eq({ blocking = false, mode = 't' }, api.nvim_get_mode())
187    api.nvim_set_current_buf(buf0)
188    eq({ blocking = false, mode = 't' }, api.nvim_get_mode())
189    exec_lua(function()
190      vim.api.nvim_set_current_buf(buf1)
191      vim.api.nvim_buf_delete(buf0, { force = true })
192    end)
193    eq({ blocking = false, mode = 't' }, api.nvim_get_mode())
194    api.nvim_set_current_buf(buf2)
195    eq({ blocking = false, mode = 't' }, api.nvim_get_mode())
196    api.nvim_set_current_buf(buf1)
197    eq({ blocking = false, mode = 't' }, api.nvim_get_mode())
198  end)
199end)
200
201local function test_terminal_with_fake_shell(backslash)
202  -- shell-test.c is a fake shell that prints its arguments and exits.
203  local shell_path = testprg('shell-test')
204  if backslash then
205    shell_path = shell_path:gsub('/', [[\]])
206  end
207
208  local screen
209
210  before_each(function()
211    clear()
212    screen = Screen.new(50, 4, { rgb = false })
213    screen._default_attr_ids = nil
214    api.nvim_set_option_value('shell', shell_path, {})
215    api.nvim_set_option_value('shellcmdflag', 'EXE', {})
216    api.nvim_set_option_value('shellxquote', '', {}) -- win: avoid extra quotes
217    t.mkdir('Xsomedir')
218    t.write_file('Xsomedir/Xuniquefile', '')
219  end)
220
221  after_each(function()
222    n.rmdir('Xsomedir')
223  end)
224
225  it('with no argument, acts like jobstart(…,{term=true})', function()
226    command('autocmd! nvim.terminal TermClose')
227    command('terminal')
228    screen:expect([[
229      ^ready $                                           |
230                                                        |
231                                                        |*2
232    ]])
233  end)
234
235  it("with no argument, and 'shell' is set to empty string", function()
236    api.nvim_set_option_value('shell', '', {})
237    eq("Vim(terminal):E91: 'shell' option is empty", t.pcall_err(command, 'terminal'))
238  end)
239
240  it("with no argument, but 'shell' has arguments, acts like jobstart(…,{term=true})", function()
241    api.nvim_set_option_value('shell', shell_path .. ' INTERACT', {})
242    command('terminal')
243    screen:expect([[
244      ^interact $                                        |
245                                                        |*3
246    ]])
247  end)
248
249  it('executes a given command through the shell', function()
250    command('terminal echo hi')
251    screen:expect([[
252      ^ready $ echo hi                                   |
253                                                        |
254      [Process exited 0]                                |
255                                                        |
256    ]])
257  end)
258
259  it("executes a given command through the shell, when 'shell' has arguments", function()
260    api.nvim_set_option_value('shell', shell_path .. ' -t jeff', {})
261    command('terminal echo hi')
262    screen:expect([[
263      ^jeff $ echo hi                                    |
264                                                        |
265      [Process exited 0]                                |
266                                                        |
267    ]])
268  end)
269
270  it('spawns in CWD effective at time of invocation', function()
271    -- Run "echo" so the default TermClose handler does not auto-delete an exitcode=0 shell.
272    command('terminal echo')
273    local dir = fn.bufname():match('^term://(.-)//')
274    local parentdir = fn.fnamemodify(fn.getcwd(), ':h') -- Absolute, so 'cdpath' cannot interfere.
275    command(('bcd %s'):format(fn.fnameescape(parentdir))) -- :terminal should use this CWD.
276    command('terminal echo')
277    eq(fn.fnamemodify(dir, ':h'), fn.bufname():match('^term://(.-)//'))
278  end)
279
280  it('allows quotes and slashes', function()
281    command([[terminal echo 'hello' \ "world"]])
282    screen:expect([[
283      ^ready $ echo 'hello' \ "world"                    |
284                                                        |
285      [Process exited 0]                                |
286                                                        |
287    ]])
288  end)
289
290  it('ex_terminal() double-free #4554', function()
291    source([[
292      autocmd BufNew * set shell=foo
293      terminal]])
294    -- Verify that BufNew actually fired (else the test is invalid).
295    eq('foo', eval('&shell'))
296  end)
297
298  it('ignores writes if the backing stream closes', function()
299    command('autocmd! nvim.terminal TermClose')
300    command('terminal')
301    feed('iiXXXXXXX')
302    poke_eventloop()
303    -- Race: Though the shell exited (and streams were closed by SIGCHLD
304    -- handler), :terminal cleanup is pending on the main-loop.
305    -- This write should be ignored (not crash, #5445).
306    feed('iiYYYYYYY')
307    assert_alive()
308  end)
309
310  it('works with findfile()', function()
311    command('autocmd! nvim.terminal TermClose')
312    command('terminal')
313    eq('term://', string.match(eval('bufname("%")'), '^term://'))
314    eq('Xsomedir/Xuniquefile', eval('findfile("Xsomedir/Xuniquefile", ".")'))
315  end)
316
317  it('works with :find', function()
318    command('autocmd! nvim.terminal TermClose')
319    command('terminal')
320    screen:expect([[
321      ^ready $                                           |
322                                                        |
323                                                        |*2
324    ]])
325    eq('term://', string.match(eval('bufname("%")'), '^term://'))
326    feed([[<C-\><C-N>]])
327    command([[find */Xuniquefile]])
328    eq('Xsomedir/Xuniquefile', eval('bufname("%")'))
329  end)
330
331  it('works with gf', function()
332    command([[terminal echo "Xsomedir/Xuniquefile"]])
333    screen:expect([[
334      ^ready $ echo "Xsomedir/Xuniquefile"               |
335                                                        |
336      [Process exited 0]                                |
337                                                        |
338    ]])
339    feed([[<C-\><C-N>]])
340    eq('term://', string.match(eval('bufname("%")'), '^term://'))
341    feed([[ggf"lgf]])
342    eq('Xsomedir/Xuniquefile', eval('bufname("%")'))
343  end)
344
345  it('with bufhidden=delete #3958', function()
346    command('set hidden')
347    eq(1, eval('&hidden'))
348    command('autocmd BufNew * setlocal bufhidden=delete')
349    for _ = 1, 5 do
350      source([[
351      execute 'edit '.reltimestr(reltime())
352      terminal]])
353    end
354  end)
355
356  describe('exit does not have long delay #27615', function()
357    for _, ut in ipairs({ 5, 50, 500, 5000, 50000, 500000 }) do
358      it(('with updatetime=%d'):format(ut), function()
359        api.nvim_set_option_value('updatetime', ut, {})
360        api.nvim_set_option_value('shellcmdflag', 'EXIT', {})
361        command('terminal 42')
362        screen:expect([[
363          ^                                                  |
364          [Process exited 42]                               |
365                                                            |*2
366        ]])
367      end)
368    end
369  end)
370end
371
372describe(':terminal (fake shell)', function()
373  test_terminal_with_fake_shell(false)
374  if is_os('win') then
375    describe("when 'shell' uses backslashes", function()
376      test_terminal_with_fake_shell(true)
377    end)
378  end
379end)