|
| 1 | +import { createUrlFilter } from '../crawl'; |
| 2 | + |
| 3 | +test('if the include array is empty allow any path', () => { |
| 4 | + const filter = createUrlFilter([], []); |
| 5 | + expect(filter({ path: '/foo' })).toBeTruthy(); |
| 6 | + expect(filter({ path: '/' })).toBeTruthy(); |
| 7 | + expect(filter({ path: '/asdf/1234' })).toBeTruthy(); |
| 8 | +}); |
| 9 | + |
| 10 | +test('only allow items matching static include glob', () => { |
| 11 | + const filter = createUrlFilter(['/foo'], []); |
| 12 | + expect(filter({ path: '/foo' })).toBeTruthy(); |
| 13 | + expect(filter({ path: '/foo/' })).toBeTruthy(); |
| 14 | + expect(filter({ path: '/foo/bar' })).toBeFalsy(); |
| 15 | + expect(filter({ path: '/foobar' })).toBeFalsy(); |
| 16 | + expect(filter({ path: '/asdf' })).toBeFalsy(); |
| 17 | +}); |
| 18 | + |
| 19 | +test('only allow items matching include glob', () => { |
| 20 | + const filter = createUrlFilter(['/foo/*'], []); |
| 21 | + expect(filter({ path: '/foo' })).toBeFalsy(); |
| 22 | + expect(filter({ path: '/foo/bar' })).toBeTruthy(); |
| 23 | + expect(filter({ path: '/foo/bar/' })).toBeTruthy(); |
| 24 | + expect(filter({ path: '/asdf' })).toBeFalsy(); |
| 25 | +}); |
| 26 | + |
| 27 | +test('allow items from multiple include globs', () => { |
| 28 | + const filter = createUrlFilter(['/foo/*', '/foo'], []); |
| 29 | + expect(filter({ path: '/foo/' })).toBeTruthy(); |
| 30 | + expect(filter({ path: '/foo' })).toBeTruthy(); |
| 31 | + expect(filter({ path: '/foo/bar' })).toBeTruthy(); |
| 32 | + expect(filter({ path: '/foo/bar/' })).toBeTruthy(); |
| 33 | + expect(filter({ path: '/foo/bar/baz' })).toBeFalsy(); |
| 34 | + expect(filter({ path: '/asdf' })).toBeFalsy(); |
| 35 | +}); |
| 36 | + |
| 37 | +test('glob with star in the middle', () => { |
| 38 | + const filter = createUrlFilter(['/foo/*/bar'], []); |
| 39 | + expect(filter({ path: '/foo/asdf/bar' })).toBeTruthy(); |
| 40 | + expect(filter({ path: '/foo/asdf/bar/' })).toBeTruthy(); |
| 41 | + expect(filter({ path: '/foo/bar' })).toBeFalsy(); |
| 42 | + expect(filter({ path: '/foo/asdf/1234' })).toBeFalsy(); |
| 43 | +}); |
| 44 | + |
| 45 | +test('removes trailing slash from glob', () => { |
| 46 | + const filter = createUrlFilter(['/foo/'], []); |
| 47 | + expect(filter({ path: '/foo/' })).toBeTruthy(); |
| 48 | + expect(filter({ path: '/foo' })).toBeTruthy(); |
| 49 | + expect(filter({ path: '/foo/bar' })).toBeFalsy(); |
| 50 | +}); |
| 51 | + |
| 52 | +test('exclude has higher precedence than include', () => { |
| 53 | + const filter = createUrlFilter(['/foo/*'], ['/foo/asdf']); |
| 54 | + expect(filter({ path: '/foo/bar' })).toBeTruthy(); |
| 55 | + expect(filter({ path: '/foo/bar/' })).toBeTruthy(); |
| 56 | + expect(filter({ path: '/foo/asdf' })).toBeFalsy(); |
| 57 | + expect(filter({ path: '/foo/bar/baz' })).toBeFalsy(); |
| 58 | + expect(filter({ path: '/foo/asdfasdf' })).toBeTruthy(); |
| 59 | +}); |
| 60 | + |
| 61 | +test('globstar and globs in exclude', () => { |
| 62 | + const filter = createUrlFilter(['/foo/**'], ['/foo/asdf/**']); |
| 63 | + expect(filter({ path: '/foo' })).toBeFalsy(); |
| 64 | + expect(filter({ path: '/foo/sdf' })).toBeTruthy(); |
| 65 | + expect(filter({ path: '/foo/sdf/asdf' })).toBeTruthy(); |
| 66 | + expect(filter({ path: '/foo/sdf/asdf/' })).toBeTruthy(); |
| 67 | + expect(filter({ path: '/foo/asdf' })).toBeTruthy(); |
| 68 | + expect(filter({ path: '/foo/asdf/foo' })).toBeFalsy(); |
| 69 | + expect(filter({ path: '/foo/asdf/foo/bar' })).toBeFalsy(); |
| 70 | +}); |
| 71 | + |
| 72 | +test('fancy globs', () => { |
| 73 | + const filter = createUrlFilter(['/{foo,bar}/*'], []); |
| 74 | + expect(filter({ path: '/foo/asdf' })).toBeTruthy(); |
| 75 | + expect(filter({ path: '/bar/asdf' })).toBeTruthy(); |
| 76 | + expect(filter({ path: '/bar/asdf/' })).toBeTruthy(); |
| 77 | + expect(filter({ path: '/1234/asdf' })).toBeFalsy(); |
| 78 | +}); |
0 commit comments