2024-09-15 03:28:14 -07:00
|
|
|
-- Functions to test :terminal and the Nvim TUI.
|
|
|
|
-- Starts a child process in a `:terminal` and sends bytes to the child via nvim_chan_send().
|
|
|
|
-- Note: the global functional/testutil.lua test-session is _host_ session, _not_
|
|
|
|
-- the child session.
|
|
|
|
--
|
|
|
|
-- - Use `setup_screen()` to test `:terminal` behavior with an arbitrary command.
|
|
|
|
-- - Use `setup_child_nvim()` to test the Nvim TUI.
|
|
|
|
-- - NOTE: Only use this if your test actually needs the full lifecycle/capabilities of the
|
|
|
|
-- builtin Nvim TUI. Most tests should just use `Screen.new()` directly, or plain old API calls.
|
|
|
|
|
2024-04-20 08:44:13 -07:00
|
|
|
local n = require('test.functional.testnvim')()
|
2015-03-25 05:14:47 -07:00
|
|
|
local Screen = require('test.functional.ui.screen')
|
2024-04-20 08:44:13 -07:00
|
|
|
|
|
|
|
local testprg = n.testprg
|
|
|
|
local exec_lua = n.exec_lua
|
|
|
|
local api = n.api
|
|
|
|
local nvim_prog = n.nvim_prog
|
2015-03-25 05:14:47 -07:00
|
|
|
|
2024-09-15 03:28:14 -07:00
|
|
|
local M = {}
|
|
|
|
|
|
|
|
function M.feed_data(data)
|
2022-11-19 05:46:04 -07:00
|
|
|
if type(data) == 'table' then
|
|
|
|
data = table.concat(data, '\n')
|
|
|
|
end
|
|
|
|
exec_lua('vim.api.nvim_chan_send(vim.b.terminal_job_id, ...)', data)
|
2015-03-25 05:14:47 -07:00
|
|
|
end
|
|
|
|
|
2024-09-15 03:28:14 -07:00
|
|
|
function M.feed_termcode(data)
|
|
|
|
M.feed_data('\027' .. data)
|
2022-11-19 05:46:04 -07:00
|
|
|
end
|
|
|
|
|
2024-12-17 06:11:41 -07:00
|
|
|
function M.feed_csi(data)
|
|
|
|
M.feed_termcode('[' .. data)
|
|
|
|
end
|
|
|
|
|
2024-09-15 03:28:14 -07:00
|
|
|
function M.make_lua_executor(session)
|
2022-11-19 05:46:04 -07:00
|
|
|
return function(code, ...)
|
|
|
|
local status, rv = session:request('nvim_exec_lua', code, { ... })
|
|
|
|
if not status then
|
|
|
|
session:stop()
|
|
|
|
error(rv[2])
|
|
|
|
end
|
|
|
|
return rv
|
|
|
|
end
|
2015-03-25 05:14:47 -07:00
|
|
|
end
|
2022-11-19 05:46:04 -07:00
|
|
|
|
2024-04-08 02:03:20 -07:00
|
|
|
-- some t for controlling the terminal. the codes were taken from
|
2015-03-25 05:14:47 -07:00
|
|
|
-- infocmp xterm-256color which is less what libvterm understands
|
|
|
|
-- civis/cnorm
|
2024-09-15 03:28:14 -07:00
|
|
|
function M.hide_cursor()
|
|
|
|
M.feed_termcode('[?25l')
|
2015-03-25 05:14:47 -07:00
|
|
|
end
|
2024-09-15 03:28:14 -07:00
|
|
|
function M.show_cursor()
|
|
|
|
M.feed_termcode('[?25h')
|
2015-03-25 05:14:47 -07:00
|
|
|
end
|
|
|
|
-- smcup/rmcup
|
2024-09-15 03:28:14 -07:00
|
|
|
function M.enter_altscreen()
|
|
|
|
M.feed_termcode('[?1049h')
|
2015-03-25 05:14:47 -07:00
|
|
|
end
|
2024-09-15 03:28:14 -07:00
|
|
|
function M.exit_altscreen()
|
|
|
|
M.feed_termcode('[?1049l')
|
2015-03-25 05:14:47 -07:00
|
|
|
end
|
|
|
|
-- character attributes
|
2024-09-15 03:28:14 -07:00
|
|
|
function M.set_fg(num)
|
|
|
|
M.feed_termcode('[38;5;' .. num .. 'm')
|
2015-03-25 05:14:47 -07:00
|
|
|
end
|
2024-09-15 03:28:14 -07:00
|
|
|
function M.set_bg(num)
|
|
|
|
M.feed_termcode('[48;5;' .. num .. 'm')
|
2015-03-25 05:14:47 -07:00
|
|
|
end
|
2024-09-15 03:28:14 -07:00
|
|
|
function M.set_bold()
|
|
|
|
M.feed_termcode('[1m')
|
2015-03-25 05:14:47 -07:00
|
|
|
end
|
2024-09-15 03:28:14 -07:00
|
|
|
function M.set_italic()
|
|
|
|
M.feed_termcode('[3m')
|
2015-03-25 05:14:47 -07:00
|
|
|
end
|
2024-09-15 03:28:14 -07:00
|
|
|
function M.set_underline()
|
|
|
|
M.feed_termcode('[4m')
|
2015-03-25 05:14:47 -07:00
|
|
|
end
|
2024-09-15 03:28:14 -07:00
|
|
|
function M.set_underdouble()
|
|
|
|
M.feed_termcode('[4:2m')
|
2022-09-24 22:54:30 -07:00
|
|
|
end
|
2024-09-15 03:28:14 -07:00
|
|
|
function M.set_undercurl()
|
|
|
|
M.feed_termcode('[4:3m')
|
2022-09-24 22:54:30 -07:00
|
|
|
end
|
2024-12-17 06:11:41 -07:00
|
|
|
function M.set_reverse()
|
|
|
|
M.feed_termcode('[7m')
|
|
|
|
end
|
2024-09-15 03:28:14 -07:00
|
|
|
function M.set_strikethrough()
|
|
|
|
M.feed_termcode('[9m')
|
2019-09-13 14:46:19 -07:00
|
|
|
end
|
2024-09-15 03:28:14 -07:00
|
|
|
function M.clear_attrs()
|
|
|
|
M.feed_termcode('[0;10m')
|
2015-03-25 05:14:47 -07:00
|
|
|
end
|
|
|
|
-- mouse
|
2024-09-15 03:28:14 -07:00
|
|
|
function M.enable_mouse()
|
|
|
|
M.feed_termcode('[?1002h')
|
2015-03-25 05:14:47 -07:00
|
|
|
end
|
2024-09-15 03:28:14 -07:00
|
|
|
function M.disable_mouse()
|
|
|
|
M.feed_termcode('[?1002l')
|
2015-03-25 05:14:47 -07:00
|
|
|
end
|
|
|
|
|
2023-12-06 16:15:37 -07:00
|
|
|
local default_command = { testprg('tty-test') }
|
2015-03-25 05:14:47 -07:00
|
|
|
|
2024-09-15 03:28:14 -07:00
|
|
|
--- Runs `cmd` in a :terminal, and returns a `Screen` object.
|
|
|
|
---
|
|
|
|
---@param extra_rows? integer Extra rows to add to the default screen.
|
|
|
|
---@param cmd? string|string[] Command to run in the terminal (default: `{ 'tty-test' }`)
|
|
|
|
---@param cols? integer Create screen with this many columns (default: 50)
|
|
|
|
---@param env? table Environment set on the `cmd` job.
|
|
|
|
---@param screen_opts? table Options for `Screen.new()`.
|
|
|
|
---@return test.functional.ui.screen # Screen attached to the global (not child) Nvim session.
|
|
|
|
function M.setup_screen(extra_rows, cmd, cols, env, screen_opts)
|
2017-02-22 08:10:10 -07:00
|
|
|
extra_rows = extra_rows and extra_rows or 0
|
2024-09-15 03:28:14 -07:00
|
|
|
cmd = cmd and cmd or default_command
|
2017-02-22 08:10:10 -07:00
|
|
|
cols = cols and cols or 50
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_command('highlight TermCursor cterm=reverse')
|
2024-06-11 09:10:34 -07:00
|
|
|
api.nvim_command('highlight StatusLineTerm ctermbg=2 ctermfg=0')
|
|
|
|
api.nvim_command('highlight StatusLineTermNC ctermbg=2 ctermfg=8')
|
2017-02-22 08:10:10 -07:00
|
|
|
|
2024-11-11 14:15:19 -07:00
|
|
|
local screen = Screen.new(cols, 7 + extra_rows, screen_opts or { rgb = false })
|
2015-03-25 05:14:47 -07:00
|
|
|
screen:set_default_attr_ids({
|
|
|
|
[1] = { reverse = true }, -- focused cursor
|
|
|
|
[2] = { background = 11 }, -- unfocused cursor
|
2016-08-09 08:01:56 -07:00
|
|
|
[3] = { bold = true },
|
2024-02-05 23:05:49 -07:00
|
|
|
[4] = { foreground = 12 }, -- NonText in :terminal session
|
2016-08-09 08:01:56 -07:00
|
|
|
[5] = { bold = true, reverse = true },
|
2024-02-05 23:05:49 -07:00
|
|
|
[6] = { foreground = 81 }, -- SpecialKey in :terminal session
|
|
|
|
[7] = { foreground = 130 }, -- LineNr in host session
|
|
|
|
[8] = { foreground = 15, background = 1 }, -- ErrorMsg in :terminal session
|
2016-08-09 08:01:56 -07:00
|
|
|
[9] = { foreground = 4 },
|
2024-02-05 23:05:49 -07:00
|
|
|
[10] = { foreground = 121 }, -- MoreMsg in :terminal session
|
|
|
|
[11] = { foreground = 11 }, -- LineNr in :terminal session
|
2023-01-08 21:20:50 -07:00
|
|
|
[12] = { underline = true },
|
|
|
|
[13] = { underline = true, reverse = true },
|
|
|
|
[14] = { underline = true, reverse = true, bold = true },
|
|
|
|
[15] = { underline = true, foreground = 12 },
|
2024-02-05 23:05:49 -07:00
|
|
|
[16] = { background = 248, foreground = 0 }, -- Visual in :terminal session
|
2024-06-11 09:10:34 -07:00
|
|
|
[17] = { background = 2, foreground = 0 }, -- StatusLineTerm
|
|
|
|
[18] = { background = 2, foreground = 8 }, -- StatusLineTermNC
|
2015-03-25 05:14:47 -07:00
|
|
|
})
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_command('enew')
|
2024-09-15 03:28:14 -07:00
|
|
|
api.nvim_call_function('termopen', { cmd, env and { env = env } or nil })
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_input('<CR>')
|
|
|
|
local vim_errmsg = api.nvim_eval('v:errmsg')
|
2017-02-22 11:14:06 -07:00
|
|
|
if vim_errmsg and '' ~= vim_errmsg then
|
|
|
|
error(vim_errmsg)
|
|
|
|
end
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_command('setlocal scrollback=10')
|
|
|
|
api.nvim_command('startinsert')
|
|
|
|
api.nvim_input('<Ignore>') -- Add input to separate two RPC requests
|
2017-02-22 11:14:06 -07:00
|
|
|
|
|
|
|
-- tty-test puts the terminal into raw mode and echoes input. Tests work by
|
|
|
|
-- feeding termcodes to control the display and asserting by screen:expect.
|
2024-09-15 03:28:14 -07:00
|
|
|
if cmd == default_command and screen_opts == nil then
|
2017-02-22 08:10:10 -07:00
|
|
|
-- Wait for "tty ready" to be printed before each test or the terminal may
|
|
|
|
-- still be in canonical mode (will echo characters for example).
|
2018-12-18 04:50:44 -07:00
|
|
|
local empty_line = (' '):rep(cols)
|
2015-10-01 11:03:40 -07:00
|
|
|
local expected = {
|
2018-12-18 04:50:44 -07:00
|
|
|
'tty ready' .. (' '):rep(cols - 9),
|
2024-12-17 06:11:41 -07:00
|
|
|
'^' .. (' '):rep(cols),
|
2015-10-01 11:03:40 -07:00
|
|
|
empty_line,
|
|
|
|
empty_line,
|
|
|
|
empty_line,
|
|
|
|
empty_line,
|
|
|
|
}
|
2017-02-22 08:10:10 -07:00
|
|
|
for _ = 1, extra_rows do
|
2015-10-01 11:03:40 -07:00
|
|
|
table.insert(expected, empty_line)
|
|
|
|
end
|
2015-03-25 05:14:47 -07:00
|
|
|
|
2018-12-18 04:50:44 -07:00
|
|
|
table.insert(expected, '{3:-- TERMINAL --}' .. ((' '):rep(cols - 14)))
|
|
|
|
screen:expect(table.concat(expected, '|\n') .. '|')
|
2015-10-01 11:03:40 -07:00
|
|
|
else
|
2022-03-02 07:30:35 -07:00
|
|
|
-- This eval also acts as a poke_eventloop().
|
2024-01-12 10:59:57 -07:00
|
|
|
if 0 == api.nvim_eval("exists('b:terminal_job_id')") then
|
2017-02-22 11:14:06 -07:00
|
|
|
error('terminal job failed to start')
|
|
|
|
end
|
2015-10-01 11:03:40 -07:00
|
|
|
end
|
2015-03-25 05:14:47 -07:00
|
|
|
return screen
|
|
|
|
end
|
|
|
|
|
2024-09-15 03:28:14 -07:00
|
|
|
--- Spawns Nvim with `args` in a :terminal, and returns a `Screen` object.
|
|
|
|
---
|
|
|
|
--- @note Only use this if you actually need the full lifecycle/capabilities of the builtin Nvim
|
|
|
|
--- TUI. Most tests should just use `Screen.new()` directly, or plain old API calls.
|
|
|
|
---
|
|
|
|
---@param args? string[] Args passed to child Nvim.
|
|
|
|
---@param opts? table Options
|
|
|
|
---@return test.functional.ui.screen # Screen attached to the global (not child) Nvim session.
|
|
|
|
function M.setup_child_nvim(args, opts)
|
2023-12-05 15:26:46 -07:00
|
|
|
opts = opts or {}
|
2024-09-15 03:28:14 -07:00
|
|
|
local argv = { nvim_prog, unpack(args or {}) }
|
2023-12-07 10:28:46 -07:00
|
|
|
|
|
|
|
local env = opts.env or {}
|
|
|
|
if not env.VIMRUNTIME then
|
|
|
|
env.VIMRUNTIME = os.getenv('VIMRUNTIME')
|
|
|
|
end
|
|
|
|
|
2024-09-15 03:28:14 -07:00
|
|
|
return M.setup_screen(opts.extra_rows, argv, opts.cols, env)
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|