blob: eabccfb371e0df26dd639df0c422cf71bbf94f02 [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
Bram Moolenaar2efc44b2019-10-05 12:09:32 +02004source check.vim
Bram Moolenaar93344c22019-08-14 21:12:05 +02005
Bram Moolenaar1e115362019-01-09 23:01:02 +01006func Test_System()
Bram Moolenaar1a613392019-09-28 15:51:37 +02007 if !has('win32')
8 call assert_equal("123\n", system('echo 123'))
9 call assert_equal(['123'], systemlist('echo 123'))
10 call assert_equal('123', system('cat', '123'))
11 call assert_equal(['123'], systemlist('cat', '123'))
12 call assert_equal(["as\<NL>df"], systemlist('cat', ["as\<NL>df"]))
13 else
14 call assert_equal("123\n", system('echo 123'))
15 call assert_equal(["123\r"], systemlist('echo 123'))
Bram Moolenaar9be0e0b2019-09-28 16:25:00 +020016 call assert_equal("123\n", system('more', '123'))
17 call assert_equal(["123\r"], systemlist('more', '123'))
18 call assert_equal(["as\r", "df\r"], systemlist('more', ["as\<NL>df"]))
Bram Moolenaar1a613392019-09-28 15:51:37 +020019 endif
20
21 if !executable('cat') || !executable('wc')
Bram Moolenaar12c44922017-01-08 13:26:03 +010022 return
23 endif
Bram Moolenaar1a613392019-09-28 15:51:37 +020024
Bram Moolenaara74e4942019-08-04 17:35:53 +020025 let out = 'echo 123'->system()
Bram Moolenaar9d9c3562017-01-08 13:55:06 +010026 " On Windows we may get a trailing space.
27 if out != "123 \n"
28 call assert_equal("123\n", out)
29 endif
30
Bram Moolenaara74e4942019-08-04 17:35:53 +020031 let out = 'echo 123'->systemlist()
Bram Moolenaar1a613392019-09-28 15:51:37 +020032 if !has('win32')
33 call assert_equal(["123"], out)
34 else
35 call assert_equal(["123\r"], out)
Bram Moolenaar9d9c3562017-01-08 13:55:06 +010036 endif
37
Bram Moolenaar1a613392019-09-28 15:51:37 +020038 if executable('cat')
39 call assert_equal('123', system('cat', '123'))
40 call assert_equal(['123'], systemlist('cat', '123'))
41 call assert_equal(["as\<NL>df"], systemlist('cat', ["as\<NL>df"]))
42 endif
Bram Moolenaar31f19ce2017-01-08 14:14:43 +010043
Bram Moolenaar12c44922017-01-08 13:26:03 +010044 new Xdummy
45 call setline(1, ['asdf', "pw\<NL>er", 'xxxx'])
Bram Moolenaar31f19ce2017-01-08 14:14:43 +010046 let out = system('wc -l', bufnr('%'))
47 " On OS/X we get leading spaces
48 let out = substitute(out, '^ *', '', '')
49 call assert_equal("3\n", out)
Bram Moolenaar9d9c3562017-01-08 13:55:06 +010050
51 let out = systemlist('wc -l', bufnr('%'))
52 " On Windows we may get a trailing CR.
53 if out != ["3\r"]
Bram Moolenaar31f19ce2017-01-08 14:14:43 +010054 " On OS/X we get leading spaces
55 if type(out) == v:t_list
56 let out[0] = substitute(out[0], '^ *', '', '')
57 endif
Bram Moolenaar9d9c3562017-01-08 13:55:06 +010058 call assert_equal(['3'], out)
59 endif
60
Bram Moolenaar1a613392019-09-28 15:51:37 +020061 if !has('win32')
62 let out = systemlist('cat', bufnr('%'))
63 call assert_equal(['asdf', "pw\<NL>er", 'xxxx'], out)
64 else
65 let out = systemlist('more', bufnr('%'))
Bram Moolenaar9be0e0b2019-09-28 16:25:00 +020066 call assert_equal(["asdf\r", "pw\r", "er\r", "xxxx\r"], out)
Bram Moolenaar9d9c3562017-01-08 13:55:06 +010067 endif
Bram Moolenaar12c44922017-01-08 13:26:03 +010068 bwipe!
69
70 call assert_fails('call system("wc -l", 99999)', 'E86:')
Bram Moolenaar1e115362019-01-09 23:01:02 +010071endfunc
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010072
Bram Moolenaar1e115362019-01-09 23:01:02 +010073func Test_system_exmode()
Bram Moolenaar97d62d42017-01-16 22:53:57 +010074 if has('unix') " echo $? only works on Unix
Bram Moolenaar93344c22019-08-14 21:12:05 +020075 let cmd = ' -es -c "source Xscript" +q; echo "result=$?"'
Bram Moolenaar97d62d42017-01-16 22:53:57 +010076 " Need to put this in a script, "catch" isn't found after an unknown
77 " function.
78 call writefile(['try', 'call doesnotexist()', 'catch', 'endtry'], 'Xscript')
Bram Moolenaar93344c22019-08-14 21:12:05 +020079 let a = system(GetVimCommand() . cmd)
Bram Moolenaar11e79bb2017-07-08 17:03:21 +020080 call assert_match('result=0', a)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010081 call assert_equal(0, v:shell_error)
Bram Moolenaarfad609d2017-01-14 19:38:36 +010082 endif
83
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010084 " Error before try does set error flag.
85 call writefile(['call nosuchfunction()', 'try', 'call doesnotexist()', 'catch', 'endtry'], 'Xscript')
Bram Moolenaar97d62d42017-01-16 22:53:57 +010086 if has('unix') " echo $? only works on Unix
Bram Moolenaar93344c22019-08-14 21:12:05 +020087 let a = system(GetVimCommand() . cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010088 call assert_notequal('0', a[0])
89 endif
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010090
Bram Moolenaar93344c22019-08-14 21:12:05 +020091 let cmd = ' -es -c "source Xscript" +q'
92 let a = system(GetVimCommand() . cmd)
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010093 call assert_notequal(0, v:shell_error)
Bram Moolenaar2b7bc562017-01-14 19:24:52 +010094 call delete('Xscript')
Bram Moolenaar97d62d42017-01-16 22:53:57 +010095
96 if has('unix') " echo $? only works on Unix
Bram Moolenaar93344c22019-08-14 21:12:05 +020097 let cmd = ' -es -c "call doesnotexist()" +q; echo $?'
98 let a = system(GetVimCommand() . cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +010099 call assert_notequal(0, a[0])
100 endif
101
Bram Moolenaar93344c22019-08-14 21:12:05 +0200102 let cmd = ' -es -c "call doesnotexist()" +q'
103 let a = system(GetVimCommand(). cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +0100104 call assert_notequal(0, v:shell_error)
105
106 if has('unix') " echo $? only works on Unix
Bram Moolenaar93344c22019-08-14 21:12:05 +0200107 let cmd = ' -es -c "call doesnotexist()|let a=1" +q; echo $?'
108 let a = system(GetVimCommand() . cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +0100109 call assert_notequal(0, a[0])
110 endif
111
Bram Moolenaar93344c22019-08-14 21:12:05 +0200112 let cmd = ' -es -c "call doesnotexist()|let a=1" +q'
113 let a = system(GetVimCommand() . cmd)
Bram Moolenaar97d62d42017-01-16 22:53:57 +0100114 call assert_notequal(0, v:shell_error)
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100115endfunc
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200116
117func Test_system_with_shell_quote()
118 CheckMSWindows
119
120 call mkdir('Xdir with spaces', 'p')
121 call system('copy "%COMSPEC%" "Xdir with spaces\cmd.exe"')
122
123 let shell_save = &shell
124 let shellxquote_save = &shellxquote
125 try
126 " Set 'shell' always needs noshellslash.
127 let shellslash_save = &shellslash
128 set noshellslash
129 let shell_tests = [
130 \ expand('$COMSPEC'),
131 \ '"' . fnamemodify('Xdir with spaces\cmd.exe', ':p') . '"',
132 \]
133 let &shellslash = shellslash_save
134
135 let sxq_tests = ['', '(', '"']
136
137 " Matrix tests: 'shell' * 'shellxquote'
138 for shell in shell_tests
139 let &shell = shell
140 for sxq in sxq_tests
141 let &shellxquote = sxq
142
143 let msg = printf('shell=%s shellxquote=%s', &shell, &shellxquote)
144
145 try
146 let out = 'echo 123'->system()
147 catch
148 call assert_report(printf('%s: %s', msg, v:exception))
149 continue
150 endtry
151
152 " On Windows we may get a trailing space and CR.
153 if out != "123 \n"
154 call assert_equal("123\n", out, msg)
155 endif
156
157 endfor
158 endfor
159
160 finally
161 let &shell = shell_save
162 let &shellxquote = shellxquote_save
163 call delete('Xdir with spaces', 'rf')
164 endtry
165endfunc