Yegappan Lakshmanan | 054794c | 2021-06-27 12:07:49 +0200 | [diff] [blame] | 1 | " Test for the shell related options ('shell', 'shellcmdflag', 'shellpipe', |
| 2 | " 'shellquote', 'shellredir', 'shellxescape', and 'shellxquote') |
| 3 | |
| 4 | source check.vim |
| 5 | source shared.vim |
| 6 | |
| 7 | func Test_shell_options() |
| 8 | " For each shell, the following options are checked: |
| 9 | " 'shellcmdflag', 'shellpipe', 'shellquote', 'shellredir', 'shellxescape', |
| 10 | " 'shellxquote' |
| 11 | let shells = [] |
| 12 | if has('unix') |
| 13 | let shells += [['sh', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''], |
| 14 | \ ['ksh', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''], |
| 15 | \ ['mksh', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''], |
| 16 | \ ['zsh', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''], |
| 17 | \ ['zsh-beta', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''], |
| 18 | \ ['bash', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''], |
| 19 | \ ['fish', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''], |
| 20 | \ ['ash', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''], |
| 21 | \ ['dash', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''], |
| 22 | \ ['csh', '-c', '|& tee', '', '>&', '', ''], |
| 23 | \ ['tcsh', '-c', '|& tee', '', '>&', '', '']] |
| 24 | endif |
| 25 | if has('win32') |
| 26 | let shells += [['cmd', '/c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', ''], |
| 27 | \ ['cmd.exe', '/c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '('], |
| 28 | \ ['powershell.exe', '-c', '>', '', '>', '"&|<>()@^', '"'], |
| 29 | \ ['powershell', '-c', '>', '', '>', '"&|<>()@^', '"'], |
| 30 | \ ['sh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 31 | \ ['ksh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 32 | \ ['mksh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 33 | \ ['pdksh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 34 | \ ['zsh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 35 | \ ['zsh-beta.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 36 | \ ['bash.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 37 | \ ['dash.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 38 | \ ['csh.exe', '-c', '>&', '', '>&', '"&|<>()@^', '"'], |
| 39 | \ ['tcsh.exe', '-c', '>&', '', '>&', '"&|<>()@^', '"']] |
| 40 | endif |
| 41 | |
| 42 | let after =<< trim END |
| 43 | let l = [&shell, &shellcmdflag, &shellpipe, &shellquote] |
| 44 | let l += [&shellredir, &shellxescape, &shellxquote] |
| 45 | call writefile([json_encode(l)], 'Xtestout') |
| 46 | qall! |
| 47 | END |
| 48 | for e in shells |
| 49 | if RunVim([], after, '--cmd "set shell=' .. e[0] .. '"') |
| 50 | call assert_equal(e, json_decode(readfile('Xtestout')[0])) |
| 51 | endif |
| 52 | endfor |
| 53 | |
| 54 | for e in shells |
| 55 | exe 'set shell=' .. e[0] |
| 56 | if e[0] =~# '.*csh$' || e[0] =~# '.*csh.exe$' |
| 57 | let str1 = "'cmd \"arg1\" '\\''arg2'\\'' \\!%#'" |
| 58 | let str2 = "'cmd \"arg1\" '\\''arg2'\\'' \\\\!\\%\\#'" |
| 59 | else |
| 60 | let str1 = "'cmd \"arg1\" '\\''arg2'\\'' !%#'" |
| 61 | let str2 = "'cmd \"arg1\" '\\''arg2'\\'' \\!\\%\\#'" |
| 62 | endif |
| 63 | call assert_equal(str1, shellescape("cmd \"arg1\" 'arg2' !%#"), e[0]) |
| 64 | call assert_equal(str2, shellescape("cmd \"arg1\" 'arg2' !%#", 1), e[0]) |
| 65 | endfor |
| 66 | set shell& |
| 67 | call delete('Xtestout') |
| 68 | endfunc |
| 69 | |
| 70 | " Test for the 'shell' option |
| 71 | func Test_shell() |
| 72 | CheckUnix |
| 73 | let save_shell = &shell |
| 74 | set shell= |
| 75 | let caught_e91 = 0 |
| 76 | try |
| 77 | shell |
| 78 | catch /E91:/ |
| 79 | let caught_e91 = 1 |
| 80 | endtry |
| 81 | call assert_equal(1, caught_e91) |
| 82 | let &shell = save_shell |
| 83 | endfunc |
| 84 | |
| 85 | " Test for the 'shellquote' option |
| 86 | func Test_shellquote() |
| 87 | CheckUnix |
| 88 | set shellquote=# |
| 89 | set verbose=20 |
| 90 | redir => v |
| 91 | silent! !echo Hello |
| 92 | redir END |
| 93 | set verbose& |
| 94 | set shellquote& |
| 95 | call assert_match(': "#echo Hello#"', v) |
| 96 | endfunc |
| 97 | |
| 98 | func Test_shellescape() |
| 99 | let save_shell = &shell |
| 100 | set shell=bash |
| 101 | call assert_equal("'text'", shellescape('text')) |
| 102 | call assert_equal("'te\"xt'", 'te"xt'->shellescape()) |
| 103 | call assert_equal("'te'\\''xt'", shellescape("te'xt")) |
| 104 | |
| 105 | call assert_equal("'te%xt'", shellescape("te%xt")) |
| 106 | call assert_equal("'te\\%xt'", shellescape("te%xt", 1)) |
| 107 | call assert_equal("'te#xt'", shellescape("te#xt")) |
| 108 | call assert_equal("'te\\#xt'", shellescape("te#xt", 1)) |
| 109 | call assert_equal("'te!xt'", shellescape("te!xt")) |
| 110 | call assert_equal("'te\\!xt'", shellescape("te!xt", 1)) |
| 111 | |
| 112 | call assert_equal("'te\nxt'", shellescape("te\nxt")) |
| 113 | call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1)) |
| 114 | set shell=tcsh |
| 115 | call assert_equal("'te\\!xt'", shellescape("te!xt")) |
| 116 | call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1)) |
| 117 | call assert_equal("'te\\\nxt'", shellescape("te\nxt")) |
| 118 | call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1)) |
| 119 | |
| 120 | let &shell = save_shell |
| 121 | endfunc |
| 122 | |
| 123 | " Test for 'shellxquote' |
| 124 | func Test_shellxquote() |
| 125 | CheckUnix |
| 126 | |
| 127 | let save_shell = &shell |
| 128 | let save_sxq = &shellxquote |
| 129 | let save_sxe = &shellxescape |
| 130 | |
| 131 | call writefile(['#!/bin/sh', 'echo "Cmd: [$*]" > Xlog'], 'Xtestshell') |
| 132 | call setfperm('Xtestshell', "r-x------") |
| 133 | set shell=./Xtestshell |
| 134 | |
| 135 | set shellxquote=\\" |
| 136 | call feedkeys(":!pwd\<CR>\<CR>", 'xt') |
| 137 | call assert_equal(['Cmd: [-c "pwd"]'], readfile('Xlog')) |
| 138 | |
| 139 | set shellxquote=( |
| 140 | call feedkeys(":!pwd\<CR>\<CR>", 'xt') |
| 141 | call assert_equal(['Cmd: [-c (pwd)]'], readfile('Xlog')) |
| 142 | |
| 143 | set shellxquote=\\"( |
| 144 | call feedkeys(":!pwd\<CR>\<CR>", 'xt') |
| 145 | call assert_equal(['Cmd: [-c "(pwd)"]'], readfile('Xlog')) |
| 146 | |
| 147 | set shellxescape=\"&<<()@^ |
| 148 | set shellxquote=( |
| 149 | call feedkeys(":!pwd\"&<<{}@^\<CR>\<CR>", 'xt') |
| 150 | call assert_equal(['Cmd: [-c (pwd^"^&^<^<{}^@^^)]'], readfile('Xlog')) |
| 151 | |
| 152 | let &shell = save_shell |
| 153 | let &shellxquote = save_sxq |
| 154 | let &shellxescape = save_sxe |
| 155 | call delete('Xtestshell') |
| 156 | call delete('Xlog') |
| 157 | endfunc |
| 158 | |
| 159 | " vim: shiftwidth=2 sts=2 expandtab |