Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80668 views
1
/**
2
* Copyright 2013 Facebook, Inc.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*
16
* @emails [email protected] [email protected]
17
*/
18
19
describe("MapUpdateTask", function() {
20
var MapUpdateTask = require('../lib/MapUpdateTask');
21
var Resource = require('../lib/resource/Resource');
22
var ResourceLoader = require('../lib/loader/ResourceLoader');
23
var ResourceMap = require('../lib/ResourceMap');
24
var ProjectConfiguration = require('../lib/resource/ProjectConfiguration');
25
var ProjectConfigurationLoader =
26
require('../lib/loader/ProjectConfigurationLoader');
27
var MessageList = require('../lib/MessageList');
28
29
var waitsForCallback = require('../lib/test_helpers/waitsForCallback');
30
var node_path = require('path');
31
32
function expectChanges(changed, expected) {
33
expect(changed.length).toBe(expected.length);
34
var pathMap = {};
35
changed.map(function(record) {
36
pathMap[record.path] = record;
37
});
38
expected.forEach(function(pair) {
39
expect(pathMap[pair[1]]).toBeDefined('Change exists ' + pair[1]);
40
if (pair[0] === 'added') {
41
expect(pathMap[pair[1]].oldResource).toBe(null);
42
expect(pathMap[pair[1]].newPath).toBe(pair[1]);
43
} else if (pair[0] === 'changed') {
44
expect(pathMap[pair[1]].oldResource.path).toBe(pair[1]);
45
expect(pathMap[pair[1]].newPath).toBe(pair[1]);
46
} else if (pair[0] === 'removed') {
47
expect(pathMap[pair[1]].oldResource.path).toBe(pair[1]);
48
expect(pathMap[pair[1]].newPath).toBe(null);
49
}
50
});
51
}
52
53
function addMtime(mtime, resource) {
54
resource.mtime = mtime;
55
return resource;
56
}
57
58
it("should find changed files", function() {
59
var files = [
60
['sub/added.js', 1300000000000],
61
['changed.js', 1300000000000],
62
['unmodified.js', 1300000000000]
63
];
64
var map = new ResourceMap([
65
addMtime(1200000000000, new Resource('sub/removed.js')),
66
addMtime(1200000000000, new Resource('changed.js')),
67
addMtime(1300000000000, new Resource('unmodified.js'))
68
]);
69
var task = new MapUpdateTask(
70
files,
71
[],
72
map);
73
var changed;
74
75
runs(function() {
76
task.on('changed', function(tmp) {
77
changed = tmp;
78
});
79
task.run();
80
});
81
82
waitsFor(function() {
83
return changed;
84
}, 300);
85
86
runs(function() {
87
expectChanges(changed, [
88
['added', node_path.join('sub','added.js')],
89
['removed', node_path.join('sub','removed.js')],
90
['changed', node_path.join('changed.js')]
91
]);
92
});
93
});
94
95
it('should find changes on package.json deletion', function() {
96
var files = [
97
['p1/a/1.js', 1300000000000],
98
['p1/b/2.js', 1300000000000]
99
];
100
var map = new ResourceMap([
101
addMtime(1300000000000, new Resource('p1/a/1.js')),
102
addMtime(1300000000000, new Resource('p1/b/2.js')),
103
addMtime(1300000000000, new ProjectConfiguration('p1/package.json', {}))
104
]);
105
var task = new MapUpdateTask(
106
files,
107
[],
108
map);
109
var changed;
110
111
runs(function() {
112
task.on('changed', function(tmp) {
113
changed = tmp;
114
});
115
task.run();
116
});
117
118
waitsFor(function() {
119
return changed;
120
}, 300);
121
122
runs(function() {
123
expectChanges(changed, [
124
['changed', node_path.join('p1','a','1.js')],
125
['changed', node_path.join('p1','b','2.js')],
126
['removed', node_path.join('p1','package.json')]
127
]);
128
});
129
});
130
131
it('should find changes on package.json deletion + haste dirs', function() {
132
var files = [
133
['p1/a/1.js', 1300000000000],
134
['p1/b/2.js', 1300000000000]
135
];
136
var map = new ResourceMap([
137
addMtime(1300000000000, new Resource('p1/a/1.js')),
138
addMtime(1300000000000, new Resource('p1/b/2.js')),
139
addMtime(1300000000000, new ProjectConfiguration('p1/package.json', {
140
haste: { roots: ['a'] }
141
}))
142
]);
143
var task = new MapUpdateTask(files, [], map);
144
var changed;
145
146
runs(function() {
147
task.on('changed', function(tmp) {
148
changed = tmp;
149
});
150
task.run();
151
});
152
153
waitsFor(function() {
154
return changed;
155
}, 300);
156
157
runs(function() {
158
expectChanges(changed, [
159
['changed', node_path.join('p1','a','1.js')],
160
['removed', node_path.join('p1','package.json')]
161
]);
162
});
163
});
164
165
it('should find changes on package.json addition', function() {
166
var files = [
167
['p1/a/1.js', 1300000000000],
168
['p1/b/2.js', 1300000000000],
169
['p1/package.json', 1300000000000]
170
];
171
var map = new ResourceMap([
172
addMtime(1300000000000, new Resource('p1/a/1.js')),
173
addMtime(1300000000000, new Resource('p1/b/2.js'))
174
]);
175
var configurationLoader = new ProjectConfigurationLoader();
176
spyOn(configurationLoader, 'loadFromPath')
177
.andCallFake(function(path, configuration, callback) {
178
callback(
179
new MessageList(),
180
new ProjectConfiguration('p1/package.json', {}));
181
});
182
183
var task = new MapUpdateTask(files, [configurationLoader], map);
184
var changed;
185
186
runs(function() {
187
task.on('changed', function(tmp) {
188
changed = tmp;
189
});
190
task.run();
191
});
192
193
waitsFor(function() {
194
return changed;
195
}, 300);
196
197
runs(function() {
198
expectChanges(changed, [
199
['changed', node_path.join('p1','a','1.js')],
200
['changed', node_path.join('p1','b','2.js')],
201
['added', node_path.join('p1','package.json')]
202
]);
203
});
204
});
205
206
it('should find changes on package.json change', function() {
207
var files = [
208
['p1/a/1.js', 1300000000000],
209
['p1/b/2.js', 1300000000000],
210
['p1/package.json', 1300000000000]
211
];
212
var map = new ResourceMap([
213
addMtime(1300000000000, new Resource('p1/a/1.js')),
214
addMtime(1300000000000, new Resource('p1/b/2.js')),
215
addMtime(1200000000000, new ProjectConfiguration('p1/package.json', {
216
haste: { roots: ['a'] }
217
}))
218
]);
219
var configurationLoader = new ProjectConfigurationLoader();
220
spyOn(configurationLoader, 'loadFromPath')
221
.andCallFake(function(path, configuration, callback) {
222
expect(path).toBe(node_path.join('p1','package.json'));
223
callback(
224
new MessageList(),
225
new ProjectConfiguration('p1/package.json', {}));
226
});
227
228
var task = new MapUpdateTask(files, [configurationLoader], map);
229
var changed;
230
231
runs(function() {
232
task.on('changed', function(tmp) {
233
changed = tmp;
234
});
235
task.run();
236
});
237
238
waitsFor(function() {
239
return changed;
240
}, 300);
241
242
runs(function() {
243
expectChanges(changed, [
244
['changed', node_path.join('p1','a','1.js')],
245
['changed', node_path.join('p1','b','2.js')],
246
['changed', node_path.join('p1','package.json')]
247
]);
248
});
249
});
250
251
it('should find changes on package.json change + haste dirs', function() {
252
var files = [
253
['p1/a/1.js', 1300000000000],
254
['p1/b/2.js', 1300000000000],
255
['p1/package.json', 1300000000000]
256
];
257
var map = new ResourceMap([
258
addMtime(1300000000000, new Resource('p1/a/1.js')),
259
addMtime(1300000000000, new Resource('p1/b/2.js')),
260
addMtime(1200000000000, new ProjectConfiguration('p1/package.json', {
261
haste: { roots: ['a'] }
262
}))
263
]);
264
var configurationLoader = new ProjectConfigurationLoader();
265
spyOn(configurationLoader, 'loadFromPath')
266
.andCallFake(function(path, configuration, callback) {
267
callback(
268
new MessageList(),
269
new ProjectConfiguration('p1/package.json', {
270
haste: { roots: ['a'] }
271
}));
272
});
273
274
var task = new MapUpdateTask(files, [configurationLoader], map);
275
var changed;
276
277
runs(function() {
278
task.on('changed', function(tmp) {
279
changed = tmp;
280
});
281
task.run();
282
});
283
284
waitsFor(function() {
285
return changed;
286
}, 300);
287
288
runs(function() {
289
expectChanges(changed, [
290
['changed', node_path.join('p1','a','1.js')],
291
['changed', node_path.join('p1','package.json')]
292
]);
293
});
294
});
295
296
it('should load resource when changed', function() {
297
var finder = [
298
['sub/added.js', 1300000000000]
299
];
300
var map = new ResourceMap([]);
301
var loader = new ResourceLoader();
302
var task = new MapUpdateTask(finder, [loader], map);
303
spyOn(loader, 'loadFromPath')
304
.andCallFake(function(path, configuration, callback) {
305
expect(path).toBe(node_path.join('sub','added.js'));
306
callback(new MessageList(), new Resource('sub/added.js'));
307
});
308
309
waitsForCallback(
310
function(callback) {
311
task.on('complete', callback);
312
task.run();
313
},
314
function() {}
315
);
316
});
317
318
it('should not load deleted resource', function() {
319
var files = [];
320
var old = addMtime(1200000000000, new Resource('sub/deleted.js'));
321
var map = new ResourceMap([old]);
322
var loader = new ResourceLoader();
323
var task = new MapUpdateTask(files, [loader], map);
324
spyOn(loader, 'loadFromPath');
325
326
waitsForCallback(
327
function(callback) {
328
task.on('complete', callback).run();
329
},
330
function() {
331
expect(loader.loadFromPath).not.toHaveBeenCalled();
332
}
333
);
334
});
335
336
it('should aggregate messages from loaders', function() {
337
var files = [
338
['sub/new1.js', 1300000000000],
339
['sub/new2.js', 1300000000000]
340
];
341
var map = new ResourceMap([]);
342
var loader = new ResourceLoader();
343
var task = new MapUpdateTask(files, [loader], map);
344
spyOn(loader, 'loadFromPath')
345
.andCallFake(function(path, configuration, callback) {
346
var messages = new MessageList();
347
messages.addError(path, 'foo', 'bar');
348
callback(messages, new Resource(path));
349
});
350
351
waitsForCallback(
352
function(callback) {
353
task.on('complete', callback).run();
354
},
355
function() {
356
expect(task.messages.length).toBe(2);
357
}
358
);
359
});
360
361
it('should aggregate messages from postProcess', function() {
362
var files = [
363
['sub/new1.js', 1300000000000],
364
['sub/new2.js', 1300000000000]
365
];
366
var map = new ResourceMap([]);
367
var loader = new ResourceLoader();
368
var task = new MapUpdateTask(files, [loader], map);
369
spyOn(loader, 'loadFromPath')
370
.andCallFake(function(path, configuration, callback) {
371
var messages = new MessageList();
372
process.nextTick(function() {
373
callback(messages, new Resource(path));
374
});
375
});
376
377
spyOn(loader, 'postProcess')
378
.andCallFake(function(map, resources, callback) {
379
expect(resources.length).toBe(2);
380
expect(resources[0]).toEqual(jasmine.any(Resource));
381
var messages = new MessageList();
382
resources.forEach(function(resource) {
383
messages.addError(resource.path, 'foo', 'bar');
384
});
385
callback(messages);
386
});
387
388
waitsForCallback(
389
function(callback) {
390
task.on('complete', callback).run();
391
},
392
function() {
393
expect(task.messages.length).toBe(2);
394
}
395
);
396
});
397
398
});
399
400