blob: 6c2fbbd15d4125abe65688f8e3179162558655bb [file] [log] [blame]
Bram Moolenaar66459b72016-08-06 19:01:55 +02001" Tests for startup.
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02002
Bram Moolenaar66459b72016-08-06 19:01:55 +02003source shared.vim
Bram Moolenaarc75e8122019-04-21 15:55:10 +02004source screendump.vim
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02005source term_util.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02006source check.vim
Bram Moolenaar66459b72016-08-06 19:01:55 +02007
8" Check that loading startup.vim works.
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02009func Test_startup_script()
10 set compatible
11 source $VIMRUNTIME/defaults.vim
12
13 call assert_equal(0, &compatible)
Bram Moolenaarcde0ff32020-04-04 14:00:39 +020014 " Restore some options, so that the following tests doesn't break
15 set nomore
16 set noshowmode
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +020017endfunc
Bram Moolenaar66459b72016-08-06 19:01:55 +020018
19" Verify the order in which plugins are loaded:
20" 1. plugins in non-after directories
21" 2. packages
22" 3. plugins in after directories
23func Test_after_comes_later()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020024 CheckFeature packages
Bram Moolenaarc79745a2019-05-20 22:12:34 +020025 let before =<< trim [CODE]
26 set nocp viminfo+=nviminfo
27 set guioptions+=M
28 let $HOME = "/does/not/exist"
29 set loadplugins
30 set rtp=Xhere,Xafter,Xanother
31 set packpath=Xhere,Xafter
32 set nomore
33 let g:sequence = ""
34 [CODE]
35
36 let after =<< trim [CODE]
37 redir! > Xtestout
38 scriptnames
39 redir END
40 redir! > Xsequence
41 echo g:sequence
42 redir END
43 quit
44 [CODE]
45
Bram Moolenaar56564962022-10-10 22:39:42 +010046 call mkdir('Xhere/plugin', 'pR')
Bram Moolenaar07ecfa62017-06-27 14:43:55 +020047 call writefile(['let g:sequence .= "here "'], 'Xhere/plugin/here.vim')
Bram Moolenaar56564962022-10-10 22:39:42 +010048 call mkdir('Xanother/plugin', 'pR')
Bram Moolenaar07ecfa62017-06-27 14:43:55 +020049 call writefile(['let g:sequence .= "another "'], 'Xanother/plugin/another.vim')
Bram Moolenaar66459b72016-08-06 19:01:55 +020050 call mkdir('Xhere/pack/foo/start/foobar/plugin', 'p')
Bram Moolenaar07ecfa62017-06-27 14:43:55 +020051 call writefile(['let g:sequence .= "pack "'], 'Xhere/pack/foo/start/foobar/plugin/foo.vim')
Bram Moolenaar66459b72016-08-06 19:01:55 +020052
Bram Moolenaar56564962022-10-10 22:39:42 +010053 call mkdir('Xafter/plugin', 'pR')
Bram Moolenaar07ecfa62017-06-27 14:43:55 +020054 call writefile(['let g:sequence .= "after "'], 'Xafter/plugin/later.vim')
Bram Moolenaar66459b72016-08-06 19:01:55 +020055
Bram Moolenaar472a0a82016-08-06 22:31:42 +020056 if RunVim(before, after, '')
Bram Moolenaar66459b72016-08-06 19:01:55 +020057
Bram Moolenaar83b3c3d2016-08-06 19:16:43 +020058 let lines = readfile('Xtestout')
Bram Moolenaar07ecfa62017-06-27 14:43:55 +020059 let expected = ['Xbefore.vim', 'here.vim', 'another.vim', 'foo.vim', 'later.vim', 'Xafter.vim']
Bram Moolenaar83b3c3d2016-08-06 19:16:43 +020060 let found = []
61 for line in lines
62 for one in expected
63 if line =~ one
64 call add(found, one)
65 endif
66 endfor
Bram Moolenaar66459b72016-08-06 19:01:55 +020067 endfor
Bram Moolenaar83b3c3d2016-08-06 19:16:43 +020068 call assert_equal(expected, found)
69 endif
Bram Moolenaar66459b72016-08-06 19:01:55 +020070
Bram Moolenaar07ecfa62017-06-27 14:43:55 +020071 call assert_equal('here another pack after', substitute(join(readfile('Xsequence', 1), ''), '\s\+$', '', ''))
72
Bram Moolenaar66459b72016-08-06 19:01:55 +020073 call delete('Xtestout')
Bram Moolenaar07ecfa62017-06-27 14:43:55 +020074 call delete('Xsequence')
Bram Moolenaar66459b72016-08-06 19:01:55 +020075endfunc
Bram Moolenaar472a0a82016-08-06 22:31:42 +020076
Bram Moolenaarce876aa2017-06-04 17:47:42 +020077func Test_pack_in_rtp_when_plugins_run()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020078 CheckFeature packages
Bram Moolenaarc79745a2019-05-20 22:12:34 +020079 let before =<< trim [CODE]
80 set nocp viminfo+=nviminfo
81 set guioptions+=M
82 let $HOME = "/does/not/exist"
83 set loadplugins
84 set rtp=Xhere
85 set packpath=Xhere
86 set nomore
87 [CODE]
88
Bram Moolenaarce876aa2017-06-04 17:47:42 +020089 let after = [
90 \ 'quit',
91 \ ]
Bram Moolenaar56564962022-10-10 22:39:42 +010092 call mkdir('Xhere/plugin', 'pR')
Bram Moolenaarce876aa2017-06-04 17:47:42 +020093 call writefile(['redir! > Xtestout', 'silent set runtimepath?', 'silent! call foo#Trigger()', 'redir END'], 'Xhere/plugin/here.vim')
94 call mkdir('Xhere/pack/foo/start/foobar/autoload', 'p')
95 call writefile(['function! foo#Trigger()', 'echo "autoloaded foo"', 'endfunction'], 'Xhere/pack/foo/start/foobar/autoload/foo.vim')
96
97 if RunVim(before, after, '')
98
99 let lines = filter(readfile('Xtestout'), '!empty(v:val)')
100 call assert_match('Xhere[/\\]pack[/\\]foo[/\\]start[/\\]foobar', get(lines, 0))
101 call assert_match('autoloaded foo', get(lines, 1))
102 endif
103
104 call delete('Xtestout')
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200105endfunc
106
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200107func Test_help_arg()
Bram Moolenaarf8c52e82021-03-17 12:27:23 +0100108 " This does not work with a GUI-only binary, such as on MS-Windows.
109 CheckAnyOf Unix NotGui
Bram Moolenaar240309c2021-03-14 16:20:37 +0100110
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200111 if RunVim([], [], '--help >Xtestout')
112 let lines = readfile('Xtestout')
113 call assert_true(len(lines) > 20)
Bram Moolenaarba98bef2016-08-07 15:51:39 +0200114 call assert_match('Vi IMproved', lines[0])
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200115
116 " check if couple of lines are there
Bram Moolenaar50fa8dd2016-08-09 22:58:21 +0200117 let found = []
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200118 for line in lines
119 if line =~ '-R.*Readonly mode'
Bram Moolenaar50fa8dd2016-08-09 22:58:21 +0200120 call add(found, 'Readonly mode')
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200121 endif
Bram Moolenaar50fa8dd2016-08-09 22:58:21 +0200122 " Watch out for a second --version line in the Gnome version.
123 if line =~ '--version.*Print version information and exit'
124 call add(found, "--version")
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200125 endif
126 endfor
Bram Moolenaar50fa8dd2016-08-09 22:58:21 +0200127 call assert_equal(['Readonly mode', '--version'], found)
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200128 endif
129 call delete('Xtestout')
130endfunc
Bram Moolenaarba98bef2016-08-07 15:51:39 +0200131
132func Test_compatible_args()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200133 let after =<< trim [CODE]
134 call writefile([string(&compatible)], "Xtestout")
135 set viminfo+=nviminfo
136 quit
137 [CODE]
138
Bram Moolenaarba98bef2016-08-07 15:51:39 +0200139 if RunVim([], after, '-C')
140 let lines = readfile('Xtestout')
141 call assert_equal('1', lines[0])
142 endif
143
144 if RunVim([], after, '-N')
145 let lines = readfile('Xtestout')
146 call assert_equal('0', lines[0])
147 endif
148
149 call delete('Xtestout')
150endfunc
151
Bram Moolenaar8f4499b2018-09-16 16:28:11 +0200152" Test the -o[N] and -O[N] arguments to open N windows split
153" horizontally or vertically.
154func Test_o_arg()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200155 let after =<< trim [CODE]
Bram Moolenaare96a2492019-06-25 04:12:16 +0200156 set cpo&vim
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200157 call writefile([winnr("$"),
158 \ winheight(1), winheight(2), &lines,
159 \ winwidth(1), winwidth(2), &columns,
160 \ bufname(winbufnr(1)), bufname(winbufnr(2))],
161 \ "Xtestout")
162 qall
163 [CODE]
164
Bram Moolenaar8f4499b2018-09-16 16:28:11 +0200165 if RunVim([], after, '-o2')
166 " Open 2 windows split horizontally. Expect:
167 " - 2 windows
168 " - both windows should have the same or almost the same height
169 " - sum of both windows height (+ 3 for both statusline and Ex command)
170 " should be equal to the number of lines
171 " - both windows should have the same width which should be equal to the
172 " number of columns
173 " - buffer of both windows should have no name
174 let [wn, wh1, wh2, ln, ww1, ww2, cn, bn1, bn2] = readfile('Xtestout')
175 call assert_equal('2', wn)
176 call assert_inrange(0, 1, wh1 - wh2)
177 call assert_equal(string(wh1 + wh2 + 3), ln)
178 call assert_equal(ww1, ww2)
179 call assert_equal(ww1, cn)
180 call assert_equal('', bn1)
181 call assert_equal('', bn2)
182 endif
183
184 if RunVim([], after, '-o foo bar')
185 " Same expectations as for -o2 but buffer names should be foo and bar
186 let [wn, wh1, wh2, ln, ww1, ww2, cn, bn1, bn2] = readfile('Xtestout')
187 call assert_equal('2', wn)
188 call assert_inrange(0, 1, wh1 - wh2)
189 call assert_equal(string(wh1 + wh2 + 3), ln)
190 call assert_equal(ww1, ww2)
191 call assert_equal(ww1, cn)
192 call assert_equal('foo', bn1)
193 call assert_equal('bar', bn2)
194 endif
195
196 if RunVim([], after, '-O2')
197 " Open 2 windows split vertically. Expect:
198 " - 2 windows
199 " - both windows should have the same or almost the same width
Bram Moolenaar036b09c2018-09-21 12:54:06 +0200200 " - sum of both windows width (+ 1 for the separator) should be equal to
201 " the number of columns
Bram Moolenaar8f4499b2018-09-16 16:28:11 +0200202 " - both windows should have the same height
203 " - window height (+ 2 for the statusline and Ex command) should be equal
204 " to the number of lines
Bram Moolenaar9e81db92018-09-18 22:37:31 +0200205 " - buffer of both windows should have no name
Bram Moolenaar8f4499b2018-09-16 16:28:11 +0200206 let [wn, wh1, wh2, ln, ww1, ww2, cn, bn1, bn2] = readfile('Xtestout')
207 call assert_equal('2', wn)
208 call assert_inrange(0, 1, ww1 - ww2)
209 call assert_equal(string(ww1 + ww2 + 1), cn)
210 call assert_equal(wh1, wh2)
211 call assert_equal(string(wh1 + 2), ln)
212 call assert_equal('', bn1)
213 call assert_equal('', bn2)
214 endif
215
216 if RunVim([], after, '-O foo bar')
217 " Same expectations as for -O2 but buffer names should be foo and bar
218 let [wn, wh1, wh2, ln, ww1, ww2, cn, bn1, bn2] = readfile('Xtestout')
219 call assert_equal('2', wn)
220 call assert_inrange(0, 1, ww1 - ww2)
221 call assert_equal(string(ww1 + ww2 + 1), cn)
222 call assert_equal(wh1, wh2)
223 call assert_equal(string(wh1 + 2), ln)
224 call assert_equal('foo', bn1)
225 call assert_equal('bar', bn2)
226 endif
Bram Moolenaar8f4499b2018-09-16 16:28:11 +0200227 call delete('Xtestout')
228endfunc
229
Bram Moolenaar9e81db92018-09-18 22:37:31 +0200230" Test the -p[N] argument to open N tabpages.
231func Test_p_arg()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200232 let after =<< trim [CODE]
233 call writefile(split(execute("tabs"), "\n"), "Xtestout")
234 qall
235 [CODE]
236
Bram Moolenaar9e81db92018-09-18 22:37:31 +0200237 if RunVim([], after, '-p2')
238 let lines = readfile('Xtestout')
239 call assert_equal(4, len(lines))
240 call assert_equal('Tab page 1', lines[0])
241 call assert_equal('> [No Name]', lines[1])
242 call assert_equal('Tab page 2', lines[2])
243 call assert_equal(' [No Name]', lines[3])
244 endif
245
246 if RunVim([], after, '-p foo bar')
247 let lines = readfile('Xtestout')
248 call assert_equal(4, len(lines))
249 call assert_equal('Tab page 1', lines[0])
250 call assert_equal('> foo', lines[1])
251 call assert_equal('Tab page 2', lines[2])
252 call assert_equal(' bar', lines[3])
253 endif
254
255 call delete('Xtestout')
256endfunc
257
Bram Moolenaar4b1c9a92018-09-19 21:06:31 +0200258" Test the -V[N] argument to set the 'verbose' option to [N]
Bram Moolenaar9e81db92018-09-18 22:37:31 +0200259func Test_V_arg()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200260 " Can't catch the output of gvim.
261 CheckNotGui
262
Bram Moolenaar9e81db92018-09-18 22:37:31 +0200263 let out = system(GetVimCommand() . ' --clean -es -X -V0 -c "set verbose?" -cq')
264 call assert_equal(" verbose=0\n", out)
265
266 let out = system(GetVimCommand() . ' --clean -es -X -V2 -c "set verbose?" -cq')
Bram Moolenaar4515bcd2020-05-03 18:21:04 +0200267 call assert_match("sourcing \"$VIMRUNTIME[\\/]defaults\.vim\"\r\nline \\d\\+: sourcing \"[^\"]*runtime[\\/]filetype\.vim\".*\n", out)
Bram Moolenaar4b1c9a92018-09-19 21:06:31 +0200268 call assert_match(" verbose=2\n", out)
Bram Moolenaar9e81db92018-09-18 22:37:31 +0200269
270 let out = system(GetVimCommand() . ' --clean -es -X -V15 -c "set verbose?" -cq')
Bram Moolenaar4b1c9a92018-09-19 21:06:31 +0200271 call assert_match("sourcing \"$VIMRUNTIME[\\/]defaults\.vim\"\r\nline 1: \" The default vimrc file\..* verbose=15\n", out)
Bram Moolenaar9e81db92018-09-18 22:37:31 +0200272endfunc
273
Christian Brabandt1d3a14e2021-05-29 19:53:50 +0200274" Test that an error is shown when the defaults.vim file could not be read
Bram Moolenaara83d0602021-06-02 16:49:32 +0200275func Test_defaults_error()
276 " Can't catch the output of gvim.
277 CheckNotGui
278 CheckNotMSWindows
279 " For unknown reasons freeing all memory does not work here, even though
280 " EXITFREE is defined.
281 CheckNotAsan
282
283 let out = system('VIMRUNTIME=/tmp ' .. GetVimCommand() .. ' --clean -cq')
284 call assert_match("E1187: Failed to source defaults.vim", out)
285
286 let out = system('VIMRUNTIME=/tmp ' .. GetVimCommand() .. ' -u DEFAULTS -cq')
287 call assert_match("E1187: Failed to source defaults.vim", out)
288endfunc
Christian Brabandt1d3a14e2021-05-29 19:53:50 +0200289
Bram Moolenaar54948182018-12-28 18:32:56 +0100290" Test the '-q [errorfile]' argument.
291func Test_q_arg()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100292 CheckFeature quickfix
293
Bram Moolenaar1e1f6122020-07-15 11:19:11 +0200294 let lines =<< trim END
295 /* some file with an error */
296 main() {
297 functionCall(arg; arg, arg);
298 return 666
299 }
300 END
Bram Moolenaar56564962022-10-10 22:39:42 +0100301 call writefile(lines, 'Xbadfile.c', 'D')
Bram Moolenaar1e1f6122020-07-15 11:19:11 +0200302
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200303 let after =<< trim [CODE]
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100304 call writefile([&errorfile, string(getpos("."))], "XtestoutQarg")
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200305 copen
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100306 w >> XtestoutQarg
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200307 qall
308 [CODE]
Bram Moolenaar54948182018-12-28 18:32:56 +0100309
310 " Test with default argument '-q'.
311 call assert_equal('errors.err', &errorfile)
Bram Moolenaar56564962022-10-10 22:39:42 +0100312 call writefile(["Xbadfile.c:4:12: error: expected ';' before '}' token"], 'errors.err', 'D')
Bram Moolenaar54948182018-12-28 18:32:56 +0100313 if RunVim([], after, '-q')
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100314 let lines = readfile('XtestoutQarg')
Bram Moolenaar54948182018-12-28 18:32:56 +0100315 call assert_equal(['errors.err',
Bram Moolenaar1e1f6122020-07-15 11:19:11 +0200316 \ '[0, 4, 12, 0]',
317 \ "Xbadfile.c|4 col 12| error: expected ';' before '}' token"],
Bram Moolenaar54948182018-12-28 18:32:56 +0100318 \ lines)
319 endif
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100320 call delete('XtestoutQarg')
Bram Moolenaar54948182018-12-28 18:32:56 +0100321
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100322 " Test with explicit argument '-q XerrorsQarg' (with space).
Bram Moolenaar56564962022-10-10 22:39:42 +0100323 call writefile(["Xbadfile.c:4:12: error: expected ';' before '}' token"], 'XerrorsQarg', 'D')
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100324 if RunVim([], after, '-q XerrorsQarg')
325 let lines = readfile('XtestoutQarg')
326 call assert_equal(['XerrorsQarg',
Bram Moolenaar1e1f6122020-07-15 11:19:11 +0200327 \ '[0, 4, 12, 0]',
328 \ "Xbadfile.c|4 col 12| error: expected ';' before '}' token"],
Bram Moolenaar54948182018-12-28 18:32:56 +0100329 \ lines)
330 endif
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100331 call delete('XtestoutQarg')
Bram Moolenaar54948182018-12-28 18:32:56 +0100332
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100333 " Test with explicit argument '-qXerrorsQarg' (without space).
334 if RunVim([], after, '-qXerrorsQarg')
335 let lines = readfile('XtestoutQarg')
336 call assert_equal(['XerrorsQarg',
Bram Moolenaar1e1f6122020-07-15 11:19:11 +0200337 \ '[0, 4, 12, 0]',
338 \ "Xbadfile.c|4 col 12| error: expected ';' before '}' token"],
Bram Moolenaar54948182018-12-28 18:32:56 +0100339 \ lines)
340 endif
341
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200342 " Test with a non-existing error file (exits with value 3)
343 let out = system(GetVimCommand() .. ' -q xyz.err')
344 call assert_equal(3, v:shell_error)
345
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100346 call delete('XtestoutQarg')
Bram Moolenaar54948182018-12-28 18:32:56 +0100347endfunc
348
Bram Moolenaar036b09c2018-09-21 12:54:06 +0200349" Test the -V[N]{filename} argument to set the 'verbose' option to N
350" and set 'verbosefile' to filename.
351func Test_V_file_arg()
Bram Moolenaar4841a7c2018-09-22 14:08:49 +0200352 if RunVim([], [], ' --clean -V2Xverbosefile -c "set verbose? verbosefile?" -cq')
Bram Moolenaar036b09c2018-09-21 12:54:06 +0200353 let out = join(readfile('Xverbosefile'), "\n")
354 call assert_match("sourcing \"$VIMRUNTIME[\\/]defaults\.vim\"\n", out)
355 call assert_match("\n verbose=2\n", out)
356 call assert_match("\n verbosefile=Xverbosefile", out)
357 endif
358
359 call delete('Xverbosefile')
360endfunc
361
362" Test the -m, -M and -R arguments:
363" -m resets 'write'
364" -M resets 'modifiable' and 'write'
365" -R sets 'readonly'
366func Test_m_M_R()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200367 let after =<< trim [CODE]
368 call writefile([&write, &modifiable, &readonly, &updatecount], "Xtestout")
369 qall
370 [CODE]
371
Bram Moolenaar036b09c2018-09-21 12:54:06 +0200372 if RunVim([], after, '')
373 let lines = readfile('Xtestout')
374 call assert_equal(['1', '1', '0', '200'], lines)
375 endif
376 if RunVim([], after, '-m')
377 let lines = readfile('Xtestout')
378 call assert_equal(['0', '1', '0', '200'], lines)
379 endif
380 if RunVim([], after, '-M')
381 let lines = readfile('Xtestout')
382 call assert_equal(['0', '0', '0', '200'], lines)
383 endif
384 if RunVim([], after, '-R')
385 let lines = readfile('Xtestout')
386 call assert_equal(['1', '1', '1', '10000'], lines)
387 endif
388
389 call delete('Xtestout')
390endfunc
391
Bram Moolenaar9e81db92018-09-18 22:37:31 +0200392" Test the -A, -F and -H arguments (Arabic, Farsi and Hebrew modes).
393func Test_A_F_H_arg()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200394 let after =<< trim [CODE]
Bram Moolenaar56564962022-10-10 22:39:42 +0100395 call writefile([&rightleft, &arabic, &fkmap, &hkmap], "Xtestout", 'D')
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200396 qall
397 [CODE]
398
Bram Moolenaar4b1c9a92018-09-19 21:06:31 +0200399 " Use silent Ex mode to avoid the hit-Enter prompt for the warning that
400 " 'encoding' is not utf-8.
401 if has('arabic') && &encoding == 'utf-8' && RunVim([], after, '-e -s -A')
Bram Moolenaar9e81db92018-09-18 22:37:31 +0200402 let lines = readfile('Xtestout')
403 call assert_equal(['1', '1', '0', '0'], lines)
404 endif
405
406 if has('farsi') && RunVim([], after, '-F')
407 let lines = readfile('Xtestout')
408 call assert_equal(['1', '0', '1', '0'], lines)
409 endif
410
411 if has('rightleft') && RunVim([], after, '-H')
412 let lines = readfile('Xtestout')
413 call assert_equal(['1', '0', '0', '1'], lines)
414 endif
Bram Moolenaar9e81db92018-09-18 22:37:31 +0200415endfunc
416
Bram Moolenaar240309c2021-03-14 16:20:37 +0100417" Test the --echo-wid argument (for GTK GUI only).
418func Test_echo_wid()
419 CheckCanRunGui
420 CheckFeature gui_gtk
421
422 if RunVim([], [], '-g --echo-wid -cq >Xtest_echo_wid')
423 let lines = readfile('Xtest_echo_wid')
424 call assert_equal(1, len(lines))
425 call assert_match('^WID: \d\+$', lines[0])
426 endif
427
428 call delete('Xtest_echo_wid')
429endfunction
430
431" Test the -reverse and +reverse arguments (for GUI only).
432func Test_reverse()
433 CheckCanRunGui
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100434 CheckAnyOf Feature:gui_gtk Feature:gui_motif
Bram Moolenaar240309c2021-03-14 16:20:37 +0100435
436 let after =<< trim [CODE]
437 call writefile([&background], "Xtest_reverse")
438 qall
439 [CODE]
440 if RunVim([], after, '-f -g -reverse')
441 let lines = readfile('Xtest_reverse')
442 call assert_equal(['dark'], lines)
443 endif
444 if RunVim([], after, '-f -g +reverse')
445 let lines = readfile('Xtest_reverse')
446 call assert_equal(['light'], lines)
447 endif
448
449 call delete('Xtest_reverse')
450endfunc
451
452" Test the -background and -foreground arguments (for GUI only).
453func Test_background_foreground()
454 CheckCanRunGui
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100455 CheckAnyOf Feature:gui_gtk Feature:gui_motif
Bram Moolenaar240309c2021-03-14 16:20:37 +0100456
457 " Is there a better way to check the effect of -background & -foreground
458 " other than merely looking at &background (dark or light)?
459 let after =<< trim [CODE]
460 call writefile([&background], "Xtest_fg_bg")
461 qall
462 [CODE]
463 if RunVim([], after, '-f -g -background darkred -foreground yellow')
464 let lines = readfile('Xtest_fg_bg')
465 call assert_equal(['dark'], lines)
466 endif
467 if RunVim([], after, '-f -g -background ivory -foreground darkgreen')
468 let lines = readfile('Xtest_fg_bg')
469 call assert_equal(['light'], lines)
470 endif
471
472 call delete('Xtest_fg_bg')
473endfunc
474
475" Test the -font argument (for GUI only).
476func Test_font()
477 CheckCanRunGui
478 CheckNotMSWindows
479
480 if has('gui_gtk')
481 let font = 'Courier 14'
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100482 elseif has('gui_motif')
Bram Moolenaar240309c2021-03-14 16:20:37 +0100483 let font = '-misc-fixed-bold-*'
484 else
485 throw 'Skipped: test does not set a valid font for this GUI'
486 endif
487
488 let after =<< trim [CODE]
489 call writefile([&guifont], "Xtest_font")
490 qall
491 [CODE]
492
493 if RunVim([], after, '--nofork -g -font "' .. font .. '"')
494 let lines = readfile('Xtest_font')
495 call assert_equal([font], lines)
496 endif
497
498 call delete('Xtest_font')
499endfunc
500
501" Test the -geometry argument (for GUI only).
502func Test_geometry()
503 CheckCanRunGui
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100504 CheckAnyOf Feature:gui_gtk Feature:gui_motif
Bram Moolenaar240309c2021-03-14 16:20:37 +0100505
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100506 if has('gui_motif')
507 " FIXME: With GUI Motif the value of getwinposx(),
Bram Moolenaar240309c2021-03-14 16:20:37 +0100508 " getwinposy() and getwinpos() do not match exactly the
509 " value given in -geometry. Why?
510 " So only check &columns and &lines for those GUIs.
511 let after =<< trim [CODE]
512 call writefile([&columns, &lines], "Xtest_geometry")
513 qall
514 [CODE]
515 if RunVim([], after, '-f -g -geometry 31x13+41+43')
516 let lines = readfile('Xtest_geometry')
517 call assert_equal(['31', '13'], lines)
518 endif
519 else
520 let after =<< trim [CODE]
521 call writefile([&columns, &lines, getwinposx(), getwinposy(), string(getwinpos())], "Xtest_geometry")
522 qall
523 [CODE]
Bram Moolenaarfa04eae2022-06-21 14:38:40 +0100524 " Some window managers have a bar at the top that pushes windows down,
525 " need to use at least 130, let's do 150
526 if RunVim([], after, '-f -g -geometry 31x13+41+150')
Bram Moolenaar240309c2021-03-14 16:20:37 +0100527 let lines = readfile('Xtest_geometry')
Bram Moolenaarb376aa22021-10-11 16:08:32 +0100528 " Depending on the GUI library and the windowing system the final size
529 " might be a bit different, allow for some tolerance. Tuned based on
530 " actual failures.
Bram Moolenaar3d031a02021-10-11 22:57:34 +0100531 call assert_inrange(31, 35, str2nr(lines[0]))
532 call assert_equal('13', lines[1])
533 call assert_equal('41', lines[2])
Bram Moolenaarfa04eae2022-06-21 14:38:40 +0100534 call assert_equal('150', lines[3])
535 call assert_equal('[41, 150]', lines[4])
Bram Moolenaar240309c2021-03-14 16:20:37 +0100536 endif
537 endif
538
539 call delete('Xtest_geometry')
540endfunc
541
542" Test the -iconic argument (for GUI only).
543func Test_iconic()
544 CheckCanRunGui
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100545 CheckAnyOf Feature:gui_gtk Feature:gui_motif
Bram Moolenaar240309c2021-03-14 16:20:37 +0100546
547 call RunVim([], [], '-f -g -iconic -cq')
548
549 " TODO: currently only start vim iconified, but does not
550 " check that vim is iconified. How could this be checked?
551endfunc
552
553
Bram Moolenaarba9ea912019-05-07 22:10:50 +0200554func Test_invalid_args()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200555 " must be able to get the output of Vim.
556 CheckUnix
557 CheckNotGui
Bram Moolenaarba9ea912019-05-07 22:10:50 +0200558
559 for opt in ['-Y', '--does-not-exist']
560 let out = split(system(GetVimCommand() .. ' ' .. opt), "\n")
561 call assert_equal(1, v:shell_error)
562 call assert_match('^VIM - Vi IMproved .* (.*)$', out[0])
563 call assert_equal('Unknown option argument: "' .. opt .. '"', out[1])
564 call assert_equal('More info with: "vim -h"', out[2])
565 endfor
566
567 for opt in ['-c', '-i', '-s', '-t', '-T', '-u', '-U', '-w', '-W', '--cmd', '--startuptime']
568 let out = split(system(GetVimCommand() .. ' ' .. opt), "\n")
569 call assert_equal(1, v:shell_error)
570 call assert_match('^VIM - Vi IMproved .* (.*)$', out[0])
571 call assert_equal('Argument missing after: "' .. opt .. '"', out[1])
572 call assert_equal('More info with: "vim -h"', out[2])
573 endfor
574
575 if has('clientserver')
Bram Moolenaarba9ea912019-05-07 22:10:50 +0200576 for opt in ['--remote', '--remote-send', '--remote-silent', '--remote-expr',
577 \ '--remote-tab', '--remote-tab-wait',
578 \ '--remote-tab-wait-silent', '--remote-tab-silent',
579 \ '--remote-wait', '--remote-wait-silent',
Bram Moolenaar27821262019-05-08 16:41:09 +0200580 \ '--servername',
Bram Moolenaarba9ea912019-05-07 22:10:50 +0200581 \ ]
582 let out = split(system(GetVimCommand() .. ' ' .. opt), "\n")
583 call assert_equal(1, v:shell_error)
584 call assert_match('^VIM - Vi IMproved .* (.*)$', out[0])
585 call assert_equal('Argument missing after: "' .. opt .. '"', out[1])
586 call assert_equal('More info with: "vim -h"', out[2])
587 endfor
588 endif
589
Bram Moolenaar240f7ab2019-05-08 17:58:15 +0200590 if has('gui_gtk')
Bram Moolenaar27821262019-05-08 16:41:09 +0200591 let out = split(system(GetVimCommand() .. ' --display'), "\n")
592 call assert_equal(1, v:shell_error)
593 call assert_match('^VIM - Vi IMproved .* (.*)$', out[0])
594 call assert_equal('Argument missing after: "--display"', out[1])
595 call assert_equal('More info with: "vim -h"', out[2])
596 endif
Bram Moolenaarba9ea912019-05-07 22:10:50 +0200597
Bram Moolenaar5416b752019-05-08 18:36:43 +0200598 if has('xterm_clipboard')
Bram Moolenaar240f7ab2019-05-08 17:58:15 +0200599 let out = split(system(GetVimCommand() .. ' -display'), "\n")
600 call assert_equal(1, v:shell_error)
601 call assert_match('^VIM - Vi IMproved .* (.*)$', out[0])
602 call assert_equal('Argument missing after: "-display"', out[1])
603 call assert_equal('More info with: "vim -h"', out[2])
604 endif
605
Bram Moolenaarba9ea912019-05-07 22:10:50 +0200606 let out = split(system(GetVimCommand() .. ' -ix'), "\n")
607 call assert_equal(1, v:shell_error)
608 call assert_match('^VIM - Vi IMproved .* (.*)$', out[0])
609 call assert_equal('Garbage after option argument: "-ix"', out[1])
610 call assert_equal('More info with: "vim -h"', out[2])
611
612 let out = split(system(GetVimCommand() .. ' - xxx'), "\n")
613 call assert_equal(1, v:shell_error)
614 call assert_match('^VIM - Vi IMproved .* (.*)$', out[0])
615 call assert_equal('Too many edit arguments: "xxx"', out[1])
616 call assert_equal('More info with: "vim -h"', out[2])
617
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100618 if has('quickfix')
Dominique Pelled176ca32021-09-09 20:45:34 +0200619 " Detect invalid repeated arguments '-t foo -t foo', '-q foo -q foo'.
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100620 for opt in ['-t', '-q']
621 let out = split(system(GetVimCommand() .. repeat(' ' .. opt .. ' foo', 2)), "\n")
622 call assert_equal(1, v:shell_error)
623 call assert_match('^VIM - Vi IMproved .* (.*)$', out[0])
624 call assert_equal('Too many edit arguments: "' .. opt .. '"', out[1])
625 call assert_equal('More info with: "vim -h"', out[2])
626 endfor
627 endif
Bram Moolenaarba9ea912019-05-07 22:10:50 +0200628
629 for opt in [' -cq', ' --cmd q', ' +', ' -S foo']
630 let out = split(system(GetVimCommand() .. repeat(opt, 11)), "\n")
631 call assert_equal(1, v:shell_error)
632 " FIXME: The error message given by Vim is not ideal in case of repeated
633 " -S foo since it does not mention -S.
634 call assert_match('^VIM - Vi IMproved .* (.*)$', out[0])
635 call assert_equal('Too many "+command", "-c command" or "--cmd command" arguments', out[1])
636 call assert_equal('More info with: "vim -h"', out[2])
637 endfor
638
Bram Moolenaar27821262019-05-08 16:41:09 +0200639 if has('gui_gtk')
Dominique Pelle6d37e8e2021-05-06 17:36:55 +0200640 let out = split(system(GetVimCommand() .. ' --socketid'), "\n")
641 call assert_equal(1, v:shell_error)
642 call assert_match('^VIM - Vi IMproved .* (.*)$', out[0])
643 call assert_equal('Argument missing after: "--socketid"', out[1])
644 call assert_equal('More info with: "vim -h"', out[2])
645
Bram Moolenaar27821262019-05-08 16:41:09 +0200646 for opt in ['--socketid x', '--socketid 0xg']
647 let out = split(system(GetVimCommand() .. ' ' .. opt), "\n")
648 call assert_equal(1, v:shell_error)
649 call assert_match('^VIM - Vi IMproved .* (.*)$', out[0])
650 call assert_equal('Invalid argument for: "--socketid"', out[1])
651 call assert_equal('More info with: "vim -h"', out[2])
652 endfor
Dominique Pelle6d37e8e2021-05-06 17:36:55 +0200653
Bram Moolenaar27821262019-05-08 16:41:09 +0200654 endif
Bram Moolenaarba9ea912019-05-07 22:10:50 +0200655endfunc
656
Bram Moolenaarba98bef2016-08-07 15:51:39 +0200657func Test_file_args()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200658 let after =<< trim [CODE]
659 call writefile(argv(), "Xtestout")
660 qall
661 [CODE]
662
Bram Moolenaarba98bef2016-08-07 15:51:39 +0200663 if RunVim([], after, '')
664 let lines = readfile('Xtestout')
665 call assert_equal(0, len(lines))
666 endif
667
668 if RunVim([], after, 'one')
669 let lines = readfile('Xtestout')
670 call assert_equal(1, len(lines))
671 call assert_equal('one', lines[0])
672 endif
673
674 if RunVim([], after, 'one two three')
675 let lines = readfile('Xtestout')
676 call assert_equal(3, len(lines))
677 call assert_equal('one', lines[0])
678 call assert_equal('two', lines[1])
679 call assert_equal('three', lines[2])
680 endif
681
682 if RunVim([], after, 'one -c echo two')
683 let lines = readfile('Xtestout')
684 call assert_equal(2, len(lines))
685 call assert_equal('one', lines[0])
686 call assert_equal('two', lines[1])
687 endif
688
689 if RunVim([], after, 'one -- -c echo two')
690 let lines = readfile('Xtestout')
691 call assert_equal(4, len(lines))
692 call assert_equal('one', lines[0])
693 call assert_equal('-c', lines[1])
694 call assert_equal('echo', lines[2])
695 call assert_equal('two', lines[3])
696 endif
697
698 call delete('Xtestout')
699endfunc
700
701func Test_startuptime()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200702 CheckFeature startuptime
Bram Moolenaarba98bef2016-08-07 15:51:39 +0200703 let after = ['qall']
704 if RunVim([], after, '--startuptime Xtestout one')
705 let lines = readfile('Xtestout')
706 let expected = ['--- VIM STARTING ---', 'parsing arguments',
707 \ 'shell init', 'inits 3', 'start termcap', 'opening buffers']
708 let found = []
709 for line in lines
710 for exp in expected
711 if line =~ exp
712 call add(found, exp)
713 endif
714 endfor
715 endfor
716 call assert_equal(expected, found)
717 endif
718 call delete('Xtestout')
719endfunc
Bram Moolenaar3a938382016-08-07 16:36:40 +0200720
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100721func Test_log()
722 CheckFeature channel
723
724 call assert_false(filereadable('Xlogfile'))
725 let after = ['qall']
726 if RunVim([], after, '--log Xlogfile')
727 call assert_equal(1, readfile('Xlogfile')
728 \ ->filter({i, l -> l =~ '==== start log session'})
729 \ ->len())
730 " second time appends to the log
731 if RunVim([], after, '--log Xlogfile')
732 call assert_equal(2, readfile('Xlogfile')
733 \ ->filter({i, l -> l =~ '==== start log session'})
734 \ ->len())
735 endif
736 endif
737 call delete('Xlogfile')
738endfunc
739
Bram Moolenaar3a938382016-08-07 16:36:40 +0200740func Test_read_stdin()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200741 let after =<< trim [CODE]
742 write Xtestout
743 quit!
744 [CODE]
745
Bram Moolenaar3a938382016-08-07 16:36:40 +0200746 if RunVimPiped([], after, '-', 'echo something | ')
747 let lines = readfile('Xtestout')
Bram Moolenaare4a76ad2016-08-07 16:50:10 +0200748 " MS-Windows adds a space after the word
749 call assert_equal(['something'], split(lines[0]))
Bram Moolenaar3a938382016-08-07 16:36:40 +0200750 endif
751 call delete('Xtestout')
752endfunc
Bram Moolenaar08cab962017-03-04 14:37:18 +0100753
754func Test_progpath()
755 " Tests normally run with "./vim" or "../vim", these must have been expanded
756 " to a full path.
757 if has('unix')
758 call assert_equal('/', v:progpath[0])
759 elseif has('win32')
760 call assert_equal(':', v:progpath[1])
761 call assert_match('[/\\]', v:progpath[2])
762 endif
763
764 " Only expect "vim" to appear in v:progname.
765 call assert_match('vim\c', v:progname)
766endfunc
Bram Moolenaard5d37532017-03-27 23:02:07 +0200767
768func Test_silent_ex_mode()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200769 " must be able to get the output of Vim.
770 CheckUnix
771 CheckNotGui
Bram Moolenaard5d37532017-03-27 23:02:07 +0200772
773 " This caused an ml_get error.
774 let out = system(GetVimCommand() . '-u NONE -es -c''set verbose=1|h|exe "%norm\<c-y>\<c-d>"'' -c cq')
775 call assert_notmatch('E315:', out)
776endfunc
Bram Moolenaar85045a72017-04-02 16:54:09 +0200777
778func Test_default_term()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200779 " must be able to get the output of Vim.
780 CheckUnix
781 CheckNotGui
Bram Moolenaar85045a72017-04-02 16:54:09 +0200782
783 let save_term = $TERM
Bram Moolenaar08f88b12017-04-02 17:21:16 +0200784 let $TERM = 'unknownxxx'
Bram Moolenaar85045a72017-04-02 16:54:09 +0200785 let out = system(GetVimCommand() . ' -c''set term'' -c cq')
786 call assert_match("defaulting to 'ansi'", out)
787 let $TERM = save_term
788endfunc
Bram Moolenaar09ca9322017-09-26 17:40:45 +0200789
790func Test_zzz_startinsert()
791 " Test :startinsert
Bram Moolenaar56564962022-10-10 22:39:42 +0100792 call writefile(['123456'], 'Xtestout', 'D')
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200793 let after =<< trim [CODE]
794 :startinsert
795 call feedkeys("foobar\<c-o>:wq\<cr>","t")
796 [CODE]
797
Bram Moolenaar09ca9322017-09-26 17:40:45 +0200798 if RunVim([], after, 'Xtestout')
799 let lines = readfile('Xtestout')
800 call assert_equal(['foobar123456'], lines)
801 endif
802 " Test :startinsert!
803 call writefile(['123456'], 'Xtestout')
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200804 let after =<< trim [CODE]
805 :startinsert!
806 call feedkeys("foobar\<c-o>:wq\<cr>","t")
807 [CODE]
808
Bram Moolenaar09ca9322017-09-26 17:40:45 +0200809 if RunVim([], after, 'Xtestout')
810 let lines = readfile('Xtestout')
811 call assert_equal(['123456foobar'], lines)
812 endif
Bram Moolenaar09ca9322017-09-26 17:40:45 +0200813endfunc
Bram Moolenaar97c2c052019-02-22 13:42:07 +0100814
815func Test_issue_3969()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200816 " Can't catch the output of gvim.
817 CheckNotGui
818
Bram Moolenaar97c2c052019-02-22 13:42:07 +0100819 " Check that message is not truncated.
820 let out = system(GetVimCommand() . ' -es -X -V1 -c "echon ''hello''" -cq')
821 call assert_equal('hello', out)
822endfunc
Bram Moolenaarc75e8122019-04-21 15:55:10 +0200823
824func Test_start_with_tabs()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200825 CheckRunVimInTerminal
Bram Moolenaarc75e8122019-04-21 15:55:10 +0200826
827 let buf = RunVimInTerminal('-p a b c', {})
828 call VerifyScreenDump(buf, 'Test_start_with_tabs', {})
829
830 " clean up
831 call StopVimInTerminal(buf)
832endfunc
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100833
Bram Moolenaard1f34e62022-01-07 19:24:20 +0000834func Test_start_in_minimal_window()
835 CheckRunVimInTerminal
836
837 let buf = RunVimInTerminal('-c "set nomore"', {'cols': 12, 'rows': 2, 'keep_t_u7': 1})
838 call term_sendkeys(buf, "ahello\<Esc>")
839 call WaitForAssert({-> assert_match('^hello', term_getline(buf, 1))})
840
841 " clean up
842 call StopVimInTerminal(buf)
843endfunc
844
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100845func Test_v_argv()
846 " Can't catch the output of gvim.
847 CheckNotGui
848
849 let out = system(GetVimCommand() . ' -es -V1 -X arg1 --cmd "echo v:argv" --cmd q')
850 let list = out->split("', '")
851 call assert_match('vim', list[0])
852 let idx = index(list, 'arg1')
853 call assert_true(idx > 2)
854 call assert_equal(['arg1', '--cmd', 'echo v:argv', '--cmd', 'q'']'], list[idx:])
855endfunc
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200856
857" Test for the "-r" recovery mode option
858func Test_r_arg()
859 " Can't catch the output of gvim.
860 CheckNotGui
861 CheckUnix
862 CheckEnglish
863 let cmd = GetVimCommand()
864 " There can be swap files anywhere, only check for the headers.
865 let expected =<< trim END
866 Swap files found:.*
867 In current directory:.*
868 In directory \~/tmp:.*
869 In directory /var/tmp:.*
870 In directory /tmp:.*
871 END
872 call assert_match(join(expected, ""), system(cmd .. " -r")->substitute("[\r\n]\\+", '', ''))
873endfunc
874
875" Test for the '-t' option to jump to a tag
876func Test_t_arg()
877 let before =<< trim [CODE]
878 set tags=Xtags
879 [CODE]
880 let after =<< trim [CODE]
881 let s = bufname('') .. ':L' .. line('.') .. 'C' .. col('.')
882 call writefile([s], "Xtestout")
883 qall
884 [CODE]
885
886 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//",
887 \ "first\tXfile1\t/^ \\zsfirst$/",
888 \ "second\tXfile1\t/^ \\zssecond$/",
889 \ "third\tXfile1\t/^ \\zsthird$/"],
Bram Moolenaar56564962022-10-10 22:39:42 +0100890 \ 'Xtags', 'D')
891 call writefile([' first', ' second', ' third'], 'Xfile1', 'D')
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200892
Bram Moolenaara2b3e7d2021-03-26 17:24:34 +0100893 for t_arg in ['-t second', '-tsecond']
Dominique Pelled176ca32021-09-09 20:45:34 +0200894 if RunVim(before, after, t_arg)
Bram Moolenaara2b3e7d2021-03-26 17:24:34 +0100895 call assert_equal(['Xfile1:L2C5'], readfile('Xtestout'), t_arg)
896 call delete('Xtestout')
897 endif
898 endfor
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200899endfunc
900
Bram Moolenaar1f33e0a2020-12-19 13:32:07 +0100901" Test the '-T' argument which sets the 'term' option.
902func Test_T_arg()
903 CheckNotGui
904 let after =<< trim [CODE]
905 call writefile([&term], "Xtest_T_arg")
906 qall
907 [CODE]
908
909 for t in ['builtin_dumb', 'builtin_ansi']
910 if RunVim([], after, '-T ' .. t)
911 let lines = readfile('Xtest_T_arg')
912 call assert_equal([t], lines)
913 endif
914 endfor
915
916 call delete('Xtest_T_arg')
917endfunc
918
919" Test the '-x' argument to read/write encrypted files.
920func Test_x_arg()
921 CheckRunVimInTerminal
922 CheckFeature cryptv
923
924 " Create an encrypted file Xtest_x_arg.
925 let buf = RunVimInTerminal('-n -x Xtest_x_arg', #{rows: 10, wait_for_ruler: 0})
926 call WaitForAssert({-> assert_match('^Enter encryption key: ', term_getline(buf, 10))})
927 call term_sendkeys(buf, "foo\n")
928 call WaitForAssert({-> assert_match('^Enter same key again: ', term_getline(buf, 10))})
929 call term_sendkeys(buf, "foo\n")
930 call WaitForAssert({-> assert_match(' All$', term_getline(buf, 10))})
931 call term_sendkeys(buf, "itest\<Esc>:w\<Enter>")
932 call WaitForAssert({-> assert_match('"Xtest_x_arg" \[New\]\[blowfish2\] 1L, 5B written',
933 \ term_getline(buf, 10))})
934 call StopVimInTerminal(buf)
935
936 " Read the encrypted file and check that it contains the expected content "test"
937 let buf = RunVimInTerminal('-n -x Xtest_x_arg', #{rows: 10, wait_for_ruler: 0})
938 call WaitForAssert({-> assert_match('^Enter encryption key: ', term_getline(buf, 10))})
939 call term_sendkeys(buf, "foo\n")
940 call WaitForAssert({-> assert_match('^Enter same key again: ', term_getline(buf, 10))})
941 call term_sendkeys(buf, "foo\n")
942 call WaitForAssert({-> assert_match('^test', term_getline(buf, 1))})
943 call StopVimInTerminal(buf)
944
945 call delete('Xtest_x_arg')
946endfunc
947
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200948" Test for entering the insert mode on startup
949func Test_start_insertmode()
950 let before =<< trim [CODE]
951 set insertmode
952 [CODE]
953 let after =<< trim [CODE]
954 call writefile(['insertmode=' .. &insertmode], 'Xtestout')
955 qall
956 [CODE]
957 if RunVim(before, after, '')
958 call assert_equal(['insertmode=1'], readfile('Xtestout'))
959 call delete('Xtestout')
960 endif
961endfunc
962
963" Test for enabling the binary mode on startup
964func Test_b_arg()
965 let after =<< trim [CODE]
966 call writefile(['binary=' .. &binary], 'Xtestout')
967 qall
968 [CODE]
969 if RunVim([], after, '-b')
970 call assert_equal(['binary=1'], readfile('Xtestout'))
971 call delete('Xtestout')
972 endif
973endfunc
974
975" Test for enabling the lisp mode on startup
976func Test_l_arg()
977 let after =<< trim [CODE]
978 let s = 'lisp=' .. &lisp .. ', showmatch=' .. &showmatch
979 call writefile([s], 'Xtestout')
980 qall
981 [CODE]
982 if RunVim([], after, '-l')
983 call assert_equal(['lisp=1, showmatch=1'], readfile('Xtestout'))
984 call delete('Xtestout')
985 endif
986endfunc
987
988" Test for specifying a non-existing vimrc file using "-u"
989func Test_missing_vimrc()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200990 CheckRunVimInTerminal
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200991 let after =<< trim [CODE]
992 call assert_match('^E282:', v:errmsg)
993 call writefile(v:errors, 'Xtestout')
994 [CODE]
Bram Moolenaar56564962022-10-10 22:39:42 +0100995 call writefile(after, 'Xafter', 'D')
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200996
997 let cmd = GetVimCommandCleanTerm() . ' -u Xvimrc_missing -S Xafter'
998 let buf = term_start(cmd, {'term_rows' : 10})
999 call WaitForAssert({-> assert_equal("running", term_getstatus(buf))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001000 call TermWait(buf)
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001001 call term_sendkeys(buf, "\n:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001002 call TermWait(buf)
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001003 call WaitForAssert({-> assert_match(':', term_getline(buf, 10))})
1004 call StopVimInTerminal(buf)
1005 call assert_equal([], readfile('Xtestout'))
Bram Moolenaar56564962022-10-10 22:39:42 +01001006
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001007 call delete('Xtestout')
1008endfunc
1009
1010" Test for using the $VIMINIT environment variable
1011func Test_VIMINIT()
1012 let after =<< trim [CODE]
1013 call assert_equal(1, exists('viminit_found'))
1014 call assert_equal('yes', viminit_found)
1015 call writefile(v:errors, 'Xtestout')
1016 qall
1017 [CODE]
Bram Moolenaar56564962022-10-10 22:39:42 +01001018 call writefile(after, 'Xafter', 'D')
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001019 let cmd = GetVimProg() . ' --not-a-term -S Xafter --cmd "set enc=utf8"'
1020 call setenv('VIMINIT', 'let viminit_found="yes"')
1021 exe "silent !" . cmd
1022 call assert_equal([], readfile('Xtestout'))
Bram Moolenaar56564962022-10-10 22:39:42 +01001023
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001024 call delete('Xtestout')
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001025endfunc
1026
1027" Test for using the $EXINIT environment variable
1028func Test_EXINIT()
1029 let after =<< trim [CODE]
1030 call assert_equal(1, exists('exinit_found'))
1031 call assert_equal('yes', exinit_found)
1032 call writefile(v:errors, 'Xtestout')
1033 qall
1034 [CODE]
Bram Moolenaar56564962022-10-10 22:39:42 +01001035 call writefile(after, 'Xafter', 'D')
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001036 let cmd = GetVimProg() . ' --not-a-term -S Xafter --cmd "set enc=utf8"'
1037 call setenv('EXINIT', 'let exinit_found="yes"')
1038 exe "silent !" . cmd
1039 call assert_equal([], readfile('Xtestout'))
Bram Moolenaar56564962022-10-10 22:39:42 +01001040
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001041 call delete('Xtestout')
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001042endfunc
1043
1044" Test for using the 'exrc' option
1045func Test_exrc()
1046 let after =<< trim [CODE]
1047 call assert_equal(1, &exrc)
1048 call assert_equal(1, &secure)
1049 call assert_equal(37, exrc_found)
1050 call writefile(v:errors, 'Xtestout')
1051 qall
1052 [CODE]
Bram Moolenaar56564962022-10-10 22:39:42 +01001053 call mkdir('Xrcdir', 'R')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001054 call writefile(['let exrc_found=37'], 'Xrcdir/.exrc')
1055 call writefile(after, 'Xrcdir/Xafter')
1056 let cmd = GetVimProg() . ' --not-a-term -S Xafter --cmd "cd Xrcdir" --cmd "set enc=utf8 exrc secure"'
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001057 exe "silent !" . cmd
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001058 call assert_equal([], readfile('Xrcdir/Xtestout'))
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001059endfunc
1060
1061" Test for starting Vim with a non-terminal as input/output
1062func Test_io_not_a_terminal()
1063 " Can't catch the output of gvim.
1064 CheckNotGui
1065 CheckUnix
1066 CheckEnglish
1067 let l = systemlist(GetVimProg() .. ' --ttyfail')
1068 call assert_equal(['Vim: Warning: Output is not to a terminal',
1069 \ 'Vim: Warning: Input is not from a terminal'], l)
1070endfunc
1071
Bram Moolenaar7007e312021-03-27 12:11:33 +01001072" Test for --not-a-term avoiding escape codes.
1073func Test_not_a_term()
1074 CheckUnix
1075 CheckNotGui
1076
1077 if &shellredir =~ '%s'
1078 let redir = printf(&shellredir, 'Xvimout')
1079 else
1080 let redir = &shellredir .. ' Xvimout'
1081 endif
1082
1083 " Without --not-a-term there are a few escape sequences.
1084 " This will take 2 seconds because of the missing --not-a-term
1085 let cmd = GetVimProg() .. ' --cmd quit ' .. redir
1086 exe "silent !" . cmd
1087 call assert_match("\<Esc>", readfile('Xvimout')->join())
1088 call delete('Xvimout')
1089
1090 " With --not-a-term there are no escape sequences.
1091 let cmd = GetVimProg() .. ' --not-a-term --cmd quit ' .. redir
1092 exe "silent !" . cmd
1093 call assert_notmatch("\<Esc>", readfile('Xvimout')->join())
1094 call delete('Xvimout')
1095endfunc
1096
Bram Moolenaar5939c352022-09-24 12:50:45 +01001097" Test quitting with CTRL-C when output is redirected.
1098func Test_redirect_Ctrl_C()
1099 CheckUnix
1100 CheckNotGui
1101 CheckRunVimInTerminal
1102
1103 let buf = Run_shell_in_terminal({})
1104 " Wait for the shell to display a prompt
1105 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
1106
1107 call term_sendkeys(buf, GetVimProg() .. " | grep word\<CR>")
1108 call WaitForAssert({-> assert_match("Output is not to a terminal", getline(1, 4)->join())})
1109 " wait for the hard coded delay, otherwise the CTRL-C interrupts startup
1110 sleep 2
1111 call term_sendkeys(buf, "\<C-C>")
1112 sleep 100m
1113 call term_sendkeys(buf, "exit\<CR>")
1114 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
1115
1116 exe buf . 'bwipe!'
1117 unlet g:job
1118endfunc
1119
Bram Moolenaar7007e312021-03-27 12:11:33 +01001120
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001121" Test for the "-w scriptout" argument
1122func Test_w_arg()
1123 " Can't catch the output of gvim.
1124 CheckNotGui
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01001125
Bram Moolenaar56564962022-10-10 22:39:42 +01001126 call writefile(["iVim Editor\<Esc>:q!\<CR>"], 'Xscriptin', 'bD')
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001127 if RunVim([], [], '-s Xscriptin -w Xscriptout')
1128 call assert_equal(["iVim Editor\e:q!\r"], readfile('Xscriptout'))
1129 call delete('Xscriptout')
1130 endif
1131 call delete('Xscriptin')
1132
1133 " Test for failing to open the script output file. This test works only when
1134 " the language is English.
1135 if v:lang == "C" || v:lang =~ '^[Ee]n'
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001136 call mkdir("Xargdir")
1137 let m = system(GetVimCommand() .. " -w Xargdir")
1138 call assert_equal("Cannot open for script output: \"Xargdir\"\n", m)
1139 call delete("Xargdir", 'rf')
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001140 endif
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01001141
1142 " A number argument sets the 'window' option
1143 call writefile(["iwindow \<C-R>=&window\<CR>\<Esc>:wq! Xresult\<CR>"], 'Xscriptin', 'b')
Bram Moolenaara2b3e7d2021-03-26 17:24:34 +01001144 for w_arg in ['-w 17', '-w17']
1145 if RunVim([], [], '-s Xscriptin ' .. w_arg)
1146 call assert_equal(["window 17"], readfile('Xresult'), w_arg)
1147 call delete('Xresult')
1148 endif
1149 endfor
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001150endfunc
1151
1152" Test for the "-s scriptin" argument
1153func Test_s_arg()
1154 " Can't catch the output of gvim.
1155 CheckNotGui
1156 CheckEnglish
1157 " Test for failing to open the script input file.
1158 let m = system(GetVimCommand() .. " -s abcxyz")
1159 call assert_equal("Cannot open for reading: \"abcxyz\"\n", m)
1160
Bram Moolenaar56564962022-10-10 22:39:42 +01001161 call writefile([], 'Xinput', 'D')
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001162 let m = system(GetVimCommand() .. " -s Xinput -s Xinput")
1163 call assert_equal("Attempt to open script file again: \"-s Xinput\"\n", m)
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001164endfunc
1165
1166" Test for the "-n" (no swap file) argument
1167func Test_n_arg()
1168 let after =<< trim [CODE]
1169 call assert_equal(0, &updatecount)
1170 call writefile(v:errors, 'Xtestout')
1171 qall
1172 [CODE]
1173 if RunVim([], after, '-n')
1174 call assert_equal([], readfile('Xtestout'))
1175 call delete('Xtestout')
1176 endif
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001177endfunc
1178
1179" Test for the "-h" (help) argument
1180func Test_h_arg()
1181 " Can't catch the output of gvim.
1182 CheckNotGui
1183 let l = systemlist(GetVimProg() .. ' -h')
1184 call assert_match('^VIM - Vi IMproved', l[0])
1185 let l = systemlist(GetVimProg() .. ' -?')
1186 call assert_match('^VIM - Vi IMproved', l[0])
1187endfunc
1188
1189" Test for the "-F" (farsi) argument
1190func Test_F_arg()
1191 " Can't catch the output of gvim.
1192 CheckNotGui
1193 let l = systemlist(GetVimProg() .. ' -F')
1194 call assert_match('^E27:', l[0])
1195endfunc
1196
1197" Test for the "-E" (improved Ex mode) argument
1198func Test_E_arg()
1199 let after =<< trim [CODE]
1200 call assert_equal('cv', mode(1))
1201 call writefile(v:errors, 'Xtestout')
1202 qall
1203 [CODE]
1204 if RunVim([], after, '-E')
1205 call assert_equal([], readfile('Xtestout'))
1206 call delete('Xtestout')
1207 endif
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001208endfunc
1209
Bram Moolenaarc5cf3692021-03-20 22:16:56 +01001210" Test for the "-D" (debugger) argument
1211func Test_D_arg()
1212 CheckRunVimInTerminal
1213
1214 let cmd = GetVimCommandCleanTerm() .. ' -D'
1215 let buf = term_start(cmd, {'term_rows' : 10})
1216 call WaitForAssert({-> assert_equal("running", term_getstatus(buf))})
1217
1218 call WaitForAssert({-> assert_equal('Entering Debug mode. Type "cont" to continue.',
1219 \ term_getline(buf, 7))})
1220 call WaitForAssert({-> assert_equal('>', term_getline(buf, 10))})
1221
1222 call StopVimInTerminal(buf)
1223endfunc
1224
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001225" Test for too many edit argument errors
1226func Test_too_many_edit_args()
1227 " Can't catch the output of gvim.
1228 CheckNotGui
1229 CheckEnglish
1230 let l = systemlist(GetVimProg() .. ' - -')
1231 call assert_match('^Too many edit arguments: "-"', l[1])
1232endfunc
1233
Bram Moolenaardf4c9af2021-01-11 19:54:42 +01001234" Test starting vim with various names: vim, ex, view, evim, etc.
1235func Test_progname()
1236 CheckUnix
1237
Bram Moolenaar56564962022-10-10 22:39:42 +01001238 call mkdir('Xprogname', 'pD')
Bram Moolenaardf4c9af2021-01-11 19:54:42 +01001239 call writefile(['silent !date',
1240 \ 'call writefile([mode(1), '
1241 \ .. '&insertmode, &diff, &readonly, &updatecount, '
1242 \ .. 'join(split(execute("message"), "\n")[1:])], "Xprogname_out")',
1243 \ 'qall'], 'Xprogname_after')
1244
1245 " +---------------------------------------------- progname
1246 " | +--------------------------------- mode(1)
1247 " | | +--------------------------- &insertmode
1248 " | | | +---------------------- &diff
1249 " | | | | +----------------- &readonly
1250 " | | | | | +-------- &updatecount
1251 " | | | | | | +--- :messages
1252 " | | | | | | |
1253 let expectations = {
1254 \ 'vim': ['n', '0', '0', '0', '200', ''],
1255 \ 'gvim': ['n', '0', '0', '0', '200', ''],
1256 \ 'ex': ['ce', '0', '0', '0', '200', ''],
1257 \ 'exim': ['cv', '0', '0', '0', '200', ''],
1258 \ 'view': ['n', '0', '0', '1', '10000', ''],
1259 \ 'gview': ['n', '0', '0', '1', '10000', ''],
1260 \ 'evim': ['n', '1', '0', '0', '200', ''],
1261 \ 'eview': ['n', '1', '0', '1', '10000', ''],
1262 \ 'rvim': ['n', '0', '0', '0', '200', 'line 1: E145: Shell commands and some functionality not allowed in rvim'],
1263 \ 'rgvim': ['n', '0', '0', '0', '200', 'line 1: E145: Shell commands and some functionality not allowed in rvim'],
1264 \ 'rview': ['n', '0', '0', '1', '10000', 'line 1: E145: Shell commands and some functionality not allowed in rvim'],
1265 \ 'rgview': ['n', '0', '0', '1', '10000', 'line 1: E145: Shell commands and some functionality not allowed in rvim'],
1266 \ 'vimdiff': ['n', '0', '1', '0', '200', ''],
1267 \ 'gvimdiff': ['n', '0', '1', '0', '200', '']}
1268
1269 let prognames = ['vim', 'gvim', 'ex', 'exim', 'view', 'gview',
1270 \ 'evim', 'eview', 'rvim', 'rgvim', 'rview', 'rgview',
1271 \ 'vimdiff', 'gvimdiff']
1272
1273 for progname in prognames
Bram Moolenaar240309c2021-03-14 16:20:37 +01001274 let run_with_gui = (progname =~# 'g') || (has('gui') && (progname ==# 'evim' || progname ==# 'eview'))
1275
1276 if empty($DISPLAY) && run_with_gui
1277 " Can't run gvim, gview (etc.) if $DISPLAY is not setup.
1278 continue
Bram Moolenaardf4c9af2021-01-11 19:54:42 +01001279 endif
1280
1281 exe 'silent !ln -s -f ' ..exepath(GetVimProg()) .. ' Xprogname/' .. progname
1282
1283 let stdout_stderr = ''
1284 if progname =~# 'g'
1285 let stdout_stderr = system('Xprogname/'..progname..' -f --clean --not-a-term -S Xprogname_after')
1286 else
1287 exe 'sil !Xprogname/'..progname..' -f --clean --not-a-term -S Xprogname_after'
1288 endif
1289
1290 if progname =~# 'g' && !has('gui')
1291 call assert_equal("E25: GUI cannot be used: Not enabled at compile time\n", stdout_stderr, progname)
1292 else
Bram Moolenaar240309c2021-03-14 16:20:37 +01001293 " GUI motif can output some warnings like this:
1294 " Warning:
1295 " Name: subMenu
1296 " Class: XmCascadeButton
1297 " Illegal mnemonic character; Could not convert X KEYSYM to a keycode
1298 " So don't check that stderr is empty with GUI Motif.
1299 if run_with_gui && !has('gui_motif')
1300 call assert_equal('', stdout_stderr, progname)
1301 endif
Bram Moolenaardf4c9af2021-01-11 19:54:42 +01001302 call assert_equal(expectations[progname], readfile('Xprogname_out'), progname)
1303 endif
1304
1305 call delete('Xprogname/' .. progname)
1306 call delete('Xprogname_out')
1307 endfor
1308
1309 call delete('Xprogname_after')
Bram Moolenaardf4c9af2021-01-11 19:54:42 +01001310endfunc
1311
Yegappan Lakshmanan36f96a52021-05-13 18:33:16 +02001312" Test for doing a write from .vimrc
1313func Test_write_in_vimrc()
Bram Moolenaar56564962022-10-10 22:39:42 +01001314 call writefile(['silent! write'], 'Xvimrc', 'D')
Yegappan Lakshmanan36f96a52021-05-13 18:33:16 +02001315 let after =<< trim [CODE]
1316 call assert_match('E32: ', v:errmsg)
1317 call writefile(v:errors, 'Xtestout')
1318 qall
1319 [CODE]
1320 if RunVim([], after, '-u Xvimrc')
1321 call assert_equal([], readfile('Xtestout'))
1322 call delete('Xtestout')
1323 endif
Yegappan Lakshmanan36f96a52021-05-13 18:33:16 +02001324endfunc
1325
Bram Moolenaara97c3632021-06-15 22:39:11 +02001326func Test_echo_true_in_cmd()
Bram Moolenaarb90ac5e2021-06-16 10:59:36 +02001327 CheckNotGui
1328
Bram Moolenaara97c3632021-06-15 22:39:11 +02001329 let lines =<< trim END
1330 echo v:true
1331 call writefile(['done'], 'Xresult')
Bram Moolenaar55b6b602021-06-15 23:05:59 +02001332 quit
Bram Moolenaara97c3632021-06-15 22:39:11 +02001333 END
Bram Moolenaar56564962022-10-10 22:39:42 +01001334 call writefile(lines, 'Xscript', 'D')
Bram Moolenaar55b6b602021-06-15 23:05:59 +02001335 if RunVim([], [], '--cmd "source Xscript"')
Bram Moolenaara97c3632021-06-15 22:39:11 +02001336 call assert_equal(['done'], readfile('Xresult'))
1337 endif
Bram Moolenaar56564962022-10-10 22:39:42 +01001338
Bram Moolenaara97c3632021-06-15 22:39:11 +02001339 call delete('Xresult')
Bram Moolenaara97c3632021-06-15 22:39:11 +02001340endfunc
1341
Bram Moolenaard3710cf2021-10-04 23:13:13 +01001342func Test_rename_buffer_on_startup()
Bram Moolenaar6d197982021-10-05 01:19:53 +01001343 CheckUnix
1344
Bram Moolenaard3710cf2021-10-04 23:13:13 +01001345 let lines =<< trim END
1346 call writefile(['done'], 'Xresult')
1347 qa!
1348 END
Bram Moolenaar56564962022-10-10 22:39:42 +01001349 call writefile(lines, 'Xscript', 'D')
Bram Moolenaard3710cf2021-10-04 23:13:13 +01001350 if RunVim([], [], "--clean -e -s --cmd 'file x|new|file x' --cmd 'so Xscript'")
1351 call assert_equal(['done'], readfile('Xresult'))
1352 endif
Bram Moolenaar56564962022-10-10 22:39:42 +01001353
Bram Moolenaard3710cf2021-10-04 23:13:13 +01001354 call delete('Xresult')
1355endfunc
1356
1357
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001358" vim: shiftwidth=2 sts=2 expandtab