test_shell.vim 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. " Test for the shell related options ('shell', 'shellcmdflag', 'shellpipe',
  2. " 'shellquote', 'shellredir', 'shellxescape', and 'shellxquote')
  3. source check.vim
  4. source shared.vim
  5. func Test_shell_options()
  6. if has('win32')
  7. " FIXME: This test is flaky on MS-Windows.
  8. let g:test_is_flaky = 1
  9. endif
  10. " The expected value of 'shellcmdflag', 'shellpipe', 'shellquote',
  11. " 'shellredir', 'shellxescape', 'shellxquote' for the supported shells.
  12. 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', '', '>&', '', ''],
  24. \ ['tcsh', '-c', '|& tee', '', '>&', '', '']]
  25. endif
  26. if has('win32')
  27. let shells += [['cmd', '/s /c', '2>&1| tee', '', '>%s 2>&1', '', '"']]
  28. endif
  29. " start a new Vim instance with 'shell' set to each of the supported shells
  30. " and check the default shell option settings
  31. let after =<< trim END
  32. let l = [&shell, &shellcmdflag, &shellpipe, &shellquote]
  33. let l += [&shellredir, &shellxescape, &shellxquote]
  34. call writefile([json_encode(l)], 'Xtestout')
  35. qall!
  36. END
  37. for e in shells
  38. if RunVim([], after, '--cmd "set shell=' .. e[0] .. '"')
  39. call assert_equal(e, json_decode(readfile('Xtestout')[0]))
  40. endif
  41. endfor
  42. " Test shellescape() for each of the shells.
  43. for e in shells
  44. exe 'set shell=' .. e[0]
  45. if e[0] =~# '.*csh$' || e[0] =~# '.*csh.exe$'
  46. let str1 = "'cmd \"arg1\" '\\''arg2'\\'' \\!%#'"
  47. let str2 = "'cmd \"arg1\" '\\''arg2'\\'' \\\\!\\%\\#'"
  48. elseif e[0] =~# '.*powershell$' || e[0] =~# '.*powershell.exe$'
  49. let str1 = "'cmd \"arg1\" ''arg2'' !%#'"
  50. let str2 = "'cmd \"arg1\" ''arg2'' \\!\\%\\#'"
  51. else
  52. let str1 = "'cmd \"arg1\" '\\''arg2'\\'' !%#'"
  53. let str2 = "'cmd \"arg1\" '\\''arg2'\\'' \\!\\%\\#'"
  54. endif
  55. call assert_equal(str1, shellescape("cmd \"arg1\" 'arg2' !%#"), e[0])
  56. call assert_equal(str2, shellescape("cmd \"arg1\" 'arg2' !%#", 1), e[0])
  57. " Try running an external command with the shell.
  58. if executable(e[0])
  59. " set the shell options for the current 'shell'
  60. let [&shellcmdflag, &shellpipe, &shellquote, &shellredir,
  61. \ &shellxescape, &shellxquote] = e[1:6]
  62. new
  63. r !echo hello
  64. call assert_equal('hello', substitute(getline(2), '\W', '', 'g'), e[0])
  65. bwipe!
  66. endif
  67. endfor
  68. set shell& shellcmdflag& shellpipe& shellquote&
  69. set shellredir& shellxescape& shellxquote&
  70. call delete('Xtestout')
  71. endfunc
  72. " Test for the 'shell' option
  73. func Test_shell()
  74. throw 'Skipped: Nvim missing :shell currently'
  75. CheckUnix
  76. let save_shell = &shell
  77. set shell=
  78. let caught_e91 = 0
  79. try
  80. shell
  81. catch /E91:/
  82. let caught_e91 = 1
  83. endtry
  84. call assert_equal(1, caught_e91)
  85. let &shell = save_shell
  86. endfunc
  87. " Test for the 'shellquote' option
  88. func Test_shellquote()
  89. CheckUnix
  90. set shellquote=#
  91. set verbose=20
  92. redir => v
  93. silent! !echo Hello
  94. redir END
  95. set verbose&
  96. set shellquote&
  97. call assert_match(': "#echo Hello#"', v)
  98. endfunc
  99. " Test for the 'shellescape' option
  100. func Test_shellescape()
  101. let save_shell = &shell
  102. set shell=bash
  103. call assert_equal("'text'", shellescape('text'))
  104. call assert_equal("'te\"xt'", 'te"xt'->shellescape())
  105. call assert_equal("'te'\\''xt'", shellescape("te'xt"))
  106. call assert_equal("'te%xt'", shellescape("te%xt"))
  107. call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
  108. call assert_equal("'te#xt'", shellescape("te#xt"))
  109. call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
  110. call assert_equal("'te!xt'", shellescape("te!xt"))
  111. call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
  112. call assert_equal("'te<cword>xt'", shellescape("te<cword>xt"))
  113. call assert_equal("'te\\<cword>xt'", shellescape("te<cword>xt", 1))
  114. call assert_equal("'te<cword>%xt'", shellescape("te<cword>%xt"))
  115. call assert_equal("'te\\<cword>\\%xt'", shellescape("te<cword>%xt", 1))
  116. call assert_equal("'te\nxt'", shellescape("te\nxt"))
  117. call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1))
  118. set shell=tcsh
  119. call assert_equal("'te\\!xt'", shellescape("te!xt"))
  120. call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1))
  121. call assert_equal("'te\\\nxt'", shellescape("te\nxt"))
  122. call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1))
  123. set shell=fish
  124. call assert_equal("'text'", shellescape('text'))
  125. call assert_equal("'te\"xt'", shellescape('te"xt'))
  126. call assert_equal("'te'\\''xt'", shellescape("te'xt"))
  127. call assert_equal("'te%xt'", shellescape("te%xt"))
  128. call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
  129. call assert_equal("'te#xt'", shellescape("te#xt"))
  130. call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
  131. call assert_equal("'te!xt'", shellescape("te!xt"))
  132. call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
  133. call assert_equal("'te\\\\xt'", shellescape("te\\xt"))
  134. call assert_equal("'te\\\\xt'", shellescape("te\\xt", 1))
  135. call assert_equal("'te\\\\'\\''xt'", shellescape("te\\'xt"))
  136. call assert_equal("'te\\\\'\\''xt'", shellescape("te\\'xt", 1))
  137. call assert_equal("'te\\\\!xt'", shellescape("te\\!xt"))
  138. call assert_equal("'te\\\\\\!xt'", shellescape("te\\!xt", 1))
  139. call assert_equal("'te\\\\%xt'", shellescape("te\\%xt"))
  140. call assert_equal("'te\\\\\\%xt'", shellescape("te\\%xt", 1))
  141. call assert_equal("'te\\\\#xt'", shellescape("te\\#xt"))
  142. call assert_equal("'te\\\\\\#xt'", shellescape("te\\#xt", 1))
  143. let &shell = save_shell
  144. endfunc
  145. " Test for 'shellslash'
  146. func Test_shellslash()
  147. CheckOption shellslash
  148. let save_shellslash = &shellslash
  149. " The shell and cmdflag, and expected slash in tempname with shellslash set or
  150. " unset. The assert checks the file separator before the leafname.
  151. " ".*\\\\[^\\\\]*$"
  152. let shells = [['cmd', '/c', '/', '/'],
  153. \ ['powershell', '-Command', '/', '/'],
  154. \ ['sh', '-c', '/', '/']]
  155. for e in shells
  156. exe 'set shell=' .. e[0] .. ' | set shellcmdflag=' .. e[1]
  157. set noshellslash
  158. let file = tempname()
  159. call assert_match('^.\+' .. e[2] .. '[^' .. e[2] .. ']\+$', file, e[0] .. ' ' .. e[1] .. ' nossl')
  160. set shellslash
  161. let file = tempname()
  162. call assert_match('^.\+' .. e[3] .. '[^' .. e[3] .. ']\+$', file, e[0] .. ' ' .. e[1] .. ' ssl')
  163. endfor
  164. let &shellslash = save_shellslash
  165. endfunc
  166. " Test for 'shellxquote'
  167. func Test_shellxquote()
  168. CheckUnix
  169. let save_shell = &shell
  170. let save_sxq = &shellxquote
  171. let save_sxe = &shellxescape
  172. call writefile(['#!/bin/sh', 'echo "Cmd: [$*]" > Xlog'], 'Xtestshell', 'D')
  173. call setfperm('Xtestshell', "r-x------")
  174. set shell=./Xtestshell
  175. set shellxquote=\\"
  176. call feedkeys(":!pwd\<CR>\<CR>", 'xt')
  177. call assert_equal(['Cmd: [-c "pwd"]'], readfile('Xlog'))
  178. set shellxquote=(
  179. call feedkeys(":!pwd\<CR>\<CR>", 'xt')
  180. call assert_equal(['Cmd: [-c (pwd)]'], readfile('Xlog'))
  181. set shellxquote=\\"(
  182. call feedkeys(":!pwd\<CR>\<CR>", 'xt')
  183. call assert_equal(['Cmd: [-c "(pwd)"]'], readfile('Xlog'))
  184. set shellxescape=\"&<<()@^
  185. set shellxquote=(
  186. call feedkeys(":!pwd\"&<<{}@^\<CR>\<CR>", 'xt')
  187. call assert_equal(['Cmd: [-c (pwd^"^&^<^<{}^@^^)]'], readfile('Xlog'))
  188. let &shell = save_shell
  189. let &shellxquote = save_sxq
  190. let &shellxescape = save_sxe
  191. call delete('Xlog')
  192. endfunc
  193. " Test for using the shell set in the $SHELL environment variable
  194. func Test_set_shell()
  195. let after =<< trim [CODE]
  196. call writefile([&shell], "Xtestout")
  197. quit!
  198. [CODE]
  199. if has('win32')
  200. let $SHELL = 'C:\with space\cmd.exe'
  201. let expected = '"C:\with space\cmd.exe"'
  202. else
  203. let $SHELL = '/bin/with space/sh'
  204. let expected = '"/bin/with space/sh"'
  205. endif
  206. if RunVimPiped([], after, '', '')
  207. let lines = readfile('Xtestout')
  208. call assert_equal(expected, lines[0])
  209. endif
  210. call delete('Xtestout')
  211. endfunc
  212. func Test_shell_repeat()
  213. CheckUnix
  214. let save_shell = &shell
  215. call writefile(['#!/bin/sh', 'echo "Cmd: [$*]" > Xlog'], 'Xtestshell', 'D')
  216. call setfperm('Xtestshell', "r-x------")
  217. set shell=./Xtestshell
  218. defer delete('Xlog')
  219. call feedkeys(":!echo coconut\<CR>", 'xt') " Run command
  220. call assert_equal(['Cmd: [-c echo coconut]'], readfile('Xlog'))
  221. call feedkeys(":!!\<CR>", 'xt') " Re-run previous
  222. call assert_equal(['Cmd: [-c echo coconut]'], readfile('Xlog'))
  223. call writefile(['empty'], 'Xlog')
  224. call feedkeys(":!\<CR>", 'xt') " :!
  225. call assert_equal(['Cmd: [-c ]'], readfile('Xlog'))
  226. call feedkeys(":!!\<CR>", 'xt') " :! doesn't clear previous command
  227. call assert_equal(['Cmd: [-c echo coconut]'], readfile('Xlog'))
  228. call feedkeys(":!echo banana\<CR>", 'xt') " Make sure setting previous command keeps working after a :! no-op
  229. call assert_equal(['Cmd: [-c echo banana]'], readfile('Xlog'))
  230. call feedkeys(":!!\<CR>", 'xt')
  231. call assert_equal(['Cmd: [-c echo banana]'], readfile('Xlog'))
  232. let &shell = save_shell
  233. endfunc
  234. func Test_shell_no_prevcmd()
  235. " this doesn't do anything, just check it doesn't crash
  236. let after =<< trim END
  237. exe "normal !!\<CR>"
  238. call writefile([v:errmsg, 'done'], 'Xtestdone')
  239. qall!
  240. END
  241. if RunVim([], after, '--clean')
  242. call assert_equal(['E34: No previous command', 'done'], readfile('Xtestdone'))
  243. endif
  244. call delete('Xtestdone')
  245. endfunc
  246. " vim: shiftwidth=2 sts=2 expandtab