executable_spec.lua 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local eq, clear, call, write_file, command = t.eq, n.clear, n.call, t.write_file, n.command
  4. local exc_exec = n.exc_exec
  5. local eval = n.eval
  6. local is_os = t.is_os
  7. describe('executable()', function()
  8. before_each(clear)
  9. it('returns 1 for commands in $PATH', function()
  10. local exe = is_os('win') and 'ping' or 'ls'
  11. eq(1, call('executable', exe))
  12. command('let $PATH = fnamemodify("./test/functional/fixtures/bin", ":p")')
  13. eq(1, call('executable', 'null'))
  14. eq(1, call('executable', 'true'))
  15. eq(1, call('executable', 'false'))
  16. end)
  17. if is_os('win') then
  18. it('exepath respects shellslash', function()
  19. command('let $PATH = fnamemodify("./test/functional/fixtures/bin", ":p")')
  20. eq(
  21. [[test\functional\fixtures\bin\null.CMD]],
  22. call('fnamemodify', call('exepath', 'null'), ':.')
  23. )
  24. command('set shellslash')
  25. eq(
  26. 'test/functional/fixtures/bin/null.CMD',
  27. call('fnamemodify', call('exepath', 'null'), ':.')
  28. )
  29. end)
  30. it('stdpath respects shellslash', function()
  31. eq([[build\Xtest_xdg\share\nvim-data]], call('fnamemodify', call('stdpath', 'data'), ':.'))
  32. command('set shellslash')
  33. eq('build/Xtest_xdg/share/nvim-data', call('fnamemodify', call('stdpath', 'data'), ':.'))
  34. end)
  35. end
  36. it('fails for invalid values', function()
  37. for _, input in ipairs({ 'v:null', 'v:true', 'v:false', '{}', '[]' }) do
  38. eq(
  39. 'Vim(call):E1174: String required for argument 1',
  40. exc_exec('call executable(' .. input .. ')')
  41. )
  42. end
  43. command('let $PATH = fnamemodify("./test/functional/fixtures/bin", ":p")')
  44. for _, input in ipairs({ 'v:null', 'v:true', 'v:false' }) do
  45. eq(
  46. 'Vim(call):E1174: String required for argument 1',
  47. exc_exec('call executable(' .. input .. ')')
  48. )
  49. end
  50. end)
  51. it('returns 0 for empty strings', function()
  52. eq(0, call('executable', '""'))
  53. end)
  54. it('returns 0 for non-existent files', function()
  55. eq(0, call('executable', 'no_such_file_exists_209ufq23f'))
  56. end)
  57. it('sibling to nvim binary', function()
  58. -- Some executable in build/bin/, *not* in $PATH nor CWD.
  59. local sibling_exe = 'printargs-test'
  60. -- Windows: siblings are in Nvim's "pseudo-$PATH".
  61. local expected = is_os('win') and 1 or 0
  62. if is_os('win') then
  63. eq('arg1=lemon;arg2=sky;arg3=tree;', call('system', sibling_exe .. ' lemon sky tree'))
  64. end
  65. eq(expected, call('executable', sibling_exe))
  66. end)
  67. describe('exec-bit', function()
  68. setup(function()
  69. clear()
  70. write_file('Xtest_not_executable', 'non-executable file')
  71. write_file('Xtest_executable', 'executable file (exec-bit set)')
  72. if not is_os('win') then -- N/A for Windows.
  73. call('system', { 'chmod', '-x', 'Xtest_not_executable' })
  74. call('system', { 'chmod', '+x', 'Xtest_executable' })
  75. end
  76. end)
  77. teardown(function()
  78. os.remove('Xtest_not_executable')
  79. os.remove('Xtest_executable')
  80. end)
  81. it('not set', function()
  82. eq(0, call('executable', 'Xtest_not_executable'))
  83. eq(0, call('executable', './Xtest_not_executable'))
  84. end)
  85. it('set, unqualified and not in $PATH', function()
  86. eq(0, call('executable', 'Xtest_executable'))
  87. end)
  88. it('set, qualified as a path', function()
  89. local expected = is_os('win') and 0 or 1
  90. eq(expected, call('executable', './Xtest_executable'))
  91. end)
  92. end)
  93. end)
  94. describe('executable() (Windows)', function()
  95. if not is_os('win') then
  96. pending('N/A for non-windows')
  97. return
  98. end
  99. local exts = { 'bat', 'exe', 'com', 'cmd' }
  100. setup(function()
  101. for _, ext in ipairs(exts) do
  102. write_file('test_executable_' .. ext .. '.' .. ext, '')
  103. end
  104. write_file('test_executable_zzz.zzz', '')
  105. end)
  106. teardown(function()
  107. for _, ext in ipairs(exts) do
  108. os.remove('test_executable_' .. ext .. '.' .. ext)
  109. end
  110. os.remove('test_executable_zzz.zzz')
  111. end)
  112. it('tries default extensions on a filename if $PATHEXT is empty', function()
  113. -- Empty $PATHEXT defaults to ".com;.exe;.bat;.cmd".
  114. clear({ env = { PATHEXT = '' } })
  115. for _, ext in ipairs(exts) do
  116. eq(1, call('executable', 'test_executable_' .. ext))
  117. end
  118. eq(0, call('executable', 'test_executable_zzz'))
  119. end)
  120. it('tries default extensions on a filepath if $PATHEXT is empty', function()
  121. -- Empty $PATHEXT defaults to ".com;.exe;.bat;.cmd".
  122. clear({ env = { PATHEXT = '' } })
  123. for _, ext in ipairs(exts) do
  124. eq(1, call('executable', '.\\test_executable_' .. ext))
  125. end
  126. eq(0, call('executable', '.\\test_executable_zzz'))
  127. end)
  128. it('system([…]), jobstart([…]) use $PATHEXT #9569', function()
  129. -- Empty $PATHEXT defaults to ".com;.exe;.bat;.cmd".
  130. clear({ env = { PATHEXT = '' } })
  131. -- Invoking `cmdscript` should find/execute `cmdscript.cmd`.
  132. eq('much success\n', call('system', { 'test/functional/fixtures/cmdscript' }))
  133. assert(0 < call('jobstart', { 'test/functional/fixtures/cmdscript' }))
  134. end)
  135. it('full path with extension', function()
  136. -- Empty $PATHEXT defaults to ".com;.exe;.bat;.cmd".
  137. clear({ env = { PATHEXT = '' } })
  138. -- Some executable we can expect in the test env.
  139. local exe = 'printargs-test'
  140. local exedir = eval("fnamemodify(v:progpath, ':h')")
  141. local exepath = exedir .. '/' .. exe .. '.exe'
  142. eq(1, call('executable', exepath))
  143. eq('arg1=lemon;arg2=sky;arg3=tree;', call('system', exepath .. ' lemon sky tree'))
  144. end)
  145. it('full path without extension', function()
  146. -- Empty $PATHEXT defaults to ".com;.exe;.bat;.cmd".
  147. clear({ env = { PATHEXT = '' } })
  148. -- Some executable we can expect in the test env.
  149. local exe = 'printargs-test'
  150. local exedir = eval("fnamemodify(v:progpath, ':h')")
  151. local exepath = exedir .. '/' .. exe
  152. eq('arg1=lemon;arg2=sky;arg3=tree;', call('system', exepath .. ' lemon sky tree'))
  153. eq(1, call('executable', exepath))
  154. end)
  155. it('respects $PATHEXT when trying extensions on a filename', function()
  156. clear({ env = { PATHEXT = '.zzz' } })
  157. for _, ext in ipairs(exts) do
  158. eq(0, call('executable', 'test_executable_' .. ext))
  159. end
  160. eq(1, call('executable', 'test_executable_zzz'))
  161. end)
  162. it('respects $PATHEXT when trying extensions on a filepath', function()
  163. clear({ env = { PATHEXT = '.zzz' } })
  164. for _, ext in ipairs(exts) do
  165. eq(0, call('executable', '.\\test_executable_' .. ext))
  166. end
  167. eq(1, call('executable', '.\\test_executable_zzz'))
  168. end)
  169. it('with weird $PATHEXT', function()
  170. clear({ env = { PATHEXT = ';' } })
  171. eq(0, call('executable', '.\\test_executable_zzz'))
  172. clear({ env = { PATHEXT = ';;;.zzz;;' } })
  173. eq(1, call('executable', '.\\test_executable_zzz'))
  174. end)
  175. it("unqualified filename, Unix-style 'shell'", function()
  176. clear({ env = { PATHEXT = '' } })
  177. command('set shell=sh')
  178. for _, ext in ipairs(exts) do
  179. eq(1, call('executable', 'test_executable_' .. ext .. '.' .. ext))
  180. end
  181. eq(1, call('executable', 'test_executable_zzz.zzz'))
  182. end)
  183. it("relative path, Unix-style 'shell' (backslashes)", function()
  184. clear({ env = { PATHEXT = '' } })
  185. command('set shell=bash.exe')
  186. for _, ext in ipairs(exts) do
  187. eq(1, call('executable', '.\\test_executable_' .. ext .. '.' .. ext))
  188. eq(1, call('executable', './test_executable_' .. ext .. '.' .. ext))
  189. end
  190. eq(1, call('executable', '.\\test_executable_zzz.zzz'))
  191. eq(1, call('executable', './test_executable_zzz.zzz'))
  192. end)
  193. it('unqualified filename, $PATHEXT contains dot', function()
  194. clear({ env = { PATHEXT = '.;.zzz' } })
  195. for _, ext in ipairs(exts) do
  196. eq(1, call('executable', 'test_executable_' .. ext .. '.' .. ext))
  197. end
  198. eq(1, call('executable', 'test_executable_zzz.zzz'))
  199. clear({ env = { PATHEXT = '.zzz;.' } })
  200. for _, ext in ipairs(exts) do
  201. eq(1, call('executable', 'test_executable_' .. ext .. '.' .. ext))
  202. end
  203. eq(1, call('executable', 'test_executable_zzz.zzz'))
  204. end)
  205. it('relative path, $PATHEXT contains dot (backslashes)', function()
  206. clear({ env = { PATHEXT = '.;.zzz' } })
  207. for _, ext in ipairs(exts) do
  208. eq(1, call('executable', '.\\test_executable_' .. ext .. '.' .. ext))
  209. eq(1, call('executable', './test_executable_' .. ext .. '.' .. ext))
  210. end
  211. eq(1, call('executable', '.\\test_executable_zzz.zzz'))
  212. eq(1, call('executable', './test_executable_zzz.zzz'))
  213. end)
  214. it('ignores case of extension', function()
  215. clear({ env = { PATHEXT = '.ZZZ' } })
  216. eq(1, call('executable', 'test_executable_zzz.zzz'))
  217. end)
  218. it('relative path does not search $PATH', function()
  219. clear({ env = { PATHEXT = '' } })
  220. eq(0, call('executable', './System32/notepad.exe'))
  221. eq(0, call('executable', '.\\System32\\notepad.exe'))
  222. eq(0, call('executable', '../notepad.exe'))
  223. eq(0, call('executable', '..\\notepad.exe'))
  224. end)
  225. end)