aboutsummaryrefslogtreecommitdiff
path: root/test/functional/terminal/channel_spec.lua
blob: 8510df53475e48293ad49259102d244ff8c90df6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
local helpers = require('test.functional.helpers')(after_each)
local Screen = require('test.functional.ui.screen')
local clear = helpers.clear
local eq = helpers.eq
local eval = helpers.eval
local command = helpers.command
local pcall_err = helpers.pcall_err
local feed = helpers.feed
local poke_eventloop = helpers.poke_eventloop
local is_os = helpers.is_os
local meths = helpers.meths
local async_meths = helpers.async_meths
local testprg = helpers.testprg
local assert_alive = helpers.assert_alive

describe('terminal channel is closed and later released if', function()
  local screen

  before_each(function()
    clear()
    screen = Screen.new()
    screen:attach()
  end)

  it('opened by nvim_open_term() and deleted by :bdelete!', function()
    command([[let id = nvim_open_term(0, {})]])
    local chans = eval('len(nvim_list_chans())')
    -- channel hasn't been released yet
    eq("Vim(call):Can't send data to closed stream",
       pcall_err(command, [[bdelete! | call chansend(id, 'test')]]))
    feed('<Ignore>')  -- add input to separate two RPC requests
    -- channel has been released after one main loop iteration
    eq(chans - 1, eval('len(nvim_list_chans())'))
  end)

  it('opened by nvim_open_term(), closed by chanclose(), and deleted by pressing a key', function()
    command('let id = nvim_open_term(0, {})')
    local chans = eval('len(nvim_list_chans())')
    -- channel has been closed but not released
    eq("Vim(call):Can't send data to closed stream",
       pcall_err(command, [[call chanclose(id) | call chansend(id, 'test')]]))
    screen:expect({any='%[Terminal closed%]'})
    eq(chans, eval('len(nvim_list_chans())'))
    -- delete terminal
    feed('i<CR>')
    -- need to first process input
    poke_eventloop()
    feed('<Ignore>')  -- add input to separate two RPC requests
    -- channel has been released after another main loop iteration
    eq(chans - 1, eval('len(nvim_list_chans())'))
  end)

  it('opened by nvim_open_term(), closed by chanclose(), and deleted by :bdelete', function()
    command('let id = nvim_open_term(0, {})')
    local chans = eval('len(nvim_list_chans())')
    -- channel has been closed but not released
    eq("Vim(call):Can't send data to closed stream",
       pcall_err(command, [[call chanclose(id) | call chansend(id, 'test')]]))
    screen:expect({any='%[Terminal closed%]'})
    eq(chans, eval('len(nvim_list_chans())'))
    -- channel still hasn't been released yet
    eq("Vim(call):Can't send data to closed stream",
       pcall_err(command, [[bdelete | call chansend(id, 'test')]]))
    feed('<Ignore>')  -- add input to separate two RPC requests
    -- channel has been released after one main loop iteration
    eq(chans - 1, eval('len(nvim_list_chans())'))
  end)

  it('opened by termopen(), exited, and deleted by pressing a key', function()
    command([[let id = termopen('echo')]])
    local chans = eval('len(nvim_list_chans())')
    -- wait for process to exit
    screen:expect({any='%[Process exited 0%]'})
    -- process has exited but channel has't been released
    eq("Vim(call):Can't send data to closed stream",
       pcall_err(command, [[call chansend(id, 'test')]]))
    eq(chans, eval('len(nvim_list_chans())'))
    -- delete terminal
    feed('i<CR>')
    -- need to first process input
    poke_eventloop()
    feed('<Ignore>')  -- add input to separate two RPC requests
    -- channel has been released after another main loop iteration
    eq(chans - 1, eval('len(nvim_list_chans())'))
  end)

  -- This indirectly covers #16264
  it('opened by termopen(), exited, and deleted by :bdelete', function()
    command([[let id = termopen('echo')]])
    local chans = eval('len(nvim_list_chans())')
    -- wait for process to exit
    screen:expect({any='%[Process exited 0%]'})
    -- process has exited but channel hasn't been released
    eq("Vim(call):Can't send data to closed stream",
       pcall_err(command, [[call chansend(id, 'test')]]))
    eq(chans, eval('len(nvim_list_chans())'))
    -- channel still hasn't been released yet
    eq("Vim(call):Can't send data to closed stream",
       pcall_err(command, [[bdelete | call chansend(id, 'test')]]))
    feed('<Ignore>')  -- add input to separate two RPC requests
    -- channel has been released after one main loop iteration
    eq(chans - 1, eval('len(nvim_list_chans())'))
  end)
end)

it('chansend sends lines to terminal channel in proper order', function()
  clear({args = {'--cmd', 'set laststatus=2'}})
  local screen = Screen.new(100, 20)
  screen:attach()
  local shells = is_os('win') and {'cmd.exe', 'pwsh.exe -nop', 'powershell.exe -nop'} or {'sh'}
  for _, sh in ipairs(shells) do
    command([[let id = termopen(']] .. sh .. [[')]])
    command([[call chansend(id, ['echo "hello"', 'echo "world"', ''])]])
    screen:expect{
      any=[[echo "hello".*echo "world"]]
    }
    command('bdelete!')
    screen:expect{
      any='%[No Name%]'
    }
  end
end)

describe('no crash when TermOpen autocommand', function()
  local screen

  before_each(function()
    clear()
    meths.set_option_value('shell', testprg('shell-test'), {})
    command('set shellcmdflag=EXE shellredir= shellpipe= shellquote= shellxquote=')
    screen = Screen.new(60, 4)
    screen:set_default_attr_ids({
      [0] = {bold = true, foreground = Screen.colors.Blue};
    })
    screen:attach()
  end)

  it('processes job exit event when using termopen()', function()
    command([[autocmd TermOpen * call input('')]])
    async_meths.command('terminal foobar')
    screen:expect{grid=[[
                                                                  |
      {0:~                                                           }|
      {0:~                                                           }|
      ^                                                            |
    ]]}
    feed('<CR>')
    screen:expect{grid=[[
      ^ready $ foobar                                              |
                                                                  |
      [Process exited 0]                                          |
                                                                  |
    ]]}
    feed('i<CR>')
    screen:expect{grid=[[
      ^                                                            |
      {0:~                                                           }|
      {0:~                                                           }|
                                                                  |
    ]]}
    assert_alive()
  end)

  it('wipes buffer and processes events when using termopen()', function()
    command([[autocmd TermOpen * bwipe! | call input('')]])
    async_meths.command('terminal foobar')
    screen:expect{grid=[[
                                                                  |
      {0:~                                                           }|
      {0:~                                                           }|
      ^                                                            |
    ]]}
    feed('<CR>')
    screen:expect{grid=[[
      ^                                                            |
      {0:~                                                           }|
      {0:~                                                           }|
                                                                  |
    ]]}
    assert_alive()
  end)

  it('wipes buffer and processes events when using nvim_open_term()', function()
    command([[autocmd TermOpen * bwipe! | call input('')]])
    async_meths.open_term(0, {})
    screen:expect{grid=[[
                                                                  |
      {0:~                                                           }|
      {0:~                                                           }|
      ^                                                            |
    ]]}
    feed('<CR>')
    screen:expect{grid=[[
      ^                                                            |
      {0:~                                                           }|
      {0:~                                                           }|
                                                                  |
    ]]}
    assert_alive()
  end)
end)