glob_spec.lua 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local eq = t.eq
  4. describe('glob', function()
  5. before_each(n.clear)
  6. after_each(n.clear)
  7. local match = function(pattern, str)
  8. return n.exec_lua(function()
  9. return require('vim.glob').to_lpeg(pattern):match(str) ~= nil
  10. end)
  11. end
  12. describe('glob matching', function()
  13. it('should match literal strings', function()
  14. eq(true, match('', ''))
  15. eq(false, match('', 'a'))
  16. eq(true, match('a', 'a'))
  17. eq(true, match('/', '/'))
  18. eq(true, match('abc', 'abc'))
  19. eq(false, match('abc', 'abcdef'))
  20. eq(false, match('abc', 'a'))
  21. eq(false, match('abc', 'bc'))
  22. eq(false, match('a', 'b'))
  23. eq(false, match('.', 'a'))
  24. eq(true, match('$', '$'))
  25. eq(true, match('/dir', '/dir'))
  26. eq(true, match('dir/', 'dir/'))
  27. eq(true, match('dir/subdir', 'dir/subdir'))
  28. eq(false, match('dir/subdir', 'subdir'))
  29. eq(false, match('dir/subdir', 'dir/subdir/file'))
  30. eq(true, match('🤠', '🤠'))
  31. end)
  32. it('should match * wildcards', function()
  33. eq(false, match('*', ''))
  34. eq(true, match('*', 'a'))
  35. eq(false, match('*', '/'))
  36. eq(false, match('*', '/a'))
  37. eq(false, match('*', 'a/'))
  38. eq(true, match('*', 'aaa'))
  39. eq(true, match('*a', 'aa'))
  40. eq(true, match('*a', 'abca'))
  41. eq(true, match('*.txt', 'file.txt'))
  42. eq(false, match('*.txt', 'file.txtxt'))
  43. eq(false, match('*.txt', 'dir/file.txt'))
  44. eq(false, match('*.txt', '/dir/file.txt'))
  45. eq(false, match('*.txt', 'C:/dir/file.txt'))
  46. eq(false, match('*.dir', 'test.dir/file'))
  47. eq(true, match('file.*', 'file.txt'))
  48. eq(false, match('file.*', 'not-file.txt'))
  49. eq(true, match('*/file.txt', 'dir/file.txt'))
  50. eq(false, match('*/file.txt', 'dir/subdir/file.txt'))
  51. eq(false, match('*/file.txt', '/dir/file.txt'))
  52. eq(true, match('dir/*', 'dir/file.txt'))
  53. eq(false, match('dir/*', 'dir'))
  54. eq(false, match('dir/*.txt', 'file.txt'))
  55. eq(true, match('dir/*.txt', 'dir/file.txt'))
  56. eq(false, match('dir/*.txt', 'dir/subdir/file.txt'))
  57. eq(false, match('dir/*/file.txt', 'dir/file.txt'))
  58. eq(true, match('dir/*/file.txt', 'dir/subdir/file.txt'))
  59. eq(false, match('dir/*/file.txt', 'dir/subdir/subdir/file.txt'))
  60. -- The spec does not describe this, but VSCode only interprets ** when it's by
  61. -- itself in a path segment, and otherwise interprets ** as consecutive * directives.
  62. -- see: https://github.com/microsoft/vscode/blob/eef30e7165e19b33daa1e15e92fa34ff4a5df0d3/src/vs/base/common/glob.ts#L112
  63. eq(true, match('a**', 'abc')) -- '**' should parse as two '*'s when not by itself in a path segment
  64. eq(true, match('**c', 'abc'))
  65. eq(false, match('a**', 'ab')) -- each '*' should still represent at least one character
  66. eq(false, match('**c', 'bc'))
  67. eq(true, match('a**', 'abcd'))
  68. eq(true, match('**d', 'abcd'))
  69. eq(false, match('a**', 'abc/d'))
  70. eq(false, match('**d', 'abc/d'))
  71. end)
  72. it('should match ? wildcards', function()
  73. eq(false, match('?', ''))
  74. eq(true, match('?', 'a'))
  75. eq(false, match('??', 'a'))
  76. eq(false, match('?', 'ab'))
  77. eq(true, match('??', 'ab'))
  78. eq(true, match('a?c', 'abc'))
  79. eq(false, match('a?c', 'a/c'))
  80. end)
  81. it('should match ** wildcards', function()
  82. eq(true, match('**', ''))
  83. eq(true, match('**', 'a'))
  84. eq(true, match('**', '/'))
  85. eq(true, match('**', 'a/'))
  86. eq(true, match('**', '/a'))
  87. eq(true, match('**', 'C:/a'))
  88. eq(true, match('**', 'a/a'))
  89. eq(true, match('**', 'a/a/a'))
  90. eq(false, match('/**', '')) -- /** matches leading / literally
  91. eq(true, match('/**', '/'))
  92. eq(true, match('/**', '/a/b/c'))
  93. eq(true, match('**/', '')) -- **/ absorbs trailing /
  94. eq(true, match('**/', '/a/b/c'))
  95. eq(true, match('**/**', ''))
  96. eq(true, match('**/**', 'a'))
  97. eq(false, match('a/**', ''))
  98. eq(false, match('a/**', 'a'))
  99. eq(true, match('a/**', 'a/b'))
  100. eq(true, match('a/**', 'a/b/c'))
  101. eq(false, match('a/**', 'b/a'))
  102. eq(false, match('a/**', '/a'))
  103. eq(false, match('**/a', ''))
  104. eq(true, match('**/a', 'a'))
  105. eq(false, match('**/a', 'a/b'))
  106. eq(true, match('**/a', '/a'))
  107. eq(true, match('**/a', '/b/a'))
  108. eq(true, match('**/a', '/c/b/a'))
  109. eq(true, match('**/a', '/a/a'))
  110. eq(true, match('**/a', '/abc/a'))
  111. eq(false, match('a/**/c', 'a'))
  112. eq(false, match('a/**/c', 'c'))
  113. eq(true, match('a/**/c', 'a/c'))
  114. eq(true, match('a/**/c', 'a/b/c'))
  115. eq(true, match('a/**/c', 'a/b/b/c'))
  116. eq(false, match('**/a/**', 'a'))
  117. eq(true, match('**/a/**', 'a/'))
  118. eq(false, match('**/a/**', '/dir/a'))
  119. eq(false, match('**/a/**', 'dir/a'))
  120. eq(true, match('**/a/**', 'dir/a/'))
  121. eq(true, match('**/a/**', 'a/dir'))
  122. eq(true, match('**/a/**', 'dir/a/dir'))
  123. eq(true, match('**/a/**', '/a/dir'))
  124. eq(true, match('**/a/**', 'C:/a/dir'))
  125. eq(false, match('**/a/**', 'a.txt'))
  126. end)
  127. it('should match {} groups', function()
  128. eq(true, match('{}', ''))
  129. eq(false, match('{}', 'a'))
  130. eq(true, match('a{}', 'a'))
  131. eq(true, match('{}a', 'a'))
  132. eq(true, match('{,}', ''))
  133. eq(true, match('{a,}', ''))
  134. eq(true, match('{a,}', 'a'))
  135. eq(true, match('{a}', 'a'))
  136. eq(false, match('{a}', 'aa'))
  137. eq(false, match('{a}', 'ab'))
  138. eq(true, match('{a?c}', 'abc'))
  139. eq(false, match('{ab}', 'a'))
  140. eq(false, match('{ab}', 'b'))
  141. eq(true, match('{ab}', 'ab'))
  142. eq(true, match('{a,b}', 'a'))
  143. eq(true, match('{a,b}', 'b'))
  144. eq(false, match('{a,b}', 'ab'))
  145. eq(true, match('{ab,cd}', 'ab'))
  146. eq(false, match('{ab,cd}', 'a'))
  147. eq(true, match('{ab,cd}', 'cd'))
  148. eq(true, match('{a,b,c}', 'c'))
  149. eq(false, match('{a,{b,c}}', 'c')) -- {} cannot nest
  150. end)
  151. it('should match [] groups', function()
  152. eq(true, match('[]', '[]')) -- empty [] is a literal
  153. eq(false, match('[a-z]', ''))
  154. eq(true, match('[a-z]', 'a'))
  155. eq(false, match('[a-z]', 'ab'))
  156. eq(true, match('[a-z]', 'z'))
  157. eq(true, match('[a-z]', 'j'))
  158. eq(false, match('[a-f]', 'j'))
  159. eq(false, match('[a-z]', '`')) -- 'a' - 1
  160. eq(false, match('[a-z]', '{')) -- 'z' + 1
  161. eq(false, match('[a-z]', 'A'))
  162. eq(false, match('[a-z]', '5'))
  163. eq(true, match('[A-Z]', 'A'))
  164. eq(true, match('[A-Z]', 'Z'))
  165. eq(true, match('[A-Z]', 'J'))
  166. eq(false, match('[A-Z]', '@')) -- 'A' - 1
  167. eq(false, match('[A-Z]', '[')) -- 'Z' + 1
  168. eq(false, match('[A-Z]', 'a'))
  169. eq(false, match('[A-Z]', '5'))
  170. eq(true, match('[a-zA-Z0-9]', 'z'))
  171. eq(true, match('[a-zA-Z0-9]', 'Z'))
  172. eq(true, match('[a-zA-Z0-9]', '9'))
  173. eq(false, match('[a-zA-Z0-9]', '&'))
  174. end)
  175. it('should match [!...] groups', function()
  176. eq(true, match('[!]', '[!]')) -- [!] is a literal
  177. eq(false, match('[!a-z]', ''))
  178. eq(false, match('[!a-z]', 'a'))
  179. eq(false, match('[!a-z]', 'z'))
  180. eq(false, match('[!a-z]', 'j'))
  181. eq(true, match('[!a-f]', 'j'))
  182. eq(false, match('[!a-f]', 'jj'))
  183. eq(true, match('[!a-z]', '`')) -- 'a' - 1
  184. eq(true, match('[!a-z]', '{')) -- 'z' + 1
  185. eq(false, match('[!a-zA-Z0-9]', 'a'))
  186. eq(false, match('[!a-zA-Z0-9]', 'A'))
  187. eq(false, match('[!a-zA-Z0-9]', '0'))
  188. eq(true, match('[!a-zA-Z0-9]', '!'))
  189. end)
  190. it('should handle long patterns', function()
  191. -- lpeg has a recursion limit of 200 by default, make sure the grammar does trigger it on
  192. -- strings longer than that
  193. local fill_200 =
  194. 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  195. eq(200, fill_200:len())
  196. local long_lit = fill_200 .. 'a'
  197. eq(false, match(long_lit, 'b'))
  198. eq(true, match(long_lit, long_lit))
  199. local long_pat = fill_200 .. 'a/**/*.c'
  200. eq(true, match(long_pat, fill_200 .. 'a/b/c/d.c'))
  201. end)
  202. it('should match complex patterns', function()
  203. eq(false, match('**/*.{c,h}', ''))
  204. eq(false, match('**/*.{c,h}', 'c'))
  205. eq(false, match('**/*.{c,h}', 'file.m'))
  206. eq(true, match('**/*.{c,h}', 'file.c'))
  207. eq(true, match('**/*.{c,h}', 'file.h'))
  208. eq(true, match('**/*.{c,h}', '/file.c'))
  209. eq(true, match('**/*.{c,h}', 'dir/subdir/file.c'))
  210. eq(true, match('**/*.{c,h}', 'dir/subdir/file.h'))
  211. eq(true, match('**/*.{c,h}', '/dir/subdir/file.c'))
  212. eq(true, match('**/*.{c,h}', 'C:/dir/subdir/file.c'))
  213. eq(true, match('/dir/**/*.{c,h}', '/dir/file.c'))
  214. eq(false, match('/dir/**/*.{c,h}', 'dir/file.c'))
  215. eq(true, match('/dir/**/*.{c,h}', '/dir/subdir/subdir/file.c'))
  216. eq(true, match('{[0-9],[a-z]}', '0'))
  217. eq(true, match('{[0-9],[a-z]}', 'a'))
  218. eq(false, match('{[0-9],[a-z]}', 'A'))
  219. -- glob is from willRename filter in typescript-language-server
  220. -- https://github.com/typescript-language-server/typescript-language-server/blob/b224b878652438bcdd639137a6b1d1a6630129e4/src/lsp-server.ts#L266
  221. eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.js'))
  222. eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.ts'))
  223. eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.mts'))
  224. eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.mjs'))
  225. eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.cjs'))
  226. eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.cts'))
  227. eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.jsx'))
  228. eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.tsx'))
  229. end)
  230. end)
  231. end)