robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. Keyesfeat(exmode): "1q:", :exmode #41010fbcb7a056c

master
4.5 KiB172 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each = t.describe, t.it, t.before_each
5local assert_alive = n.assert_alive
6local command = n.command
7local feed_command = n.feed_command
8local feed = n.feed
9local eval = n.eval
10local eq = t.eq
11local run = n.run
12local pcall_err = t.pcall_err
13local exec_capture = n.exec_capture
14local poke_eventloop = n.poke_eventloop
15
16describe('exit:', function()
17  local cid
18
19  before_each(function()
20    n.clear()
21    cid = n.api.nvim_get_chan_info(0).id
22  end)
23
24  it('v:exiting defaults to v:null', function()
25    eq(1, eval('v:exiting is v:null'))
26    eq('', eval('v:exitreason'))
27  end)
28
29  local function test_exiting(setup_fn)
30    local function on_setup()
31      command(('autocmd QuitPre     * call rpcrequest(%d, "exit", "QuitPre")'):format(cid))
32      command(('autocmd ExitPre     * call rpcrequest(%d, "exit", "ExitPre")'):format(cid))
33      command(('autocmd VimLeavePre * call rpcrequest(%d, "exit", "VimLeavePre")'):format(cid))
34      command(('autocmd VimLeave    * call rpcrequest(%d, "exit", "VimLeave")'):format(cid))
35      setup_fn()
36    end
37    local received = {}
38    local function on_request(name, args)
39      eq('exit', name)
40      table.insert(received, args)
41      eq('quit', eval('v:exitreason'))
42      if args[1] == 'VimLeavePre' or args[1] == 'VimLeave' then
43        eq(0, eval('v:exiting'))
44      end
45      return ''
46    end
47    run(on_request, nil, on_setup)
48    eq({ { 'QuitPre' }, { 'ExitPre' }, { 'VimLeavePre' }, { 'VimLeave' } }, received)
49  end
50
51  it('v:exiting=0, v:exitreason=quit on normal exit', function()
52    test_exiting(function()
53      command('quit')
54    end)
55  end)
56
57  it('v:exiting=0, v:exitreason=quit on exit from Ex mode try-catch vim-patch:8.0.0184', function()
58    test_exiting(function()
59      feed('1q:')
60      feed_command('try', 'call NoFunction()', 'catch', 'echo "bye"', 'endtry', 'quit')
61    end)
62  end)
63
64  it('resets v:exitreason if quit is cancelled', function()
65    n.api.nvim_buf_set_lines(0, 0, -1, true, { 'modified' })
66    pcall_err(command, 'quit')
67    eq('', eval('v:exitreason'))
68  end)
69end)
70
71describe(':cquit', function()
72  local function test_cq(cmdline, exit_code, redir_msg)
73    if redir_msg then
74      n.clear()
75      eq(
76        redir_msg,
77        pcall_err(function()
78          return exec_capture(cmdline)
79        end)
80      )
81      poke_eventloop()
82      assert_alive()
83      n.check_close()
84    else
85      local p = n.spawn_wait('--cmd', cmdline)
86      eq(exit_code, p.status)
87    end
88  end
89
90  it('exits with non-zero after :cquit', function()
91    test_cq('cquit', 1, nil)
92  end)
93
94  it('exits with non-zero after :cquit 123', function()
95    test_cq('cquit 123', 123, nil)
96  end)
97
98  it('exits with non-zero after :123 cquit', function()
99    test_cq('123 cquit', 123, nil)
100  end)
101
102  it('exits with 0 after :cquit 0', function()
103    test_cq('cquit 0', 0, nil)
104  end)
105
106  it('exits with 0 after :0 cquit', function()
107    test_cq('0 cquit', 0, nil)
108  end)
109
110  it('exits with redir msg for multiple exit codes after :cquit 1 2', function()
111    test_cq(
112      'cquit 1 2',
113      nil,
114      'nvim_exec2(), line 1: Vim(cquit):E488: Trailing characters: 2: cquit 1 2'
115    )
116  end)
117
118  it('exits with redir msg for non-number exit code after :cquit X', function()
119    test_cq(
120      'cquit X',
121      nil,
122      'nvim_exec2(), line 1: Vim(cquit):E488: Trailing characters: X: cquit X'
123    )
124  end)
125
126  it('exits with redir msg for negative exit code after :cquit -1', function()
127    test_cq(
128      'cquit -1',
129      nil,
130      'nvim_exec2(), line 1: Vim(cquit):E488: Trailing characters: -1: cquit -1'
131    )
132  end)
133end)
134
135describe('when piping to stdin, no crash during exit', function()
136  before_each(function()
137    n.clear()
138  end)
139
140  it('after :quit non-last window in vim.schedule() callback #14379', function()
141    n.fn.system({
142      n.nvim_prog,
143      '-es',
144      '--cmd',
145      "lua vim.schedule(function() vim.cmd('vsplit | quit') end)",
146      '+quit',
147    }, '')
148    eq(0, n.api.nvim_get_vvar('shell_error'))
149  end)
150
151  it('after :quit non-last window in vim.defer_fn() callback #14379', function()
152    n.fn.system({
153      n.nvim_prog,
154      '-es',
155      '--cmd',
156      "lua vim.defer_fn(function() vim.cmd('vsplit | quit') end, 0)",
157      '+quit',
158    }, '')
159    eq(0, n.api.nvim_get_vvar('shell_error'))
160  end)
161
162  it('after closing v:stderr channel', function()
163    n.fn.system({
164      n.nvim_prog,
165      '-es',
166      '--cmd',
167      'call chanclose(v:stderr)',
168      '+quit',
169    }, '')
170    eq(0, n.api.nvim_get_vvar('shell_error'))
171  end)
172end)