Bram Moolenaar | 12a96de | 2018-03-11 14:44:18 +0100 | [diff] [blame] | 1 | " Tests for exiting Vim. |
| 2 | |
Bram Moolenaar | 12a96de | 2018-03-11 14:44:18 +0100 | [diff] [blame] | 3 | func Test_exiting() |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 4 | let after =<< trim [CODE] |
| 5 | au QuitPre * call writefile(["QuitPre"], "Xtestout") |
| 6 | au ExitPre * call writefile(["ExitPre"], "Xtestout", "a") |
| 7 | quit |
| 8 | [CODE] |
| 9 | |
Bram Moolenaar | 12a96de | 2018-03-11 14:44:18 +0100 | [diff] [blame] | 10 | if RunVim([], after, '') |
| 11 | call assert_equal(['QuitPre', 'ExitPre'], readfile('Xtestout')) |
| 12 | endif |
| 13 | call delete('Xtestout') |
| 14 | |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 15 | let after =<< trim [CODE] |
| 16 | au QuitPre * call writefile(["QuitPre"], "Xtestout") |
| 17 | au ExitPre * call writefile(["ExitPre"], "Xtestout", "a") |
| 18 | help |
| 19 | wincmd w |
| 20 | quit |
| 21 | [CODE] |
| 22 | |
Bram Moolenaar | 12a96de | 2018-03-11 14:44:18 +0100 | [diff] [blame] | 23 | if RunVim([], after, '') |
| 24 | call assert_equal(['QuitPre', 'ExitPre'], readfile('Xtestout')) |
| 25 | endif |
| 26 | call delete('Xtestout') |
| 27 | |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 28 | let after =<< trim [CODE] |
| 29 | au QuitPre * call writefile(["QuitPre"], "Xtestout") |
| 30 | au ExitPre * call writefile(["ExitPre"], "Xtestout", "a") |
| 31 | split |
| 32 | new |
| 33 | qall |
| 34 | [CODE] |
| 35 | |
Bram Moolenaar | 12a96de | 2018-03-11 14:44:18 +0100 | [diff] [blame] | 36 | if RunVim([], after, '') |
| 37 | call assert_equal(['QuitPre', 'ExitPre'], readfile('Xtestout')) |
| 38 | endif |
| 39 | call delete('Xtestout') |
| 40 | |
Bram Moolenaar | 34ba06b | 2019-10-20 22:27:10 +0200 | [diff] [blame] | 41 | " ExitPre autocommand splits the window, so that it's no longer the last one. |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 42 | let after =<< trim [CODE] |
| 43 | au QuitPre * call writefile(["QuitPre"], "Xtestout", "a") |
| 44 | au ExitPre * call writefile(["ExitPre"], "Xtestout", "a") |
| 45 | augroup nasty |
| 46 | au ExitPre * split |
| 47 | augroup END |
| 48 | quit |
| 49 | augroup nasty |
| 50 | au! ExitPre |
| 51 | augroup END |
| 52 | quit |
| 53 | [CODE] |
| 54 | |
Bram Moolenaar | 12a96de | 2018-03-11 14:44:18 +0100 | [diff] [blame] | 55 | if RunVim([], after, '') |
| 56 | call assert_equal(['QuitPre', 'ExitPre', 'QuitPre', 'ExitPre'], |
| 57 | \ readfile('Xtestout')) |
| 58 | endif |
| 59 | call delete('Xtestout') |
Bram Moolenaar | 34ba06b | 2019-10-20 22:27:10 +0200 | [diff] [blame] | 60 | |
| 61 | " ExitPre autocommand splits and closes the window, so that there is still |
| 62 | " one window but it's a different one. |
| 63 | let after =<< trim [CODE] |
| 64 | au QuitPre * call writefile(["QuitPre"], "Xtestout", "a") |
| 65 | au ExitPre * call writefile(["ExitPre"], "Xtestout", "a") |
| 66 | augroup nasty |
| 67 | au ExitPre * split | only |
| 68 | augroup END |
| 69 | quit |
| 70 | augroup nasty |
| 71 | au! ExitPre |
| 72 | augroup END |
| 73 | quit |
| 74 | [CODE] |
| 75 | |
| 76 | if RunVim([], after, '') |
| 77 | call assert_equal(['QuitPre', 'ExitPre', 'QuitPre', 'ExitPre'], |
| 78 | \ readfile('Xtestout')) |
| 79 | endif |
| 80 | call delete('Xtestout') |
Bram Moolenaar | 411da64 | 2023-05-10 16:53:27 +0100 | [diff] [blame] | 81 | |
| 82 | " ExitPre autocommand also executed on :wqall |
| 83 | let after =<< trim [CODE] |
| 84 | au QuitPre * call writefile(["QuitPre"], "Xtestout", "a") |
| 85 | au ExitPre * call writefile(["ExitPre"], "Xtestout", "a") |
| 86 | wqall |
| 87 | [CODE] |
| 88 | |
| 89 | if RunVim([], after, '') |
| 90 | call assert_equal(['QuitPre', 'ExitPre'], readfile('Xtestout')) |
| 91 | endif |
| 92 | call delete('Xtestout') |
Bram Moolenaar | 12a96de | 2018-03-11 14:44:18 +0100 | [diff] [blame] | 93 | endfunc |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 94 | |
Bram Moolenaar | f0068c5 | 2020-11-30 17:42:10 +0100 | [diff] [blame] | 95 | " Test for getting the Vim exit code from v:exiting |
| 96 | func Test_exit_code() |
| 97 | call assert_equal(v:null, v:exiting) |
| 98 | |
| 99 | let before =<< trim [CODE] |
| 100 | au QuitPre * call writefile(['qp = ' .. v:exiting], 'Xtestout', 'a') |
| 101 | au ExitPre * call writefile(['ep = ' .. v:exiting], 'Xtestout', 'a') |
| 102 | au VimLeavePre * call writefile(['lp = ' .. v:exiting], 'Xtestout', 'a') |
| 103 | au VimLeave * call writefile(['l = ' .. v:exiting], 'Xtestout', 'a') |
| 104 | [CODE] |
| 105 | |
| 106 | if RunVim(before, ['quit'], '') |
| 107 | call assert_equal(['qp = v:null', 'ep = v:null', 'lp = 0', 'l = 0'], readfile('Xtestout')) |
| 108 | endif |
| 109 | call delete('Xtestout') |
| 110 | |
| 111 | if RunVim(before, ['cquit'], '') |
| 112 | call assert_equal(['lp = 1', 'l = 1'], readfile('Xtestout')) |
| 113 | endif |
| 114 | call delete('Xtestout') |
| 115 | |
| 116 | if RunVim(before, ['cquit 4'], '') |
| 117 | call assert_equal(['lp = 4', 'l = 4'], readfile('Xtestout')) |
| 118 | endif |
| 119 | call delete('Xtestout') |
| 120 | endfunc |
| 121 | |
Bram Moolenaar | ca0c1ca | 2022-02-22 12:08:07 +0000 | [diff] [blame] | 122 | func Test_exit_error_reading_input() |
| 123 | CheckNotGui |
Bram Moolenaar | 29a9e69 | 2022-02-22 18:48:11 +0000 | [diff] [blame] | 124 | CheckNotMSWindows |
Bram Moolenaar | 68eab67 | 2022-02-22 17:42:48 +0000 | [diff] [blame] | 125 | " The early exit causes memory not to be freed somehow |
| 126 | CheckNotAsan |
Bram Moolenaar | cf801d4 | 2022-06-21 18:34:42 +0100 | [diff] [blame] | 127 | CheckNotValgrind |
Bram Moolenaar | ca0c1ca | 2022-02-22 12:08:07 +0000 | [diff] [blame] | 128 | |
Bram Moolenaar | 5c645a2 | 2022-09-21 22:00:03 +0100 | [diff] [blame] | 129 | call writefile([":au VimLeave * call writefile(['l = ' .. v:exiting], 'Xtestout')", ":tabnew", "q:"], 'Xscript', 'bD') |
Bram Moolenaar | ca0c1ca | 2022-02-22 12:08:07 +0000 | [diff] [blame] | 130 | |
Bram Moolenaar | 68eab67 | 2022-02-22 17:42:48 +0000 | [diff] [blame] | 131 | if RunVim([], [], '<Xscript') |
zeertzjq | 6a8b136 | 2022-02-23 12:23:08 +0000 | [diff] [blame] | 132 | call assert_equal(1, v:shell_error) |
Bram Moolenaar | ca0c1ca | 2022-02-22 12:08:07 +0000 | [diff] [blame] | 133 | call assert_equal(['l = 1'], readfile('Xtestout')) |
| 134 | endif |
Bram Moolenaar | ca0c1ca | 2022-02-22 12:08:07 +0000 | [diff] [blame] | 135 | call delete('Xtestout') |
| 136 | endfun |
| 137 | |
| 138 | |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 139 | " vim: shiftwidth=2 sts=2 expandtab |