exit_spec.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local assert_alive = n.assert_alive
  4. local command = n.command
  5. local feed_command = n.feed_command
  6. local feed = n.feed
  7. local eval = n.eval
  8. local eq = t.eq
  9. local run = n.run
  10. local fn = n.fn
  11. local nvim_prog = n.nvim_prog
  12. local pcall_err = t.pcall_err
  13. local exec_capture = n.exec_capture
  14. local poke_eventloop = n.poke_eventloop
  15. describe('v:exiting', function()
  16. local cid
  17. before_each(function()
  18. n.clear()
  19. cid = n.api.nvim_get_chan_info(0).id
  20. end)
  21. it('defaults to v:null', function()
  22. eq(1, eval('v:exiting is v:null'))
  23. end)
  24. local function test_exiting(setup_fn)
  25. local function on_setup()
  26. command('autocmd VimLeavePre * call rpcrequest(' .. cid .. ', "exit", "VimLeavePre")')
  27. command('autocmd VimLeave * call rpcrequest(' .. cid .. ', "exit", "VimLeave")')
  28. setup_fn()
  29. end
  30. local requests_args = {}
  31. local function on_request(name, args)
  32. eq('exit', name)
  33. table.insert(requests_args, args)
  34. eq(0, eval('v:exiting'))
  35. return ''
  36. end
  37. run(on_request, nil, on_setup)
  38. eq({ { 'VimLeavePre' }, { 'VimLeave' } }, requests_args)
  39. end
  40. it('is 0 on normal exit', function()
  41. test_exiting(function()
  42. command('quit')
  43. end)
  44. end)
  45. it('is 0 on exit from Ex mode involving try-catch vim-patch:8.0.0184', function()
  46. test_exiting(function()
  47. feed('gQ')
  48. feed_command('try', 'call NoFunction()', 'catch', 'echo "bye"', 'endtry', 'quit')
  49. end)
  50. end)
  51. end)
  52. describe(':cquit', function()
  53. local function test_cq(cmdline, exit_code, redir_msg)
  54. if redir_msg then
  55. eq(
  56. redir_msg,
  57. pcall_err(function()
  58. return exec_capture(cmdline)
  59. end)
  60. )
  61. poke_eventloop()
  62. assert_alive()
  63. else
  64. fn.system({ nvim_prog, '-u', 'NONE', '-i', 'NONE', '--headless', '--cmd', cmdline })
  65. eq(exit_code, eval('v:shell_error'))
  66. end
  67. end
  68. before_each(function()
  69. n.clear()
  70. end)
  71. it('exits with non-zero after :cquit', function()
  72. test_cq('cquit', 1, nil)
  73. end)
  74. it('exits with non-zero after :cquit 123', function()
  75. test_cq('cquit 123', 123, nil)
  76. end)
  77. it('exits with non-zero after :123 cquit', function()
  78. test_cq('123 cquit', 123, nil)
  79. end)
  80. it('exits with 0 after :cquit 0', function()
  81. test_cq('cquit 0', 0, nil)
  82. end)
  83. it('exits with 0 after :0 cquit', function()
  84. test_cq('0 cquit', 0, nil)
  85. end)
  86. it('exits with redir msg for multiple exit codes after :cquit 1 2', function()
  87. test_cq('cquit 1 2', nil, 'nvim_exec2(): Vim(cquit):E488: Trailing characters: 2: cquit 1 2')
  88. end)
  89. it('exits with redir msg for non-number exit code after :cquit X', function()
  90. test_cq('cquit X', nil, 'nvim_exec2(): Vim(cquit):E488: Trailing characters: X: cquit X')
  91. end)
  92. it('exits with redir msg for negative exit code after :cquit -1', function()
  93. test_cq('cquit -1', nil, 'nvim_exec2(): Vim(cquit):E488: Trailing characters: -1: cquit -1')
  94. end)
  95. end)