robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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