/
TreeFS-test.js
176 lines (161 loc) Β· 5.61 KB
/
TreeFS-test.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall react_native
*/
import type TreeFS from '../TreeFS';
let mockPathModule;
jest.mock('path', () => mockPathModule);
describe.each([['win32'], ['posix']])('TreeFS on %s', platform => {
// Convenience function to write paths with posix separators but convert them
// to system separators
const p: string => string = filePath =>
platform === 'win32'
? filePath.replaceAll('/', '\\').replace(/^\\/, 'C:\\')
: filePath;
let tfs: TreeFS;
beforeEach(() => {
jest.resetModules();
mockPathModule = jest.requireActual<{}>('path')[platform];
const TreeFS = require('../TreeFS').default;
tfs = new TreeFS({
rootDir: p('/project'),
files: new Map([
[p('foo/another.js'), ['', 123, 0, 0, '', '', 0]],
[p('foo/link-to-bar.js'), ['', 0, 0, 0, '', '', p('../bar.js')]],
[p('foo/link-to-another.js'), ['', 0, 0, 0, '', '', p('another.js')]],
[p('../outside/external.js'), ['', 0, 0, 0, '', '', 0]],
[p('bar.js'), ['', 234, 0, 0, '', '', 0]],
[p('link-to-foo'), ['', 456, 0, 0, '', '', p('./foo')]],
[p('root'), ['', 0, 0, 0, '', '', '..']],
[p('link-to-nowhere'), ['', 0, 0, 0, '', '', p('./nowhere')]],
[p('link-to-self'), ['', 0, 0, 0, '', '', p('./link-to-self')]],
[p('link-cycle-1'), ['', 0, 0, 0, '', '', p('./link-cycle-2')]],
[p('link-cycle-2'), ['', 0, 0, 0, '', '', p('./link-cycle-1')]],
]),
});
});
test('all files iterator returns all regular files by real path', () => {
expect(tfs.getAllFiles().sort()).toEqual([
p('/outside/external.js'),
p('/project/bar.js'),
p('/project/foo/another.js'),
]);
});
test.each([
p('/outside/external.js'),
p('/project/bar.js'),
p('/project/foo/another.js'),
p('/project/foo/link-to-another.js'),
p('/project/link-to-foo/another.js'),
p('/project/link-to-foo/link-to-another.js'),
p('/project/root/outside/external.js'),
])('existence check passes for regular files via symlinks: %s', filePath => {
expect(tfs.exists(filePath)).toBe(true);
});
test('existence check fails for directories, symlinks to directories, or symlinks to nowhere', () => {
expect(tfs.exists(p('/project/foo'))).toBe(false);
expect(tfs.exists(p('/project/link-to-foo'))).toBe(false);
expect(tfs.exists(p('/project/link-to-nowhere'))).toBe(false);
});
test('implements linkStats()', () => {
expect(tfs.linkStats(p('/project/link-to-foo/another.js'))).toEqual({
fileType: 'f',
modifiedTime: 123,
});
expect(tfs.linkStats(p('bar.js'))).toEqual({
fileType: 'f',
modifiedTime: 234,
});
expect(tfs.linkStats(p('./link-to-foo'))).toEqual({
fileType: 'l',
modifiedTime: 456,
});
});
describe('realPath', () => {
test.each([
[p('/project/foo/link-to-another.js'), p('/project/foo/another.js')],
[p('/project/foo/link-to-bar.js'), p('/project/bar.js')],
[p('link-to-foo/link-to-another.js'), p('/project/foo/another.js')],
[p('/project/root/outside/external.js'), p('/outside/external.js')],
[p('/outside/../project/bar.js'), p('/project/bar.js')],
])('%s -> %s', (givenPath, expectedRealPath) =>
expect(tfs.realPath(givenPath)).toEqual(expectedRealPath),
);
test.each([
[p('/project/foo')],
[p('/project/bar.js/bad-parent')],
[p('/project/root/outside')],
[p('/project/link-to-nowhere')],
[p('/project/not/exists')],
])('returns null for directories or broken paths: %s', givenPath =>
expect(tfs.realPath(givenPath)).toEqual(null),
);
});
describe('matchFilesWithContext', () => {
test('non-recursive, skipping deep paths', () => {
expect(
tfs.matchFilesWithContext(p('/project'), {
filter: new RegExp(
// Test starting with `./` since this is mandatory for parity with Webpack.
/^\.\/.*/,
),
recursive: false,
}),
).toEqual([p('/project/bar.js')]);
});
test('inner directory', () => {
expect(
tfs.matchFilesWithContext(p('/project/foo'), {
filter: new RegExp(/.*/),
recursive: true,
}),
).toEqual([
p('/project/foo/another.js'),
p('/project/foo/link-to-bar.js'),
p('/project/foo/link-to-another.js'),
]);
});
test('outside rootDir', () => {
expect(
tfs.matchFilesWithContext(p('/outside'), {
filter: new RegExp(/.*/),
recursive: true,
}),
).toEqual([p('/outside/external.js')]);
});
test('recursive', () => {
expect(
tfs.matchFilesWithContext(p('/project'), {
filter: new RegExp(/.*/),
recursive: true,
}),
).toEqual([
p('/project/foo/another.js'),
p('/project/foo/link-to-bar.js'),
p('/project/foo/link-to-another.js'),
p('/project/bar.js'),
p('/project/link-to-foo/another.js'),
p('/project/link-to-foo/link-to-bar.js'),
p('/project/link-to-foo/link-to-another.js'),
p('/project/root/outside/external.js'),
]);
});
test('recursive with filter', () => {
expect(
tfs.matchFilesWithContext(p('/project'), {
filter: new RegExp(/\/another\.js/),
recursive: true,
}),
).toEqual([
p('/project/foo/another.js'),
p('/project/link-to-foo/another.js'),
]);
});
});
});