blob: 4e0c349ec83b857ddfc27294970b34317ad6d724 [file] [log] [blame]
Bram Moolenaar12c44922017-01-08 13:26:03 +01001" Tests for system() and systemlist()
2
Bram Moolenaar1e115362019-01-09 23:01:02 +01003func Test_System()
Bram Moolenaar1a613392019-09-28 15:51:37 +02004 if !has('win32')
5 call assert_equal("123\n", system('echo 123'))
6 call assert_equal(['123'], systemlist('echo 123'))
7 call assert_equal('123', system('cat', '123'))
8 call assert_equal(['123'], systemlist('cat', '123'))
9 call assert_equal(["as\<NL>df"], systemlist('cat', ["as\<NL>df"]))
10 else
11 call assert_equal("123\n", system('echo 123'))
12 call assert_equal(["123\r"], systemlist('echo 123'))
Bram Moolenaar4cc45a32020-07-23 14:51:02 +020013 call assert_equal("123\n", system('more.com', '123'))
14 call assert_equal(["123\r"], systemlist('more.com', '123'))
15 call assert_equal(["as\r", "df\r"], systemlist('more.com', ["as\<NL>df"]))
Bram Moolenaar1a613392019-09-28 15:51:37 +020016 endif
17
Bram Moolenaar12c44922017-01-08 13:26:03 +010018 new Xdummy
19 call setline(1, ['asdf', "pw\<NL>er", 'xxxx'])
Bram Moolenaar9d9c3562017-01-08 13:55:06 +010020
Bram Moolenaar077ff432019-10-28 00:42:21 +010021 if executable('wc')
22 let out = system('wc -l', bufnr('%'))
Bram Moolenaar31f19ce2017-01-08 14:14:43 +010023 " On OS/X we get leading spaces
Bram Moolenaar077ff432019-10-28 00:42:21 +010024 let out = substitute(out, '^ *', '', '')
25 call assert_equal("3\n", out)
26
27 let out = systemlist('wc -l', bufnr('%'))
28 " On Windows we may get a trailing CR.
29 if out != ["3\r"]
30 " On OS/X we get leading spaces
31 if type(out) == v:t_list
32 let out[0] = substitute(out[0], '^ *', '', '')
33 endif
34 call assert_equal(['3'], out)
Bram Moolenaar31f19ce2017-01-08 14:14:43 +010035 endif
Bram Moolenaar9d9c3562017-01-08 13:55:06 +010036 endif
37
Bram Moolenaar1a613392019-09-28 15:51:37 +020038 if !has('win32')
39 let out = systemlist('cat', bufnr('%'))
40 call assert_equal(['asdf', "pw\<NL>er", 'xxxx'], out)
41 else
Bram Moolenaar4cc45a32020-07-23 14:51:02 +020042 let out = systemlist('more.com', bufnr('%'))
Bram Moolenaar9be0e0b2019-09-28 16:25:00 +020043 call assert_equal(["asdf\r", "pw\r", "er\r", "xxxx\r"], out)
Bram Moolenaar9d9c3562017-01-08 13:55:06 +010044 endif
Bram Moolenaar12c44922017-01-08 13:26:03 +010045 bwipe!
46
47 call assert_fails('call system("wc -l", 99999)', 'E86:')
Bram Moolenaar1e115362019-01-09 23:01:02 +010048endfunc
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010049
Bram Moolenaar1e115362019-01-09 23:01:02 +010050func Test_system_exmode()
Bram Moolenaar97d62d42017-01-16 22:53:57 +010051 if has('unix') " echo $? only works on Unix
Bram Moolenaar93344c22019-08-14 21:12:05 +020052 let cmd = ' -es -c "source Xscript" +q; echo "result=$?"'
Bram Moolenaar97d62d42017-01-16 22:53:57 +010053 " Need to put this in a script, "catch" isn't found after an unknown
54 " function.
Bram Moolenaar56564962022-10-10 22:39:42 +010055 call writefile(['try', 'call doesnotexist()', 'catch', 'endtry'], 'Xscript', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +020056 let a = system(GetVimCommand() . cmd)
Bram Moolenaar11e79bb2017-07-08 17:03:21 +020057 call assert_match('result=0', a)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010058 call assert_equal(0, v:shell_error)
Bram Moolenaarfad609d2017-01-14 19:38:36 +010059 endif
60
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010061 " Error before try does set error flag.
62 call writefile(['call nosuchfunction()', 'try', 'call doesnotexist()', 'catch', 'endtry'], 'Xscript')
Bram Moolenaar97d62d42017-01-16 22:53:57 +010063 if has('unix') " echo $? only works on Unix
Bram Moolenaar93344c22019-08-14 21:12:05 +020064 let a = system(GetVimCommand() . cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010065 call assert_notequal('0', a[0])
66 endif
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010067
Bram Moolenaar93344c22019-08-14 21:12:05 +020068 let cmd = ' -es -c "source Xscript" +q'
69 let a = system(GetVimCommand() . cmd)
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010070 call assert_notequal(0, v:shell_error)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010071
72 if has('unix') " echo $? only works on Unix
Bram Moolenaar93344c22019-08-14 21:12:05 +020073 let cmd = ' -es -c "call doesnotexist()" +q; echo $?'
74 let a = system(GetVimCommand() . cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010075 call assert_notequal(0, a[0])
76 endif
77
Bram Moolenaar93344c22019-08-14 21:12:05 +020078 let cmd = ' -es -c "call doesnotexist()" +q'
79 let a = system(GetVimCommand(). cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010080 call assert_notequal(0, v:shell_error)
81
82 if has('unix') " echo $? only works on Unix
Bram Moolenaar93344c22019-08-14 21:12:05 +020083 let cmd = ' -es -c "call doesnotexist()|let a=1" +q; echo $?'
84 let a = system(GetVimCommand() . cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010085 call assert_notequal(0, a[0])
86 endif
87
Bram Moolenaar93344c22019-08-14 21:12:05 +020088 let cmd = ' -es -c "call doesnotexist()|let a=1" +q'
89 let a = system(GetVimCommand() . cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010090 call assert_notequal(0, v:shell_error)
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010091endfunc
Bram Moolenaar2efc44b2019-10-05 12:09:32 +020092
93func Test_system_with_shell_quote()
94 CheckMSWindows
95
96 call mkdir('Xdir with spaces', 'p')
97 call system('copy "%COMSPEC%" "Xdir with spaces\cmd.exe"')
98
99 let shell_save = &shell
100 let shellxquote_save = &shellxquote
101 try
102 " Set 'shell' always needs noshellslash.
103 let shellslash_save = &shellslash
104 set noshellslash
105 let shell_tests = [
106 \ expand('$COMSPEC'),
107 \ '"' . fnamemodify('Xdir with spaces\cmd.exe', ':p') . '"',
108 \]
109 let &shellslash = shellslash_save
110
111 let sxq_tests = ['', '(', '"']
112
113 " Matrix tests: 'shell' * 'shellxquote'
114 for shell in shell_tests
115 let &shell = shell
116 for sxq in sxq_tests
117 let &shellxquote = sxq
118
119 let msg = printf('shell=%s shellxquote=%s', &shell, &shellxquote)
120
121 try
122 let out = 'echo 123'->system()
123 catch
124 call assert_report(printf('%s: %s', msg, v:exception))
125 continue
126 endtry
127
128 " On Windows we may get a trailing space and CR.
129 if out != "123 \n"
130 call assert_equal("123\n", out, msg)
131 endif
132
133 endfor
134 endfor
135
136 finally
137 let &shell = shell_save
138 let &shellxquote = shellxquote_save
139 call delete('Xdir with spaces', 'rf')
140 endtry
141endfunc
Bram Moolenaar0ff5ded2020-05-07 18:43:44 +0200142
Bram Moolenaar0ff5ded2020-05-07 18:43:44 +0200143" vim: shiftwidth=2 sts=2 expandtab