blob: 1ed4c5ec3e67079527fda0c362b3a2bc36745076 [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 Lakshmananffec6dd2021-06-27 22:09:59 +0200100 endif
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +0200101 endfor
Yegappan Lakshmananffec6dd2021-06-27 22:09:59 +0200102 set shell& shellcmdflag& shellpipe& shellquote&
103 set shellredir& shellxescape& shellxquote&
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +0200104 call delete('Xtestout')
105endfunc
106
107" Test for the 'shell' option
108func Test_shell()
109 CheckUnix
110 let save_shell = &shell
111 set shell=
112 let caught_e91 = 0
113 try
114 shell
115 catch /E91:/
116 let caught_e91 = 1
117 endtry
118 call assert_equal(1, caught_e91)
119 let &shell = save_shell
120endfunc
121
122" Test for the 'shellquote' option
123func Test_shellquote()
124 CheckUnix
125 set shellquote=#
126 set verbose=20
127 redir => v
128 silent! !echo Hello
129 redir END
130 set verbose&
131 set shellquote&
132 call assert_match(': "#echo Hello#"', v)
133endfunc
134
Yegappan Lakshmananffec6dd2021-06-27 22:09:59 +0200135" Test for the 'shellescape' option
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +0200136func Test_shellescape()
137 let save_shell = &shell
138 set shell=bash
139 call assert_equal("'text'", shellescape('text'))
140 call assert_equal("'te\"xt'", 'te"xt'->shellescape())
141 call assert_equal("'te'\\''xt'", shellescape("te'xt"))
142
143 call assert_equal("'te%xt'", shellescape("te%xt"))
144 call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
145 call assert_equal("'te#xt'", shellescape("te#xt"))
146 call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
147 call assert_equal("'te!xt'", shellescape("te!xt"))
148 call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
149
150 call assert_equal("'te\nxt'", shellescape("te\nxt"))
151 call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1))
152 set shell=tcsh
153 call assert_equal("'te\\!xt'", shellescape("te!xt"))
154 call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1))
155 call assert_equal("'te\\\nxt'", shellescape("te\nxt"))
156 call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1))
157
158 let &shell = save_shell
159endfunc
160
Mike Williams12795022021-06-28 20:53:58 +0200161" Test for 'shellslash'
162func Test_shellslash()
163 CheckOption shellslash
164 let save_shellslash = &shellslash
165 " The shell and cmdflag, and expected slash in tempname with shellslash set or
166 " unset. The assert checks the file separator before the leafname.
167 " ".*\\\\[^\\\\]*$"
168 let shells = [['cmd', '/c', '\\', '/'],
169 \ ['powershell', '-Command', '\\', '/'],
Mike Williamsa3d1b292021-06-30 20:56:00 +0200170 \ ['pwsh', '-Command', '\\', '/'],
171 \ ['pwsh', '-c', '\\', '/'],
Mike Williams12795022021-06-28 20:53:58 +0200172 \ ['sh', '-c', '/', '/']]
173 for e in shells
174 exe 'set shell=' .. e[0] .. ' | set shellcmdflag=' .. e[1]
175 set noshellslash
176 let file = tempname()
177 call assert_match('^.\+' .. e[2] .. '[^' .. e[2] .. ']\+$', file, e[0] .. ' ' .. e[1] .. ' nossl')
178 set shellslash
179 let file = tempname()
180 call assert_match('^.\+' .. e[3] .. '[^' .. e[3] .. ']\+$', file, e[0] .. ' ' .. e[1] .. ' ssl')
181 endfor
182 let &shellslash = save_shellslash
183endfunc
184
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +0200185" Test for 'shellxquote'
186func Test_shellxquote()
187 CheckUnix
188
189 let save_shell = &shell
190 let save_sxq = &shellxquote
191 let save_sxe = &shellxescape
192
193 call writefile(['#!/bin/sh', 'echo "Cmd: [$*]" > Xlog'], 'Xtestshell')
194 call setfperm('Xtestshell', "r-x------")
195 set shell=./Xtestshell
196
197 set shellxquote=\\"
198 call feedkeys(":!pwd\<CR>\<CR>", 'xt')
199 call assert_equal(['Cmd: [-c "pwd"]'], readfile('Xlog'))
200
201 set shellxquote=(
202 call feedkeys(":!pwd\<CR>\<CR>", 'xt')
203 call assert_equal(['Cmd: [-c (pwd)]'], readfile('Xlog'))
204
205 set shellxquote=\\"(
206 call feedkeys(":!pwd\<CR>\<CR>", 'xt')
207 call assert_equal(['Cmd: [-c "(pwd)"]'], readfile('Xlog'))
208
209 set shellxescape=\"&<<()@^
210 set shellxquote=(
211 call feedkeys(":!pwd\"&<<{}@^\<CR>\<CR>", 'xt')
212 call assert_equal(['Cmd: [-c (pwd^"^&^<^<{}^@^^)]'], readfile('Xlog'))
213
214 let &shell = save_shell
215 let &shellxquote = save_sxq
216 let &shellxescape = save_sxe
217 call delete('Xtestshell')
218 call delete('Xlog')
219endfunc
220
Yegappan Lakshmananffec6dd2021-06-27 22:09:59 +0200221" Test for using the shell set in the $SHELL environment variable
222func Test_set_shell()
223 let after =<< trim [CODE]
224 call writefile([&shell], "Xtestout")
225 quit!
226 [CODE]
227
228 if has('win32')
229 let $SHELL = 'C:\with space\cmd.exe'
230 let expected = '"C:\with space\cmd.exe"'
231 else
232 let $SHELL = '/bin/with space/sh'
233 let expected = '/bin/with\ space/sh'
234 endif
235
236 if RunVimPiped([], after, '', '')
237 let lines = readfile('Xtestout')
238 call assert_equal(expected, lines[0])
239 endif
240 call delete('Xtestout')
241endfunc
242
Yegappan Lakshmanan054794c2021-06-27 12:07:49 +0200243" vim: shiftwidth=2 sts=2 expandtab