exit_spec.lua 2.7 KB

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