2024-04-20 08:44:13 -07:00
|
|
|
local t = require('test.testutil')
|
|
|
|
local n = require('test.functional.testnvim')()
|
2015-04-13 20:53:16 -07:00
|
|
|
local Screen = require('test.functional.ui.screen')
|
2024-04-20 08:44:13 -07:00
|
|
|
|
|
|
|
local assert_alive = n.assert_alive
|
|
|
|
local clear, poke_eventloop = n.clear, n.poke_eventloop
|
|
|
|
local testprg, source, eq = n.testprg, n.source, t.eq
|
|
|
|
local feed = n.feed
|
|
|
|
local feed_command, eval = n.feed_command, n.eval
|
|
|
|
local fn = n.fn
|
|
|
|
local api = n.api
|
2024-04-08 02:03:20 -07:00
|
|
|
local retry = t.retry
|
|
|
|
local ok = t.ok
|
2024-04-20 08:44:13 -07:00
|
|
|
local command = n.command
|
2024-04-08 02:03:20 -07:00
|
|
|
local skip = t.skip
|
|
|
|
local is_os = t.is_os
|
|
|
|
local is_ci = t.is_ci
|
2016-08-15 16:42:12 -07:00
|
|
|
|
2015-04-13 20:53:16 -07:00
|
|
|
describe(':terminal', function()
|
|
|
|
local screen
|
|
|
|
|
2016-10-20 13:51:46 -07:00
|
|
|
before_each(function()
|
|
|
|
clear()
|
2024-11-11 14:15:19 -07:00
|
|
|
screen = Screen.new(50, 4, { rgb = false })
|
2024-03-22 03:02:52 -07:00
|
|
|
screen._default_attr_ids = nil
|
2016-10-20 13:51:46 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('does not interrupt Press-ENTER prompt #2748', function()
|
|
|
|
-- Ensure that :messages shows Press-ENTER.
|
|
|
|
source([[
|
|
|
|
echomsg "msg1"
|
|
|
|
echomsg "msg2"
|
2017-02-21 07:16:48 -07:00
|
|
|
echomsg "msg3"
|
2016-10-20 13:51:46 -07:00
|
|
|
]])
|
|
|
|
-- Invoke a command that emits frequent terminal activity.
|
2022-06-22 05:51:52 -07:00
|
|
|
feed([[:terminal "]] .. testprg('shell-test') .. [[" REP 9999 !terminal_output!<cr>]])
|
2018-02-05 15:38:30 -07:00
|
|
|
feed([[<C-\><C-N>]])
|
2020-10-19 11:17:51 -07:00
|
|
|
poke_eventloop()
|
2018-02-05 15:38:30 -07:00
|
|
|
-- Wait for some terminal activity.
|
|
|
|
retry(nil, 4000, function()
|
2024-01-12 10:59:57 -07:00
|
|
|
ok(fn.line('$') > 6)
|
2018-02-05 15:38:30 -07:00
|
|
|
end)
|
2017-04-08 14:12:26 -07:00
|
|
|
feed_command('messages')
|
2016-10-20 13:51:46 -07:00
|
|
|
screen:expect([[
|
|
|
|
msg1 |
|
|
|
|
msg2 |
|
2017-02-21 07:16:48 -07:00
|
|
|
msg3 |
|
2016-10-20 13:51:46 -07:00
|
|
|
Press ENTER or type command to continue^ |
|
|
|
|
]])
|
|
|
|
end)
|
|
|
|
|
2018-06-05 21:36:19 -07:00
|
|
|
it('reads output buffer on terminal reporting #4151', function()
|
2022-11-21 17:13:30 -07:00
|
|
|
skip(is_ci('cirrus') or is_os('win'))
|
|
|
|
if is_os('win') then
|
2018-06-05 21:36:19 -07:00
|
|
|
feed_command(
|
|
|
|
[[terminal powershell -NoProfile -NoLogo -Command Write-Host -NoNewline "\"$([char]27)[6n\""; Start-Sleep -Milliseconds 500 ]]
|
|
|
|
)
|
|
|
|
else
|
|
|
|
feed_command([[terminal printf '\e[6n'; sleep 0.5 ]])
|
|
|
|
end
|
2018-08-20 09:51:25 -07:00
|
|
|
screen:expect { any = '%^%[%[1;1R' }
|
2018-06-05 21:36:19 -07:00
|
|
|
end)
|
|
|
|
|
2017-02-21 07:06:44 -07:00
|
|
|
it('in normal-mode :split does not move cursor', function()
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2017-10-05 09:38:10 -07:00
|
|
|
feed_command(
|
|
|
|
[[terminal for /L \\%I in (1,0,2) do ( echo foo & ping -w 100 -n 1 127.0.0.1 > nul )]]
|
|
|
|
)
|
|
|
|
else
|
|
|
|
feed_command([[terminal while true; do echo foo; sleep .1; done]])
|
|
|
|
end
|
2018-02-05 15:38:30 -07:00
|
|
|
feed([[<C-\><C-N>M]]) -- move cursor away from last line
|
2020-10-19 11:17:51 -07:00
|
|
|
poke_eventloop()
|
2017-02-21 07:06:44 -07:00
|
|
|
eq(3, eval("line('$')")) -- window height
|
|
|
|
eq(2, eval("line('.')")) -- cursor is in the middle
|
2017-04-08 14:12:26 -07:00
|
|
|
feed_command('vsplit')
|
2017-02-21 07:06:44 -07:00
|
|
|
eq(2, eval("line('.')")) -- cursor stays where we put it
|
2017-04-08 14:12:26 -07:00
|
|
|
feed_command('split')
|
2017-02-21 07:06:44 -07:00
|
|
|
eq(2, eval("line('.')")) -- cursor stays where we put it
|
|
|
|
end)
|
|
|
|
|
2018-02-05 15:38:30 -07:00
|
|
|
it('Enter/Leave does not increment jumplist #3723', function()
|
|
|
|
feed_command('terminal')
|
|
|
|
local function enter_and_leave()
|
2024-01-12 10:59:57 -07:00
|
|
|
local lines_before = fn.line('$')
|
2018-02-05 15:38:30 -07:00
|
|
|
-- Create a new line (in the shell). For a normal buffer this
|
|
|
|
-- increments the jumplist; for a terminal-buffer it should not. #3723
|
|
|
|
feed('i')
|
2020-10-19 11:17:51 -07:00
|
|
|
poke_eventloop()
|
2018-02-05 15:38:30 -07:00
|
|
|
feed('<CR><CR><CR><CR>')
|
2020-10-19 11:17:51 -07:00
|
|
|
poke_eventloop()
|
2018-02-05 15:38:30 -07:00
|
|
|
feed([[<C-\><C-N>]])
|
2020-10-19 11:17:51 -07:00
|
|
|
poke_eventloop()
|
2018-02-05 15:38:30 -07:00
|
|
|
-- Wait for >=1 lines to be created.
|
|
|
|
retry(nil, 4000, function()
|
2024-01-12 10:59:57 -07:00
|
|
|
ok(fn.line('$') > lines_before)
|
2018-02-05 15:38:30 -07:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
enter_and_leave()
|
|
|
|
enter_and_leave()
|
|
|
|
enter_and_leave()
|
2024-01-12 10:59:57 -07:00
|
|
|
ok(fn.line('$') > 6) -- Verify assumption.
|
|
|
|
local jumps = fn.split(fn.execute('jumps'), '\n')
|
2018-02-05 15:38:30 -07:00
|
|
|
eq(' jump line col file/text', jumps[1])
|
|
|
|
eq(3, #jumps)
|
|
|
|
end)
|
|
|
|
|
2021-10-09 18:15:46 -07:00
|
|
|
it('nvim_get_mode() in :terminal', function()
|
2023-07-22 21:46:56 -07:00
|
|
|
command('terminal')
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ blocking = false, mode = 'nt' }, api.nvim_get_mode())
|
2021-10-09 18:15:46 -07:00
|
|
|
feed('i')
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ blocking = false, mode = 't' }, api.nvim_get_mode())
|
2021-10-09 18:15:46 -07:00
|
|
|
feed([[<C-\><C-N>]])
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ blocking = false, mode = 'nt' }, api.nvim_get_mode())
|
2021-10-09 18:15:46 -07:00
|
|
|
end)
|
|
|
|
|
2019-04-07 16:13:43 -07:00
|
|
|
it(':stopinsert RPC request exits terminal-mode #7807', function()
|
2023-07-22 21:46:56 -07:00
|
|
|
command('terminal')
|
2019-04-07 16:13:43 -07:00
|
|
|
feed('i[tui] insert-mode')
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ blocking = false, mode = 't' }, api.nvim_get_mode())
|
2019-04-07 16:13:43 -07:00
|
|
|
command('stopinsert')
|
2023-07-22 21:46:56 -07:00
|
|
|
feed('<Ignore>') -- Add input to separate two RPC requests
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ blocking = false, mode = 'nt' }, api.nvim_get_mode())
|
2019-04-07 16:13:43 -07:00
|
|
|
end)
|
2019-04-18 22:49:05 -07:00
|
|
|
|
|
|
|
it(":stopinsert in normal mode doesn't break insert mode #9889", function()
|
2023-07-22 21:46:56 -07:00
|
|
|
command('terminal')
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ blocking = false, mode = 'nt' }, api.nvim_get_mode())
|
2023-07-22 21:46:56 -07:00
|
|
|
command('stopinsert')
|
|
|
|
feed('<Ignore>') -- Add input to separate two RPC requests
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ blocking = false, mode = 'nt' }, api.nvim_get_mode())
|
2019-04-18 22:49:05 -07:00
|
|
|
feed('a')
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ blocking = false, mode = 't' }, api.nvim_get_mode())
|
2019-04-18 22:49:05 -07:00
|
|
|
end)
|
2023-10-28 18:32:03 -07:00
|
|
|
|
|
|
|
it('switching to terminal buffer in Insert mode goes to Terminal mode #7164', function()
|
|
|
|
command('terminal')
|
|
|
|
command('vnew')
|
|
|
|
feed('i')
|
|
|
|
command('let g:events = []')
|
|
|
|
command('autocmd InsertLeave * let g:events += ["InsertLeave"]')
|
|
|
|
command('autocmd TermEnter * let g:events += ["TermEnter"]')
|
|
|
|
command('inoremap <F2> <Cmd>wincmd p<CR>')
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ blocking = false, mode = 'i' }, api.nvim_get_mode())
|
2023-10-28 18:32:03 -07:00
|
|
|
feed('<F2>')
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ blocking = false, mode = 't' }, api.nvim_get_mode())
|
2023-10-28 18:32:03 -07:00
|
|
|
eq({ 'InsertLeave', 'TermEnter' }, eval('g:events'))
|
|
|
|
end)
|
2024-01-15 19:42:09 -07:00
|
|
|
|
|
|
|
it('switching to terminal buffer immediately after :stopinsert #27031', function()
|
|
|
|
command('terminal')
|
|
|
|
command('vnew')
|
|
|
|
feed('i')
|
|
|
|
eq({ blocking = false, mode = 'i' }, api.nvim_get_mode())
|
|
|
|
command('stopinsert | wincmd p')
|
|
|
|
eq({ blocking = false, mode = 'nt' }, api.nvim_get_mode())
|
|
|
|
end)
|
2016-10-20 13:51:46 -07:00
|
|
|
end)
|
|
|
|
|
2023-11-29 16:15:34 -07:00
|
|
|
local function test_terminal_with_fake_shell(backslash)
|
|
|
|
-- shell-test.c is a fake shell that prints its arguments and exits.
|
|
|
|
local shell_path = testprg('shell-test')
|
|
|
|
if backslash then
|
|
|
|
shell_path = shell_path:gsub('/', [[\]])
|
|
|
|
end
|
|
|
|
|
2016-10-20 13:51:46 -07:00
|
|
|
local screen
|
|
|
|
|
2015-04-13 20:53:16 -07:00
|
|
|
before_each(function()
|
|
|
|
clear()
|
2024-11-11 14:15:19 -07:00
|
|
|
screen = Screen.new(50, 4, { rgb = false })
|
2024-03-22 03:02:52 -07:00
|
|
|
screen._default_attr_ids = nil
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_option_value('shell', shell_path, {})
|
|
|
|
api.nvim_set_option_value('shellcmdflag', 'EXE', {})
|
2024-02-25 18:47:49 -07:00
|
|
|
api.nvim_set_option_value('shellxquote', '', {}) -- win: avoid extra quotes
|
2015-04-13 20:53:16 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('with no argument, acts like termopen()', function()
|
2023-11-29 17:56:21 -07:00
|
|
|
command('autocmd! nvim_terminal TermClose')
|
|
|
|
feed_command('terminal')
|
2024-02-25 18:47:49 -07:00
|
|
|
screen:expect([[
|
2023-11-29 17:56:21 -07:00
|
|
|
^ready $ |
|
|
|
|
[Process exited 0] |
|
2015-04-13 20:53:16 -07:00
|
|
|
|
|
2023-11-29 17:56:21 -07:00
|
|
|
:terminal |
|
2015-04-13 20:53:16 -07:00
|
|
|
]])
|
|
|
|
end)
|
|
|
|
|
2016-03-29 05:47:30 -07:00
|
|
|
it("with no argument, and 'shell' is set to empty string", function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_option_value('shell', '', {})
|
2023-11-29 17:56:21 -07:00
|
|
|
feed_command('terminal')
|
2016-03-29 05:47:30 -07:00
|
|
|
screen:expect([[
|
|
|
|
^ |
|
2023-12-09 05:42:00 -07:00
|
|
|
~ |*2
|
2016-03-29 05:47:30 -07:00
|
|
|
E91: 'shell' option is empty |
|
|
|
|
]])
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("with no argument, but 'shell' has arguments, acts like termopen()", function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_option_value('shell', shell_path .. ' INTERACT', {})
|
2023-11-29 17:56:21 -07:00
|
|
|
feed_command('terminal')
|
2016-03-29 05:47:30 -07:00
|
|
|
screen:expect([[
|
2023-11-29 17:56:21 -07:00
|
|
|
^interact $ |
|
2023-12-09 05:42:00 -07:00
|
|
|
|*2
|
2017-05-25 03:27:56 -07:00
|
|
|
:terminal |
|
2016-03-29 05:47:30 -07:00
|
|
|
]])
|
|
|
|
end)
|
|
|
|
|
2015-04-13 20:53:16 -07:00
|
|
|
it('executes a given command through the shell', function()
|
2023-11-29 17:56:21 -07:00
|
|
|
feed_command('terminal echo hi')
|
2015-04-13 20:53:16 -07:00
|
|
|
screen:expect([[
|
2017-05-25 03:27:56 -07:00
|
|
|
^ready $ echo hi |
|
2015-04-13 20:53:16 -07:00
|
|
|
|
|
2015-11-10 20:27:50 -07:00
|
|
|
[Process exited 0] |
|
2017-05-25 03:27:56 -07:00
|
|
|
:terminal echo hi |
|
2015-04-13 20:53:16 -07:00
|
|
|
]])
|
|
|
|
end)
|
|
|
|
|
2016-03-29 05:47:30 -07:00
|
|
|
it("executes a given command through the shell, when 'shell' has arguments", function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_option_value('shell', shell_path .. ' -t jeff', {})
|
2023-11-29 17:56:21 -07:00
|
|
|
feed_command('terminal echo hi')
|
2016-03-29 05:47:30 -07:00
|
|
|
screen:expect([[
|
2017-05-25 03:27:56 -07:00
|
|
|
^jeff $ echo hi |
|
2016-03-29 05:47:30 -07:00
|
|
|
|
|
|
|
|
[Process exited 0] |
|
2017-05-25 03:27:56 -07:00
|
|
|
:terminal echo hi |
|
2016-03-29 05:47:30 -07:00
|
|
|
]])
|
|
|
|
end)
|
|
|
|
|
2015-04-13 20:53:16 -07:00
|
|
|
it('allows quotes and slashes', function()
|
2023-11-29 17:56:21 -07:00
|
|
|
feed_command([[terminal echo 'hello' \ "world"]])
|
2015-04-13 20:53:16 -07:00
|
|
|
screen:expect([[
|
2017-05-25 03:27:56 -07:00
|
|
|
^ready $ echo 'hello' \ "world" |
|
2015-04-13 20:53:16 -07:00
|
|
|
|
|
2015-11-10 20:27:50 -07:00
|
|
|
[Process exited 0] |
|
2017-05-25 03:27:56 -07:00
|
|
|
:terminal echo 'hello' \ "world" |
|
2015-04-13 20:53:16 -07:00
|
|
|
]])
|
|
|
|
end)
|
2016-05-15 13:40:37 -07:00
|
|
|
|
|
|
|
it('ex_terminal() double-free #4554', function()
|
|
|
|
source([[
|
|
|
|
autocmd BufNew * set shell=foo
|
|
|
|
terminal]])
|
2016-05-14 19:56:11 -07:00
|
|
|
-- Verify that BufNew actually fired (else the test is invalid).
|
|
|
|
eq('foo', eval('&shell'))
|
2016-05-15 13:40:37 -07:00
|
|
|
end)
|
2016-10-11 14:14:18 -07:00
|
|
|
|
|
|
|
it('ignores writes if the backing stream closes', function()
|
2023-11-29 17:56:21 -07:00
|
|
|
command('autocmd! nvim_terminal TermClose')
|
|
|
|
feed_command('terminal')
|
2023-11-29 16:06:23 -07:00
|
|
|
feed('iiXXXXXXX')
|
|
|
|
poke_eventloop()
|
|
|
|
-- Race: Though the shell exited (and streams were closed by SIGCHLD
|
|
|
|
-- handler), :terminal cleanup is pending on the main-loop.
|
|
|
|
-- This write should be ignored (not crash, #5445).
|
|
|
|
feed('iiYYYYYYY')
|
|
|
|
assert_alive()
|
2016-10-11 14:14:18 -07:00
|
|
|
end)
|
|
|
|
|
2017-01-24 20:59:07 -07:00
|
|
|
it('works with findfile()', function()
|
2023-11-29 17:56:21 -07:00
|
|
|
command('autocmd! nvim_terminal TermClose')
|
2017-04-08 14:12:26 -07:00
|
|
|
feed_command('terminal')
|
2017-01-24 20:59:07 -07:00
|
|
|
eq('term://', string.match(eval('bufname("%")'), '^term://'))
|
|
|
|
eq('scripts/shadacat.py', eval('findfile("scripts/shadacat.py", ".")'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('works with :find', function()
|
2023-11-29 17:56:21 -07:00
|
|
|
command('autocmd! nvim_terminal TermClose')
|
|
|
|
feed_command('terminal')
|
2017-01-24 20:59:07 -07:00
|
|
|
screen:expect([[
|
2023-11-29 17:56:21 -07:00
|
|
|
^ready $ |
|
|
|
|
[Process exited 0] |
|
2017-01-24 20:59:07 -07:00
|
|
|
|
|
2023-11-29 17:56:21 -07:00
|
|
|
:terminal |
|
2017-01-24 20:59:07 -07:00
|
|
|
]])
|
|
|
|
eq('term://', string.match(eval('bufname("%")'), '^term://'))
|
2018-02-05 15:38:30 -07:00
|
|
|
feed([[<C-\><C-N>]])
|
2017-04-08 14:12:26 -07:00
|
|
|
feed_command([[find */shadacat.py]])
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2017-03-28 02:07:58 -07:00
|
|
|
eq('scripts\\shadacat.py', eval('bufname("%")'))
|
|
|
|
else
|
|
|
|
eq('scripts/shadacat.py', eval('bufname("%")'))
|
|
|
|
end
|
2017-01-24 20:59:07 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('works with gf', function()
|
2023-11-29 17:56:21 -07:00
|
|
|
feed_command([[terminal echo "scripts/shadacat.py"]])
|
2024-02-25 18:47:49 -07:00
|
|
|
screen:expect([[
|
2017-05-25 03:27:56 -07:00
|
|
|
^ready $ echo "scripts/shadacat.py" |
|
2017-01-24 20:59:07 -07:00
|
|
|
|
|
|
|
|
[Process exited 0] |
|
2017-05-25 03:27:56 -07:00
|
|
|
:terminal echo "scripts/shadacat.py" |
|
2017-01-24 20:59:07 -07:00
|
|
|
]])
|
2018-02-05 15:38:30 -07:00
|
|
|
feed([[<C-\><C-N>]])
|
2017-01-24 20:59:07 -07:00
|
|
|
eq('term://', string.match(eval('bufname("%")'), '^term://'))
|
2018-02-05 15:38:30 -07:00
|
|
|
feed([[ggf"lgf]])
|
2017-01-24 20:59:07 -07:00
|
|
|
eq('scripts/shadacat.py', eval('bufname("%")'))
|
|
|
|
end)
|
|
|
|
|
2019-07-08 09:42:52 -07:00
|
|
|
it('with bufhidden=delete #3958', function()
|
|
|
|
command('set hidden')
|
|
|
|
eq(1, eval('&hidden'))
|
|
|
|
command('autocmd BufNew * setlocal bufhidden=delete')
|
|
|
|
for _ = 1, 5 do
|
|
|
|
source([[
|
|
|
|
execute 'edit '.reltimestr(reltime())
|
|
|
|
terminal]])
|
|
|
|
end
|
|
|
|
end)
|
2024-02-25 18:47:49 -07:00
|
|
|
|
|
|
|
describe('exit does not have long delay #27615', function()
|
|
|
|
for _, ut in ipairs({ 5, 50, 500, 5000, 50000, 500000 }) do
|
|
|
|
it(('with updatetime=%d'):format(ut), function()
|
|
|
|
api.nvim_set_option_value('updatetime', ut, {})
|
|
|
|
api.nvim_set_option_value('shellcmdflag', 'EXIT', {})
|
|
|
|
feed_command('terminal 42')
|
|
|
|
screen:expect([[
|
|
|
|
^ |
|
|
|
|
[Process exited 42] |
|
|
|
|
|
|
|
|
|
:terminal 42 |
|
|
|
|
]])
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end)
|
2023-11-29 16:15:34 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
describe(':terminal (with fake shell)', function()
|
|
|
|
test_terminal_with_fake_shell(false)
|
|
|
|
if is_os('win') then
|
|
|
|
describe("when 'shell' uses backslashes", function()
|
|
|
|
test_terminal_with_fake_shell(true)
|
|
|
|
end)
|
|
|
|
end
|
2015-04-13 20:53:16 -07:00
|
|
|
end)
|