blob: dfe36837236c698d3d3ea98a421b80ba09e86638 [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 Moolenaar1a613392019-09-28 15:51:37 +02006 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'))
Bram Moolenaar9be0e0b2019-09-28 16:25:00 +020015 call assert_equal("123\n", system('more', '123'))
16 call assert_equal(["123\r"], systemlist('more', '123'))
17 call assert_equal(["as\r", "df\r"], systemlist('more', ["as\<NL>df"]))
Bram Moolenaar1a613392019-09-28 15:51:37 +020018 endif
19
20 if !executable('cat') || !executable('wc')
Bram Moolenaar12c44922017-01-08 13:26:03 +010021 return
22 endif
Bram Moolenaar1a613392019-09-28 15:51:37 +020023
Bram Moolenaara74e4942019-08-04 17:35:53 +020024 let out = 'echo 123'->system()
Bram Moolenaar9d9c3562017-01-08 13:55:06 +010025 " On Windows we may get a trailing space.
26 if out != "123 \n"
27 call assert_equal("123\n", out)
28 endif
29
Bram Moolenaara74e4942019-08-04 17:35:53 +020030 let out = 'echo 123'->systemlist()
Bram Moolenaar1a613392019-09-28 15:51:37 +020031 if !has('win32')
32 call assert_equal(["123"], out)
33 else
34 call assert_equal(["123\r"], out)
Bram Moolenaar9d9c3562017-01-08 13:55:06 +010035 endif
36
Bram Moolenaar1a613392019-09-28 15:51:37 +020037 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
Bram Moolenaar31f19ce2017-01-08 14:14:43 +010042
Bram Moolenaar12c44922017-01-08 13:26:03 +010043 new Xdummy
44 call setline(1, ['asdf', "pw\<NL>er", 'xxxx'])
Bram Moolenaar31f19ce2017-01-08 14:14:43 +010045 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)
Bram Moolenaar9d9c3562017-01-08 13:55:06 +010049
50 let out = systemlist('wc -l', bufnr('%'))
51 " On Windows we may get a trailing CR.
52 if out != ["3\r"]
Bram Moolenaar31f19ce2017-01-08 14:14:43 +010053 " On OS/X we get leading spaces
54 if type(out) == v:t_list
55 let out[0] = substitute(out[0], '^ *', '', '')
56 endif
Bram Moolenaar9d9c3562017-01-08 13:55:06 +010057 call assert_equal(['3'], out)
58 endif
59
Bram Moolenaar1a613392019-09-28 15:51:37 +020060 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('%'))
Bram Moolenaar9be0e0b2019-09-28 16:25:00 +020065 call assert_equal(["asdf\r", "pw\r", "er\r", "xxxx\r"], out)
Bram Moolenaar9d9c3562017-01-08 13:55:06 +010066 endif
Bram Moolenaar12c44922017-01-08 13:26:03 +010067 bwipe!
68
69 call assert_fails('call system("wc -l", 99999)', 'E86:')
Bram Moolenaar1e115362019-01-09 23:01:02 +010070endfunc
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010071
Bram Moolenaar1e115362019-01-09 23:01:02 +010072func Test_system_exmode()
Bram Moolenaar97d62d42017-01-16 22:53:57 +010073 if has('unix') " echo $? only works on Unix
Bram Moolenaar93344c22019-08-14 21:12:05 +020074 let cmd = ' -es -c "source Xscript" +q; echo "result=$?"'
Bram Moolenaar97d62d42017-01-16 22:53:57 +010075 " 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')
Bram Moolenaar93344c22019-08-14 21:12:05 +020078 let a = system(GetVimCommand() . cmd)
Bram Moolenaar11e79bb2017-07-08 17:03:21 +020079 call assert_match('result=0', a)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010080 call assert_equal(0, v:shell_error)
Bram Moolenaarfad609d2017-01-14 19:38:36 +010081 endif
82
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010083 " Error before try does set error flag.
84 call writefile(['call nosuchfunction()', 'try', 'call doesnotexist()', 'catch', 'endtry'], 'Xscript')
Bram Moolenaar97d62d42017-01-16 22:53:57 +010085 if has('unix') " echo $? only works on Unix
Bram Moolenaar93344c22019-08-14 21:12:05 +020086 let a = system(GetVimCommand() . cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010087 call assert_notequal('0', a[0])
88 endif
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010089
Bram Moolenaar93344c22019-08-14 21:12:05 +020090 let cmd = ' -es -c "source Xscript" +q'
91 let a = system(GetVimCommand() . cmd)
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010092 call assert_notequal(0, v:shell_error)
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010093 call delete('Xscript')
Bram Moolenaar97d62d42017-01-16 22:53:57 +010094
95 if has('unix') " echo $? only works on Unix
Bram Moolenaar93344c22019-08-14 21:12:05 +020096 let cmd = ' -es -c "call doesnotexist()" +q; echo $?'
97 let a = system(GetVimCommand() . cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010098 call assert_notequal(0, a[0])
99 endif
100
Bram Moolenaar93344c22019-08-14 21:12:05 +0200101 let cmd = ' -es -c "call doesnotexist()" +q'
102 let a = system(GetVimCommand(). cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +0100103 call assert_notequal(0, v:shell_error)
104
105 if has('unix') " echo $? only works on Unix
Bram Moolenaar93344c22019-08-14 21:12:05 +0200106 let cmd = ' -es -c "call doesnotexist()|let a=1" +q; echo $?'
107 let a = system(GetVimCommand() . cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +0100108 call assert_notequal(0, a[0])
109 endif
110
Bram Moolenaar93344c22019-08-14 21:12:05 +0200111 let cmd = ' -es -c "call doesnotexist()|let a=1" +q'
112 let a = system(GetVimCommand() . cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +0100113 call assert_notequal(0, v:shell_error)
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100114endfunc