|
| 1 | +import { Ignore } from './ignore' |
| 2 | + |
| 3 | +const make = (...patterns: string[]) => new Ignore().add(patterns) |
| 4 | + |
| 5 | +describe('Ignore', () => { |
| 6 | + it('should ignore nothing without rules', () => { |
| 7 | + expect(new Ignore().ignores('a/b.js')).toBe(false) |
| 8 | + }) |
| 9 | + |
| 10 | + it('should return false for an empty path', () => { |
| 11 | + expect(make('*').ignores('')).toBe(false) |
| 12 | + }) |
| 13 | + |
| 14 | + it('should skip blank lines and comments, and support escaped hash', () => { |
| 15 | + const ig = make('# comment', '', ' ', '\\#hash') |
| 16 | + expect(ig.ignores('# comment')).toBe(false) |
| 17 | + expect(ig.ignores('#hash')).toBe(true) |
| 18 | + }) |
| 19 | + |
| 20 | + it('should split multi-line strings', () => { |
| 21 | + const ig = new Ignore().add('a\r\nb\n#c\n') |
| 22 | + expect(ig.ignores('a')).toBe(true) |
| 23 | + expect(ig.ignores('b')).toBe(true) |
| 24 | + expect(ig.ignores('#c')).toBe(false) |
| 25 | + }) |
| 26 | + |
| 27 | + it('should match a basename pattern at any depth and below', () => { |
| 28 | + const ig = make('foo') |
| 29 | + expect(ig.ignores('foo')).toBe(true) |
| 30 | + expect(ig.ignores('a/foo')).toBe(true) |
| 31 | + expect(ig.ignores('a/b/foo')).toBe(true) |
| 32 | + expect(ig.ignores('foo/bar')).toBe(true) |
| 33 | + expect(ig.ignores('foobar')).toBe(false) |
| 34 | + }) |
| 35 | + |
| 36 | + it('should not let wildcards cross a slash', () => { |
| 37 | + expect(make('*.log').ignores('x/y.log')).toBe(true) |
| 38 | + expect(make('*.log').ignores('a.txt')).toBe(false) |
| 39 | + expect(make('a*c').ignores('a/c')).toBe(false) |
| 40 | + expect(make('a*c').ignores('abc')).toBe(true) |
| 41 | + }) |
| 42 | + |
| 43 | + it('should support question marks', () => { |
| 44 | + const ig = make('foo?bar') |
| 45 | + expect(ig.ignores('fooxbar')).toBe(true) |
| 46 | + expect(ig.ignores('foo/bar')).toBe(false) |
| 47 | + expect(ig.ignores('fooxxbar')).toBe(false) |
| 48 | + }) |
| 49 | + |
| 50 | + it('should anchor patterns with a leading or middle slash', () => { |
| 51 | + expect(make('/a.js').ignores('a.js')).toBe(true) |
| 52 | + expect(make('/a.js').ignores('b/a.js')).toBe(false) |
| 53 | + expect(make('doc/frotz').ignores('doc/frotz')).toBe(true) |
| 54 | + expect(make('doc/frotz').ignores('a/doc/frotz')).toBe(false) |
| 55 | + }) |
| 56 | + |
| 57 | + it('should match only directories for a trailing slash', () => { |
| 58 | + const ig = make('build/') |
| 59 | + expect(ig.ignores('build')).toBe(false) |
| 60 | + expect(ig.ignores('build/')).toBe(true) |
| 61 | + expect(ig.ignores('build/x.js')).toBe(true) |
| 62 | + expect(ig.ignores('src/build/')).toBe(true) |
| 63 | + }) |
| 64 | + |
| 65 | + it('should re-include with negation', () => { |
| 66 | + const ig = make('*.js', '!keep.js') |
| 67 | + expect(ig.ignores('keep.js')).toBe(false) |
| 68 | + expect(ig.ignores('other.js')).toBe(true) |
| 69 | + }) |
| 70 | + |
| 71 | + it('should let the last matching rule win', () => { |
| 72 | + expect(make('a', '!a', 'a').ignores('a')).toBe(true) |
| 73 | + }) |
| 74 | + |
| 75 | + it('should not re-include a file inside an ignored directory', () => { |
| 76 | + const ig = make('/abc/', '!/abc/a.js') |
| 77 | + expect(ig.ignores('abc/a.js')).toBe(true) |
| 78 | + }) |
| 79 | + |
| 80 | + it('should re-include a file when only its contents are ignored', () => { |
| 81 | + const ig = make('foo/*', '!foo/bar.js') |
| 82 | + expect(ig.ignores('foo/bar.js')).toBe(false) |
| 83 | + expect(ig.ignores('foo/baz.js')).toBe(true) |
| 84 | + expect(ig.ignores('foo/bar/x')).toBe(true) |
| 85 | + }) |
| 86 | + |
| 87 | + it('should support a leading globstar', () => { |
| 88 | + const ig = make('**/foo') |
| 89 | + expect(ig.ignores('foo')).toBe(true) |
| 90 | + expect(ig.ignores('a/b/foo')).toBe(true) |
| 91 | + expect(ig.ignores('a/b/foo/c')).toBe(true) |
| 92 | + }) |
| 93 | + |
| 94 | + it('should support a trailing globstar without matching the folder', () => { |
| 95 | + const ig = make('abc/**') |
| 96 | + expect(ig.ignores('abc')).toBe(false) |
| 97 | + expect(ig.ignores('abc/a')).toBe(true) |
| 98 | + expect(ig.ignores('abc/d/e')).toBe(true) |
| 99 | + }) |
| 100 | + |
| 101 | + it('should support a middle globstar', () => { |
| 102 | + const ig = make('a/**/b') |
| 103 | + expect(ig.ignores('a/b')).toBe(true) |
| 104 | + expect(ig.ignores('a/x/b')).toBe(true) |
| 105 | + expect(ig.ignores('a/x/y/b')).toBe(true) |
| 106 | + expect(ig.ignores('a/x/c')).toBe(false) |
| 107 | + expect(ig.ignores('b/a/b')).toBe(false) |
| 108 | + }) |
| 109 | + |
| 110 | + it('should support bracket expressions', () => { |
| 111 | + expect(make('*.[oa]').ignores('x.o')).toBe(true) |
| 112 | + expect(make('*.[oa]').ignores('x.c')).toBe(false) |
| 113 | + expect(make('file[0-9]').ignores('file5')).toBe(true) |
| 114 | + expect(make('file[0-9]').ignores('filea')).toBe(false) |
| 115 | + expect(make('[!a].txt').ignores('a.txt')).toBe(false) |
| 116 | + expect(make('[!a].txt').ignores('b.txt')).toBe(true) |
| 117 | + expect(make('a[/]b').ignores('a/b')).toBe(false) |
| 118 | + }) |
| 119 | + |
| 120 | + it('should match nothing for an unterminated bracket expression', () => { |
| 121 | + const ig = make('*.[a-') |
| 122 | + expect(ig.ignores('a.a')).toBe(false) |
| 123 | + expect(ig.ignores('a.-')).toBe(false) |
| 124 | + }) |
| 125 | + |
| 126 | + it('should treat an escaped star as a literal', () => { |
| 127 | + const ig = make('a\\*') |
| 128 | + expect(ig.ignores('a*')).toBe(true) |
| 129 | + expect(ig.ignores('ab')).toBe(false) |
| 130 | + }) |
| 131 | + |
| 132 | + it('should trim unescaped trailing spaces only', () => { |
| 133 | + expect(make('abc ').ignores('abc')).toBe(true) |
| 134 | + const ig = make('abc\\ ') |
| 135 | + expect(ig.ignores('abc ')).toBe(true) |
| 136 | + expect(ig.ignores('abc')).toBe(false) |
| 137 | + }) |
| 138 | + |
| 139 | + it('should ignore a trailing unpaired backslash', () => { |
| 140 | + expect(make('test\\').ignores('test')).toBe(false) |
| 141 | + }) |
| 142 | + |
| 143 | + it('should be case-insensitive by default and configurable', () => { |
| 144 | + expect(make('README').ignores('readme')).toBe(true) |
| 145 | + |
| 146 | + const strict = new Ignore({ ignore_case: false }).add('README') |
| 147 | + expect(strict.ignores('readme')).toBe(false) |
| 148 | + expect(strict.ignores('README')).toBe(true) |
| 149 | + }) |
| 150 | + |
| 151 | + it('should invalidate its cache when rules are added', () => { |
| 152 | + const ig = new Ignore().add('a') |
| 153 | + expect(ig.ignores('a')).toBe(true) |
| 154 | + ig.add('!a') |
| 155 | + expect(ig.ignores('a')).toBe(false) |
| 156 | + }) |
| 157 | +}) |
0 commit comments