aboutsummaryrefslogtreecommitdiff
path: root/test/functional/terminal/channel_spec.lua
blob: b5f3c2bd31a3119eb2e4bac59e9360fbbd0e7e32 (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
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

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')]]))
    -- 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()
    -- 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')]]))
    -- 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()
    -- 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')]]))
    -- channel has been released after one main loop iteration
    eq(chans - 1, eval('len(nvim_list_chans())'))
  end)
end)