blob: f82f751a91854213eda63b164a0d28da8c8a09c1 [file] [log] [blame]
Bram Moolenaar12c44922017-01-08 13:26:03 +01001" Tests for system() and systemlist()
2
3function! Test_System()
4 if !executable('echo') || !executable('cat') || !executable('wc')
5 return
6 endif
Bram Moolenaar9d9c3562017-01-08 13:55:06 +01007 let out = system('echo 123')
8 " On Windows we may get a trailing space.
9 if out != "123 \n"
10 call assert_equal("123\n", out)
11 endif
12
13 let out = systemlist('echo 123')
14 " On Windows we may get a trailing space and CR.
15 if out != ["123 \r"]
16 call assert_equal(['123'], out)
17 endif
18
Bram Moolenaar12c44922017-01-08 13:26:03 +010019 call assert_equal('123', system('cat', '123'))
20 call assert_equal(['123'], systemlist('cat', '123'))
21 call assert_equal(["as\<NL>df"], systemlist('cat', ["as\<NL>df"]))
Bram Moolenaar31f19ce2017-01-08 14:14:43 +010022
Bram Moolenaar12c44922017-01-08 13:26:03 +010023 new Xdummy
24 call setline(1, ['asdf', "pw\<NL>er", 'xxxx'])
Bram Moolenaar31f19ce2017-01-08 14:14:43 +010025 let out = system('wc -l', bufnr('%'))
26 " On OS/X we get leading spaces
27 let out = substitute(out, '^ *', '', '')
28 call assert_equal("3\n", out)
Bram Moolenaar9d9c3562017-01-08 13:55:06 +010029
30 let out = systemlist('wc -l', bufnr('%'))
31 " On Windows we may get a trailing CR.
32 if out != ["3\r"]
Bram Moolenaar31f19ce2017-01-08 14:14:43 +010033 " On OS/X we get leading spaces
34 if type(out) == v:t_list
35 let out[0] = substitute(out[0], '^ *', '', '')
36 endif
Bram Moolenaar9d9c3562017-01-08 13:55:06 +010037 call assert_equal(['3'], out)
38 endif
39
40 let out = systemlist('cat', bufnr('%'))
41 " On Windows we may get a trailing CR.
42 if out != ["asdf\r", "pw\<NL>er\r", "xxxx\r"]
43 call assert_equal(['asdf', "pw\<NL>er", 'xxxx'], out)
44 endif
Bram Moolenaar12c44922017-01-08 13:26:03 +010045 bwipe!
46
47 call assert_fails('call system("wc -l", 99999)', 'E86:')
48endfunction
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010049
50function! Test_system_exmode()
51 let cmd=" -es -u NONE -c 'source Xscript' +q; echo $?"
52 " Need to put this in a script, "catch" isn't found after an unknown
53 " function.
54 call writefile(['try', 'call doesnotexist()', 'catch', 'endtry'], 'Xscript')
55 let a = system(v:progpath . cmd)
56 call assert_equal('0', a[0])
57 call assert_equal(0, v:shell_error)
58
59 " Error before try does set error flag.
60 call writefile(['call nosuchfunction()', 'try', 'call doesnotexist()', 'catch', 'endtry'], 'Xscript')
61 let a = system(v:progpath . cmd)
62 call assert_notequal('0', a[0])
63
64 let cmd=" -es -u NONE -c 'source Xscript' +q"
65 let a = system(v:progpath . cmd)
66 call assert_notequal(0, v:shell_error)
67
68 let cmd=" -es -u NONE -c 'call doesnotexist()' +q; echo $?"
69 let a = system(v:progpath. cmd)
70 call assert_notequal(0, a[0])
71
72 let cmd=" -es -u NONE -c 'call doesnotexist()' +q"
73 let a = system(v:progpath. cmd)
74 call assert_notequal(0, v:shell_error)
75
76 let cmd=" -es -u NONE -c 'call doesnotexist()|let a=1' +q; echo $?"
77 let a = system(v:progpath. cmd)
78 call assert_notequal(0, a[0])
79
80 let cmd=" -es -u NONE -c 'call doesnotexist()|let a=1' +q"
81 let a = system(v:progpath. cmd)
82 call assert_notequal(0, v:shell_error)
83
84 call delete('Xscript')
85endfunc