path_spec.lua (5623B)
1 local t = require('test.testutil') 2 local n = require('test.functional.testnvim')() 3 4 local clear = n.clear 5 local command = n.command 6 local eq = t.eq 7 local eval = n.eval 8 local feed = n.feed 9 local fn = n.fn 10 local insert = n.insert 11 local is_os = t.is_os 12 local mkdir = t.mkdir 13 local rmdir = n.rmdir 14 local write_file = t.write_file 15 16 local function join_path(...) 17 local pathsep = (is_os('win') and '\\' or '/') 18 return table.concat({ ... }, pathsep) 19 end 20 21 describe('path collapse', function() 22 local targetdir 23 local expected_path 24 25 before_each(function() 26 targetdir = join_path('test', 'functional', 'fixtures') 27 clear() 28 command('edit ' .. join_path(targetdir, 'tty-test.c')) 29 expected_path = eval('expand("%:p")') 30 end) 31 32 it('with /./ segment #7117', function() 33 command('edit ' .. join_path(targetdir, '.', 'tty-test.c')) 34 eq(expected_path, eval('expand("%:p")')) 35 end) 36 37 it('with ./ prefix #7117', function() 38 command('edit ' .. join_path('.', targetdir, 'tty-test.c')) 39 eq(expected_path, eval('expand("%:p")')) 40 end) 41 42 it('with ./ prefix, after directory change #7117', function() 43 command('edit ' .. join_path('.', targetdir, 'tty-test.c')) 44 command('cd test') 45 eq(expected_path, eval('expand("%:p")')) 46 end) 47 48 it('with /../ segment #7117', function() 49 command('edit ' .. join_path(targetdir, '..', 'fixtures', 'tty-test.c')) 50 eq(expected_path, eval('expand("%:p")')) 51 end) 52 53 it('with ../ and different starting directory #7117', function() 54 command('cd test') 55 command('edit ' .. join_path('..', targetdir, 'tty-test.c')) 56 eq(expected_path, eval('expand("%:p")')) 57 end) 58 59 it('with ./../ and different starting directory #7117', function() 60 command('cd test') 61 command('edit ' .. join_path('.', '..', targetdir, 'tty-test.c')) 62 eq(expected_path, eval('expand("%:p")')) 63 end) 64 end) 65 66 describe('expand wildcard', function() 67 before_each(clear) 68 69 it('with special characters #24421', function() 70 local folders = is_os('win') and { 71 '{folder}', 72 'folder$name', 73 } or { 74 'folder-name', 75 'folder#name', 76 } 77 for _, folder in ipairs(folders) do 78 mkdir(folder) 79 local file = join_path(folder, 'file.txt') 80 write_file(file, '') 81 eq(file, eval('expand("' .. folder .. '/*")')) 82 rmdir(folder) 83 end 84 end) 85 end) 86 87 describe('file search', function() 88 local testdir = 'Xtest_path_spec' 89 90 before_each(clear) 91 92 setup(function() 93 mkdir(testdir) 94 end) 95 96 teardown(function() 97 rmdir(testdir) 98 end) 99 100 it('gf finds multibyte filename in line #20517', function() 101 command('cd test/functional/fixtures') 102 insert('filename_with_unicode_ααα') 103 eq('', eval('expand("%")')) 104 feed('gf') 105 eq('filename_with_unicode_ααα', eval('expand("%:t")')) 106 end) 107 108 it('gf/<cfile> matches Windows drive-letter filepaths (without ":" in &isfname)', function() 109 local iswin = is_os('win') 110 local function test_cfile(input, expected, expected_win) 111 expected = (iswin and expected_win or expected) or input 112 command('%delete') 113 insert(input) 114 command('norm! 0') 115 eq(expected, eval('expand("<cfile>")')) 116 end 117 118 test_cfile([[c:/d:/foo/bar.txt]]) -- TODO(justinmk): should return "d:/foo/bar.txt" ? 119 test_cfile([[//share/c:/foo/bar/]]) 120 test_cfile([[file://c:/foo/bar]]) 121 test_cfile([[file://c:/foo/bar:42]]) 122 test_cfile([[file://c:/foo/bar:42:666]]) 123 test_cfile([[https://c:/foo/bar]]) 124 test_cfile([[\foo\bar]], [[foo]], [[\foo\bar]]) 125 test_cfile([[/foo/bar]], [[/foo/bar]]) 126 test_cfile([[c:\foo\bar]], [[c:]], [[c:\foo\bar]]) 127 test_cfile([[c:\foo\bar:42:666]], [[c:]], [[c:\foo\bar]]) 128 test_cfile([[c:/foo/bar]]) 129 test_cfile([[c:/foo/bar:42]], [[c:/foo/bar]]) 130 test_cfile([[c:/foo/bar:42:666]], [[c:/foo/bar]]) 131 test_cfile([[c:foo\bar]], [[c]]) 132 test_cfile([[c:foo/bar]], [[c]]) 133 test_cfile([[c:foo]], [[c]]) 134 -- Examples from: https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats#example-ways-to-refer-to-the-same-file 135 test_cfile([[c:\temp\test-file.txt]], [[c:]], [[c:\temp\test-file.txt]]) 136 test_cfile( 137 [[\\127.0.0.1\c$\temp\test-file.txt]], 138 [[127.0.0.1]], 139 [[\\127.0.0.1\c$\temp\test-file.txt]] 140 ) 141 test_cfile( 142 [[\\LOCALHOST\c$\temp\test-file.txt]], 143 [[LOCALHOST]], 144 [[\\LOCALHOST\c$\temp\test-file.txt]] 145 ) 146 -- not supported yet 147 test_cfile([[\\.\c:\temp\test-file.txt]], [[.]], [[\\.\c]]) 148 -- not supported yet 149 test_cfile([[\\?\c:\temp\test-file.txt]], [[c:]], [[\\]]) 150 test_cfile( 151 [[\\.\UNC\LOCALHOST\c$\temp\test-file.txt]], 152 [[.]], 153 [[\\.\UNC\LOCALHOST\c$\temp\test-file.txt]] 154 ) 155 test_cfile( 156 [[\\127.0.0.1\c$\temp\test-file.txt]], 157 [[127.0.0.1]], 158 [[\\127.0.0.1\c$\temp\test-file.txt]] 159 ) 160 end) 161 162 ---@param funcname 'finddir' | 'findfile' 163 local function test_find_func(funcname, folder, item) 164 local d = join_path(testdir, folder) 165 mkdir(d) 166 local expected = join_path(d, item) 167 if funcname == 'finddir' then 168 mkdir(expected) 169 else 170 write_file(expected, '') 171 end 172 eq(expected, fn[funcname](item, d:gsub(' ', [[\ ]]))) 173 end 174 175 it('finddir()', function() 176 test_find_func('finddir', 'directory', 'folder') 177 test_find_func('finddir', 'directory', 'folder name') 178 test_find_func('finddir', 'fold#er name', 'directory') 179 end) 180 181 it('findfile()', function() 182 test_find_func('findfile', 'directory', 'file.txt') 183 test_find_func('findfile', 'directory', 'file name.txt') 184 test_find_func('findfile', 'fold#er name', 'file.txt') 185 end) 186 end)