channel_spec.lua 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local Screen = require('test.functional.ui.screen')
  4. local clear = n.clear
  5. local eq = t.eq
  6. local eval = n.eval
  7. local command = n.command
  8. local pcall_err = t.pcall_err
  9. local feed = n.feed
  10. local poke_eventloop = n.poke_eventloop
  11. local is_os = t.is_os
  12. local api = n.api
  13. local async_meths = n.async_meths
  14. local testprg = n.testprg
  15. local assert_alive = n.assert_alive
  16. describe('terminal channel is closed and later released if', function()
  17. local screen
  18. before_each(function()
  19. clear()
  20. screen = Screen.new()
  21. end)
  22. it('opened by nvim_open_term() and deleted by :bdelete!', function()
  23. command([[let id = nvim_open_term(0, {})]])
  24. local chans = eval('len(nvim_list_chans())')
  25. -- channel hasn't been released yet
  26. eq(
  27. "Vim(call):Can't send data to closed stream",
  28. pcall_err(command, [[bdelete! | call chansend(id, 'test')]])
  29. )
  30. feed('<Ignore>') -- add input to separate two RPC requests
  31. -- channel has been released after one main loop iteration
  32. eq(chans - 1, eval('len(nvim_list_chans())'))
  33. end)
  34. it('opened by nvim_open_term(), closed by chanclose(), and deleted by pressing a key', function()
  35. command('let id = nvim_open_term(0, {})')
  36. local chans = eval('len(nvim_list_chans())')
  37. -- channel has been closed but not released
  38. eq(
  39. "Vim(call):Can't send data to closed stream",
  40. pcall_err(command, [[call chanclose(id) | call chansend(id, 'test')]])
  41. )
  42. screen:expect({ any = '%[Terminal closed%]' })
  43. eq(chans, eval('len(nvim_list_chans())'))
  44. -- delete terminal
  45. feed('i<CR>')
  46. -- need to first process input
  47. poke_eventloop()
  48. feed('<Ignore>') -- add input to separate two RPC requests
  49. -- channel has been released after another main loop iteration
  50. eq(chans - 1, eval('len(nvim_list_chans())'))
  51. end)
  52. it('opened by nvim_open_term(), closed by chanclose(), and deleted by :bdelete', function()
  53. command('let id = nvim_open_term(0, {})')
  54. local chans = eval('len(nvim_list_chans())')
  55. -- channel has been closed but not released
  56. eq(
  57. "Vim(call):Can't send data to closed stream",
  58. pcall_err(command, [[call chanclose(id) | call chansend(id, 'test')]])
  59. )
  60. screen:expect({ any = '%[Terminal closed%]' })
  61. eq(chans, eval('len(nvim_list_chans())'))
  62. -- channel still hasn't been released yet
  63. eq(
  64. "Vim(call):Can't send data to closed stream",
  65. pcall_err(command, [[bdelete | call chansend(id, 'test')]])
  66. )
  67. feed('<Ignore>') -- add input to separate two RPC requests
  68. -- channel has been released after one main loop iteration
  69. eq(chans - 1, eval('len(nvim_list_chans())'))
  70. end)
  71. it('opened by termopen(), exited, and deleted by pressing a key', function()
  72. command([[let id = termopen('echo')]])
  73. local chans = eval('len(nvim_list_chans())')
  74. -- wait for process to exit
  75. screen:expect({ any = '%[Process exited 0%]' })
  76. -- process has exited but channel has't been released
  77. eq(
  78. "Vim(call):Can't send data to closed stream",
  79. pcall_err(command, [[call chansend(id, 'test')]])
  80. )
  81. eq(chans, eval('len(nvim_list_chans())'))
  82. -- delete terminal
  83. feed('i<CR>')
  84. -- need to first process input
  85. poke_eventloop()
  86. feed('<Ignore>') -- add input to separate two RPC requests
  87. -- channel has been released after another main loop iteration
  88. eq(chans - 1, eval('len(nvim_list_chans())'))
  89. end)
  90. -- This indirectly covers #16264
  91. it('opened by termopen(), exited, and deleted by :bdelete', function()
  92. command([[let id = termopen('echo')]])
  93. local chans = eval('len(nvim_list_chans())')
  94. -- wait for process to exit
  95. screen:expect({ any = '%[Process exited 0%]' })
  96. -- process has exited but channel hasn't been released
  97. eq(
  98. "Vim(call):Can't send data to closed stream",
  99. pcall_err(command, [[call chansend(id, 'test')]])
  100. )
  101. eq(chans, eval('len(nvim_list_chans())'))
  102. -- channel still hasn't been released yet
  103. eq(
  104. "Vim(call):Can't send data to closed stream",
  105. pcall_err(command, [[bdelete | call chansend(id, 'test')]])
  106. )
  107. feed('<Ignore>') -- add input to separate two RPC requests
  108. -- channel has been released after one main loop iteration
  109. eq(chans - 1, eval('len(nvim_list_chans())'))
  110. end)
  111. end)
  112. it('chansend sends lines to terminal channel in proper order', function()
  113. clear({ args = { '--cmd', 'set laststatus=2' } })
  114. local screen = Screen.new(100, 20)
  115. screen._default_attr_ids = nil
  116. local shells = is_os('win') and { 'cmd.exe', 'pwsh.exe -nop', 'powershell.exe -nop' } or { 'sh' }
  117. for _, sh in ipairs(shells) do
  118. command([[let id = termopen(']] .. sh .. [[')]])
  119. command([[call chansend(id, ['echo "hello"', 'echo "world"', ''])]])
  120. screen:expect {
  121. any = [[echo "hello".*echo "world"]],
  122. }
  123. command('bdelete!')
  124. screen:expect {
  125. any = '%[No Name%]',
  126. }
  127. end
  128. end)
  129. describe('no crash when TermOpen autocommand', function()
  130. local screen
  131. before_each(function()
  132. clear()
  133. api.nvim_set_option_value('shell', testprg('shell-test'), {})
  134. command('set shellcmdflag=EXE shellredir= shellpipe= shellquote= shellxquote=')
  135. screen = Screen.new(60, 4)
  136. screen:set_default_attr_ids({
  137. [0] = { bold = true, foreground = Screen.colors.Blue },
  138. })
  139. end)
  140. it('processes job exit event when using termopen()', function()
  141. command([[autocmd TermOpen * call input('')]])
  142. async_meths.nvim_command('terminal foobar')
  143. screen:expect {
  144. grid = [[
  145. |
  146. {0:~ }|*2
  147. ^ |
  148. ]],
  149. }
  150. feed('<CR>')
  151. screen:expect {
  152. grid = [[
  153. ^ready $ foobar |
  154. |
  155. [Process exited 0] |
  156. |
  157. ]],
  158. }
  159. feed('i<CR>')
  160. screen:expect {
  161. grid = [[
  162. ^ |
  163. {0:~ }|*2
  164. |
  165. ]],
  166. }
  167. assert_alive()
  168. end)
  169. it('wipes buffer and processes events when using termopen()', function()
  170. command([[autocmd TermOpen * bwipe! | call input('')]])
  171. async_meths.nvim_command('terminal foobar')
  172. screen:expect {
  173. grid = [[
  174. |
  175. {0:~ }|*2
  176. ^ |
  177. ]],
  178. }
  179. feed('<CR>')
  180. screen:expect {
  181. grid = [[
  182. ^ |
  183. {0:~ }|*2
  184. |
  185. ]],
  186. }
  187. assert_alive()
  188. end)
  189. it('wipes buffer and processes events when using nvim_open_term()', function()
  190. command([[autocmd TermOpen * bwipe! | call input('')]])
  191. async_meths.nvim_open_term(0, {})
  192. screen:expect {
  193. grid = [[
  194. |
  195. {0:~ }|*2
  196. ^ |
  197. ]],
  198. }
  199. feed('<CR>')
  200. screen:expect {
  201. grid = [[
  202. ^ |
  203. {0:~ }|*2
  204. |
  205. ]],
  206. }
  207. assert_alive()
  208. end)
  209. end)
  210. describe('nvim_open_term', function()
  211. local screen
  212. before_each(function()
  213. clear()
  214. screen = Screen.new(8, 10)
  215. end)
  216. it('with force_crlf=true converts newlines', function()
  217. local win = api.nvim_get_current_win()
  218. local buf = api.nvim_create_buf(false, true)
  219. local term = api.nvim_open_term(buf, { force_crlf = true })
  220. api.nvim_win_set_buf(win, buf)
  221. api.nvim_chan_send(term, 'here\nthere\nfoo\r\nbar\n\ntest')
  222. screen:expect {
  223. grid = [[
  224. ^here |
  225. there |
  226. foo |
  227. bar |
  228. |
  229. test |
  230. |*4
  231. ]],
  232. }
  233. api.nvim_chan_send(term, '\nfirst')
  234. screen:expect {
  235. grid = [[
  236. ^here |
  237. there |
  238. foo |
  239. bar |
  240. |
  241. test |
  242. first |
  243. |*3
  244. ]],
  245. }
  246. end)
  247. it('with force_crlf=false does not convert newlines', function()
  248. local win = api.nvim_get_current_win()
  249. local buf = api.nvim_create_buf(false, true)
  250. local term = api.nvim_open_term(buf, { force_crlf = false })
  251. api.nvim_win_set_buf(win, buf)
  252. api.nvim_chan_send(term, 'here\nthere')
  253. screen:expect { grid = [[
  254. ^here |
  255. there |
  256. |*8
  257. ]] }
  258. end)
  259. end)