| 1 |
test = require('tape'); |
| 2 |
const { join } = require('path'); |
| 3 |
const $ = require('../src'); |
| 4 |
|
| 5 |
test('standard', t => { |
| 6 |
t.equal(typeof $, 'function', 'constructor is a typeof function'); |
| 7 |
t.end(); |
| 8 |
}); |
| 9 |
|
| 10 |
test('base', t => { |
| 11 |
[ |
| 12 |
// should strip glob magic to return parent path |
| 13 |
['.', '.'], |
| 14 |
['.*', '.'], |
| 15 |
['/.*', '/'], |
| 16 |
['/.*/', '/'], |
| 17 |
['a/.*/b', 'a'], |
| 18 |
['a*/.*/b', '.'], |
| 19 |
['*/a/b/c', '.'], |
| 20 |
['*', '.'], |
| 21 |
['*/', '.'], |
| 22 |
['*/*', '.'], |
| 23 |
['*/*/', '.'], |
| 24 |
['**', '.'], |
| 25 |
['**/', '.'], |
| 26 |
['**/*', '.'], |
| 27 |
['**/*/', '.'], |
| 28 |
['/*.js', '/'], |
| 29 |
['*.js', '.'], |
| 30 |
['**/*.js', '.'], |
| 31 |
['{a,b}', '.'], |
| 32 |
['/{a,b}', '/'], |
| 33 |
['/{a,b}/', '/'], |
| 34 |
['(a|b)', '.'], |
| 35 |
['/(a|b)', '/'], |
| 36 |
['./(a|b)', '.'], |
| 37 |
['a/(b c)', 'a', 'not an extglob'], |
| 38 |
['a/(b c)/d', 'a/(b c)', 'not an extglob'], |
| 39 |
['path/to/*.js', 'path/to'], |
| 40 |
['/root/path/to/*.js', '/root/path/to'], |
| 41 |
['chapter/foo [bar]/', 'chapter'], |
| 42 |
['path/[a-z]', 'path'], |
| 43 |
['path/{to,from}', 'path'], |
| 44 |
['path/(to|from)', 'path'], |
| 45 |
//['path/(foo bar)/subdir/foo.*', 'path/(foo bar)/subdir'], |
| 46 |
['path/!(to|from)', 'path'], |
| 47 |
['path/?(to|from)', 'path'], |
| 48 |
['path/+(to|from)', 'path'], |
| 49 |
['path/*(to|from)', 'path'], |
| 50 |
['path/@(to|from)', 'path'], |
| 51 |
['path/!/foo', 'path/!'], |
| 52 |
['path/?/foo', 'path/?'], |
| 53 |
['path/+/foo', 'path/+'], |
| 54 |
['path/*/foo', 'path'], |
| 55 |
['path/@/foo', 'path/@'], |
| 56 |
['path/!/foo/', 'path/!'], |
| 57 |
['path/?/foo/', 'path/?'], |
| 58 |
['path/+/foo/', 'path/+'], |
| 59 |
['path/*/foo/', 'path'], |
| 60 |
//['path/@/foo/', 'path/@/foo'], |
| 61 |
['path/**/*', 'path'], |
| 62 |
['path/**/subdir/foo.*', 'path'], |
| 63 |
['path/subdir/**/foo.js', 'path/subdir'], |
| 64 |
['path/!subdir/foo.js', 'path/!subdir'], |
| 65 |
// should respect escaped characters |
| 66 |
['path/\\*\\*/subdir/foo.*', 'path/**/subdir'], |
| 67 |
['path/\\[\\*\\]/subdir/foo.*', 'path/[*]/subdir'], |
| 68 |
['path/\\*(a|b)/subdir/foo.*', 'path'], |
| 69 |
['path/\\*/(a|b)/subdir/foo.*', 'path/*'], |
| 70 |
['path/\\*\\(a\\|b\\)/subdir/foo.*', 'path/*(a|b)/subdir'], |
| 71 |
['path/\\[foo bar\\]/subdir/foo.*', 'path/[foo bar]/subdir'], |
| 72 |
['path/\\[bar]/', 'path'], |
| 73 |
['path/foo \\[bar]/', 'path'], |
| 74 |
// should return parent dirname from non-glob paths |
| 75 |
['path', '.'],, |
| 76 |
['path/foo', 'path'], |
| 77 |
['path/foo/', 'path'], |
| 78 |
['path/foo/bar.js', 'path/foo'], |
| 79 |
['path', '.'], |
| 80 |
['path/foo', 'path'], |
| 81 |
['path/foo/', 'path'], |
| 82 |
['path/foo/bar.js', 'path/foo'], |
| 83 |
// glob2base test patterns |
| 84 |
['js/*.js', 'js'], |
| 85 |
['js/**/test/*.js', 'js'], |
| 86 |
['js/test/wow.js', 'js/test'], |
| 87 |
['js/test/wow.js', 'js/test'], |
| 88 |
['js/t[a-z]st}/*.js', 'js'], |
| 89 |
['js/{src,test}/*.js', 'js'], |
| 90 |
['js/test{0..9}/*.js', 'js'], |
| 91 |
['js/t+(wo|est)/*.js', 'js'], |
| 92 |
['js/t(wo|est)/*.js', 'js'], |
| 93 |
['js/t/(wo|est)/*.js', 'js/t'], |
| 94 |
// should get a base name from a complex brace glob |
| 95 |
['lib/{components,pages}/**/{test,another}/*.txt', 'lib'], |
| 96 |
['js/test/**/{images,components}/*.js', 'js/test'], |
| 97 |
['ooga/{booga,sooga}/**/dooga/{eooga,fooga}', 'ooga'] |
| 98 |
].forEach(([x, y]) => { |
| 99 |
t.is($(x).base, y); |
| 100 |
}); |
| 101 |
|
| 102 |
t.end(); |
| 103 |
}); |
| 104 |
|
| 105 |
test('glob', t => { |
| 106 |
[ |
| 107 |
['', ''], |
| 108 |
['*', '*'], |
| 109 |
['.*', '.*'], |
| 110 |
['/.*', '.*'], |
| 111 |
['path/**/*', '**/*'], |
| 112 |
['/path/**/*', '**/*'], |
| 113 |
['root/(foo|bar)/path/**/*', '(foo|bar)/path/**/*'], |
| 114 |
['@(test)/root/(foo|bar)/path/**/*', '@(test)/root/(foo|bar)/path/**/*'], |
| 115 |
].forEach(([x, y]) => { |
| 116 |
t.is($(x).glob, y); |
| 117 |
}); |
| 118 |
|
| 119 |
t.end(); |
| 120 |
}) |
| 121 |
|
| 122 |
test('isGlob', t => { |
| 123 |
// should be true if it is a glob pattern |
| 124 |
['*.js', '!*.js', '!foo', '!foo.js', '**/abc.js', 'abc/*.js', '@.(?:abc)', '@.(?!abc)'].forEach(x => { |
| 125 |
t.true($(x).isGlob); |
| 126 |
}); |
| 127 |
|
| 128 |
// should not match escaped globs |
| 129 |
['\\!\\*.js', '\\!foo', '\\!foo.js', '\\*(foo).js', '\\*.js', '\\*\\*/abc.js', 'abc/\\*.js'].forEach(x => { |
| 130 |
t.false($(x).isGlob); |
| 131 |
}); |
| 132 |
|
| 133 |
// should be false if it is not a glob pattern |
| 134 |
['', '~/abc', '~/abc', '~/(abc)', '+~(abc)', '.', '@.(abc)', 'aa', 'who?', 'why!?', 'where???', |
| 135 |
'abc!/def/!ghi.js', 'abc.js', 'abc/def/!ghi.js', 'abc/def/ghi.js'].forEach(x => { |
| 136 |
t.false($(x).isGlob); |
| 137 |
}); |
| 138 |
|
| 139 |
// should be true if the path has a regex capture group |
| 140 |
['abc/(?!foo).js', 'abc/(?:foo).js', 'abc/(?=foo).js', 'abc/(a|b).js', 'abc/(a|b|c).js', 'abc/(foo bar)/*.js'].forEach(x => { |
| 141 |
t.true($(x).isGlob); |
| 142 |
}); |
| 143 |
|
| 144 |
// should be true if the path has parens but is not a valid capture group |
| 145 |
['abc/(?foo).js', 'abc/(a b c).js', 'abc/(ab).js', 'abc/(abc).js', 'abc/(foo bar).js'].forEach(x => { |
| 146 |
//t.true($(x).isGlob); |
| 147 |
}); |
| 148 |
|
| 149 |
// should be false if the capture group is imbalanced |
| 150 |
['abc/(?ab.js', 'abc/(ab.js', 'abc/(a|b.js', 'abc/(a|b|c.js'].forEach(x => { |
| 151 |
t.false($(x).isGlob); |
| 152 |
}); |
| 153 |
|
| 154 |
// should be false if the group is escaped |
| 155 |
['abc/\\(a|b).js', 'abc/\\(a|b|c).js'].forEach(x => { |
| 156 |
t.false($(x).isGlob); |
| 157 |
}); |
| 158 |
|
| 159 |
// should be true if glob chars exist and `options.strict` is false |
| 160 |
[ |
| 161 |
'$(abc)', |
| 162 |
'&(abc)', |
| 163 |
'? (abc)', |
| 164 |
'?.js', |
| 165 |
'abc/(?ab.js', |
| 166 |
'abc/(ab.js', |
| 167 |
'abc/(a|b.js', |
| 168 |
'abc/(a|b|c.js', |
| 169 |
'abc/(foo).js', |
| 170 |
'abc/?.js', |
| 171 |
'abc/[1-3.js', |
| 172 |
'abc/[^abc.js', |
| 173 |
'abc/[abc.js', |
| 174 |
'abc/foo?.js', |
| 175 |
'abc/{abc.js', |
| 176 |
'Who?.js' |
| 177 |
].forEach(x => { |
| 178 |
t.true($(x, {strict: false}).isGlob); |
| 179 |
}); |
| 180 |
|
| 181 |
// should be false if the first delim is escaped and options.strict is false |
| 182 |
[ |
| 183 |
['abc/\\(a|b).js'], |
| 184 |
['abc/(a|b\\).js', true], |
| 185 |
['abc/\\(a|b|c).js'], |
| 186 |
['abc/\\(a|b|c.js'], |
| 187 |
['abc/\\[abc].js'], |
| 188 |
['abc/\\[abc.js'] |
| 189 |
].forEach(([x, y]) => { |
| 190 |
t.false($(x, {strict: y || false}).isGlob); |
| 191 |
}); |
| 192 |
|
| 193 |
t.true($('abc/(a|b\\).js', {strict: false})); |
| 194 |
|
| 195 |
// should be true if the path has a regex character class |
| 196 |
['abc/[abc].js', 'abc/[^abc].js', 'abc/[1-3].js'].forEach(x => { |
| 197 |
t.true($(x).isGlob); |
| 198 |
}); |
| 199 |
|
| 200 |
// should be false if the character class is not balanced |
| 201 |
['abc/[abc.js', 'abc/[^abc.js', 'abc/[1-3.js'].forEach(x => { |
| 202 |
t.false($(x).isGlob); |
| 203 |
}); |
| 204 |
|
| 205 |
// should be false if the character class is escaped |
| 206 |
['abc/\\[abc].js', 'abc/\\[^abc].js', 'abc/\\[1-3].js'].forEach(x => { |
| 207 |
t.false($(x).isGlob); |
| 208 |
}); |
| 209 |
|
| 210 |
// should be true if the path has brace characters |
| 211 |
['abc/{a,b}.js', 'abc/{a..z}.js', 'abc/{a..z..2}.js'].forEach(x => { |
| 212 |
t.true($(x).isGlob); |
| 213 |
}); |
| 214 |
|
| 215 |
// should be false if (basic) braces are not balanced |
| 216 |
['abc/\\{a,b}.js', 'abc/\\{a..z}.js', 'abc/\\{a..z..2}.js'].forEach(x => { |
| 217 |
t.false($(x).isGlob); |
| 218 |
}); |
| 219 |
|
| 220 |
// should not be true if the path has regex characters |
| 221 |
['$(abc)', '&(abc)', 'Who?.js', '? (abc)', '?.js', 'abc/?.js'].forEach(x => { |
| 222 |
t.false($(x).isGlob); |
| 223 |
}); |
| 224 |
|
| 225 |
[ |
| 226 |
'!&(abc)', |
| 227 |
'!*.js', |
| 228 |
'!foo', |
| 229 |
'!foo.js', |
| 230 |
'**/abc.js', |
| 231 |
'*.js', |
| 232 |
'*z(abc)', |
| 233 |
'[1-10].js', |
| 234 |
'[^abc].js', |
| 235 |
'[a-j]*[^c]b/c', |
| 236 |
'[abc].js', |
| 237 |
'a/b/c/[a-z].js', |
| 238 |
'abc/(aaa|bbb).js', |
| 239 |
'abc/*.js', |
| 240 |
'abc/{a,b}.js', |
| 241 |
'abc/{a..z..2}.js', |
| 242 |
'abc/{a..z}.js' |
| 243 |
].forEach(x => { |
| 244 |
t.true($(x).isGlob); |
| 245 |
}); |
| 246 |
|
| 247 |
// should be false if regex characters are escaped |
| 248 |
[ |
| 249 |
'\\?.js', |
| 250 |
'\\[1-10\\].js', |
| 251 |
'\\[^abc\\].js', |
| 252 |
'\\[a-j\\]\\*\\[^c\\]b/c', |
| 253 |
'\\[abc\\].js', |
| 254 |
'\\a/b/c/\\[a-z\\].js', |
| 255 |
'abc/\\(aaa|bbb).js', |
| 256 |
'abc/\\?.js' |
| 257 |
].forEach(x => { |
| 258 |
t.false($(x).isGlob); |
| 259 |
}); |
| 260 |
|
| 261 |
// should be true if it has an extglob |
| 262 |
[ |
| 263 |
'abc/!(a).js', |
| 264 |
'abc/!(a|b).js', |
| 265 |
'abc/(ab)*.js', |
| 266 |
'abc/(a|b).js', |
| 267 |
'abc/*(a).js', |
| 268 |
'abc/*(a|b).js', |
| 269 |
'abc/+(a).js', |
| 270 |
'abc/+(a|b).js', |
| 271 |
'abc/?(a).js', |
| 272 |
'abc/?(a|b).js', |
| 273 |
'abc/@(a).js', |
| 274 |
'abc/@(a|b).js' |
| 275 |
].forEach(x => { |
| 276 |
t.true($(x).isGlob); |
| 277 |
}); |
| 278 |
|
| 279 |
// should be false if extglob characters are escaped |
| 280 |
[ |
| 281 |
'abc/\\*.js', |
| 282 |
'abc/\\*\\*.js', |
| 283 |
'abc/\\@(a).js', |
| 284 |
'abc/\\!(a).js', |
| 285 |
'abc/\\+(a).js', |
| 286 |
'abc/\\*(a).js', |
| 287 |
'abc/\\?(a).js' |
| 288 |
].forEach(x => { |
| 289 |
t.false($(x).isGlob); |
| 290 |
}); |
| 291 |
|
| 292 |
[ |
| 293 |
'abc/\\@(a|b).js', |
| 294 |
'abc/\\!(a|b).js', |
| 295 |
'abc/\\+(a|b).js', |
| 296 |
'abc/\\*(a|b).js', |
| 297 |
'abc/\\?(a|b).js', |
| 298 |
'abc/\\@(a\\|b).js', |
| 299 |
'abc/\\!(a\\|b).js', |
| 300 |
'abc/\\+(a\\|b).js', |
| 301 |
'abc/\\*(a\\|b).js', |
| 302 |
'abc/\\?(a\\|b).js' |
| 303 |
].forEach(x => { |
| 304 |
t.true($(x).isGlob); |
| 305 |
}); |
| 306 |
|
| 307 |
// should not return true for non-extglob parens |
| 308 |
t.false($('C:/Program Files (x86)/').isGlob); |
| 309 |
|
| 310 |
// should be true if it has glob characters and is not a valid path |
| 311 |
['abc/[*].js', 'abc/*.js'].forEach(x => t.true($(x).isGlob)); |
| 312 |
|
| 313 |
// should be false if it is a valid non-glob path |
| 314 |
[ |
| 315 |
'abc/?.js', |
| 316 |
'abc/!.js', |
| 317 |
'abc/@.js', |
| 318 |
'abc/+.js' |
| 319 |
].forEach(x => { |
| 320 |
t.false($(x).isGlob); |
| 321 |
}); |
| 322 |
|
| 323 |
// should return true when the string has an extglob |
| 324 |
[ |
| 325 |
'?(abc)', |
| 326 |
'@(abc)', |
| 327 |
'!(abc)', |
| 328 |
'*(abc)', |
| 329 |
'+(abc)', |
| 330 |
'xyz/?(abc)/xyz', |
| 331 |
'xyz/@(abc)/xyz', |
| 332 |
'xyz/!(abc)/xyz', |
| 333 |
'xyz/*(abc)/xyz', |
| 334 |
'xyz/+(abc)/xyz', |
| 335 |
'?(abc|xyz)/xyz', |
| 336 |
'@(abc|xyz)', |
| 337 |
'!(abc|xyz)', |
| 338 |
'*(abc|xyz)', |
| 339 |
'+(abc|xyz)' |
| 340 |
].forEach(x => { |
| 341 |
t.true($(x).isGlob); |
| 342 |
}); |
| 343 |
|
| 344 |
// should not match escaped extglobs |
| 345 |
[ |
| 346 |
'\\?(abc)', |
| 347 |
'\\@(abc)', |
| 348 |
'\\!(abc)', |
| 349 |
'\\*(abc)', |
| 350 |
'\\+(abc)', |
| 351 |
'xyz/\\?(abc)/xyz', |
| 352 |
'xyz/\\@(abc)/xyz', |
| 353 |
'xyz/\\!(abc)/xyz', |
| 354 |
'xyz/\\*(abc)/xyz', |
| 355 |
'xyz/\\+(abc)/xyz' |
| 356 |
].forEach(x => { |
| 357 |
t.false($(x).isGlob); |
| 358 |
}); |
| 359 |
|
| 360 |
// should detect when an glob is in the same pattern as an escaped glob |
| 361 |
[ |
| 362 |
'\\?(abc|xyz)/xyz', |
| 363 |
'\\@(abc|xyz)', |
| 364 |
'\\!(abc|xyz)', |
| 365 |
'\\*(abc|xyz)', |
| 366 |
'\\+(abc|xyz)', |
| 367 |
'\\?(abc)/?(abc)', |
| 368 |
'\\@(abc)/@(abc)', |
| 369 |
'\\!(abc)/!(abc)', |
| 370 |
'\\*(abc)/*(abc)', |
| 371 |
'\\+(abc)/+(abc)', |
| 372 |
'xyz/\\?(abc)/xyz/def/?(abc)/xyz', |
| 373 |
'xyz/\\@(abc)/xyz/def/@(abc)/xyz', |
| 374 |
'xyz/\\!(abc)/xyz/def/!(abc)/xyz', |
| 375 |
'xyz/\\*(abc)/xyz/def/*(abc)/xyz', |
| 376 |
'xyz/\\+(abc)/xyz/def/+(abc)/xyz', |
| 377 |
'\\?(abc|xyz)/xyz/?(abc|xyz)/xyz', |
| 378 |
'\\@(abc|xyz)/@(abc|xyz)', |
| 379 |
'\\!(abc|xyz)/!(abc|xyz)', |
| 380 |
'\\*(abc|xyz)/*(abc|xyz)', |
| 381 |
'\\+(abc|xyz)/+(abc|xyz)' |
| 382 |
].forEach(x => { |
| 383 |
t.true($(x).isGlob); |
| 384 |
}); |
| 385 |
|
| 386 |
t.end(); |
| 387 |
}); |