blob: 3e0919a02e8c3831e7b6b3a9e437bf84f75533dc [file] [log] [blame]
Bram Moolenaar12a96de2018-03-11 14:44:18 +01001" Tests for exiting Vim.
2
Bram Moolenaar12a96de2018-03-11 14:44:18 +01003func Test_exiting()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02004 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 Moolenaar12a96de2018-03-11 14:44:18 +010010 if RunVim([], after, '')
11 call assert_equal(['QuitPre', 'ExitPre'], readfile('Xtestout'))
12 endif
13 call delete('Xtestout')
14
Bram Moolenaarc79745a2019-05-20 22:12:34 +020015 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 Moolenaar12a96de2018-03-11 14:44:18 +010023 if RunVim([], after, '')
24 call assert_equal(['QuitPre', 'ExitPre'], readfile('Xtestout'))
25 endif
26 call delete('Xtestout')
27
Bram Moolenaarc79745a2019-05-20 22:12:34 +020028 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 Moolenaar12a96de2018-03-11 14:44:18 +010036 if RunVim([], after, '')
37 call assert_equal(['QuitPre', 'ExitPre'], readfile('Xtestout'))
38 endif
39 call delete('Xtestout')
40
Bram Moolenaar34ba06b2019-10-20 22:27:10 +020041 " ExitPre autocommand splits the window, so that it's no longer the last one.
Bram Moolenaarc79745a2019-05-20 22:12:34 +020042 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 Moolenaar12a96de2018-03-11 14:44:18 +010055 if RunVim([], after, '')
56 call assert_equal(['QuitPre', 'ExitPre', 'QuitPre', 'ExitPre'],
57 \ readfile('Xtestout'))
58 endif
59 call delete('Xtestout')
Bram Moolenaar34ba06b2019-10-20 22:27:10 +020060
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 Moolenaar411da642023-05-10 16:53:27 +010081
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 Moolenaar12a96de2018-03-11 14:44:18 +010093endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020094
Bram Moolenaarf0068c52020-11-30 17:42:10 +010095" Test for getting the Vim exit code from v:exiting
96func 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')
120endfunc
121
Bram Moolenaarca0c1ca2022-02-22 12:08:07 +0000122func Test_exit_error_reading_input()
123 CheckNotGui
Bram Moolenaar29a9e692022-02-22 18:48:11 +0000124 CheckNotMSWindows
Bram Moolenaar68eab672022-02-22 17:42:48 +0000125 " The early exit causes memory not to be freed somehow
126 CheckNotAsan
Bram Moolenaarcf801d42022-06-21 18:34:42 +0100127 CheckNotValgrind
Bram Moolenaarca0c1ca2022-02-22 12:08:07 +0000128
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100129 call writefile([":au VimLeave * call writefile(['l = ' .. v:exiting], 'Xtestout')", ":tabnew", "q:"], 'Xscript', 'bD')
Bram Moolenaarca0c1ca2022-02-22 12:08:07 +0000130
Bram Moolenaar68eab672022-02-22 17:42:48 +0000131 if RunVim([], [], '<Xscript')
zeertzjq6a8b1362022-02-23 12:23:08 +0000132 call assert_equal(1, v:shell_error)
Bram Moolenaarca0c1ca2022-02-22 12:08:07 +0000133 call assert_equal(['l = 1'], readfile('Xtestout'))
134 endif
Bram Moolenaarca0c1ca2022-02-22 12:08:07 +0000135 call delete('Xtestout')
136endfun
137
138
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200139" vim: shiftwidth=2 sts=2 expandtab