blob: deee51fa1ce1a70953f51640401153ec54dbea6e [file] [log] [blame]
Bram Moolenaar12c44922017-01-08 13:26:03 +01001" Tests for system() and systemlist()
2
Bram Moolenaar93344c22019-08-14 21:12:05 +02003source shared.vim
4
Bram Moolenaar1e115362019-01-09 23:01:02 +01005func Test_System()
Bram Moolenaar12c44922017-01-08 13:26:03 +01006 if !executable('echo') || !executable('cat') || !executable('wc')
7 return
8 endif
Bram Moolenaara74e4942019-08-04 17:35:53 +02009 let out = 'echo 123'->system()
Bram Moolenaar9d9c3562017-01-08 13:55:06 +010010 " On Windows we may get a trailing space.
11 if out != "123 \n"
12 call assert_equal("123\n", out)
13 endif
14
Bram Moolenaara74e4942019-08-04 17:35:53 +020015 let out = 'echo 123'->systemlist()
Bram Moolenaar9d9c3562017-01-08 13:55:06 +010016 " On Windows we may get a trailing space and CR.
17 if out != ["123 \r"]
18 call assert_equal(['123'], out)
19 endif
20
Bram Moolenaar12c44922017-01-08 13:26:03 +010021 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"]))
Bram Moolenaar31f19ce2017-01-08 14:14:43 +010024
Bram Moolenaar12c44922017-01-08 13:26:03 +010025 new Xdummy
26 call setline(1, ['asdf', "pw\<NL>er", 'xxxx'])
Bram Moolenaar31f19ce2017-01-08 14:14:43 +010027 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)
Bram Moolenaar9d9c3562017-01-08 13:55:06 +010031
32 let out = systemlist('wc -l', bufnr('%'))
33 " On Windows we may get a trailing CR.
34 if out != ["3\r"]
Bram Moolenaar31f19ce2017-01-08 14:14:43 +010035 " On OS/X we get leading spaces
36 if type(out) == v:t_list
37 let out[0] = substitute(out[0], '^ *', '', '')
38 endif
Bram Moolenaar9d9c3562017-01-08 13:55:06 +010039 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
Bram Moolenaar12c44922017-01-08 13:26:03 +010047 bwipe!
48
49 call assert_fails('call system("wc -l", 99999)', 'E86:')
Bram Moolenaar1e115362019-01-09 23:01:02 +010050endfunc
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010051
Bram Moolenaar1e115362019-01-09 23:01:02 +010052func Test_system_exmode()
Bram Moolenaar97d62d42017-01-16 22:53:57 +010053 if has('unix') " echo $? only works on Unix
Bram Moolenaar93344c22019-08-14 21:12:05 +020054 let cmd = ' -es -c "source Xscript" +q; echo "result=$?"'
Bram Moolenaar97d62d42017-01-16 22:53:57 +010055 " 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')
Bram Moolenaar93344c22019-08-14 21:12:05 +020058 let a = system(GetVimCommand() . cmd)
Bram Moolenaar11e79bb2017-07-08 17:03:21 +020059 call assert_match('result=0', a)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010060 call assert_equal(0, v:shell_error)
Bram Moolenaarfad609d2017-01-14 19:38:36 +010061 endif
62
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010063 " Error before try does set error flag.
64 call writefile(['call nosuchfunction()', 'try', 'call doesnotexist()', 'catch', 'endtry'], 'Xscript')
Bram Moolenaar97d62d42017-01-16 22:53:57 +010065 if has('unix') " echo $? only works on Unix
Bram Moolenaar93344c22019-08-14 21:12:05 +020066 let a = system(GetVimCommand() . cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010067 call assert_notequal('0', a[0])
68 endif
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010069
Bram Moolenaar93344c22019-08-14 21:12:05 +020070 let cmd = ' -es -c "source Xscript" +q'
71 let a = system(GetVimCommand() . cmd)
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010072 call assert_notequal(0, v:shell_error)
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010073 call delete('Xscript')
Bram Moolenaar97d62d42017-01-16 22:53:57 +010074
75 if has('unix') " echo $? only works on Unix
Bram Moolenaar93344c22019-08-14 21:12:05 +020076 let cmd = ' -es -c "call doesnotexist()" +q; echo $?'
77 let a = system(GetVimCommand() . cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010078 call assert_notequal(0, a[0])
79 endif
80
Bram Moolenaar93344c22019-08-14 21:12:05 +020081 let cmd = ' -es -c "call doesnotexist()" +q'
82 let a = system(GetVimCommand(). cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010083 call assert_notequal(0, v:shell_error)
84
85 if has('unix') " echo $? only works on Unix
Bram Moolenaar93344c22019-08-14 21:12:05 +020086 let cmd = ' -es -c "call doesnotexist()|let a=1" +q; echo $?'
87 let a = system(GetVimCommand() . cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010088 call assert_notequal(0, a[0])
89 endif
90
Bram Moolenaar93344c22019-08-14 21:12:05 +020091 let cmd = ' -es -c "call doesnotexist()|let a=1" +q'
92 let a = system(GetVimCommand() . cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010093 call assert_notequal(0, v:shell_error)
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010094endfunc