robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
7.8 KiB265 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each, setup, teardown =
5  t.describe, t.it, t.before_each, t.setup, t.teardown
6local clear = n.clear
7local command = n.command
8local eq = t.eq
9local eval = n.eval
10local feed = n.feed
11local fn = n.fn
12local insert = n.insert
13local is_os = t.is_os
14local mkdir = t.mkdir
15local rmdir = n.rmdir
16local write_file = t.write_file
17local api = n.api
18
19local function join_path(...)
20  return table.concat({ ... }, '/')
21end
22
23describe('path', function()
24  local targetdir
25  local expected_path
26
27  before_each(function()
28    targetdir = join_path('test', 'functional', 'fixtures')
29    clear()
30    command('edit ' .. join_path(targetdir, 'tty-test.c'))
31    expected_path = eval('expand("%:p")')
32  end)
33
34  it('with /./ segment #7117', function()
35    command('edit ' .. join_path(targetdir, '.', 'tty-test.c'))
36    eq(expected_path, eval('expand("%:p")'))
37  end)
38
39  it('with ./ prefix #7117', function()
40    command('edit ' .. join_path('.', targetdir, 'tty-test.c'))
41    eq(expected_path, eval('expand("%:p")'))
42  end)
43
44  it('with ./ prefix, after directory change #7117', function()
45    command('edit ' .. join_path('.', targetdir, 'tty-test.c'))
46    command('cd test')
47    eq(expected_path, eval('expand("%:p")'))
48  end)
49
50  it('with /../ segment #7117', function()
51    command('edit ' .. join_path(targetdir, '..', 'fixtures', 'tty-test.c'))
52    eq(expected_path, eval('expand("%:p")'))
53  end)
54
55  it('with ../ and different starting directory #7117', function()
56    command('cd test')
57    command('edit ' .. join_path('..', targetdir, 'tty-test.c'))
58    eq(expected_path, eval('expand("%:p")'))
59  end)
60
61  it('with ./../ and different starting directory #7117', function()
62    command('cd test')
63    command('edit ' .. join_path('.', '..', targetdir, 'tty-test.c'))
64    eq(expected_path, eval('expand("%:p")'))
65  end)
66
67  it('with implicit drive letter #40013', function()
68    t.skip(not is_os('win'), 'N/A: only works on Windows')
69    command('edit ' .. expected_path:sub(3))
70    eq(1, #fn.getbufinfo())
71    eq(expected_path, eval('expand("%:p")'))
72  end)
73end)
74
75describe('startup', function()
76  local home, foo, bar = vim.fs.normalize('~'), 'Xtest-foo.lua', 'Xtest-bar.lua'
77
78  setup(function()
79    write_file(('%s/%s'):format(home, foo), 'local foo = 1;')
80    write_file(('%s/%s'):format(home, bar), '')
81  end)
82
83  teardown(function()
84    os.remove(('%s/%s'):format(home, foo))
85    os.remove(('%s/%s'):format(home, bar))
86  end)
87
88  it('expands tilde-prefixed paths #29380', function()
89    t.skip(not is_os('win'), 'N/A: shell expands ~ on Unix')
90    clear {
91      args = { ('~/%s'):format(foo), ('~\\%s'):format(bar) },
92    }
93    local expected = { ('%s/%s'):format(home, foo), ('%s/%s'):format(home, bar) }
94    eq(expected, vim.tbl_map(fn.bufname, api.nvim_list_bufs()))
95    eq(expected, fn.argv())
96  end)
97
98  it('normalizes v:progpath #39382', function()
99    clear()
100    eq(
101      n.nvim_prog,
102      fn.system({
103        is_os('win') and n.nvim_prog:gsub('/', '\\') or n.nvim_prog,
104        '--clean',
105        '--headless',
106        '+echo v:progpath',
107        '+q',
108      })
109    )
110  end)
111
112  it('normalizes -u arguments and triggers autocmd events #39382', function()
113    local file = ('%s/%s'):format(home, foo)
114    if is_os('win') then
115      file = file:gsub('/', '\\')
116    end
117    clear {
118      args_rm = { '--cmd', '-u' },
119      args = {
120        '--cmd',
121        ('autocmd SourcePre */%s let g:matched = 1'):format(foo),
122        '-u',
123        file,
124      },
125    }
126    local scripts = fn.getscriptinfo({ name = foo })
127    eq(1, #scripts)
128    eq(('%s/%s'):format(home, foo), scripts[1].name)
129    t.ok(fn.eval('g:matched'))
130  end)
131end)
132
133describe('expand wildcard', function()
134  before_each(clear)
135
136  it('with special characters #24421', function()
137    local folders = is_os('win') and {
138      '{folder}',
139      'folder$name',
140    } or {
141      'folder-name',
142      'folder#name',
143    }
144    for _, folder in ipairs(folders) do
145      mkdir(folder)
146      local file = ('%s%sfile.txt'):format(folder, n.get_pathsep())
147      write_file(file, '')
148      eq(file, fn.expand(('%s/*'):format(folder)))
149      rmdir(folder)
150    end
151  end)
152end)
153
154describe('file search', function()
155  local testdir = 'Xtest_path_spec'
156
157  before_each(clear)
158
159  setup(function()
160    mkdir(testdir)
161  end)
162
163  teardown(function()
164    rmdir(testdir)
165  end)
166
167  it('gf finds multibyte filename in line #20517', function()
168    command('cd test/functional/fixtures')
169    insert('filename_with_unicode_ααα')
170    eq('', eval('expand("%")'))
171    feed('gf')
172    eq('filename_with_unicode_ααα', eval('expand("%:t")'))
173  end)
174
175  it('gf/<cfile> matches Windows drive-letter filepaths (without ":" in &isfname)', function()
176    local iswin = is_os('win')
177    local function test_cfile(input, expected, expected_win)
178      expected = (iswin and expected_win or expected) or input
179      command('%delete')
180      insert(input)
181      command('norm! 0')
182      eq(expected, eval('expand("<cfile>")'))
183    end
184
185    -- test_cfile([[c:/d:/e:/foo/bar.txt]], 'c:/d:/e') -- TODO(justinmk): should return "d:/foo/bar.txt" ?
186    test_cfile([[c:/d:/foo/bar.txt]]) -- TODO(justinmk): should return "d:/foo/bar.txt" ?
187    test_cfile([[//share/c:/foo/bar/]])
188    test_cfile([[file://c:/foo/bar]])
189    test_cfile([[file://c:/foo/bar:42]])
190    test_cfile([[file://c:/foo/bar:42:666]])
191    test_cfile([[https://c:/foo/bar]])
192    test_cfile([[\foo\bar]], [[foo]], [[\foo\bar]])
193    test_cfile([[/foo/bar]], [[/foo/bar]])
194    test_cfile([[c:\foo\bar]], [[c:]], [[c:\foo\bar]])
195    test_cfile([[c:\foo\bar:42:666]], [[c:]], [[c:\foo\bar]])
196    test_cfile([[c:/foo/bar]])
197    test_cfile([[c:/foo/bar:42]], [[c:/foo/bar]])
198    test_cfile([[c:/foo/bar:42:666]], [[c:/foo/bar]])
199    test_cfile([[c:foo\bar]], [[c]])
200    test_cfile([[c:foo/bar]], [[c]])
201    test_cfile([[c:foo]], [[c]])
202    -- Examples from: https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats#example-ways-to-refer-to-the-same-file
203    test_cfile([[c:\temp\test-file.txt]], [[c:]], [[c:\temp\test-file.txt]])
204    test_cfile(
205      [[\\127.0.0.1\c$\temp\test-file.txt]],
206      [[127.0.0.1]],
207      [[\\127.0.0.1\c$\temp\test-file.txt]]
208    )
209    test_cfile(
210      [[\\LOCALHOST\c$\temp\test-file.txt]],
211      [[LOCALHOST]],
212      [[\\LOCALHOST\c$\temp\test-file.txt]]
213    )
214    -- not supported yet
215    test_cfile([[\\.\c:\temp\test-file.txt]], [[.]], [[\\.\c]])
216    -- not supported yet
217    test_cfile([[\\?\c:\temp\test-file.txt]], [[c:]], [[\\]])
218    test_cfile(
219      [[\\.\UNC\LOCALHOST\c$\temp\test-file.txt]],
220      [[.]],
221      [[\\.\UNC\LOCALHOST\c$\temp\test-file.txt]]
222    )
223    test_cfile(
224      [[\\127.0.0.1\c$\temp\test-file.txt]],
225      [[127.0.0.1]],
226      [[\\127.0.0.1\c$\temp\test-file.txt]]
227    )
228  end)
229
230  it('gf/<cfile> handles local file: paths', function()
231    local path = fn.fnamemodify(join_path(testdir, 'gf-target.txt'), ':p'):gsub('\\', '/')
232    local uri = 'file:' .. (is_os('win') and '/' or '') .. path
233    write_file(path, '')
234    insert(uri)
235    command('norm! 0')
236    eq(path, eval('expand("<cfile>")'))
237    feed('gf')
238    eq(path, fn.fnamemodify(eval('expand("%:p")'), ':p'):gsub('\\', '/'))
239  end)
240
241  ---@param funcname 'finddir' | 'findfile'
242  local function test_find_func(funcname, folder, item)
243    local d = join_path(testdir, folder)
244    mkdir(d)
245    local expected = join_path(d, item)
246    if funcname == 'finddir' then
247      mkdir(expected)
248    else
249      write_file(expected, '')
250    end
251    eq(expected, fn[funcname](item, d:gsub(' ', [[\ ]])))
252  end
253
254  it('finddir()', function()
255    test_find_func('finddir', 'directory', 'folder')
256    test_find_func('finddir', 'directory', 'folder name')
257    test_find_func('finddir', 'fold#er name', 'directory')
258  end)
259
260  it('findfile()', function()
261    test_find_func('findfile', 'directory', 'file.txt')
262    test_find_func('findfile', 'directory', 'file name.txt')
263    test_find_func('findfile', 'fold#er name', 'file.txt')
264  end)
265end)