2024-04-20 08:44:13 -07:00
|
|
|
local t = require('test.testutil')
|
|
|
|
local n = require('test.functional.testnvim')()
|
2024-09-15 03:28:14 -07:00
|
|
|
local tt = require('test.functional.testterm')
|
2024-04-20 08:44:13 -07:00
|
|
|
local uv = vim.uv
|
2024-04-08 02:03:20 -07:00
|
|
|
|
2024-04-20 08:44:13 -07:00
|
|
|
local clear, command, testprg = n.clear, n.command, n.testprg
|
|
|
|
local eval, eq, neq, retry = n.eval, t.eq, t.neq, t.retry
|
2024-04-08 02:03:20 -07:00
|
|
|
local matches = t.matches
|
|
|
|
local ok = t.ok
|
2024-04-20 08:44:13 -07:00
|
|
|
local feed = n.feed
|
|
|
|
local api = n.api
|
2024-04-08 02:03:20 -07:00
|
|
|
local pcall_err = t.pcall_err
|
2024-04-20 08:44:13 -07:00
|
|
|
local assert_alive = n.assert_alive
|
2024-04-08 02:03:20 -07:00
|
|
|
local skip = t.skip
|
|
|
|
local is_os = t.is_os
|
2015-11-15 07:45:23 -07:00
|
|
|
|
2019-09-14 15:54:19 -07:00
|
|
|
describe('autocmd TermClose', function()
|
2015-11-15 07:45:23 -07:00
|
|
|
before_each(function()
|
|
|
|
clear()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_option_value('shell', testprg('shell-test'), {})
|
2021-08-20 10:45:28 -07:00
|
|
|
command('set shellcmdflag=EXE shellredir= shellpipe= shellquote= shellxquote=')
|
2015-11-15 07:45:23 -07:00
|
|
|
end)
|
|
|
|
|
2022-07-05 02:31:49 -07:00
|
|
|
local function test_termclose_delete_own_buf()
|
2023-02-28 18:56:25 -07:00
|
|
|
-- The terminal process needs to keep running so that TermClose isn't triggered immediately.
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_option_value('shell', string.format('"%s" INTERACT', testprg('shell-test')), {})
|
2022-07-05 02:31:49 -07:00
|
|
|
command('autocmd TermClose * bdelete!')
|
|
|
|
command('terminal')
|
2022-11-05 03:40:02 -07:00
|
|
|
matches(
|
|
|
|
'^TermClose Autocommands for "%*": Vim%(bdelete%):E937: Attempt to delete a buffer that is in use: term://',
|
2022-10-15 01:10:56 -07:00
|
|
|
pcall_err(command, 'bdelete!')
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2022-07-05 02:31:49 -07:00
|
|
|
assert_alive()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- TODO: fixed after merging patches for `can_unload_buffer`?
|
|
|
|
pending('TermClose deleting its own buffer, altbuf = buffer 1 #10386', function()
|
|
|
|
test_termclose_delete_own_buf()
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('TermClose deleting its own buffer, altbuf NOT buffer 1 #10386', function()
|
|
|
|
command('edit foo1')
|
|
|
|
test_termclose_delete_own_buf()
|
|
|
|
end)
|
|
|
|
|
2017-05-28 18:17:12 -07:00
|
|
|
it('triggers when fast-exiting terminal job stops', function()
|
2017-03-15 15:28:37 -07:00
|
|
|
command('autocmd TermClose * let g:test_termclose = 23')
|
|
|
|
command('terminal')
|
2019-07-02 13:44:11 -07:00
|
|
|
-- shell-test exits immediately.
|
|
|
|
retry(nil, nil, function()
|
|
|
|
neq(-1, eval('jobwait([&channel], 0)[0]'))
|
|
|
|
end)
|
2019-08-04 18:17:53 -07:00
|
|
|
retry(nil, nil, function()
|
|
|
|
eq(23, eval('g:test_termclose'))
|
|
|
|
end)
|
2015-11-15 07:45:23 -07:00
|
|
|
end)
|
2016-02-19 19:51:50 -07:00
|
|
|
|
2017-05-28 18:17:12 -07:00
|
|
|
it('triggers when long-running terminal job gets stopped', function()
|
2023-03-11 14:56:16 -07:00
|
|
|
skip(is_os('win'))
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_option_value('shell', is_os('win') and 'cmd.exe' or 'sh', {})
|
2017-05-28 18:17:12 -07:00
|
|
|
command('autocmd TermClose * let g:test_termclose = 23')
|
|
|
|
command('terminal')
|
|
|
|
command('call jobstop(b:terminal_job_id)')
|
|
|
|
retry(nil, nil, function()
|
|
|
|
eq(23, eval('g:test_termclose'))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2022-04-12 07:48:08 -07:00
|
|
|
it('kills job trapping SIGTERM', function()
|
2022-11-21 17:13:30 -07:00
|
|
|
skip(is_os('win'))
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_option_value('shell', 'sh', {})
|
|
|
|
api.nvim_set_option_value('shellcmdflag', '-c', {})
|
2017-05-28 18:17:12 -07:00
|
|
|
command(
|
|
|
|
[[ let g:test_job = jobstart('trap "" TERM && echo 1 && sleep 60', { ]]
|
|
|
|
.. [[ 'on_stdout': {-> execute('let g:test_job_started = 1')}, ]]
|
|
|
|
.. [[ 'on_exit': {-> execute('let g:test_job_exited = 1')}}) ]]
|
|
|
|
)
|
|
|
|
retry(nil, nil, function()
|
|
|
|
eq(1, eval('get(g:, "test_job_started", 0)'))
|
|
|
|
end)
|
|
|
|
|
2024-01-12 05:03:25 -07:00
|
|
|
uv.update_time()
|
|
|
|
local start = uv.now()
|
2017-05-28 18:17:12 -07:00
|
|
|
command('call jobstop(g:test_job)')
|
|
|
|
retry(nil, nil, function()
|
|
|
|
eq(1, eval('get(g:, "test_job_exited", 0)'))
|
|
|
|
end)
|
2024-01-12 05:03:25 -07:00
|
|
|
uv.update_time()
|
|
|
|
local duration = uv.now() - start
|
2018-03-05 21:22:48 -07:00
|
|
|
-- Nvim begins SIGTERM after KILL_TIMEOUT_MS.
|
|
|
|
ok(duration >= 2000)
|
|
|
|
ok(duration <= 4000) -- Epsilon for slow CI
|
2017-05-28 18:17:12 -07:00
|
|
|
end)
|
|
|
|
|
2022-04-12 07:48:08 -07:00
|
|
|
it('kills PTY job trapping SIGHUP and SIGTERM', function()
|
2022-11-21 17:13:30 -07:00
|
|
|
skip(is_os('win'))
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_option_value('shell', 'sh', {})
|
|
|
|
api.nvim_set_option_value('shellcmdflag', '-c', {})
|
2017-05-28 18:17:12 -07:00
|
|
|
command(
|
|
|
|
[[ let g:test_job = jobstart('trap "" HUP TERM && echo 1 && sleep 60', { ]]
|
|
|
|
.. [[ 'pty': 1,]]
|
|
|
|
.. [[ 'on_stdout': {-> execute('let g:test_job_started = 1')}, ]]
|
|
|
|
.. [[ 'on_exit': {-> execute('let g:test_job_exited = 1')}}) ]]
|
|
|
|
)
|
|
|
|
retry(nil, nil, function()
|
|
|
|
eq(1, eval('get(g:, "test_job_started", 0)'))
|
|
|
|
end)
|
|
|
|
|
2024-01-12 05:03:25 -07:00
|
|
|
uv.update_time()
|
|
|
|
local start = uv.now()
|
2017-05-28 18:17:12 -07:00
|
|
|
command('call jobstop(g:test_job)')
|
|
|
|
retry(nil, nil, function()
|
|
|
|
eq(1, eval('get(g:, "test_job_exited", 0)'))
|
|
|
|
end)
|
2024-01-12 05:03:25 -07:00
|
|
|
uv.update_time()
|
|
|
|
local duration = uv.now() - start
|
2018-03-05 21:22:48 -07:00
|
|
|
-- Nvim begins SIGKILL after (2 * KILL_TIMEOUT_MS).
|
|
|
|
ok(duration >= 4000)
|
|
|
|
ok(duration <= 7000) -- Epsilon for slow CI
|
2017-05-28 18:17:12 -07:00
|
|
|
end)
|
|
|
|
|
2016-02-19 19:51:50 -07:00
|
|
|
it('reports the correct <abuf>', function()
|
2017-03-15 15:28:37 -07:00
|
|
|
command('set hidden')
|
2021-09-04 14:28:08 -07:00
|
|
|
command('set shellcmdflag=EXE')
|
2017-03-15 15:28:37 -07:00
|
|
|
command('autocmd TermClose * let g:abuf = expand("<abuf>")')
|
|
|
|
command('edit foo')
|
|
|
|
command('edit bar')
|
2016-02-19 19:51:50 -07:00
|
|
|
eq(2, eval('bufnr("%")'))
|
2017-03-15 15:28:37 -07:00
|
|
|
|
2021-09-04 14:28:08 -07:00
|
|
|
command('terminal ls')
|
2017-04-05 15:16:15 -07:00
|
|
|
retry(nil, nil, function()
|
|
|
|
eq(3, eval('bufnr("%")'))
|
|
|
|
end)
|
2017-03-15 15:28:37 -07:00
|
|
|
|
|
|
|
command('buffer 1')
|
2017-04-05 15:16:15 -07:00
|
|
|
retry(nil, nil, function()
|
|
|
|
eq(1, eval('bufnr("%")'))
|
|
|
|
end)
|
2017-03-15 15:28:37 -07:00
|
|
|
|
|
|
|
command('3bdelete!')
|
2017-04-05 15:16:15 -07:00
|
|
|
retry(nil, nil, function()
|
|
|
|
eq('3', eval('g:abuf'))
|
|
|
|
end)
|
2018-11-26 14:14:18 -07:00
|
|
|
feed('<c-c>:qa!<cr>')
|
2016-02-19 19:51:50 -07:00
|
|
|
end)
|
2021-08-20 10:45:28 -07:00
|
|
|
|
|
|
|
it('exposes v:event.status', function()
|
|
|
|
command('set shellcmdflag=EXIT')
|
|
|
|
command('autocmd TermClose * let g:status = v:event.status')
|
|
|
|
|
|
|
|
command('terminal 0')
|
|
|
|
retry(nil, nil, function()
|
|
|
|
eq(0, eval('g:status'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
command('terminal 42')
|
|
|
|
retry(nil, nil, function()
|
|
|
|
eq(42, eval('g:status'))
|
|
|
|
end)
|
|
|
|
end)
|
2015-11-15 07:45:23 -07:00
|
|
|
end)
|
2019-09-14 15:54:19 -07:00
|
|
|
|
|
|
|
it('autocmd TermEnter, TermLeave', function()
|
|
|
|
clear()
|
|
|
|
command('let g:evs = []')
|
|
|
|
command('autocmd TermOpen * call add(g:evs, ["TermOpen", mode()])')
|
|
|
|
command('autocmd TermClose * call add(g:evs, ["TermClose", mode()])')
|
|
|
|
command('autocmd TermEnter * call add(g:evs, ["TermEnter", mode()])')
|
|
|
|
command('autocmd TermLeave * call add(g:evs, ["TermLeave", mode()])')
|
|
|
|
command('terminal')
|
|
|
|
|
|
|
|
feed('i')
|
|
|
|
eq({ { 'TermOpen', 'n' }, { 'TermEnter', 't' } }, eval('g:evs'))
|
|
|
|
feed([[<C-\><C-n>]])
|
|
|
|
feed('A')
|
|
|
|
eq(
|
|
|
|
{ { 'TermOpen', 'n' }, { 'TermEnter', 't' }, { 'TermLeave', 'n' }, { 'TermEnter', 't' } },
|
|
|
|
eval('g:evs')
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2019-09-14 15:54:19 -07:00
|
|
|
|
|
|
|
-- TermLeave is also triggered by :quit.
|
|
|
|
command('split foo')
|
2023-10-17 16:19:29 -07:00
|
|
|
feed('<Ignore>') -- Add input to separate two RPC requests
|
2019-09-14 15:54:19 -07:00
|
|
|
command('wincmd w')
|
|
|
|
feed('i')
|
|
|
|
command('q!')
|
2023-10-17 16:19:29 -07:00
|
|
|
feed('<Ignore>') -- Add input to separate two RPC requests
|
|
|
|
eq({
|
|
|
|
{ 'TermOpen', 'n' },
|
|
|
|
{ 'TermEnter', 't' },
|
|
|
|
{ 'TermLeave', 'n' },
|
|
|
|
{ 'TermEnter', 't' },
|
|
|
|
{ 'TermLeave', 'n' },
|
|
|
|
{ 'TermEnter', 't' },
|
|
|
|
{ 'TermClose', 't' },
|
|
|
|
{ 'TermLeave', 'n' },
|
|
|
|
}, eval('g:evs'))
|
2019-09-14 15:54:19 -07:00
|
|
|
end)
|
2023-07-22 17:53:59 -07:00
|
|
|
|
|
|
|
describe('autocmd TextChangedT', function()
|
2024-10-12 20:02:28 -07:00
|
|
|
local screen
|
|
|
|
before_each(function()
|
|
|
|
clear()
|
|
|
|
screen = tt.setup_screen()
|
|
|
|
end)
|
2023-07-22 17:53:59 -07:00
|
|
|
|
|
|
|
it('works', function()
|
|
|
|
command('autocmd TextChangedT * ++once let g:called = 1')
|
2024-04-08 02:03:20 -07:00
|
|
|
tt.feed_data('a')
|
2023-07-22 17:53:59 -07:00
|
|
|
retry(nil, nil, function()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(1, api.nvim_get_var('called'))
|
2023-07-22 17:53:59 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('cannot delete terminal buffer', function()
|
2024-11-21 17:08:12 -07:00
|
|
|
command('autocmd TextChangedT * bwipe!')
|
2024-04-08 02:03:20 -07:00
|
|
|
tt.feed_data('a')
|
2023-07-22 17:53:59 -07:00
|
|
|
screen:expect({ any = 'E937: ' })
|
2024-11-21 17:08:12 -07:00
|
|
|
feed('<CR>')
|
|
|
|
command('autocmd! TextChangedT')
|
2024-01-12 05:44:54 -07:00
|
|
|
matches(
|
|
|
|
'^E937: Attempt to delete a buffer that is in use: term://',
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_get_vvar('errmsg')
|
2024-01-12 05:44:54 -07:00
|
|
|
)
|
2023-07-22 17:53:59 -07:00
|
|
|
end)
|
|
|
|
end)
|