robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
2.6 KiB86 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each = t.describe, t.it, t.before_each
5local eq, clear, call = t.eq, n.clear, n.call
6local command = n.command
7local matches = t.matches
8local pcall_err = t.pcall_err
9local is_os = t.is_os
10local set_shell_powershell = n.set_shell_powershell
11local eval = n.eval
12
13local find_dummies = function(ext_pat)
14  local tmp_path = eval('$PATH')
15  command('let $PATH = fnamemodify("./test/functional/fixtures/bin", ":p")')
16  matches('null' .. ext_pat, call('exepath', 'null'))
17  matches('true' .. ext_pat, call('exepath', 'true'))
18  matches('false' .. ext_pat, call('exepath', 'false'))
19  command("let $PATH = '" .. tmp_path .. "'")
20end
21
22describe('exepath()', function()
23  before_each(clear)
24
25  it('fails for invalid values', function()
26    for _, input in ipairs({ 'v:null', 'v:true', 'v:false', '{}', '[]' }) do
27      eq(
28        'Vim(call):E1174: String required for argument 1',
29        pcall_err(command, 'call exepath(' .. input .. ')')
30      )
31    end
32    eq(
33      'Vim(call):E1175: Non-empty string required for argument 1',
34      pcall_err(command, 'call exepath("")')
35    )
36    command('let $PATH = fnamemodify("./test/functional/fixtures/bin", ":p")')
37    for _, input in ipairs({ 'v:null', 'v:true', 'v:false' }) do
38      eq(
39        'Vim(call):E1174: String required for argument 1',
40        pcall_err(command, 'call exepath(' .. input .. ')')
41      )
42    end
43  end)
44
45  if is_os('win') then
46    it('returns 1 for commands in $PATH (Windows)', function()
47      local exe = 'ping'
48      matches(exe .. '%.EXE$', call('exepath', exe))
49    end)
50
51    it('append extension if omitted', function()
52      local filename = 'cmd'
53      local pathext = '.exe'
54      clear({ env = { PATHEXT = pathext } })
55      eq(call('exepath', filename .. pathext), call('exepath', filename))
56    end)
57
58    it(
59      'returns file WITH extension if files both with and without extension exist in $PATH',
60      function()
61        local ext_pat = '%.CMD$'
62        find_dummies(ext_pat)
63        set_shell_powershell()
64        find_dummies(ext_pat)
65      end
66    )
67  else
68    it('returns 1 for commands in $PATH (not Windows)', function()
69      local exe = 'ls'
70      matches(exe .. '$', call('exepath', exe))
71    end)
72
73    it(
74      'returns file WITHOUT extension if files both with and without extension exist in $PATH',
75      function()
76        find_dummies('$')
77      end
78    )
79  end
80
81  it('respects shellslash #39524', function()
82    t.skip(not is_os('win'), "N/A: 'shellslash' only works on Windows")
83    local path = call('exepath', 'cmd.exe') ---@type string
84    t.ok(nil == path:find('/'))
85  end)
86end)