| 1 |
import * as assert from 'assert'; |
| 2 |
|
| 3 |
import Settings, { Options } from '../../settings'; |
| 4 |
import * as tests from '../../tests'; |
| 5 |
import { EntryFilterFunction, Pattern, Entry } from '../../types'; |
| 6 |
import DeepFilter from './deep'; |
| 7 |
|
| 8 |
type FilterOptions = { |
| 9 |
base?: string; |
| 10 |
positive: Pattern[]; |
| 11 |
negative?: Pattern[]; |
| 12 |
options?: Options; |
| 13 |
}; |
| 14 |
|
| 15 |
const DIRECTORY_ENTRY_LEVEL_1 = tests.entry.builder().path('root').directory().build(); |
| 16 |
const DIRECTORY_ENTRY_LEVEL_2 = tests.entry.builder().path('root/directory').directory().build(); |
| 17 |
const DIRECTORY_ENTRY_LEVEL_3 = tests.entry.builder().path('root/nested/directory').directory().build(); |
| 18 |
|
| 19 |
function getDeepFilterInstance(options?: Options): DeepFilter { |
| 20 |
const settings = new Settings(options); |
| 21 |
|
| 22 |
return new DeepFilter(settings, { |
| 23 |
dot: settings.dot |
| 24 |
}); |
| 25 |
} |
| 26 |
|
| 27 |
function getFilter(options: FilterOptions): EntryFilterFunction { |
| 28 |
const base = options.base ?? '.'; |
| 29 |
const negative = options.negative ?? []; |
| 30 |
|
| 31 |
return getDeepFilterInstance(options.options).getFilter(base, options.positive, negative); |
| 32 |
} |
| 33 |
|
| 34 |
function getResult(entry: Entry, options: FilterOptions): boolean { |
| 35 |
const filter = getFilter(options); |
| 36 |
|
| 37 |
return filter(entry); |
| 38 |
} |
| 39 |
|
| 40 |
function accept(entry: Entry, options: FilterOptions): void { |
| 41 |
assert.strictEqual(getResult(entry, options), true); |
| 42 |
} |
| 43 |
|
| 44 |
function reject(entry: Entry, options: FilterOptions): void { |
| 45 |
assert.strictEqual(getResult(entry, options), false); |
| 46 |
} |
| 47 |
|
| 48 |
describe('Providers → Filters → Deep', () => { |
| 49 |
describe('Constructor', () => { |
| 50 |
it('should create instance of class', () => { |
| 51 |
const filter = getDeepFilterInstance(); |
| 52 |
|
| 53 |
assert.ok(filter instanceof DeepFilter); |
| 54 |
}); |
| 55 |
}); |
| 56 |
|
| 57 |
describe('.getFilter', () => { |
| 58 |
describe('options.deep', () => { |
| 59 |
it('should reject when an option has "0" as value', () => { |
| 60 |
reject(DIRECTORY_ENTRY_LEVEL_1, { |
| 61 |
positive: ['**/*'], |
| 62 |
options: { deep: 0 } |
| 63 |
}); |
| 64 |
}); |
| 65 |
|
| 66 |
it('should reject when the depth of entry is greater than an allowable value (without base)', () => { |
| 67 |
reject(DIRECTORY_ENTRY_LEVEL_3, { |
| 68 |
positive: ['**/*'], |
| 69 |
options: { deep: 1 } |
| 70 |
}); |
| 71 |
}); |
| 72 |
|
| 73 |
it('should reject when the depth of entry is greater than an allowable value (with base as current level)', () => { |
| 74 |
reject(DIRECTORY_ENTRY_LEVEL_3, { |
| 75 |
positive: ['**/*'], |
| 76 |
options: { deep: 1 } |
| 77 |
}); |
| 78 |
}); |
| 79 |
|
| 80 |
it('should reject when the depth of entry is greater than an allowable value (with nested base)', () => { |
| 81 |
reject(DIRECTORY_ENTRY_LEVEL_3, { |
| 82 |
base: 'root/a', |
| 83 |
positive: ['root/a/*'], |
| 84 |
options: { deep: 1 } |
| 85 |
}); |
| 86 |
}); |
| 87 |
|
| 88 |
it('should accept when an option has "Infinity" as value', () => { |
| 89 |
accept(DIRECTORY_ENTRY_LEVEL_1, { |
| 90 |
positive: ['**/*'], |
| 91 |
options: { deep: Infinity } |
| 92 |
}); |
| 93 |
}); |
| 94 |
}); |
| 95 |
|
| 96 |
describe('options.followSymbolicLinks', () => { |
| 97 |
it('should reject when an entry is symbolic link and option is disabled', () => { |
| 98 |
const entry = tests.entry.builder().path('root').directory().symlink().build(); |
| 99 |
|
| 100 |
reject(entry, { |
| 101 |
positive: ['**/*'], |
| 102 |
options: { followSymbolicLinks: false } |
| 103 |
}); |
| 104 |
}); |
| 105 |
|
| 106 |
it('should accept when an entry is symbolic link and option is enabled', () => { |
| 107 |
const entry = tests.entry.builder().path('root').directory().symlink().build(); |
| 108 |
|
| 109 |
accept(entry, { |
| 110 |
positive: ['**/*'], |
| 111 |
options: { followSymbolicLinks: true } |
| 112 |
}); |
| 113 |
}); |
| 114 |
}); |
| 115 |
|
| 116 |
describe('Positive pattern', () => { |
| 117 |
it('should reject when an entry does not match to the positive pattern', () => { |
| 118 |
reject(DIRECTORY_ENTRY_LEVEL_1, { |
| 119 |
positive: ['non-root/*'] |
| 120 |
}); |
| 121 |
}); |
| 122 |
|
| 123 |
it('should reject when an entry starts with leading dot and does not match to the positive pattern', () => { |
| 124 |
const entry = tests.entry.builder().path('./root').directory().build(); |
| 125 |
|
| 126 |
reject(entry, { |
| 127 |
positive: ['non-root/*'] |
| 128 |
}); |
| 129 |
}); |
| 130 |
|
| 131 |
it('should accept when an entry match to the positive pattern with leading dot', () => { |
| 132 |
const entry = tests.entry.builder().path('./root').directory().build(); |
| 133 |
|
| 134 |
accept(entry, { |
| 135 |
positive: ['./root/*'] |
| 136 |
}); |
| 137 |
}); |
| 138 |
|
| 139 |
it('should accept when the positive pattern does not match by level, but the "baseNameMatch" is enabled', () => { |
| 140 |
accept(DIRECTORY_ENTRY_LEVEL_2, { |
| 141 |
positive: ['*'], |
| 142 |
options: { baseNameMatch: true } |
| 143 |
}); |
| 144 |
}); |
| 145 |
|
| 146 |
it('should accept when the positive pattern has a globstar', () => { |
| 147 |
accept(DIRECTORY_ENTRY_LEVEL_3, { |
| 148 |
positive: ['**/*'] |
| 149 |
}); |
| 150 |
}); |
| 151 |
}); |
| 152 |
|
| 153 |
describe('Negative pattern', () => { |
| 154 |
it('should reject when an entry match to the negative pattern', () => { |
| 155 |
reject(DIRECTORY_ENTRY_LEVEL_2, { |
| 156 |
positive: ['**/*'], |
| 157 |
negative: ['root/**'] |
| 158 |
}); |
| 159 |
}); |
| 160 |
|
| 161 |
it('should accept when the negative pattern has no effect to depth reading', () => { |
| 162 |
accept(DIRECTORY_ENTRY_LEVEL_3, { |
| 163 |
positive: ['**/*'], |
| 164 |
negative: ['**/*'] |
| 165 |
}); |
| 166 |
}); |
| 167 |
|
| 168 |
it('should accept when an entry does not match to the negative pattern', () => { |
| 169 |
accept(DIRECTORY_ENTRY_LEVEL_3, { |
| 170 |
positive: ['**/*'], |
| 171 |
negative: ['non-root/**/*'] |
| 172 |
}); |
| 173 |
}); |
| 174 |
}); |
| 175 |
}); |
| 176 |
|
| 177 |
describe('Immutability', () => { |
| 178 |
it('should return the data without changes', () => { |
| 179 |
const filter = getFilter({ |
| 180 |
positive: ['**/*'] |
| 181 |
}); |
| 182 |
|
| 183 |
const reference = tests.entry.builder().path('root/directory').directory().build(); |
| 184 |
const entry = tests.entry.builder().path('root/directory').directory().build(); |
| 185 |
|
| 186 |
filter(entry); |
| 187 |
|
| 188 |
assert.deepStrictEqual(entry, reference); |
| 189 |
}); |
| 190 |
}); |
| 191 |
}); |