path_spec.lua 5.5 KB

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