blob: 647d34870d6a6b3958723d075ba47faf099ca6dc [file] [log] [blame]
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +02001" Test for the shell related options ('shell', 'shellcmdflag', 'shellpipe',
2" 'shellquote', 'shellredir', 'shellxescape', and 'shellxquote')
3
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +02004func Test_shell_options()
K.Takata0500e872022-09-08 12:28:02 +01005 if has('win32')
6 " FIXME: This test is flaky on MS-Windows.
7 let g:test_is_flaky = 1
8 endif
9
Yegappan Lakshmananffec6dd2021-06-27 22:09:59 +020010 " The expected value of 'shellcmdflag', 'shellpipe', 'shellquote',
11 " 'shellredir', 'shellxescape', 'shellxquote' for the supported shells.
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +020012 let shells = []
13 if has('unix')
14 let shells += [['sh', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''],
15 \ ['ksh', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''],
16 \ ['mksh', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''],
17 \ ['zsh', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''],
18 \ ['zsh-beta', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''],
19 \ ['bash', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''],
20 \ ['fish', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''],
21 \ ['ash', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''],
22 \ ['dash', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''],
23 \ ['csh', '-c', '|& tee', '', '>&', '', ''],
Mike Williamsa3d1b292021-06-30 20:56:00 +020024 \ ['tcsh', '-c', '|& tee', '', '>&', '', ''],
25 \ ['pwsh', '-c', '>%s 2>&1', '', '>%s 2>&1', '', '']]
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +020026 endif
27 if has('win32')
28 let shells += [['cmd', '/c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', ''],
29 \ ['cmd.exe', '/c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '('],
Mike Williams12795022021-06-28 20:53:58 +020030 \ ['powershell.exe', '-Command', '2>&1 | Out-File -Encoding default',
31 \ '', '2>&1 | Out-File -Encoding default', '"&|<>()@^', '"'],
32 \ ['powershell', '-Command', '2>&1 | Out-File -Encoding default', '',
33 \ '2>&1 | Out-File -Encoding default', '"&|<>()@^', '"'],
Mike Williamsa3d1b292021-06-30 20:56:00 +020034 \ ['pwsh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
35 \ ['pwsh', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +020036 \ ['sh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
37 \ ['ksh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
38 \ ['mksh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
39 \ ['pdksh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
40 \ ['zsh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
41 \ ['zsh-beta.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
42 \ ['bash.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
43 \ ['dash.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
44 \ ['csh.exe', '-c', '>&', '', '>&', '"&|<>()@^', '"'],
45 \ ['tcsh.exe', '-c', '>&', '', '>&', '"&|<>()@^', '"']]
46 endif
47
Yegappan Lakshmananffec6dd2021-06-27 22:09:59 +020048 " start a new Vim instance with 'shell' set to each of the supported shells
49 " and check the default shell option settings
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +020050 let after =<< trim END
51 let l = [&shell, &shellcmdflag, &shellpipe, &shellquote]
52 let l += [&shellredir, &shellxescape, &shellxquote]
53 call writefile([json_encode(l)], 'Xtestout')
54 qall!
55 END
56 for e in shells
57 if RunVim([], after, '--cmd "set shell=' .. e[0] .. '"')
58 call assert_equal(e, json_decode(readfile('Xtestout')[0]))
59 endif
60 endfor
61
Yegappan Lakshmananffec6dd2021-06-27 22:09:59 +020062 " Test shellescape() for each of the shells.
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +020063 for e in shells
64 exe 'set shell=' .. e[0]
65 if e[0] =~# '.*csh$' || e[0] =~# '.*csh.exe$'
Bram Moolenaar66315972021-09-01 14:31:51 +020066 let str1 = "'cmd \"arg1\" '\\''arg2'\\'' \\!%# \\'\\'' \\\\! \\% \\#'"
67 let str2 = "'cmd \"arg1\" '\\''arg2'\\'' \\\\!\\%\\# \\'\\'' \\\\\\! \\\\% \\\\#'"
Mike Williams12795022021-06-28 20:53:58 +020068 elseif e[0] =~# '.*powershell$' || e[0] =~# '.*powershell.exe$'
Mike Williamsa3d1b292021-06-30 20:56:00 +020069 \ || e[0] =~# '.*pwsh$' || e[0] =~# '.*pwsh.exe$'
Bram Moolenaar66315972021-09-01 14:31:51 +020070 let str1 = "'cmd \"arg1\" ''arg2'' !%# \\'' \\! \\% \\#'"
71 let str2 = "'cmd \"arg1\" ''arg2'' \\!\\%\\# \\'' \\\\! \\\\% \\\\#'"
Jason Cox6e823512021-08-29 12:36:49 +020072 elseif e[0] =~# '.*fish$' || e[0] =~# '.*fish.exe$'
Bram Moolenaar66315972021-09-01 14:31:51 +020073 let str1 = "'cmd \"arg1\" '\\''arg2'\\'' !%# \\\\'\\'' \\\\! \\\\% \\\\#'"
74 let str2 = "'cmd \"arg1\" '\\''arg2'\\'' \\!\\%\\# \\\\'\\'' \\\\\\! \\\\\\% \\\\\\#'"
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +020075 else
Bram Moolenaar66315972021-09-01 14:31:51 +020076 let str1 = "'cmd \"arg1\" '\\''arg2'\\'' !%# \\'\\'' \\! \\% \\#'"
77 let str2 = "'cmd \"arg1\" '\\''arg2'\\'' \\!\\%\\# \\'\\'' \\\\! \\\\% \\\\#'"
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +020078 endif
Bram Moolenaar66315972021-09-01 14:31:51 +020079 call assert_equal(str1, shellescape("cmd \"arg1\" 'arg2' !%# \\' \\! \\% \\#"), e[0])
80 call assert_equal(str2, shellescape("cmd \"arg1\" 'arg2' !%# \\' \\! \\% \\#", 1), e[0])
Yegappan Lakshmananffec6dd2021-06-27 22:09:59 +020081
82 " Try running an external command with the shell.
83 if executable(e[0])
84 " set the shell options for the current 'shell'
85 let [&shellcmdflag, &shellpipe, &shellquote, &shellredir,
86 \ &shellxescape, &shellxquote] = e[1:6]
87 new
Mike Williamsa3d1b292021-06-30 20:56:00 +020088 try
89 r !echo hello
90 call assert_equal('hello', substitute(getline(2), '\W', '', 'g'), e[0])
91 catch
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +020092 call assert_report('Failed to run shell command, shell: ' .. e[0]
93 \ .. ', caught ' .. v:exception)
Mike Williamsa3d1b292021-06-30 20:56:00 +020094 finally
95 bwipe!
96 endtry
Yegappan Lakshmanan0a016672022-10-03 16:05:28 +010097
98 " filter buffer contents through an external command
99 new
100 call setline(1, ['tom', 'sam', 'andy'])
101 try
102 %!sort
103 call assert_equal(['andy', 'sam', 'tom'], getline(1, '$'), e[0])
104 catch
105 call assert_report($'Failed to filter buffer contents, shell: {e[0]}, caught {v:exception}')
106 finally
107 bwipe!
108 endtry
Yegappan Lakshmananffec6dd2021-06-27 22:09:59 +0200109 endif
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +0200110 endfor
Yegappan Lakshmananffec6dd2021-06-27 22:09:59 +0200111 set shell& shellcmdflag& shellpipe& shellquote&
112 set shellredir& shellxescape& shellxquote&
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +0200113 call delete('Xtestout')
114endfunc
115
116" Test for the 'shell' option
117func Test_shell()
118 CheckUnix
119 let save_shell = &shell
120 set shell=
121 let caught_e91 = 0
122 try
123 shell
124 catch /E91:/
125 let caught_e91 = 1
126 endtry
127 call assert_equal(1, caught_e91)
128 let &shell = save_shell
129endfunc
130
131" Test for the 'shellquote' option
132func Test_shellquote()
133 CheckUnix
134 set shellquote=#
135 set verbose=20
136 redir => v
137 silent! !echo Hello
138 redir END
139 set verbose&
140 set shellquote&
141 call assert_match(': "#echo Hello#"', v)
142endfunc
143
Yegappan Lakshmananffec6dd2021-06-27 22:09:59 +0200144" Test for the 'shellescape' option
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +0200145func Test_shellescape()
146 let save_shell = &shell
147 set shell=bash
148 call assert_equal("'text'", shellescape('text'))
149 call assert_equal("'te\"xt'", 'te"xt'->shellescape())
150 call assert_equal("'te'\\''xt'", shellescape("te'xt"))
151
152 call assert_equal("'te%xt'", shellescape("te%xt"))
153 call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
154 call assert_equal("'te#xt'", shellescape("te#xt"))
155 call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
156 call assert_equal("'te!xt'", shellescape("te!xt"))
157 call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
zeertzjq88c8c542024-05-30 19:27:25 +0200158 call assert_equal("'te<cword>xt'", shellescape("te<cword>xt"))
159 call assert_equal("'te\\<cword>xt'", shellescape("te<cword>xt", 1))
160 call assert_equal("'te<cword>%xt'", shellescape("te<cword>%xt"))
161 call assert_equal("'te\\<cword>\\%xt'", shellescape("te<cword>%xt", 1))
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +0200162
163 call assert_equal("'te\nxt'", shellescape("te\nxt"))
164 call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1))
165 set shell=tcsh
166 call assert_equal("'te\\!xt'", shellescape("te!xt"))
167 call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1))
168 call assert_equal("'te\\\nxt'", shellescape("te\nxt"))
169 call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1))
170
171 let &shell = save_shell
172endfunc
173
Mike Williams12795022021-06-28 20:53:58 +0200174" Test for 'shellslash'
175func Test_shellslash()
176 CheckOption shellslash
177 let save_shellslash = &shellslash
178 " The shell and cmdflag, and expected slash in tempname with shellslash set or
179 " unset. The assert checks the file separator before the leafname.
180 " ".*\\\\[^\\\\]*$"
181 let shells = [['cmd', '/c', '\\', '/'],
182 \ ['powershell', '-Command', '\\', '/'],
Mike Williamsa3d1b292021-06-30 20:56:00 +0200183 \ ['pwsh', '-Command', '\\', '/'],
184 \ ['pwsh', '-c', '\\', '/'],
Mike Williams12795022021-06-28 20:53:58 +0200185 \ ['sh', '-c', '/', '/']]
186 for e in shells
187 exe 'set shell=' .. e[0] .. ' | set shellcmdflag=' .. e[1]
188 set noshellslash
189 let file = tempname()
190 call assert_match('^.\+' .. e[2] .. '[^' .. e[2] .. ']\+$', file, e[0] .. ' ' .. e[1] .. ' nossl')
191 set shellslash
192 let file = tempname()
193 call assert_match('^.\+' .. e[3] .. '[^' .. e[3] .. ']\+$', file, e[0] .. ' ' .. e[1] .. ' ssl')
194 endfor
195 let &shellslash = save_shellslash
196endfunc
197
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +0200198" Test for 'shellxquote'
199func Test_shellxquote()
200 CheckUnix
201
202 let save_shell = &shell
203 let save_sxq = &shellxquote
204 let save_sxe = &shellxescape
205
Bram Moolenaar56564962022-10-10 22:39:42 +0100206 call writefile(['#!/bin/sh', 'echo "Cmd: [$*]" > Xlog'], 'Xtestshell', 'D')
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +0200207 call setfperm('Xtestshell', "r-x------")
208 set shell=./Xtestshell
209
210 set shellxquote=\\"
211 call feedkeys(":!pwd\<CR>\<CR>", 'xt')
212 call assert_equal(['Cmd: [-c "pwd"]'], readfile('Xlog'))
213
214 set shellxquote=(
215 call feedkeys(":!pwd\<CR>\<CR>", 'xt')
216 call assert_equal(['Cmd: [-c (pwd)]'], readfile('Xlog'))
217
218 set shellxquote=\\"(
219 call feedkeys(":!pwd\<CR>\<CR>", 'xt')
220 call assert_equal(['Cmd: [-c "(pwd)"]'], readfile('Xlog'))
221
222 set shellxescape=\"&<<()@^
223 set shellxquote=(
224 call feedkeys(":!pwd\"&<<{}@^\<CR>\<CR>", 'xt')
225 call assert_equal(['Cmd: [-c (pwd^"^&^<^<{}^@^^)]'], readfile('Xlog'))
226
227 let &shell = save_shell
228 let &shellxquote = save_sxq
229 let &shellxescape = save_sxe
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +0200230 call delete('Xlog')
231endfunc
232
Yegappan Lakshmananffec6dd2021-06-27 22:09:59 +0200233" Test for using the shell set in the $SHELL environment variable
234func Test_set_shell()
235 let after =<< trim [CODE]
236 call writefile([&shell], "Xtestout")
237 quit!
238 [CODE]
239
240 if has('win32')
241 let $SHELL = 'C:\with space\cmd.exe'
242 let expected = '"C:\with space\cmd.exe"'
243 else
244 let $SHELL = '/bin/with space/sh'
245 let expected = '/bin/with\ space/sh'
246 endif
247
248 if RunVimPiped([], after, '', '')
249 let lines = readfile('Xtestout')
250 call assert_equal(expected, lines[0])
251 endif
252 call delete('Xtestout')
253endfunc
254
Bram Moolenaar8107a2a2022-10-17 18:00:23 +0100255func Test_shell_repeat()
256 CheckUnix
257
258 let save_shell = &shell
259
260 call writefile(['#!/bin/sh', 'echo "Cmd: [$*]" > Xlog'], 'Xtestshell', 'D')
261 call setfperm('Xtestshell', "r-x------")
262 set shell=./Xtestshell
263 defer delete('Xlog')
264
265 call feedkeys(":!echo coconut\<CR>", 'xt') " Run command
266 call assert_equal(['Cmd: [-c echo coconut]'], readfile('Xlog'))
267
268 call feedkeys(":!!\<CR>", 'xt') " Re-run previous
269 call assert_equal(['Cmd: [-c echo coconut]'], readfile('Xlog'))
270
271 call writefile(['empty'], 'Xlog')
Martin Tournoij9c50eeb2022-10-22 09:02:56 +0100272 call feedkeys(":!\<CR>", 'xt') " :!
273 call assert_equal(['Cmd: [-c ]'], readfile('Xlog'))
Bram Moolenaar8107a2a2022-10-17 18:00:23 +0100274
275 call feedkeys(":!!\<CR>", 'xt') " :! doesn't clear previous command
276 call assert_equal(['Cmd: [-c echo coconut]'], readfile('Xlog'))
277
278 call feedkeys(":!echo banana\<CR>", 'xt') " Make sure setting previous command keeps working after a :! no-op
279 call assert_equal(['Cmd: [-c echo banana]'], readfile('Xlog'))
280 call feedkeys(":!!\<CR>", 'xt')
281 call assert_equal(['Cmd: [-c echo banana]'], readfile('Xlog'))
282
283 let &shell = save_shell
284endfunc
285
Bram Moolenaar66004472022-11-12 16:36:35 +0000286func Test_shell_no_prevcmd()
287 " this doesn't do anything, just check it doesn't crash
288 let after =<< trim END
289 exe "normal !!\<CR>"
290 call writefile([v:errmsg, 'done'], 'Xtestdone')
291 qall!
292 END
293 if RunVim([], after, '--clean')
294 call assert_equal(['E34: No previous command', 'done'], readfile('Xtestdone'))
295 endif
296 call delete('Xtestdone')
297endfunc
298
zeertzjq53fed232025-03-30 15:01:56 +0200299func Test_shell_filter_buffer_with_nul_bytes()
300 CheckUnix
301 new
302 set noshelltemp
303 " \n is a NUL byte
304 let lines = ["aaa\nbbb\nccc\nddd\neee", "fff\nggg\nhhh\niii\njjj"]
305 call setline(1, lines)
306 %!cat
307 call assert_equal(lines, getline(1, '$'))
308
309 set shelltemp&
310 bwipe!
311endfunc
312
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +0200313" vim: shiftwidth=2 sts=2 expandtab