glob_spec.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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('/', '/'))
  19. eq(true, match('abc', 'abc'))
  20. eq(false, match('abc', 'abcdef'))
  21. eq(false, match('abc', 'a'))
  22. eq(false, match('abc', 'bc'))
  23. eq(false, match('a', 'b'))
  24. eq(false, match('.', 'a'))
  25. eq(true, match('$', '$'))
  26. eq(true, match('a,b', 'a,b'))
  27. eq(true, match('/dir', '/dir'))
  28. eq(true, match('dir/', 'dir/'))
  29. eq(true, match('dir/subdir', 'dir/subdir'))
  30. eq(false, match('dir/subdir', 'subdir'))
  31. eq(false, match('dir/subdir', 'dir/subdir/file'))
  32. eq(true, match('🤠', '🤠'))
  33. end)
  34. it('should match * wildcards', function()
  35. eq(true, match('*', ''))
  36. eq(true, match('*', ' '))
  37. eq(true, match('*', 'a'))
  38. eq(false, match('*', '/'))
  39. eq(false, match('*', '/a'))
  40. eq(false, match('*', 'a/'))
  41. eq(true, match('*', 'aaa'))
  42. eq(true, match('*a', 'aa'))
  43. eq(true, match('*a', 'abca'))
  44. eq(true, match('*.ts', '.ts'))
  45. eq(true, match('*.txt', 'file.txt'))
  46. eq(false, match('*.txt', 'file.txtxt'))
  47. eq(false, match('*.txt', 'dir/file.txt'))
  48. eq(false, match('*.txt', '/dir/file.txt'))
  49. eq(false, match('*.txt', 'C:/dir/file.txt'))
  50. eq(false, match('*.dir', 'test.dir/file'))
  51. eq(true, match('file.*', 'file.txt'))
  52. eq(false, match('file.*', 'not-file.txt'))
  53. eq(true, match('*/file.txt', 'dir/file.txt'))
  54. eq(false, match('*/file.txt', 'dir/subdir/file.txt'))
  55. eq(false, match('*/file.txt', '/dir/file.txt'))
  56. eq(true, match('dir/*', 'dir/file.txt'))
  57. eq(false, match('dir/*', 'dir'))
  58. eq(false, match('dir/*.txt', 'file.txt'))
  59. eq(true, match('dir/*.txt', 'dir/file.txt'))
  60. eq(false, match('dir/*.txt', 'dir/subdir/file.txt'))
  61. eq(false, match('dir/*/file.txt', 'dir/file.txt'))
  62. eq(true, match('dir/*/file.txt', 'dir/subdir/file.txt'))
  63. eq(false, match('dir/*/file.txt', 'dir/subdir/subdir/file.txt'))
  64. eq(true, match('a*b*c*d*e*', 'axbxcxdxe'))
  65. eq(true, match('a*b*c*d*e*', 'axbxcxdxexxx'))
  66. eq(true, match('a*b?c*x', 'abxbbxdbxebxczzx'))
  67. eq(false, match('a*b?c*x', 'abxbbxdbxebxczzy'))
  68. eq(true, match('a*b*[cy]*d*e*', 'axbxcxdxexxx'))
  69. eq(true, match('a*b*[cy]*d*e*', 'axbxyxdxexxx'))
  70. eq(true, match('a*b*[cy]*d*e*', 'axbxxxyxdxexxx'))
  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. eq(false, match('a/', 'a/.b'))
  81. eq(true, match('?/?', 'a/b'))
  82. eq(true, match('/??', '/ab'))
  83. eq(true, match('/?b', '/ab'))
  84. eq(false, match('foo?bar', 'foo/bar'))
  85. end)
  86. it('should match ** wildcards', function()
  87. eq(true, match('**', ''))
  88. eq(true, match('**', 'a'))
  89. eq(true, match('**', '/'))
  90. eq(true, match('**', 'a/'))
  91. eq(true, match('**', '/a'))
  92. eq(true, match('**', 'C:/a'))
  93. eq(true, match('**', 'a/a'))
  94. eq(true, match('**', 'a/a/a'))
  95. eq(false, match('/**', '')) -- /** matches leading / literally
  96. eq(true, match('/**', '/'))
  97. eq(true, match('/**', '/a/b/c'))
  98. eq(true, match('**/', '')) -- **/ absorbs trailing /
  99. eq(false, match('**/', '/a/b/c'))
  100. eq(true, match('**/**', ''))
  101. eq(true, match('**/**', 'a'))
  102. eq(false, match('a/**', ''))
  103. eq(false, match('a/**', 'a'))
  104. eq(true, match('a/**', 'a/b'))
  105. eq(true, match('a/**', 'a/b/c'))
  106. eq(false, match('a/**', 'b/a'))
  107. eq(false, match('a/**', '/a'))
  108. eq(false, match('**/a', ''))
  109. eq(true, match('**/a', 'a'))
  110. eq(false, match('**/a', 'a/b'))
  111. eq(true, match('**/a', '/a'))
  112. eq(true, match('**/a', '/b/a'))
  113. eq(true, match('**/a', '/c/b/a'))
  114. eq(true, match('**/a', '/a/a'))
  115. eq(true, match('**/a', '/abc/a'))
  116. eq(false, match('a/**/c', 'a'))
  117. eq(false, match('a/**/c', 'c'))
  118. eq(true, match('a/**/c', 'a/c'))
  119. eq(true, match('a/**/c', 'a/b/c'))
  120. eq(true, match('a/**/c', 'a/b/b/c'))
  121. eq(false, match('**/a/**', 'a'))
  122. eq(true, match('**/a/**', 'a/'))
  123. eq(false, match('**/a/**', '/dir/a'))
  124. eq(false, match('**/a/**', 'dir/a'))
  125. eq(true, match('**/a/**', 'dir/a/'))
  126. eq(true, match('**/a/**', 'a/dir'))
  127. eq(true, match('**/a/**', 'dir/a/dir'))
  128. eq(true, match('**/a/**', '/a/dir'))
  129. eq(true, match('**/a/**', 'C:/a/dir'))
  130. eq(false, match('**/a/**', 'a.txt'))
  131. end)
  132. it('should match {} groups', function()
  133. eq(true, match('{,}', ''))
  134. eq(true, match('{a,}', ''))
  135. eq(true, match('{a,}', 'a'))
  136. eq(true, match('{a,b}', 'a'))
  137. eq(true, match('{a,b}', 'b'))
  138. eq(false, match('{a,b}', 'ab'))
  139. eq(true, match('{ab,cd}', 'ab'))
  140. eq(false, match('{ab,cd}', 'a'))
  141. eq(true, match('{ab,cd}', 'cd'))
  142. eq(true, match('{a,b,c}', 'c'))
  143. eq(true, match('{a,{b,c}}', 'c'))
  144. eq(true, match('a{,/}*.txt', 'a.txt'))
  145. eq(true, match('a{,/}*.txt', 'ab.txt'))
  146. eq(true, match('a{,/}*.txt', 'a/b.txt'))
  147. eq(true, match('a{,/}*.txt', 'a/ab.txt'))
  148. eq(true, match('a/{a{a,b},b}', 'a/aa'))
  149. eq(true, match('a/{a{a,b},b}', 'a/ab'))
  150. eq(false, match('a/{a{a,b},b}', 'a/ac'))
  151. eq(true, match('a/{a{a,b},b}', 'a/b'))
  152. eq(false, match('a/{a{a,b},b}', 'a/c'))
  153. eq(true, match('foo{bar,b*z}', 'foobar'))
  154. eq(true, match('foo{bar,b*z}', 'foobuzz'))
  155. eq(true, match('foo{bar,b*z}', 'foobarz'))
  156. eq(true, match('{a,b}/c/{d,e}/**/*est.ts', 'a/c/d/one/two/three.test.ts'))
  157. eq(true, match('{a,{d,e}b}/c', 'a/c'))
  158. eq(true, match('{**/a,**/b}', 'b'))
  159. end)
  160. it('should match [] groups', function()
  161. eq(true, match('[]', '[]')) -- empty [] is a literal
  162. eq(false, match('[a-z]', ''))
  163. eq(true, match('[a-z]', 'a'))
  164. eq(false, match('[a-z]', 'ab'))
  165. eq(true, match('[a-z]', 'z'))
  166. eq(true, match('[a-z]', 'j'))
  167. eq(false, match('[a-f]', 'j'))
  168. eq(false, match('[a-z]', '`')) -- 'a' - 1
  169. eq(false, match('[a-z]', '{')) -- 'z' + 1
  170. eq(false, match('[a-z]', 'A'))
  171. eq(false, match('[a-z]', '5'))
  172. eq(true, match('[A-Z]', 'A'))
  173. eq(true, match('[A-Z]', 'Z'))
  174. eq(true, match('[A-Z]', 'J'))
  175. eq(false, match('[A-Z]', '@')) -- 'A' - 1
  176. eq(false, match('[A-Z]', '[')) -- 'Z' + 1
  177. eq(false, match('[A-Z]', 'a'))
  178. eq(false, match('[A-Z]', '5'))
  179. eq(true, match('[a-zA-Z0-9]', 'z'))
  180. eq(true, match('[a-zA-Z0-9]', 'Z'))
  181. eq(true, match('[a-zA-Z0-9]', '9'))
  182. eq(false, match('[a-zA-Z0-9]', '&'))
  183. eq(true, match('[?]', '?'))
  184. eq(false, match('[?]', 'a'))
  185. eq(true, match('[*]', '*'))
  186. eq(false, match('[*]', 'a'))
  187. eq(true, match('[\\!]', '!'))
  188. eq(true, match('a\\*b', 'a*b'))
  189. eq(false, match('a\\*b', 'axb'))
  190. end)
  191. it('should match [!...] groups', function()
  192. eq(true, match('[!]', '[!]')) -- [!] is a literal
  193. eq(false, match('[!a-z]', ''))
  194. eq(false, match('[!a-z]', 'a'))
  195. eq(false, match('[!a-z]', 'z'))
  196. eq(false, match('[!a-z]', 'j'))
  197. eq(true, match('[!a-f]', 'j'))
  198. eq(false, match('[!a-f]', 'jj'))
  199. eq(true, match('[!a-z]', '`')) -- 'a' - 1
  200. eq(true, match('[!a-z]', '{')) -- 'z' + 1
  201. eq(false, match('[!a-zA-Z0-9]', 'a'))
  202. eq(false, match('[!a-zA-Z0-9]', 'A'))
  203. eq(false, match('[!a-zA-Z0-9]', '0'))
  204. eq(true, match('[!a-zA-Z0-9]', '!'))
  205. end)
  206. it('should handle long patterns', function()
  207. -- lpeg has a recursion limit of 200 by default, make sure the grammar does trigger it on
  208. -- strings longer than that
  209. local fill_200 = ('a'):rep(200)
  210. eq(200, fill_200:len())
  211. local long_lit = fill_200 .. 'a'
  212. eq(false, match(long_lit, 'b'))
  213. eq(true, match(long_lit, long_lit))
  214. local long_pat = fill_200 .. 'a/**/*.c'
  215. eq(true, match(long_pat, fill_200 .. 'a/b/c/d.c'))
  216. end)
  217. -- New test for unicode patterns from assets
  218. it('should match unicode patterns', function()
  219. eq(true, match('😎/¢£.{ts,tsx,js,jsx}', '😎/¢£.ts'))
  220. eq(true, match('😎/¢£.{ts,tsx,js,jsx}', '😎/¢£.tsx'))
  221. eq(true, match('😎/¢£.{ts,tsx,js,jsx}', '😎/¢£.js'))
  222. eq(true, match('😎/¢£.{ts,tsx,js,jsx}', '😎/¢£.jsx'))
  223. eq(false, match('😎/¢£.{ts,tsx,js,jsx}', '😎/¢£.jsxxxxxxxx'))
  224. eq(true, match('*é*', 'café noir'))
  225. eq(true, match('caf*noir', 'café noir'))
  226. eq(true, match('caf*noir', 'cafeenoir'))
  227. eq(true, match('F[ë£a]', 'Fë'))
  228. eq(true, match('F[ë£a]', 'F£'))
  229. eq(true, match('F[ë£a]', 'Fa'))
  230. end)
  231. it('should match complex patterns', function()
  232. eq(false, match('**/*.{c,h}', ''))
  233. eq(false, match('**/*.{c,h}', 'c'))
  234. eq(false, match('**/*.{c,h}', 'file.m'))
  235. eq(true, match('**/*.{c,h}', 'file.c'))
  236. eq(true, match('**/*.{c,h}', 'file.h'))
  237. eq(true, match('**/*.{c,h}', '/file.c'))
  238. eq(true, match('**/*.{c,h}', 'dir/subdir/file.c'))
  239. eq(true, match('**/*.{c,h}', 'dir/subdir/file.h'))
  240. eq(true, match('**/*.{c,h}', '/dir/subdir/file.c'))
  241. eq(true, match('**/*.{c,h}', 'C:/dir/subdir/file.c'))
  242. eq(true, match('/dir/**/*.{c,h}', '/dir/file.c'))
  243. eq(false, match('/dir/**/*.{c,h}', 'dir/file.c'))
  244. eq(true, match('/dir/**/*.{c,h}', '/dir/subdir/subdir/file.c'))
  245. eq(true, match('{[0-9],[a-z]}', '0'))
  246. eq(true, match('{[0-9],[a-z]}', 'a'))
  247. eq(false, match('{[0-9],[a-z]}', 'A'))
  248. -- glob is from willRename filter in typescript-language-server
  249. -- https://github.com/typescript-language-server/typescript-language-server/blob/b224b878652438bcdd639137a6b1d1a6630129e4/src/lsp-server.ts#L266
  250. eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.js'))
  251. eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.ts'))
  252. eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.mts'))
  253. eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.mjs'))
  254. eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.cjs'))
  255. eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.cts'))
  256. eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.jsx'))
  257. eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.tsx'))
  258. end)
  259. end)
  260. end)