-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathignore.test.ts
More file actions
52 lines (40 loc) · 2.19 KB
/
Copy pathignore.test.ts
File metadata and controls
52 lines (40 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import path from 'node:path';
import { expect, test } from 'rstack/test';
import { normalizeFmtConfig } from '../../src/fmt/config.ts';
import { createFmtIgnoreMatcher } from '../../src/fmt/ignore.ts';
const rootPath = path.join(import.meta.dirname, 'project');
const createMatcher = (ignorePatterns: string[]) =>
createFmtIgnoreMatcher(normalizeFmtConfig({ ignorePatterns }, rootPath));
test('matches gitignore patterns relative to the config root', () => {
const isIgnored = createMatcher(['dist/', '*.snap', '/root.js', '# comment', '\\#generated.js']);
expect(isIgnored(path.join(rootPath, 'dist/index.js'))).toBe(true);
expect(isIgnored(path.join(rootPath, 'src/data.snap'))).toBe(true);
expect(isIgnored(path.join(rootPath, 'root.js'))).toBe(true);
expect(isIgnored(path.join(rootPath, 'nested/root.js'))).toBe(false);
expect(isIgnored(path.join(rootPath, '#generated.js'))).toBe(true);
expect(isIgnored(path.join(rootPath, 'src/index.js'))).toBe(false);
});
test('applies negated patterns in declaration order', () => {
const isIgnored = createMatcher(['*.js', '!src/keep.js']);
const isIgnoredAgain = createMatcher(['*.js', '!src/keep.js', 'src/keep.js']);
const isReincluded = createMatcher(['dist', '!dist']);
const filePath = path.join(rootPath, 'src/keep.js');
expect(isIgnored(filePath)).toBe(false);
expect(isIgnored(path.join(rootPath, 'src/drop.js'))).toBe(true);
expect(isIgnoredAgain(filePath)).toBe(true);
expect(isReincluded(path.join(rootPath, 'dist'))).toBe(false);
});
test('does not let explicit files bypass ignore patterns', () => {
const isIgnored = createMatcher(['generated/']);
const explicitFilePath = path.join(rootPath, 'generated/output.js');
expect(isIgnored(explicitFilePath)).toBe(true);
});
test('matches parent directory patterns without validation', () => {
const isIgnored = createMatcher(['../shared/*.js']);
expect(isIgnored(path.join(rootPath, '../shared/index.js'))).toBe(true);
expect(isIgnored(path.join(rootPath, 'shared/index.js'))).toBe(false);
});
test('does not ignore files when no patterns are configured', () => {
const isIgnored = createMatcher([]);
expect(isIgnored(path.join(rootPath, 'src/index.js'))).toBe(false);
});