neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

exit_spec.lua (3725B)


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