exit_spec.lua 2.9 KB

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