blob: 3fc59bb5b9a91e945f3dba0d709d31d12788fd4c [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
4
5" Check that loading startup.vim works.
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02006func Test_startup_script()
7 set compatible
8 source $VIMRUNTIME/defaults.vim
9
10 call assert_equal(0, &compatible)
11endfunc
Bram Moolenaar66459b72016-08-06 19:01:55 +020012
13" Verify the order in which plugins are loaded:
14" 1. plugins in non-after directories
15" 2. packages
16" 3. plugins in after directories
17func Test_after_comes_later()
Bram Moolenaar32860432016-08-06 19:24:23 +020018 if !has('packages')
19 return
20 endif
Bram Moolenaar66459b72016-08-06 19:01:55 +020021 let before = [
Bram Moolenaar446cce62016-08-06 21:37:27 +020022 \ 'set nocp viminfo+=nviminfo',
23 \ 'set guioptions+=M',
Bram Moolenaar66459b72016-08-06 19:01:55 +020024 \ 'let $HOME = "/does/not/exist"',
25 \ 'set loadplugins',
Bram Moolenaar07ecfa62017-06-27 14:43:55 +020026 \ 'set rtp=Xhere,Xafter,Xanother',
Bram Moolenaar66459b72016-08-06 19:01:55 +020027 \ 'set packpath=Xhere,Xafter',
28 \ 'set nomore',
Bram Moolenaar07ecfa62017-06-27 14:43:55 +020029 \ 'let g:sequence = ""',
Bram Moolenaar66459b72016-08-06 19:01:55 +020030 \ ]
31 let after = [
32 \ 'redir! > Xtestout',
33 \ 'scriptnames',
34 \ 'redir END',
Bram Moolenaar07ecfa62017-06-27 14:43:55 +020035 \ 'redir! > Xsequence',
36 \ 'echo g:sequence',
37 \ 'redir END',
Bram Moolenaar66459b72016-08-06 19:01:55 +020038 \ 'quit',
39 \ ]
40 call mkdir('Xhere/plugin', 'p')
Bram Moolenaar07ecfa62017-06-27 14:43:55 +020041 call writefile(['let g:sequence .= "here "'], 'Xhere/plugin/here.vim')
42 call mkdir('Xanother/plugin', 'p')
43 call writefile(['let g:sequence .= "another "'], 'Xanother/plugin/another.vim')
Bram Moolenaar66459b72016-08-06 19:01:55 +020044 call mkdir('Xhere/pack/foo/start/foobar/plugin', 'p')
Bram Moolenaar07ecfa62017-06-27 14:43:55 +020045 call writefile(['let g:sequence .= "pack "'], 'Xhere/pack/foo/start/foobar/plugin/foo.vim')
Bram Moolenaar66459b72016-08-06 19:01:55 +020046
47 call mkdir('Xafter/plugin', 'p')
Bram Moolenaar07ecfa62017-06-27 14:43:55 +020048 call writefile(['let g:sequence .= "after "'], 'Xafter/plugin/later.vim')
Bram Moolenaar66459b72016-08-06 19:01:55 +020049
Bram Moolenaar472a0a82016-08-06 22:31:42 +020050 if RunVim(before, after, '')
Bram Moolenaar66459b72016-08-06 19:01:55 +020051
Bram Moolenaar83b3c3d2016-08-06 19:16:43 +020052 let lines = readfile('Xtestout')
Bram Moolenaar07ecfa62017-06-27 14:43:55 +020053 let expected = ['Xbefore.vim', 'here.vim', 'another.vim', 'foo.vim', 'later.vim', 'Xafter.vim']
Bram Moolenaar83b3c3d2016-08-06 19:16:43 +020054 let found = []
55 for line in lines
56 for one in expected
57 if line =~ one
58 call add(found, one)
59 endif
60 endfor
Bram Moolenaar66459b72016-08-06 19:01:55 +020061 endfor
Bram Moolenaar83b3c3d2016-08-06 19:16:43 +020062 call assert_equal(expected, found)
63 endif
Bram Moolenaar66459b72016-08-06 19:01:55 +020064
Bram Moolenaar07ecfa62017-06-27 14:43:55 +020065 call assert_equal('here another pack after', substitute(join(readfile('Xsequence', 1), ''), '\s\+$', '', ''))
66
Bram Moolenaar66459b72016-08-06 19:01:55 +020067 call delete('Xtestout')
Bram Moolenaar07ecfa62017-06-27 14:43:55 +020068 call delete('Xsequence')
Bram Moolenaar66459b72016-08-06 19:01:55 +020069 call delete('Xhere', 'rf')
Bram Moolenaar07ecfa62017-06-27 14:43:55 +020070 call delete('Xanother', 'rf')
Bram Moolenaar66459b72016-08-06 19:01:55 +020071 call delete('Xafter', 'rf')
72endfunc
Bram Moolenaar472a0a82016-08-06 22:31:42 +020073
Bram Moolenaarce876aa2017-06-04 17:47:42 +020074func Test_pack_in_rtp_when_plugins_run()
75 if !has('packages')
76 return
77 endif
78 let before = [
79 \ 'set nocp viminfo+=nviminfo',
80 \ 'set guioptions+=M',
81 \ 'let $HOME = "/does/not/exist"',
82 \ 'set loadplugins',
83 \ 'set rtp=Xhere',
84 \ 'set packpath=Xhere',
85 \ 'set nomore',
86 \ ]
87 let after = [
88 \ 'quit',
89 \ ]
90 call mkdir('Xhere/plugin', 'p')
91 call writefile(['redir! > Xtestout', 'silent set runtimepath?', 'silent! call foo#Trigger()', 'redir END'], 'Xhere/plugin/here.vim')
92 call mkdir('Xhere/pack/foo/start/foobar/autoload', 'p')
93 call writefile(['function! foo#Trigger()', 'echo "autoloaded foo"', 'endfunction'], 'Xhere/pack/foo/start/foobar/autoload/foo.vim')
94
95 if RunVim(before, after, '')
96
97 let lines = filter(readfile('Xtestout'), '!empty(v:val)')
98 call assert_match('Xhere[/\\]pack[/\\]foo[/\\]start[/\\]foobar', get(lines, 0))
99 call assert_match('autoloaded foo', get(lines, 1))
100 endif
101
102 call delete('Xtestout')
103 call delete('Xhere', 'rf')
104endfunc
105
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200106func Test_help_arg()
Bram Moolenaar3321e9d2016-08-06 23:03:59 +0200107 if !has('unix') && has('gui')
108 " this doesn't work with gvim on MS-Windows
109 return
110 endif
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()
133 let after = [
134 \ 'call writefile([string(&compatible)], "Xtestout")',
135 \ 'set viminfo+=nviminfo',
136 \ 'quit',
137 \ ]
138 if RunVim([], after, '-C')
139 let lines = readfile('Xtestout')
140 call assert_equal('1', lines[0])
141 endif
142
143 if RunVim([], after, '-N')
144 let lines = readfile('Xtestout')
145 call assert_equal('0', lines[0])
146 endif
147
148 call delete('Xtestout')
149endfunc
150
Bram Moolenaar8f4499b2018-09-16 16:28:11 +0200151" Test the -o[N] and -O[N] arguments to open N windows split
152" horizontally or vertically.
153func Test_o_arg()
154 let after = [
155 \ 'call writefile([winnr("$"),
156 \ winheight(1), winheight(2), &lines,
157 \ winwidth(1), winwidth(2), &columns,
158 \ bufname(winbufnr(1)), bufname(winbufnr(2))],
159 \ "Xtestout")',
160 \ 'qall',
161 \ ]
162 if RunVim([], after, '-o2')
163 " Open 2 windows split horizontally. Expect:
164 " - 2 windows
165 " - both windows should have the same or almost the same height
166 " - sum of both windows height (+ 3 for both statusline and Ex command)
167 " should be equal to the number of lines
168 " - both windows should have the same width which should be equal to the
169 " number of columns
170 " - buffer of both windows should have no name
171 let [wn, wh1, wh2, ln, ww1, ww2, cn, bn1, bn2] = readfile('Xtestout')
172 call assert_equal('2', wn)
173 call assert_inrange(0, 1, wh1 - wh2)
174 call assert_equal(string(wh1 + wh2 + 3), ln)
175 call assert_equal(ww1, ww2)
176 call assert_equal(ww1, cn)
177 call assert_equal('', bn1)
178 call assert_equal('', bn2)
179 endif
180
181 if RunVim([], after, '-o foo bar')
182 " Same expectations as for -o2 but buffer names should be foo and bar
183 let [wn, wh1, wh2, ln, ww1, ww2, cn, bn1, bn2] = readfile('Xtestout')
184 call assert_equal('2', wn)
185 call assert_inrange(0, 1, wh1 - wh2)
186 call assert_equal(string(wh1 + wh2 + 3), ln)
187 call assert_equal(ww1, ww2)
188 call assert_equal(ww1, cn)
189 call assert_equal('foo', bn1)
190 call assert_equal('bar', bn2)
191 endif
192
193 if RunVim([], after, '-O2')
194 " Open 2 windows split vertically. Expect:
195 " - 2 windows
196 " - both windows should have the same or almost the same width
197 " - sum of both windows width (+ 1 separator) should be equal to the
198 " number of columns
199 " - both windows should have the same height
200 " - window height (+ 2 for the statusline and Ex command) should be equal
201 " to the number of lines
Bram Moolenaar9e81db92018-09-18 22:37:31 +0200202 " - buffer of both windows should have no name
Bram Moolenaar8f4499b2018-09-16 16:28:11 +0200203 let [wn, wh1, wh2, ln, ww1, ww2, cn, bn1, bn2] = readfile('Xtestout')
204 call assert_equal('2', wn)
205 call assert_inrange(0, 1, ww1 - ww2)
206 call assert_equal(string(ww1 + ww2 + 1), cn)
207 call assert_equal(wh1, wh2)
208 call assert_equal(string(wh1 + 2), ln)
209 call assert_equal('', bn1)
210 call assert_equal('', bn2)
211 endif
212
213 if RunVim([], after, '-O foo bar')
214 " Same expectations as for -O2 but buffer names should be foo and bar
215 let [wn, wh1, wh2, ln, ww1, ww2, cn, bn1, bn2] = readfile('Xtestout')
216 call assert_equal('2', wn)
217 call assert_inrange(0, 1, ww1 - ww2)
218 call assert_equal(string(ww1 + ww2 + 1), cn)
219 call assert_equal(wh1, wh2)
220 call assert_equal(string(wh1 + 2), ln)
221 call assert_equal('foo', bn1)
222 call assert_equal('bar', bn2)
223 endif
224
225 call delete('Xtestout')
226endfunc
227
Bram Moolenaar9e81db92018-09-18 22:37:31 +0200228" Test the -p[N] argument to open N tabpages.
229func Test_p_arg()
230 let after = [
231 \ 'call writefile(split(execute("tabs"), "\n"), "Xtestout")',
232 \ 'qall',
233 \ ]
234 if RunVim([], after, '-p2')
235 let lines = readfile('Xtestout')
236 call assert_equal(4, len(lines))
237 call assert_equal('Tab page 1', lines[0])
238 call assert_equal('> [No Name]', lines[1])
239 call assert_equal('Tab page 2', lines[2])
240 call assert_equal(' [No Name]', lines[3])
241 endif
242
243 if RunVim([], after, '-p foo bar')
244 let lines = readfile('Xtestout')
245 call assert_equal(4, len(lines))
246 call assert_equal('Tab page 1', lines[0])
247 call assert_equal('> foo', lines[1])
248 call assert_equal('Tab page 2', lines[2])
249 call assert_equal(' bar', lines[3])
250 endif
251
252 call delete('Xtestout')
253endfunc
254
Bram Moolenaar4b1c9a92018-09-19 21:06:31 +0200255" Test the -V[N] argument to set the 'verbose' option to [N]
Bram Moolenaar9e81db92018-09-18 22:37:31 +0200256func Test_V_arg()
Bram Moolenaar4b1c9a92018-09-19 21:06:31 +0200257 if has('gui_running')
258 " Can't catch the output of gvim.
259 return
260 endif
Bram Moolenaar9e81db92018-09-18 22:37:31 +0200261 let out = system(GetVimCommand() . ' --clean -es -X -V0 -c "set verbose?" -cq')
262 call assert_equal(" verbose=0\n", out)
263
264 let out = system(GetVimCommand() . ' --clean -es -X -V2 -c "set verbose?" -cq')
Bram Moolenaar4b1c9a92018-09-19 21:06:31 +0200265 call assert_match("sourcing \"$VIMRUNTIME[\\/]defaults\.vim\"\r\nSearching for \"filetype\.vim\".*\n", out)
266 call assert_match(" verbose=2\n", out)
Bram Moolenaar9e81db92018-09-18 22:37:31 +0200267
268 let out = system(GetVimCommand() . ' --clean -es -X -V15 -c "set verbose?" -cq')
Bram Moolenaar4b1c9a92018-09-19 21:06:31 +0200269 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 +0200270endfunc
271
272" Test the -A, -F and -H arguments (Arabic, Farsi and Hebrew modes).
273func Test_A_F_H_arg()
274 let after = [
275 \ 'call writefile([&rightleft, &arabic, &fkmap, &hkmap], "Xtestout")',
276 \ 'qall',
277 \ ]
Bram Moolenaar4b1c9a92018-09-19 21:06:31 +0200278 " Use silent Ex mode to avoid the hit-Enter prompt for the warning that
279 " 'encoding' is not utf-8.
280 if has('arabic') && &encoding == 'utf-8' && RunVim([], after, '-e -s -A')
Bram Moolenaar9e81db92018-09-18 22:37:31 +0200281 let lines = readfile('Xtestout')
282 call assert_equal(['1', '1', '0', '0'], lines)
283 endif
284
285 if has('farsi') && RunVim([], after, '-F')
286 let lines = readfile('Xtestout')
287 call assert_equal(['1', '0', '1', '0'], lines)
288 endif
289
290 if has('rightleft') && RunVim([], after, '-H')
291 let lines = readfile('Xtestout')
292 call assert_equal(['1', '0', '0', '1'], lines)
293 endif
294
295 call delete('Xtestout')
296endfunc
297
Bram Moolenaarba98bef2016-08-07 15:51:39 +0200298func Test_file_args()
299 let after = [
300 \ 'call writefile(argv(), "Xtestout")',
301 \ 'qall',
302 \ ]
303 if RunVim([], after, '')
304 let lines = readfile('Xtestout')
305 call assert_equal(0, len(lines))
306 endif
307
308 if RunVim([], after, 'one')
309 let lines = readfile('Xtestout')
310 call assert_equal(1, len(lines))
311 call assert_equal('one', lines[0])
312 endif
313
314 if RunVim([], after, 'one two three')
315 let lines = readfile('Xtestout')
316 call assert_equal(3, len(lines))
317 call assert_equal('one', lines[0])
318 call assert_equal('two', lines[1])
319 call assert_equal('three', lines[2])
320 endif
321
322 if RunVim([], after, 'one -c echo two')
323 let lines = readfile('Xtestout')
324 call assert_equal(2, len(lines))
325 call assert_equal('one', lines[0])
326 call assert_equal('two', lines[1])
327 endif
328
329 if RunVim([], after, 'one -- -c echo two')
330 let lines = readfile('Xtestout')
331 call assert_equal(4, len(lines))
332 call assert_equal('one', lines[0])
333 call assert_equal('-c', lines[1])
334 call assert_equal('echo', lines[2])
335 call assert_equal('two', lines[3])
336 endif
337
338 call delete('Xtestout')
339endfunc
340
341func Test_startuptime()
342 if !has('startuptime')
343 return
344 endif
345 let after = ['qall']
346 if RunVim([], after, '--startuptime Xtestout one')
347 let lines = readfile('Xtestout')
348 let expected = ['--- VIM STARTING ---', 'parsing arguments',
349 \ 'shell init', 'inits 3', 'start termcap', 'opening buffers']
350 let found = []
351 for line in lines
352 for exp in expected
353 if line =~ exp
354 call add(found, exp)
355 endif
356 endfor
357 endfor
358 call assert_equal(expected, found)
359 endif
360 call delete('Xtestout')
361endfunc
Bram Moolenaar3a938382016-08-07 16:36:40 +0200362
363func Test_read_stdin()
364 let after = [
365 \ 'write Xtestout',
366 \ 'quit!',
367 \ ]
368 if RunVimPiped([], after, '-', 'echo something | ')
369 let lines = readfile('Xtestout')
Bram Moolenaare4a76ad2016-08-07 16:50:10 +0200370 " MS-Windows adds a space after the word
371 call assert_equal(['something'], split(lines[0]))
Bram Moolenaar3a938382016-08-07 16:36:40 +0200372 endif
373 call delete('Xtestout')
374endfunc
Bram Moolenaar08cab962017-03-04 14:37:18 +0100375
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100376func Test_set_shell()
377 let after = [
378 \ 'call writefile([&shell], "Xtestout")',
379 \ 'quit!',
380 \ ]
381 let $SHELL = '/bin/with space/sh'
382 if RunVimPiped([], after, '', '')
383 let lines = readfile('Xtestout')
384 " MS-Windows adds a space after the word
385 call assert_equal('/bin/with\ space/sh', lines[0])
386 endif
387 call delete('Xtestout')
388endfunc
389
Bram Moolenaar08cab962017-03-04 14:37:18 +0100390func Test_progpath()
391 " Tests normally run with "./vim" or "../vim", these must have been expanded
392 " to a full path.
393 if has('unix')
394 call assert_equal('/', v:progpath[0])
395 elseif has('win32')
396 call assert_equal(':', v:progpath[1])
397 call assert_match('[/\\]', v:progpath[2])
398 endif
399
400 " Only expect "vim" to appear in v:progname.
401 call assert_match('vim\c', v:progname)
402endfunc
Bram Moolenaard5d37532017-03-27 23:02:07 +0200403
404func Test_silent_ex_mode()
405 if !has('unix') || has('gui_running')
406 " can't get output of Vim.
407 return
408 endif
409
410 " This caused an ml_get error.
411 let out = system(GetVimCommand() . '-u NONE -es -c''set verbose=1|h|exe "%norm\<c-y>\<c-d>"'' -c cq')
412 call assert_notmatch('E315:', out)
413endfunc
Bram Moolenaar85045a72017-04-02 16:54:09 +0200414
415func Test_default_term()
416 if !has('unix') || has('gui_running')
417 " can't get output of Vim.
418 return
419 endif
420
421 let save_term = $TERM
Bram Moolenaar08f88b12017-04-02 17:21:16 +0200422 let $TERM = 'unknownxxx'
Bram Moolenaar85045a72017-04-02 16:54:09 +0200423 let out = system(GetVimCommand() . ' -c''set term'' -c cq')
424 call assert_match("defaulting to 'ansi'", out)
425 let $TERM = save_term
426endfunc
Bram Moolenaar09ca9322017-09-26 17:40:45 +0200427
428func Test_zzz_startinsert()
429 " Test :startinsert
430 call writefile(['123456'], 'Xtestout')
431 let after = [
432 \ ':startinsert',
433 \ 'call feedkeys("foobar\<c-o>:wq\<cr>","t")'
434 \ ]
435 if RunVim([], after, 'Xtestout')
436 let lines = readfile('Xtestout')
437 call assert_equal(['foobar123456'], lines)
438 endif
439 " Test :startinsert!
440 call writefile(['123456'], 'Xtestout')
441 let after = [
442 \ ':startinsert!',
443 \ 'call feedkeys("foobar\<c-o>:wq\<cr>","t")'
444 \ ]
445 if RunVim([], after, 'Xtestout')
446 let lines = readfile('Xtestout')
447 call assert_equal(['123456foobar'], lines)
448 endif
449 call delete('Xtestout')
450endfunc