-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
node.test.js
220 lines (188 loc) · 5.13 KB
/
node.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
'use strict';
const skipOnWindows = require('../../../../../scripts/skip_on_windows');
jest.mock('child_process', () => ({
spawn: jest.fn((cmd, args) => {
let closeCallback;
return {
stdout: {
on: jest.fn().mockImplementation((event, callback) => {
if (event === 'data') {
setTimeout(() => {
callback(mockResponse);
setTimeout(closeCallback, 0);
}, 0);
} else if (event === 'close') {
closeCallback = callback;
}
}),
setEncoding: jest.fn(),
},
};
}),
}));
jest.mock('fs', () => {
let mtime = 32;
const stat = (path, callback) => {
setTimeout(
() =>
callback(null, {
isDirectory() {
return path.endsWith('/directory');
},
isSymbolicLink() {
return false;
},
mtime: {
getTime() {
return mtime++;
},
},
}),
0,
);
};
return {
lstat: jest.fn(stat),
readdir: jest.fn((dir, callback) => {
if (dir === '/fruits') {
setTimeout(() => callback(null, ['directory', 'tomato.js']), 0);
} else if (dir === '/fruits/directory') {
setTimeout(() => callback(null, ['strawberry.js']), 0);
}
}),
stat: jest.fn(stat),
};
});
const pearMatcher = path => /pear/.test(path);
let mockResponse;
let nodeCrawl;
let childProcess;
describe('node crawler', () => {
skipOnWindows.suite();
beforeEach(() => {
jest.resetModules();
// Remove the "process.platform" property descriptor so it can be writable.
delete process.platform;
mockResponse = [
'/fruits/pear.js',
'/fruits/strawberry.js',
'/fruits/tomato.js',
].join('\n');
});
it('crawls for files based on patterns', () => {
process.platform = 'linux';
childProcess = require('child_process');
nodeCrawl = require('../node');
mockResponse = [
'/fruits/pear.js',
'/fruits/strawberry.js',
'/fruits/tomato.js',
'/vegetables/melon.json',
].join('\n');
const promise = nodeCrawl({
data: {
files: Object.create(null),
},
extensions: ['js', 'json'],
ignore: pearMatcher,
roots: ['/fruits', '/vegtables'],
}).then(data => {
expect(childProcess.spawn).lastCalledWith('find', [
'/fruits',
'/vegtables',
'-type',
'f',
'(',
'-iname',
'*.js',
'-o',
'-iname',
'*.json',
')',
]);
expect(data.files).not.toBe(null);
expect(data.files).toEqual({
'/fruits/strawberry.js': ['', 32, 0, []],
'/fruits/tomato.js': ['', 33, 0, []],
'/vegetables/melon.json': ['', 34, 0, []],
});
});
return promise;
});
it('updates only changed files', () => {
process.platform = 'linux';
nodeCrawl = require('../node');
const files = Object.create(null);
// In this test sample, strawberry is changed and tomato is unchanged
const tomato = ['', 33, 1, []];
files['/fruits/strawberry.js'] = ['', 30, 1, []];
files['/fruits/tomato.js'] = tomato;
return nodeCrawl({
data: {files},
extensions: ['js'],
ignore: pearMatcher,
roots: ['/fruits'],
}).then(data => {
expect(data.files).toEqual({
'/fruits/strawberry.js': ['', 32, 0, []],
'/fruits/tomato.js': tomato,
});
// Make sure it is the *same* unchanged object.
expect(data.files['/fruits/tomato.js']).toBe(tomato);
});
});
it('uses node fs APIs on windows', () => {
process.platform = 'win32';
nodeCrawl = require('../node');
const files = Object.create(null);
return nodeCrawl({
data: {files},
extensions: ['js'],
ignore: pearMatcher,
roots: ['/fruits'],
}).then(data => {
expect(data.files).toEqual({
'/fruits/directory/strawberry.js': ['', 33, 0, []],
'/fruits/tomato.js': ['', 32, 0, []],
});
});
});
it('uses node fs APIs if "forceNodeFilesystemAPI" is set to true, regardless of platform', () => {
process.platform = 'linux';
nodeCrawl = require('../node');
const files = Object.create(null);
return nodeCrawl({
data: {files},
extensions: ['js'],
forceNodeFilesystemAPI: true,
ignore: pearMatcher,
roots: ['/fruits'],
}).then(data => {
expect(data.files).toEqual({
'/fruits/directory/strawberry.js': ['', 33, 0, []],
'/fruits/tomato.js': ['', 32, 0, []],
});
});
});
it('completes with empty roots', () => {
process.platform = 'win32';
nodeCrawl = require('../node');
const files = Object.create(null);
return nodeCrawl({
data: {files},
extensions: ['js'],
forceNodeFilesystemAPI: true,
ignore: pearMatcher,
roots: [],
}).then(data => {
expect(data.files).toEqual({});
});
});
});