xref: /vim-8.2.3635/src/testdir/test_system.vim (revision 5be4ceec)
1" Tests for system() and systemlist()
2
3source shared.vim
4
5func Test_System()
6  if !executable('echo') || !executable('cat') || !executable('wc')
7    return
8  endif
9  let out = 'echo 123'->system()
10  " On Windows we may get a trailing space.
11  if out != "123 \n"
12    call assert_equal("123\n", out)
13  endif
14
15  let out = 'echo 123'->systemlist()
16  " On Windows we may get a trailing space and CR.
17  if out != ["123 \r"]
18    call assert_equal(['123'], out)
19  endif
20
21  call assert_equal('123',   system('cat', '123'))
22  call assert_equal(['123'], systemlist('cat', '123'))
23  call assert_equal(["as\<NL>df"], systemlist('cat', ["as\<NL>df"]))
24
25  new Xdummy
26  call setline(1, ['asdf', "pw\<NL>er", 'xxxx'])
27  let out = system('wc -l', bufnr('%'))
28  " On OS/X we get leading spaces
29  let out = substitute(out, '^ *', '', '')
30  call assert_equal("3\n", out)
31
32  let out = systemlist('wc -l', bufnr('%'))
33  " On Windows we may get a trailing CR.
34  if out != ["3\r"]
35    " On OS/X we get leading spaces
36    if type(out) == v:t_list
37      let out[0] = substitute(out[0], '^ *', '', '')
38    endif
39    call assert_equal(['3'],  out)
40  endif
41
42  let out = systemlist('cat', bufnr('%'))
43  " On Windows we may get a trailing CR.
44  if out != ["asdf\r", "pw\<NL>er\r", "xxxx\r"]
45    call assert_equal(['asdf', "pw\<NL>er", 'xxxx'],  out)
46  endif
47  bwipe!
48
49  call assert_fails('call system("wc -l", 99999)', 'E86:')
50endfunc
51
52func Test_system_exmode()
53  if has('unix') " echo $? only works on Unix
54    let cmd = ' -es -c "source Xscript" +q; echo "result=$?"'
55    " Need to put this in a script, "catch" isn't found after an unknown
56    " function.
57    call writefile(['try', 'call doesnotexist()', 'catch', 'endtry'], 'Xscript')
58    let a = system(GetVimCommand() . cmd)
59    call assert_match('result=0', a)
60    call assert_equal(0, v:shell_error)
61  endif
62
63  " Error before try does set error flag.
64  call writefile(['call nosuchfunction()', 'try', 'call doesnotexist()', 'catch', 'endtry'], 'Xscript')
65  if has('unix') " echo $? only works on Unix
66    let a = system(GetVimCommand() . cmd)
67    call assert_notequal('0', a[0])
68  endif
69
70  let cmd = ' -es -c "source Xscript" +q'
71  let a = system(GetVimCommand() . cmd)
72  call assert_notequal(0, v:shell_error)
73  call delete('Xscript')
74
75  if has('unix') " echo $? only works on Unix
76    let cmd = ' -es -c "call doesnotexist()" +q; echo $?'
77    let a = system(GetVimCommand() . cmd)
78    call assert_notequal(0, a[0])
79  endif
80
81  let cmd = ' -es -c "call doesnotexist()" +q'
82  let a = system(GetVimCommand(). cmd)
83  call assert_notequal(0, v:shell_error)
84
85  if has('unix') " echo $? only works on Unix
86    let cmd = ' -es -c "call doesnotexist()|let a=1" +q; echo $?'
87    let a = system(GetVimCommand() . cmd)
88    call assert_notequal(0, a[0])
89  endif
90
91  let cmd = ' -es -c "call doesnotexist()|let a=1" +q'
92  let a = system(GetVimCommand() . cmd)
93  call assert_notequal(0, v:shell_error)
94endfunc
95