testterm.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. -- Functions to test :terminal and the Nvim TUI.
  2. -- Starts a child process in a `:terminal` and sends bytes to the child via nvim_chan_send().
  3. -- Note: the global functional/testutil.lua test-session is _host_ session, _not_
  4. -- the child session.
  5. --
  6. -- - Use `setup_screen()` to test `:terminal` behavior with an arbitrary command.
  7. -- - Use `setup_child_nvim()` to test the Nvim TUI.
  8. -- - NOTE: Only use this if your test actually needs the full lifecycle/capabilities of the
  9. -- builtin Nvim TUI. Most tests should just use `Screen.new()` directly, or plain old API calls.
  10. local n = require('test.functional.testnvim')()
  11. local Screen = require('test.functional.ui.screen')
  12. local testprg = n.testprg
  13. local exec_lua = n.exec_lua
  14. local api = n.api
  15. local nvim_prog = n.nvim_prog
  16. local M = {}
  17. function M.feed_data(data)
  18. if type(data) == 'table' then
  19. data = table.concat(data, '\n')
  20. end
  21. exec_lua('vim.api.nvim_chan_send(vim.b.terminal_job_id, ...)', data)
  22. end
  23. function M.feed_termcode(data)
  24. M.feed_data('\027' .. data)
  25. end
  26. function M.make_lua_executor(session)
  27. return function(code, ...)
  28. local status, rv = session:request('nvim_exec_lua', code, { ... })
  29. if not status then
  30. session:stop()
  31. error(rv[2])
  32. end
  33. return rv
  34. end
  35. end
  36. -- some t for controlling the terminal. the codes were taken from
  37. -- infocmp xterm-256color which is less what libvterm understands
  38. -- civis/cnorm
  39. function M.hide_cursor()
  40. M.feed_termcode('[?25l')
  41. end
  42. function M.show_cursor()
  43. M.feed_termcode('[?25h')
  44. end
  45. -- smcup/rmcup
  46. function M.enter_altscreen()
  47. M.feed_termcode('[?1049h')
  48. end
  49. function M.exit_altscreen()
  50. M.feed_termcode('[?1049l')
  51. end
  52. -- character attributes
  53. function M.set_fg(num)
  54. M.feed_termcode('[38;5;' .. num .. 'm')
  55. end
  56. function M.set_bg(num)
  57. M.feed_termcode('[48;5;' .. num .. 'm')
  58. end
  59. function M.set_bold()
  60. M.feed_termcode('[1m')
  61. end
  62. function M.set_italic()
  63. M.feed_termcode('[3m')
  64. end
  65. function M.set_underline()
  66. M.feed_termcode('[4m')
  67. end
  68. function M.set_underdouble()
  69. M.feed_termcode('[4:2m')
  70. end
  71. function M.set_undercurl()
  72. M.feed_termcode('[4:3m')
  73. end
  74. function M.set_strikethrough()
  75. M.feed_termcode('[9m')
  76. end
  77. function M.clear_attrs()
  78. M.feed_termcode('[0;10m')
  79. end
  80. -- mouse
  81. function M.enable_mouse()
  82. M.feed_termcode('[?1002h')
  83. end
  84. function M.disable_mouse()
  85. M.feed_termcode('[?1002l')
  86. end
  87. local default_command = { testprg('tty-test') }
  88. --- Runs `cmd` in a :terminal, and returns a `Screen` object.
  89. ---
  90. ---@param extra_rows? integer Extra rows to add to the default screen.
  91. ---@param cmd? string|string[] Command to run in the terminal (default: `{ 'tty-test' }`)
  92. ---@param cols? integer Create screen with this many columns (default: 50)
  93. ---@param env? table Environment set on the `cmd` job.
  94. ---@param screen_opts? table Options for `Screen.new()`.
  95. ---@return test.functional.ui.screen # Screen attached to the global (not child) Nvim session.
  96. function M.setup_screen(extra_rows, cmd, cols, env, screen_opts)
  97. extra_rows = extra_rows and extra_rows or 0
  98. cmd = cmd and cmd or default_command
  99. cols = cols and cols or 50
  100. api.nvim_command('highlight TermCursor cterm=reverse')
  101. api.nvim_command('highlight TermCursorNC ctermbg=11')
  102. api.nvim_command('highlight StatusLineTerm ctermbg=2 ctermfg=0')
  103. api.nvim_command('highlight StatusLineTermNC ctermbg=2 ctermfg=8')
  104. local screen = Screen.new(cols, 7 + extra_rows, screen_opts or { rgb = false })
  105. screen:set_default_attr_ids({
  106. [1] = { reverse = true }, -- focused cursor
  107. [2] = { background = 11 }, -- unfocused cursor
  108. [3] = { bold = true },
  109. [4] = { foreground = 12 }, -- NonText in :terminal session
  110. [5] = { bold = true, reverse = true },
  111. [6] = { foreground = 81 }, -- SpecialKey in :terminal session
  112. [7] = { foreground = 130 }, -- LineNr in host session
  113. [8] = { foreground = 15, background = 1 }, -- ErrorMsg in :terminal session
  114. [9] = { foreground = 4 },
  115. [10] = { foreground = 121 }, -- MoreMsg in :terminal session
  116. [11] = { foreground = 11 }, -- LineNr in :terminal session
  117. [12] = { underline = true },
  118. [13] = { underline = true, reverse = true },
  119. [14] = { underline = true, reverse = true, bold = true },
  120. [15] = { underline = true, foreground = 12 },
  121. [16] = { background = 248, foreground = 0 }, -- Visual in :terminal session
  122. [17] = { background = 2, foreground = 0 }, -- StatusLineTerm
  123. [18] = { background = 2, foreground = 8 }, -- StatusLineTermNC
  124. })
  125. api.nvim_command('enew')
  126. api.nvim_call_function('termopen', { cmd, env and { env = env } or nil })
  127. api.nvim_input('<CR>')
  128. local vim_errmsg = api.nvim_eval('v:errmsg')
  129. if vim_errmsg and '' ~= vim_errmsg then
  130. error(vim_errmsg)
  131. end
  132. api.nvim_command('setlocal scrollback=10')
  133. api.nvim_command('startinsert')
  134. api.nvim_input('<Ignore>') -- Add input to separate two RPC requests
  135. -- tty-test puts the terminal into raw mode and echoes input. Tests work by
  136. -- feeding termcodes to control the display and asserting by screen:expect.
  137. if cmd == default_command and screen_opts == nil then
  138. -- Wait for "tty ready" to be printed before each test or the terminal may
  139. -- still be in canonical mode (will echo characters for example).
  140. local empty_line = (' '):rep(cols)
  141. local expected = {
  142. 'tty ready' .. (' '):rep(cols - 9),
  143. '{1: }' .. (' '):rep(cols - 1),
  144. empty_line,
  145. empty_line,
  146. empty_line,
  147. empty_line,
  148. }
  149. for _ = 1, extra_rows do
  150. table.insert(expected, empty_line)
  151. end
  152. table.insert(expected, '{3:-- TERMINAL --}' .. ((' '):rep(cols - 14)))
  153. screen:expect(table.concat(expected, '|\n') .. '|')
  154. else
  155. -- This eval also acts as a poke_eventloop().
  156. if 0 == api.nvim_eval("exists('b:terminal_job_id')") then
  157. error('terminal job failed to start')
  158. end
  159. end
  160. return screen
  161. end
  162. --- Spawns Nvim with `args` in a :terminal, and returns a `Screen` object.
  163. ---
  164. --- @note Only use this if you actually need the full lifecycle/capabilities of the builtin Nvim
  165. --- TUI. Most tests should just use `Screen.new()` directly, or plain old API calls.
  166. ---
  167. ---@param args? string[] Args passed to child Nvim.
  168. ---@param opts? table Options
  169. ---@return test.functional.ui.screen # Screen attached to the global (not child) Nvim session.
  170. function M.setup_child_nvim(args, opts)
  171. opts = opts or {}
  172. local argv = { nvim_prog, unpack(args or {}) }
  173. local env = opts.env or {}
  174. if not env.VIMRUNTIME then
  175. env.VIMRUNTIME = os.getenv('VIMRUNTIME')
  176. end
  177. return M.setup_screen(opts.extra_rows, argv, opts.cols, env)
  178. end
  179. return M