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