xref: /vim-8.2.3635/src/testdir/test_startup.vim (revision 577fadfc)
1" Tests for startup.
2
3source shared.vim
4
5" Check that loading startup.vim works.
6func Test_startup_script()
7  set compatible
8  source $VIMRUNTIME/defaults.vim
9
10  call assert_equal(0, &compatible)
11endfunc
12
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()
18  if !has('packages')
19    return
20  endif
21  let before = [
22	\ 'set nocp viminfo+=nviminfo',
23	\ 'set guioptions+=M',
24	\ 'let $HOME = "/does/not/exist"',
25	\ 'set loadplugins',
26	\ 'set rtp=Xhere,Xafter,Xanother',
27	\ 'set packpath=Xhere,Xafter',
28	\ 'set nomore',
29	\ 'let g:sequence = ""',
30	\ ]
31  let after = [
32	\ 'redir! > Xtestout',
33	\ 'scriptnames',
34	\ 'redir END',
35	\ 'redir! > Xsequence',
36	\ 'echo g:sequence',
37	\ 'redir END',
38	\ 'quit',
39	\ ]
40  call mkdir('Xhere/plugin', 'p')
41  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')
44  call mkdir('Xhere/pack/foo/start/foobar/plugin', 'p')
45  call writefile(['let g:sequence .= "pack "'], 'Xhere/pack/foo/start/foobar/plugin/foo.vim')
46
47  call mkdir('Xafter/plugin', 'p')
48  call writefile(['let g:sequence .= "after "'], 'Xafter/plugin/later.vim')
49
50  if RunVim(before, after, '')
51
52    let lines = readfile('Xtestout')
53    let expected = ['Xbefore.vim', 'here.vim', 'another.vim', 'foo.vim', 'later.vim', 'Xafter.vim']
54    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
61    endfor
62    call assert_equal(expected, found)
63  endif
64
65  call assert_equal('here another pack after', substitute(join(readfile('Xsequence', 1), ''), '\s\+$', '', ''))
66
67  call delete('Xtestout')
68  call delete('Xsequence')
69  call delete('Xhere', 'rf')
70  call delete('Xanother', 'rf')
71  call delete('Xafter', 'rf')
72endfunc
73
74func 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
106func Test_help_arg()
107  if !has('unix') && has('gui')
108    " this doesn't work with gvim on MS-Windows
109    return
110  endif
111  if RunVim([], [], '--help >Xtestout')
112    let lines = readfile('Xtestout')
113    call assert_true(len(lines) > 20)
114    call assert_match('Vi IMproved', lines[0])
115
116    " check if  couple of lines are there
117    let found = []
118    for line in lines
119      if line =~ '-R.*Readonly mode'
120	call add(found, 'Readonly mode')
121      endif
122      " 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")
125      endif
126    endfor
127    call assert_equal(['Readonly mode', '--version'], found)
128  endif
129  call delete('Xtestout')
130endfunc
131
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
151" 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 for the separator) should be equal to
198    "   the 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
202    " - buffer of both windows should have no name
203    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
228" 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
255" Test the -V[N] argument to set the 'verbose' option to [N]
256func Test_V_arg()
257  if has('gui_running')
258    " Can't catch the output of gvim.
259    return
260  endif
261  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')
265  call assert_match("sourcing \"$VIMRUNTIME[\\/]defaults\.vim\"\r\nSearching for \"filetype\.vim\".*\n", out)
266  call assert_match("  verbose=2\n", out)
267
268  let out = system(GetVimCommand() . ' --clean -es -X -V15 -c "set verbose?" -cq')
269   call assert_match("sourcing \"$VIMRUNTIME[\\/]defaults\.vim\"\r\nline 1: \" The default vimrc file\..*  verbose=15\n", out)
270endfunc
271
272" Test the '-q [errorfile]' argument.
273func Test_q_arg()
274  let source_file = has('win32') ? '..\memfile.c' : '../memfile.c'
275  let after = [
276	\ 'call writefile([&errorfile, string(getpos("."))], "Xtestout")',
277	\ 'copen',
278	\ 'w >> Xtestout',
279	\ 'qall'
280	\ ]
281
282  " Test with default argument '-q'.
283  call assert_equal('errors.err', &errorfile)
284  call writefile(["../memfile.c:1482:5: error: expected ';' before '}' token"], 'errors.err')
285  if RunVim([], after, '-q')
286    let lines = readfile('Xtestout')
287    call assert_equal(['errors.err',
288	\              '[0, 1482, 5, 0]',
289	\              source_file . "|1482 col 5| error: expected ';' before '}' token"],
290	\             lines)
291  endif
292  call delete('Xtestout')
293  call delete('errors.err')
294
295  " Test with explicit argument '-q Xerrors' (with space).
296  call writefile(["../memfile.c:1482:5: error: expected ';' before '}' token"], 'Xerrors')
297  if RunVim([], after, '-q Xerrors')
298    let lines = readfile('Xtestout')
299    call assert_equal(['Xerrors',
300	\              '[0, 1482, 5, 0]',
301	\              source_file . "|1482 col 5| error: expected ';' before '}' token"],
302	\             lines)
303  endif
304  call delete('Xtestout')
305
306  " Test with explicit argument '-qXerrors' (without space).
307  if RunVim([], after, '-qXerrors')
308    let lines = readfile('Xtestout')
309    call assert_equal(['Xerrors',
310	\              '[0, 1482, 5, 0]',
311	\              source_file . "|1482 col 5| error: expected ';' before '}' token"],
312	\             lines)
313  endif
314
315  call delete('Xtestout')
316  call delete('Xerrors')
317endfunc
318
319" Test the -V[N]{filename} argument to set the 'verbose' option to N
320" and set 'verbosefile' to filename.
321func Test_V_file_arg()
322  if RunVim([], [], ' --clean -V2Xverbosefile -c "set verbose? verbosefile?" -cq')
323    let out = join(readfile('Xverbosefile'), "\n")
324    call assert_match("sourcing \"$VIMRUNTIME[\\/]defaults\.vim\"\n", out)
325    call assert_match("\n  verbose=2\n", out)
326    call assert_match("\n  verbosefile=Xverbosefile", out)
327  endif
328
329  call delete('Xverbosefile')
330endfunc
331
332" Test the -m, -M and -R arguments:
333" -m resets 'write'
334" -M resets 'modifiable' and 'write'
335" -R sets 'readonly'
336func Test_m_M_R()
337  let after = [
338	\ 'call writefile([&write, &modifiable, &readonly, &updatecount], "Xtestout")',
339	\ 'qall',
340	\ ]
341  if RunVim([], after, '')
342    let lines = readfile('Xtestout')
343    call assert_equal(['1', '1', '0', '200'], lines)
344  endif
345  if RunVim([], after, '-m')
346    let lines = readfile('Xtestout')
347    call assert_equal(['0', '1', '0', '200'], lines)
348  endif
349  if RunVim([], after, '-M')
350    let lines = readfile('Xtestout')
351    call assert_equal(['0', '0', '0', '200'], lines)
352  endif
353  if RunVim([], after, '-R')
354    let lines = readfile('Xtestout')
355    call assert_equal(['1', '1', '1', '10000'], lines)
356  endif
357
358  call delete('Xtestout')
359endfunc
360
361" Test the -A, -F and -H arguments (Arabic, Farsi and Hebrew modes).
362func Test_A_F_H_arg()
363  let after = [
364	\ 'call writefile([&rightleft, &arabic, &fkmap, &hkmap], "Xtestout")',
365	\ 'qall',
366	\ ]
367  " Use silent Ex mode to avoid the hit-Enter prompt for the warning that
368  " 'encoding' is not utf-8.
369  if has('arabic') && &encoding == 'utf-8' && RunVim([], after, '-e -s -A')
370    let lines = readfile('Xtestout')
371    call assert_equal(['1', '1', '0', '0'], lines)
372  endif
373
374  if has('farsi') && RunVim([], after, '-F')
375    let lines = readfile('Xtestout')
376    call assert_equal(['1', '0', '1', '0'], lines)
377  endif
378
379  if has('rightleft') && RunVim([], after, '-H')
380    let lines = readfile('Xtestout')
381    call assert_equal(['1', '0', '0', '1'], lines)
382  endif
383
384  call delete('Xtestout')
385endfunc
386
387func Test_file_args()
388  let after = [
389	\ 'call writefile(argv(), "Xtestout")',
390	\ 'qall',
391	\ ]
392  if RunVim([], after, '')
393    let lines = readfile('Xtestout')
394    call assert_equal(0, len(lines))
395  endif
396
397  if RunVim([], after, 'one')
398    let lines = readfile('Xtestout')
399    call assert_equal(1, len(lines))
400    call assert_equal('one', lines[0])
401  endif
402
403  if RunVim([], after, 'one two three')
404    let lines = readfile('Xtestout')
405    call assert_equal(3, len(lines))
406    call assert_equal('one', lines[0])
407    call assert_equal('two', lines[1])
408    call assert_equal('three', lines[2])
409  endif
410
411  if RunVim([], after, 'one -c echo two')
412    let lines = readfile('Xtestout')
413    call assert_equal(2, len(lines))
414    call assert_equal('one', lines[0])
415    call assert_equal('two', lines[1])
416  endif
417
418  if RunVim([], after, 'one -- -c echo two')
419    let lines = readfile('Xtestout')
420    call assert_equal(4, len(lines))
421    call assert_equal('one', lines[0])
422    call assert_equal('-c', lines[1])
423    call assert_equal('echo', lines[2])
424    call assert_equal('two', lines[3])
425  endif
426
427  call delete('Xtestout')
428endfunc
429
430func Test_startuptime()
431  if !has('startuptime')
432    return
433  endif
434  let after = ['qall']
435  if RunVim([], after, '--startuptime Xtestout one')
436    let lines = readfile('Xtestout')
437    let expected = ['--- VIM STARTING ---', 'parsing arguments',
438	  \ 'shell init', 'inits 3', 'start termcap', 'opening buffers']
439    let found = []
440    for line in lines
441      for exp in expected
442	if line =~ exp
443	  call add(found, exp)
444	endif
445      endfor
446    endfor
447    call assert_equal(expected, found)
448  endif
449  call delete('Xtestout')
450endfunc
451
452func Test_read_stdin()
453  let after = [
454	\ 'write Xtestout',
455	\ 'quit!',
456	\ ]
457  if RunVimPiped([], after, '-', 'echo something | ')
458    let lines = readfile('Xtestout')
459    " MS-Windows adds a space after the word
460    call assert_equal(['something'], split(lines[0]))
461  endif
462  call delete('Xtestout')
463endfunc
464
465func Test_set_shell()
466  let after = [
467	\ 'call writefile([&shell], "Xtestout")',
468	\ 'quit!',
469	\ ]
470  let $SHELL = '/bin/with space/sh'
471  if RunVimPiped([], after, '', '')
472    let lines = readfile('Xtestout')
473    " MS-Windows adds a space after the word
474    call assert_equal('/bin/with\ space/sh', lines[0])
475  endif
476  call delete('Xtestout')
477endfunc
478
479func Test_progpath()
480  " Tests normally run with "./vim" or "../vim", these must have been expanded
481  " to a full path.
482  if has('unix')
483    call assert_equal('/', v:progpath[0])
484  elseif has('win32')
485    call assert_equal(':', v:progpath[1])
486    call assert_match('[/\\]', v:progpath[2])
487  endif
488
489  " Only expect "vim" to appear in v:progname.
490  call assert_match('vim\c', v:progname)
491endfunc
492
493func Test_silent_ex_mode()
494  if !has('unix') || has('gui_running')
495    " can't get output of Vim.
496    return
497  endif
498
499  " This caused an ml_get error.
500  let out = system(GetVimCommand() . '-u NONE -es -c''set verbose=1|h|exe "%norm\<c-y>\<c-d>"'' -c cq')
501  call assert_notmatch('E315:', out)
502endfunc
503
504func Test_default_term()
505  if !has('unix') || has('gui_running')
506    " can't get output of Vim.
507    return
508  endif
509
510  let save_term = $TERM
511  let $TERM = 'unknownxxx'
512  let out = system(GetVimCommand() . ' -c''set term'' -c cq')
513  call assert_match("defaulting to 'ansi'", out)
514  let $TERM = save_term
515endfunc
516
517func Test_zzz_startinsert()
518  " Test :startinsert
519  call writefile(['123456'], 'Xtestout')
520  let after = [
521	\ ':startinsert',
522	\ 'call feedkeys("foobar\<c-o>:wq\<cr>","t")'
523	\ ]
524  if RunVim([], after, 'Xtestout')
525    let lines = readfile('Xtestout')
526    call assert_equal(['foobar123456'], lines)
527  endif
528  " Test :startinsert!
529  call writefile(['123456'], 'Xtestout')
530  let after = [
531	\ ':startinsert!',
532	\ 'call feedkeys("foobar\<c-o>:wq\<cr>","t")'
533	\ ]
534  if RunVim([], after, 'Xtestout')
535    let lines = readfile('Xtestout')
536    call assert_equal(['123456foobar'], lines)
537  endif
538  call delete('Xtestout')
539endfunc
540
541func Test_issue_3969()
542  if has('gui_running')
543    " Can't catch the output of gvim.
544    return
545  endif
546  " Check that message is not truncated.
547  let out = system(GetVimCommand() . ' -es -X -V1 -c "echon ''hello''" -cq')
548  call assert_equal('hello', out)
549endfunc
550