ex_terminal_spec.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local Screen = require('test.functional.ui.screen')
  4. local assert_alive = n.assert_alive
  5. local clear, poke_eventloop = n.clear, n.poke_eventloop
  6. local testprg, source, eq = n.testprg, n.source, t.eq
  7. local feed = n.feed
  8. local feed_command, eval = n.feed_command, n.eval
  9. local fn = n.fn
  10. local api = n.api
  11. local retry = t.retry
  12. local ok = t.ok
  13. local command = n.command
  14. local skip = t.skip
  15. local is_os = t.is_os
  16. local is_ci = t.is_ci
  17. describe(':terminal', function()
  18. local screen
  19. before_each(function()
  20. clear()
  21. screen = Screen.new(50, 4, { rgb = false })
  22. screen._default_attr_ids = nil
  23. end)
  24. it('does not interrupt Press-ENTER prompt #2748', function()
  25. -- Ensure that :messages shows Press-ENTER.
  26. source([[
  27. echomsg "msg1"
  28. echomsg "msg2"
  29. echomsg "msg3"
  30. ]])
  31. -- Invoke a command that emits frequent terminal activity.
  32. feed([[:terminal "]] .. testprg('shell-test') .. [[" REP 9999 !terminal_output!<cr>]])
  33. feed([[<C-\><C-N>]])
  34. poke_eventloop()
  35. -- Wait for some terminal activity.
  36. retry(nil, 4000, function()
  37. ok(fn.line('$') > 6)
  38. end)
  39. feed_command('messages')
  40. screen:expect([[
  41. msg1 |
  42. msg2 |
  43. msg3 |
  44. Press ENTER or type command to continue^ |
  45. ]])
  46. end)
  47. it('reads output buffer on terminal reporting #4151', function()
  48. skip(is_ci('cirrus') or is_os('win'))
  49. if is_os('win') then
  50. feed_command(
  51. [[terminal powershell -NoProfile -NoLogo -Command Write-Host -NoNewline "\"$([char]27)[6n\""; Start-Sleep -Milliseconds 500 ]]
  52. )
  53. else
  54. feed_command([[terminal printf '\e[6n'; sleep 0.5 ]])
  55. end
  56. screen:expect { any = '%^%[%[1;1R' }
  57. end)
  58. it('in normal-mode :split does not move cursor', function()
  59. if is_os('win') then
  60. feed_command(
  61. [[terminal for /L \\%I in (1,0,2) do ( echo foo & ping -w 100 -n 1 127.0.0.1 > nul )]]
  62. )
  63. else
  64. feed_command([[terminal while true; do echo foo; sleep .1; done]])
  65. end
  66. feed([[<C-\><C-N>M]]) -- move cursor away from last line
  67. poke_eventloop()
  68. eq(3, eval("line('$')")) -- window height
  69. eq(2, eval("line('.')")) -- cursor is in the middle
  70. feed_command('vsplit')
  71. eq(2, eval("line('.')")) -- cursor stays where we put it
  72. feed_command('split')
  73. eq(2, eval("line('.')")) -- cursor stays where we put it
  74. end)
  75. it('Enter/Leave does not increment jumplist #3723', function()
  76. feed_command('terminal')
  77. local function enter_and_leave()
  78. local lines_before = fn.line('$')
  79. -- Create a new line (in the shell). For a normal buffer this
  80. -- increments the jumplist; for a terminal-buffer it should not. #3723
  81. feed('i')
  82. poke_eventloop()
  83. feed('<CR><CR><CR><CR>')
  84. poke_eventloop()
  85. feed([[<C-\><C-N>]])
  86. poke_eventloop()
  87. -- Wait for >=1 lines to be created.
  88. retry(nil, 4000, function()
  89. ok(fn.line('$') > lines_before)
  90. end)
  91. end
  92. enter_and_leave()
  93. enter_and_leave()
  94. enter_and_leave()
  95. ok(fn.line('$') > 6) -- Verify assumption.
  96. local jumps = fn.split(fn.execute('jumps'), '\n')
  97. eq(' jump line col file/text', jumps[1])
  98. eq(3, #jumps)
  99. end)
  100. it('nvim_get_mode() in :terminal', function()
  101. command('terminal')
  102. eq({ blocking = false, mode = 'nt' }, api.nvim_get_mode())
  103. feed('i')
  104. eq({ blocking = false, mode = 't' }, api.nvim_get_mode())
  105. feed([[<C-\><C-N>]])
  106. eq({ blocking = false, mode = 'nt' }, api.nvim_get_mode())
  107. end)
  108. it(':stopinsert RPC request exits terminal-mode #7807', function()
  109. command('terminal')
  110. feed('i[tui] insert-mode')
  111. eq({ blocking = false, mode = 't' }, api.nvim_get_mode())
  112. command('stopinsert')
  113. feed('<Ignore>') -- Add input to separate two RPC requests
  114. eq({ blocking = false, mode = 'nt' }, api.nvim_get_mode())
  115. end)
  116. it(":stopinsert in normal mode doesn't break insert mode #9889", function()
  117. command('terminal')
  118. eq({ blocking = false, mode = 'nt' }, api.nvim_get_mode())
  119. command('stopinsert')
  120. feed('<Ignore>') -- Add input to separate two RPC requests
  121. eq({ blocking = false, mode = 'nt' }, api.nvim_get_mode())
  122. feed('a')
  123. eq({ blocking = false, mode = 't' }, api.nvim_get_mode())
  124. end)
  125. it('switching to terminal buffer in Insert mode goes to Terminal mode #7164', function()
  126. command('terminal')
  127. command('vnew')
  128. feed('i')
  129. command('let g:events = []')
  130. command('autocmd InsertLeave * let g:events += ["InsertLeave"]')
  131. command('autocmd TermEnter * let g:events += ["TermEnter"]')
  132. command('inoremap <F2> <Cmd>wincmd p<CR>')
  133. eq({ blocking = false, mode = 'i' }, api.nvim_get_mode())
  134. feed('<F2>')
  135. eq({ blocking = false, mode = 't' }, api.nvim_get_mode())
  136. eq({ 'InsertLeave', 'TermEnter' }, eval('g:events'))
  137. end)
  138. it('switching to terminal buffer immediately after :stopinsert #27031', function()
  139. command('terminal')
  140. command('vnew')
  141. feed('i')
  142. eq({ blocking = false, mode = 'i' }, api.nvim_get_mode())
  143. command('stopinsert | wincmd p')
  144. eq({ blocking = false, mode = 'nt' }, api.nvim_get_mode())
  145. end)
  146. end)
  147. local function test_terminal_with_fake_shell(backslash)
  148. -- shell-test.c is a fake shell that prints its arguments and exits.
  149. local shell_path = testprg('shell-test')
  150. if backslash then
  151. shell_path = shell_path:gsub('/', [[\]])
  152. end
  153. local screen
  154. before_each(function()
  155. clear()
  156. screen = Screen.new(50, 4, { rgb = false })
  157. screen._default_attr_ids = nil
  158. api.nvim_set_option_value('shell', shell_path, {})
  159. api.nvim_set_option_value('shellcmdflag', 'EXE', {})
  160. api.nvim_set_option_value('shellxquote', '', {}) -- win: avoid extra quotes
  161. end)
  162. it('with no argument, acts like jobstart(…,{term=true})', function()
  163. command('autocmd! nvim.terminal TermClose')
  164. feed_command('terminal')
  165. screen:expect([[
  166. ^ready $ |
  167. [Process exited 0] |
  168. |
  169. :terminal |
  170. ]])
  171. end)
  172. it("with no argument, and 'shell' is set to empty string", function()
  173. api.nvim_set_option_value('shell', '', {})
  174. feed_command('terminal')
  175. screen:expect([[
  176. ^ |
  177. ~ |*2
  178. E91: 'shell' option is empty |
  179. ]])
  180. end)
  181. it("with no argument, but 'shell' has arguments, acts like jobstart(…,{term=true})", function()
  182. api.nvim_set_option_value('shell', shell_path .. ' INTERACT', {})
  183. feed_command('terminal')
  184. screen:expect([[
  185. ^interact $ |
  186. |*2
  187. :terminal |
  188. ]])
  189. end)
  190. it('executes a given command through the shell', function()
  191. feed_command('terminal echo hi')
  192. screen:expect([[
  193. ^ready $ echo hi |
  194. |
  195. [Process exited 0] |
  196. :terminal echo hi |
  197. ]])
  198. end)
  199. it("executes a given command through the shell, when 'shell' has arguments", function()
  200. api.nvim_set_option_value('shell', shell_path .. ' -t jeff', {})
  201. feed_command('terminal echo hi')
  202. screen:expect([[
  203. ^jeff $ echo hi |
  204. |
  205. [Process exited 0] |
  206. :terminal echo hi |
  207. ]])
  208. end)
  209. it('allows quotes and slashes', function()
  210. feed_command([[terminal echo 'hello' \ "world"]])
  211. screen:expect([[
  212. ^ready $ echo 'hello' \ "world" |
  213. |
  214. [Process exited 0] |
  215. :terminal echo 'hello' \ "world" |
  216. ]])
  217. end)
  218. it('ex_terminal() double-free #4554', function()
  219. source([[
  220. autocmd BufNew * set shell=foo
  221. terminal]])
  222. -- Verify that BufNew actually fired (else the test is invalid).
  223. eq('foo', eval('&shell'))
  224. end)
  225. it('ignores writes if the backing stream closes', function()
  226. command('autocmd! nvim.terminal TermClose')
  227. feed_command('terminal')
  228. feed('iiXXXXXXX')
  229. poke_eventloop()
  230. -- Race: Though the shell exited (and streams were closed by SIGCHLD
  231. -- handler), :terminal cleanup is pending on the main-loop.
  232. -- This write should be ignored (not crash, #5445).
  233. feed('iiYYYYYYY')
  234. assert_alive()
  235. end)
  236. it('works with findfile()', function()
  237. command('autocmd! nvim.terminal TermClose')
  238. feed_command('terminal')
  239. eq('term://', string.match(eval('bufname("%")'), '^term://'))
  240. eq('scripts/shadacat.py', eval('findfile("scripts/shadacat.py", ".")'))
  241. end)
  242. it('works with :find', function()
  243. command('autocmd! nvim.terminal TermClose')
  244. feed_command('terminal')
  245. screen:expect([[
  246. ^ready $ |
  247. [Process exited 0] |
  248. |
  249. :terminal |
  250. ]])
  251. eq('term://', string.match(eval('bufname("%")'), '^term://'))
  252. feed([[<C-\><C-N>]])
  253. feed_command([[find */shadacat.py]])
  254. if is_os('win') then
  255. eq('scripts\\shadacat.py', eval('bufname("%")'))
  256. else
  257. eq('scripts/shadacat.py', eval('bufname("%")'))
  258. end
  259. end)
  260. it('works with gf', function()
  261. feed_command([[terminal echo "scripts/shadacat.py"]])
  262. screen:expect([[
  263. ^ready $ echo "scripts/shadacat.py" |
  264. |
  265. [Process exited 0] |
  266. :terminal echo "scripts/shadacat.py" |
  267. ]])
  268. feed([[<C-\><C-N>]])
  269. eq('term://', string.match(eval('bufname("%")'), '^term://'))
  270. feed([[ggf"lgf]])
  271. eq('scripts/shadacat.py', eval('bufname("%")'))
  272. end)
  273. it('with bufhidden=delete #3958', function()
  274. command('set hidden')
  275. eq(1, eval('&hidden'))
  276. command('autocmd BufNew * setlocal bufhidden=delete')
  277. for _ = 1, 5 do
  278. source([[
  279. execute 'edit '.reltimestr(reltime())
  280. terminal]])
  281. end
  282. end)
  283. describe('exit does not have long delay #27615', function()
  284. for _, ut in ipairs({ 5, 50, 500, 5000, 50000, 500000 }) do
  285. it(('with updatetime=%d'):format(ut), function()
  286. api.nvim_set_option_value('updatetime', ut, {})
  287. api.nvim_set_option_value('shellcmdflag', 'EXIT', {})
  288. feed_command('terminal 42')
  289. screen:expect([[
  290. ^ |
  291. [Process exited 42] |
  292. |
  293. :terminal 42 |
  294. ]])
  295. end)
  296. end
  297. end)
  298. end
  299. describe(':terminal (with fake shell)', function()
  300. test_terminal_with_fake_shell(false)
  301. if is_os('win') then
  302. describe("when 'shell' uses backslashes", function()
  303. test_terminal_with_fake_shell(true)
  304. end)
  305. end
  306. end)