blob: 7d91dff358133ae48e059fbb3cd26d36790f28cb [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
4source check.vim
5source shared.vim
6
7func Test_shell_options()
K.Takata0500e872022-09-08 12:28:02 +01008 if has('win32')
9 " FIXME: This test is flaky on MS-Windows.
10 let g:test_is_flaky = 1
11 endif
12
Yegappan Lakshmananffec6dd2021-06-27 22:09:59 +020013 " The expected value of 'shellcmdflag', 'shellpipe', 'shellquote',
14 " 'shellredir', 'shellxescape', 'shellxquote' for the supported shells.
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +020015 let shells = []
16 if has('unix')
17 let shells += [['sh', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''],
18 \ ['ksh', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''],
19 \ ['mksh', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''],
20 \ ['zsh', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''],
21 \ ['zsh-beta', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''],
22 \ ['bash', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''],
23 \ ['fish', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''],
24 \ ['ash', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''],
25 \ ['dash', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''],
26 \ ['csh', '-c', '|& tee', '', '>&', '', ''],
Mike Williamsa3d1b292021-06-30 20:56:00 +020027 \ ['tcsh', '-c', '|& tee', '', '>&', '', ''],
28 \ ['pwsh', '-c', '>%s 2>&1', '', '>%s 2>&1', '', '']]
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +020029 endif
30 if has('win32')
31 let shells += [['cmd', '/c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', ''],
32 \ ['cmd.exe', '/c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '('],
Mike Williams12795022021-06-28 20:53:58 +020033 \ ['powershell.exe', '-Command', '2>&1 | Out-File -Encoding default',
34 \ '', '2>&1 | Out-File -Encoding default', '"&|<>()@^', '"'],
35 \ ['powershell', '-Command', '2>&1 | Out-File -Encoding default', '',
36 \ '2>&1 | Out-File -Encoding default', '"&|<>()@^', '"'],
Mike Williamsa3d1b292021-06-30 20:56:00 +020037 \ ['pwsh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
38 \ ['pwsh', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +020039 \ ['sh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
40 \ ['ksh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
41 \ ['mksh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
42 \ ['pdksh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
43 \ ['zsh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
44 \ ['zsh-beta.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
45 \ ['bash.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
46 \ ['dash.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
47 \ ['csh.exe', '-c', '>&', '', '>&', '"&|<>()@^', '"'],
48 \ ['tcsh.exe', '-c', '>&', '', '>&', '"&|<>()@^', '"']]
49 endif
50
Yegappan Lakshmananffec6dd2021-06-27 22:09:59 +020051 " start a new Vim instance with 'shell' set to each of the supported shells
52 " and check the default shell option settings
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +020053 let after =<< trim END
54 let l = [&shell, &shellcmdflag, &shellpipe, &shellquote]
55 let l += [&shellredir, &shellxescape, &shellxquote]
56 call writefile([json_encode(l)], 'Xtestout')
57 qall!
58 END
59 for e in shells
60 if RunVim([], after, '--cmd "set shell=' .. e[0] .. '"')
61 call assert_equal(e, json_decode(readfile('Xtestout')[0]))
62 endif
63 endfor
64
Yegappan Lakshmananffec6dd2021-06-27 22:09:59 +020065 " Test shellescape() for each of the shells.
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +020066 for e in shells
67 exe 'set shell=' .. e[0]
68 if e[0] =~# '.*csh$' || e[0] =~# '.*csh.exe$'
Bram Moolenaar66315972021-09-01 14:31:51 +020069 let str1 = "'cmd \"arg1\" '\\''arg2'\\'' \\!%# \\'\\'' \\\\! \\% \\#'"
70 let str2 = "'cmd \"arg1\" '\\''arg2'\\'' \\\\!\\%\\# \\'\\'' \\\\\\! \\\\% \\\\#'"
Mike Williams12795022021-06-28 20:53:58 +020071 elseif e[0] =~# '.*powershell$' || e[0] =~# '.*powershell.exe$'
Mike Williamsa3d1b292021-06-30 20:56:00 +020072 \ || e[0] =~# '.*pwsh$' || e[0] =~# '.*pwsh.exe$'
Bram Moolenaar66315972021-09-01 14:31:51 +020073 let str1 = "'cmd \"arg1\" ''arg2'' !%# \\'' \\! \\% \\#'"
74 let str2 = "'cmd \"arg1\" ''arg2'' \\!\\%\\# \\'' \\\\! \\\\% \\\\#'"
Jason Cox6e823512021-08-29 12:36:49 +020075 elseif e[0] =~# '.*fish$' || e[0] =~# '.*fish.exe$'
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 else
Bram Moolenaar66315972021-09-01 14:31:51 +020079 let str1 = "'cmd \"arg1\" '\\''arg2'\\'' !%# \\'\\'' \\! \\% \\#'"
80 let str2 = "'cmd \"arg1\" '\\''arg2'\\'' \\!\\%\\# \\'\\'' \\\\! \\\\% \\\\#'"
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +020081 endif
Bram Moolenaar66315972021-09-01 14:31:51 +020082 call assert_equal(str1, shellescape("cmd \"arg1\" 'arg2' !%# \\' \\! \\% \\#"), e[0])
83 call assert_equal(str2, shellescape("cmd \"arg1\" 'arg2' !%# \\' \\! \\% \\#", 1), e[0])
Yegappan Lakshmananffec6dd2021-06-27 22:09:59 +020084
85 " Try running an external command with the shell.
86 if executable(e[0])
87 " set the shell options for the current 'shell'
88 let [&shellcmdflag, &shellpipe, &shellquote, &shellredir,
89 \ &shellxescape, &shellxquote] = e[1:6]
90 new
Mike Williamsa3d1b292021-06-30 20:56:00 +020091 try
92 r !echo hello
93 call assert_equal('hello', substitute(getline(2), '\W', '', 'g'), e[0])
94 catch
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +020095 call assert_report('Failed to run shell command, shell: ' .. e[0]
96 \ .. ', caught ' .. v:exception)
Mike Williamsa3d1b292021-06-30 20:56:00 +020097 finally
98 bwipe!
99 endtry
Yegappan Lakshmanan0a016672022-10-03 16:05:28 +0100100
101 " filter buffer contents through an external command
102 new
103 call setline(1, ['tom', 'sam', 'andy'])
104 try
105 %!sort
106 call assert_equal(['andy', 'sam', 'tom'], getline(1, '$'), e[0])
107 catch
108 call assert_report($'Failed to filter buffer contents, shell: {e[0]}, caught {v:exception}')
109 finally
110 bwipe!
111 endtry
Yegappan Lakshmananffec6dd2021-06-27 22:09:59 +0200112 endif
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +0200113 endfor
Yegappan Lakshmananffec6dd2021-06-27 22:09:59 +0200114 set shell& shellcmdflag& shellpipe& shellquote&
115 set shellredir& shellxescape& shellxquote&
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +0200116 call delete('Xtestout')
117endfunc
118
119" Test for the 'shell' option
120func Test_shell()
121 CheckUnix
122 let save_shell = &shell
123 set shell=
124 let caught_e91 = 0
125 try
126 shell
127 catch /E91:/
128 let caught_e91 = 1
129 endtry
130 call assert_equal(1, caught_e91)
131 let &shell = save_shell
132endfunc
133
134" Test for the 'shellquote' option
135func Test_shellquote()
136 CheckUnix
137 set shellquote=#
138 set verbose=20
139 redir => v
140 silent! !echo Hello
141 redir END
142 set verbose&
143 set shellquote&
144 call assert_match(': "#echo Hello#"', v)
145endfunc
146
Yegappan Lakshmananffec6dd2021-06-27 22:09:59 +0200147" Test for the 'shellescape' option
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +0200148func Test_shellescape()
149 let save_shell = &shell
150 set shell=bash
151 call assert_equal("'text'", shellescape('text'))
152 call assert_equal("'te\"xt'", 'te"xt'->shellescape())
153 call assert_equal("'te'\\''xt'", shellescape("te'xt"))
154
155 call assert_equal("'te%xt'", shellescape("te%xt"))
156 call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
157 call assert_equal("'te#xt'", shellescape("te#xt"))
158 call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
159 call assert_equal("'te!xt'", shellescape("te!xt"))
160 call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
161
162 call assert_equal("'te\nxt'", shellescape("te\nxt"))
163 call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1))
164 set shell=tcsh
165 call assert_equal("'te\\!xt'", shellescape("te!xt"))
166 call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1))
167 call assert_equal("'te\\\nxt'", shellescape("te\nxt"))
168 call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1))
169
170 let &shell = save_shell
171endfunc
172
Mike Williams12795022021-06-28 20:53:58 +0200173" Test for 'shellslash'
174func Test_shellslash()
175 CheckOption shellslash
176 let save_shellslash = &shellslash
177 " The shell and cmdflag, and expected slash in tempname with shellslash set or
178 " unset. The assert checks the file separator before the leafname.
179 " ".*\\\\[^\\\\]*$"
180 let shells = [['cmd', '/c', '\\', '/'],
181 \ ['powershell', '-Command', '\\', '/'],
Mike Williamsa3d1b292021-06-30 20:56:00 +0200182 \ ['pwsh', '-Command', '\\', '/'],
183 \ ['pwsh', '-c', '\\', '/'],
Mike Williams12795022021-06-28 20:53:58 +0200184 \ ['sh', '-c', '/', '/']]
185 for e in shells
186 exe 'set shell=' .. e[0] .. ' | set shellcmdflag=' .. e[1]
187 set noshellslash
188 let file = tempname()
189 call assert_match('^.\+' .. e[2] .. '[^' .. e[2] .. ']\+$', file, e[0] .. ' ' .. e[1] .. ' nossl')
190 set shellslash
191 let file = tempname()
192 call assert_match('^.\+' .. e[3] .. '[^' .. e[3] .. ']\+$', file, e[0] .. ' ' .. e[1] .. ' ssl')
193 endfor
194 let &shellslash = save_shellslash
195endfunc
196
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +0200197" Test for 'shellxquote'
198func Test_shellxquote()
199 CheckUnix
200
201 let save_shell = &shell
202 let save_sxq = &shellxquote
203 let save_sxe = &shellxescape
204
Bram Moolenaar56564962022-10-10 22:39:42 +0100205 call writefile(['#!/bin/sh', 'echo "Cmd: [$*]" > Xlog'], 'Xtestshell', 'D')
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +0200206 call setfperm('Xtestshell', "r-x------")
207 set shell=./Xtestshell
208
209 set shellxquote=\\"
210 call feedkeys(":!pwd\<CR>\<CR>", 'xt')
211 call assert_equal(['Cmd: [-c "pwd"]'], readfile('Xlog'))
212
213 set shellxquote=(
214 call feedkeys(":!pwd\<CR>\<CR>", 'xt')
215 call assert_equal(['Cmd: [-c (pwd)]'], readfile('Xlog'))
216
217 set shellxquote=\\"(
218 call feedkeys(":!pwd\<CR>\<CR>", 'xt')
219 call assert_equal(['Cmd: [-c "(pwd)"]'], readfile('Xlog'))
220
221 set shellxescape=\"&<<()@^
222 set shellxquote=(
223 call feedkeys(":!pwd\"&<<{}@^\<CR>\<CR>", 'xt')
224 call assert_equal(['Cmd: [-c (pwd^"^&^<^<{}^@^^)]'], readfile('Xlog'))
225
226 let &shell = save_shell
227 let &shellxquote = save_sxq
228 let &shellxescape = save_sxe
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +0200229 call delete('Xlog')
230endfunc
231
Yegappan Lakshmananffec6dd2021-06-27 22:09:59 +0200232" Test for using the shell set in the $SHELL environment variable
233func Test_set_shell()
234 let after =<< trim [CODE]
235 call writefile([&shell], "Xtestout")
236 quit!
237 [CODE]
238
239 if has('win32')
240 let $SHELL = 'C:\with space\cmd.exe'
241 let expected = '"C:\with space\cmd.exe"'
242 else
243 let $SHELL = '/bin/with space/sh'
244 let expected = '/bin/with\ space/sh'
245 endif
246
247 if RunVimPiped([], after, '', '')
248 let lines = readfile('Xtestout')
249 call assert_equal(expected, lines[0])
250 endif
251 call delete('Xtestout')
252endfunc
253
Bram Moolenaar8107a2a2022-10-17 18:00:23 +0100254func Test_shell_repeat()
255 CheckUnix
256
257 let save_shell = &shell
258
259 call writefile(['#!/bin/sh', 'echo "Cmd: [$*]" > Xlog'], 'Xtestshell', 'D')
260 call setfperm('Xtestshell', "r-x------")
261 set shell=./Xtestshell
262 defer delete('Xlog')
263
264 call feedkeys(":!echo coconut\<CR>", 'xt') " Run command
265 call assert_equal(['Cmd: [-c echo coconut]'], readfile('Xlog'))
266
267 call feedkeys(":!!\<CR>", 'xt') " Re-run previous
268 call assert_equal(['Cmd: [-c echo coconut]'], readfile('Xlog'))
269
270 call writefile(['empty'], 'Xlog')
Martin Tournoij9c50eeb2022-10-22 09:02:56 +0100271 call feedkeys(":!\<CR>", 'xt') " :!
272 call assert_equal(['Cmd: [-c ]'], readfile('Xlog'))
Bram Moolenaar8107a2a2022-10-17 18:00:23 +0100273
274 call feedkeys(":!!\<CR>", 'xt') " :! doesn't clear previous command
275 call assert_equal(['Cmd: [-c echo coconut]'], readfile('Xlog'))
276
277 call feedkeys(":!echo banana\<CR>", 'xt') " Make sure setting previous command keeps working after a :! no-op
278 call assert_equal(['Cmd: [-c echo banana]'], readfile('Xlog'))
279 call feedkeys(":!!\<CR>", 'xt')
280 call assert_equal(['Cmd: [-c echo banana]'], readfile('Xlog'))
281
282 let &shell = save_shell
283endfunc
284
Bram Moolenaar66004472022-11-12 16:36:35 +0000285func Test_shell_no_prevcmd()
286 " this doesn't do anything, just check it doesn't crash
287 let after =<< trim END
288 exe "normal !!\<CR>"
289 call writefile([v:errmsg, 'done'], 'Xtestdone')
290 qall!
291 END
292 if RunVim([], after, '--clean')
293 call assert_equal(['E34: No previous command', 'done'], readfile('Xtestdone'))
294 endif
295 call delete('Xtestdone')
296endfunc
297
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +0200298" vim: shiftwidth=2 sts=2 expandtab