testterm.lua 6.5 KB

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