-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
preprocessor.spec.js
497 lines (397 loc) · 15.4 KB
/
preprocessor.spec.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
'use strict'
const mocks = require('mocks')
const di = require('di')
const path = require('path')
const events = require('../../lib/events')
describe('preprocessor', () => {
let pp
let m
let mockFs
let emitterSetting
// mimic first few bytes of a pdf file
const binarydata = new Buffer([0x25, 0x50, 0x44, 0x66, 0x46, 0x00]) // eslint-disable-line node/no-deprecated-api
beforeEach(() => {
mockFs = mocks.fs.create({
some: {
'a.js': mocks.fs.file(0, 'content'),
'b.js': mocks.fs.file(0, 'content'),
'a.txt': mocks.fs.file(0, 'some-text'),
'photo.png': mocks.fs.file(0, binarydata),
'CAM_PHOTO.JPG': mocks.fs.file(0, binarydata),
'.dir': {
'a.js': mocks.fs.file(0, 'content')
}
}
})
const mocks_ = {
'graceful-fs': mockFs,
minimatch: require('minimatch')
}
emitterSetting = { 'emitter': ['value', new events.EventEmitter()] }
m = mocks.loadFile(path.join(__dirname, '/../../lib/preprocessor.js'), mocks_)
})
it('should preprocess matching file', (done) => {
const fakePreprocessor = sinon.spy((content, file, done) => {
file.path = file.path + '-preprocessed'
done(null, 'new-content')
})
const injector = new di.Injector([{
'preprocessor:fake': [
'factory', function () { return fakePreprocessor }
]
}, emitterSetting])
pp = m.createPriorityPreprocessor({ '**/*.js': ['fake'] }, {}, null, injector)
const file = { originalPath: '/some/a.js', path: 'path' }
pp(file, () => {
expect(fakePreprocessor).to.have.been.called
expect(file.path).to.equal('path-preprocessed')
expect(file.content).to.equal('new-content')
done()
})
})
it('should match directories starting with a dot', (done) => {
const fakePreprocessor = sinon.spy((content, file, done) => {
file.path = file.path + '-preprocessed'
done(null, 'new-content')
})
const injector = new di.Injector([{
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
}, emitterSetting])
pp = m.createPriorityPreprocessor({ '**/*.js': ['fake'] }, {}, null, injector)
const file = { originalPath: '/some/.dir/a.js', path: 'path' }
pp(file, () => {
expect(fakePreprocessor).to.have.been.called
expect(file.path).to.equal('path-preprocessed')
expect(file.content).to.equal('new-content')
done()
})
})
it('should get content if preprocessor is an async function or return Promise with content', (done) => {
const fakePreprocessor = sinon.spy(async (content, file, done) => {
file.path = file.path + '-preprocessed'
return 'new-content'
})
const injector = new di.Injector([{
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
}, emitterSetting])
pp = m.createPriorityPreprocessor({ '**/*.js': ['fake'] }, {}, null, injector)
const file = { originalPath: '/some/.dir/a.js', path: 'path' }
pp(file, () => {
expect(fakePreprocessor).to.have.been.called
expect(file.path).to.equal('path-preprocessed')
expect(file.content).to.equal('new-content')
done()
})
})
it('should get content if preprocessor is an async function still calling done()', (done) => {
const fakePreprocessor = sinon.spy(async (content, file, done) => {
file.path = file.path + '-preprocessed'
done(null, 'new-content')
})
const injector = new di.Injector([{
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
}, emitterSetting])
pp = m.createPriorityPreprocessor({ '**/*.js': ['fake'] }, {}, null, injector)
const file = { originalPath: '/some/.dir/a.js', path: 'path' }
pp(file, () => {
expect(fakePreprocessor).to.have.been.called
expect(file.path).to.equal('path-preprocessed')
expect(file.content).to.equal('new-content')
done()
})
})
it('should check patterns after creation when invoked', (done) => {
const fakePreprocessor = sinon.spy((content, file, done) => {
file.path = file.path + '-preprocessed'
done(null, 'new-content')
})
const injector = new di.Injector([{
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
}, emitterSetting])
const config = { '**/*.txt': ['fake'] }
pp = m.createPriorityPreprocessor(config, {}, null, injector)
const file = { originalPath: '/some/a.js', path: 'path' }
config['**/*.js'] = ['fake']
pp(file, () => {
expect(fakePreprocessor).to.have.been.called
expect(file.path).to.equal('path-preprocessed')
expect(file.content).to.equal('new-content')
done()
})
})
it('should ignore not matching file', (done) => {
const fakePreprocessor = sinon.spy((content, file, done) => {
done(null, '')
})
const injector = new di.Injector([{
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
}, emitterSetting])
pp = m.createPriorityPreprocessor({ '**/*.js': ['fake'] }, {}, null, injector)
const file = { originalPath: '/some/a.txt', path: 'path' }
pp(file, () => {
expect(fakePreprocessor).to.not.have.been.called
done()
})
})
it('should apply all preprocessors', (done) => {
const fakePreprocessor1 = sinon.spy((content, file, done) => {
file.path = file.path + '-p1'
done(null, content + '-c1')
})
const fakePreprocessor2 = sinon.spy((content, file, done) => {
file.path = file.path + '-p2'
done(content + '-c2')
})
const injector = new di.Injector([{
'preprocessor:fake1': ['factory', function () { return fakePreprocessor1 }],
'preprocessor:fake2': ['factory', function () { return fakePreprocessor2 }]
}, emitterSetting])
pp = m.createPriorityPreprocessor({ '**/*.js': ['fake1', 'fake2'] }, {}, null, injector)
const file = { originalPath: '/some/a.js', path: 'path' }
pp(file, () => {
expect(fakePreprocessor1).to.have.been.calledOnce
expect(fakePreprocessor2).to.have.been.calledOnce
expect(file.path).to.equal('path-p1-p2')
expect(file.content).to.equal('content-c1-c2')
done()
})
})
it('should compute SHA', (done) => {
pp = m.createPriorityPreprocessor({}, {}, null, new di.Injector([emitterSetting]))
const file = { originalPath: '/some/a.js', path: 'path' }
pp(file, () => {
expect(file.sha).to.exist
expect(file.sha.length).to.equal(40)
const previousSHA = file.sha
pp(file, () => {
expect(file.sha).to.equal(previousSHA)
mockFs._touchFile('/some/a.js', null, 'new-content')
pp(file, () => {
expect(file.sha.length).to.equal(40)
expect(file.sha).not.to.equal(previousSHA)
done()
})
})
})
})
it('should compute SHA from content returned by a processor', (done) => {
const fakePreprocessor = sinon.spy((content, file, done) => {
done(null, content + '-processed')
})
const injector = new di.Injector([{
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
}, emitterSetting])
pp = m.createPriorityPreprocessor({ '**/a.js': ['fake'] }, {}, null, injector)
const fileProcess = { originalPath: '/some/a.js', path: 'path' }
const fileSkip = { originalPath: '/some/b.js', path: 'path' }
pp(fileProcess, () => {
pp(fileSkip, () => {
expect(fileProcess.sha).to.exist
expect(fileProcess.sha.length).to.equal(40)
expect(fileSkip.sha).to.exist
expect(fileSkip.sha.length).to.equal(40)
expect(fileProcess.sha).not.to.equal(fileSkip.sha)
done()
})
})
})
it('should return error if any preprocessor fails', (done) => {
const failingPreprocessor = sinon.spy((content, file, done) => {
done(new Error('Some error'), null)
})
const injector = new di.Injector([{
'preprocessor:failing': ['factory', function () { return failingPreprocessor }]
}, emitterSetting])
pp = m.createPriorityPreprocessor({ '**/*.js': ['failing'] }, {}, null, injector)
const file = { originalPath: '/some/a.js', path: 'path' }
pp(file, (err) => {
expect(err).to.exist
done()
})
})
it('should stop preprocessing after an error', (done) => {
const failingPreprocessor = sinon.spy((content, file, done) => {
done(new Error('Some error'), null)
})
const fakePreprocessor = sinon.spy((content, file, done) => {
done(null, content)
})
const injector = new di.Injector([{
'preprocessor:failing': ['factory', function () { return failingPreprocessor }],
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
}, emitterSetting])
pp = m.createPriorityPreprocessor({ '**/*.js': ['failing', 'fake'] }, {}, null, injector)
const file = { originalPath: '/some/a.js', path: 'path' }
pp(file, () => {
expect(fakePreprocessor).not.to.have.been.called
done()
})
})
describe('when fs.readFile fails', () => {
const file = { originalPath: '/some/a.js', path: 'path' }
const getReadFileCallback = (nthCall) => {
return mockFs.readFile.args[nthCall][1]
}
beforeEach(() => {
sinon.stub(mockFs, 'readFile')
})
it('should retry up to 3 times', (done) => {
const fakePreprocessor = sinon.spy((content, file, done) => {
done(null, content)
})
const injector = new di.Injector([{
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
}, emitterSetting])
const pp = m.createPriorityPreprocessor({ '**/*.js': ['fake'] }, {}, null, injector)
pp(file, () => {
expect(fakePreprocessor).to.have.been.called
done()
})
getReadFileCallback(0)('error')
getReadFileCallback(1)('error')
const thirdCallback = getReadFileCallback(2)
mockFs.readFile.restore()
thirdCallback('error')
})
it('should throw after 3 retries', (done) => {
const injector = new di.Injector([{}, emitterSetting])
const pp = m.createPriorityPreprocessor({ '**/*.js': [] }, {}, null, injector)
pp(file, () => { })
getReadFileCallback(0)('error')
getReadFileCallback(1)('error')
getReadFileCallback(2)('error')
expect(() => getReadFileCallback(0)('error')).to.throw('error')
done()
})
})
it('should not preprocess binary files by default', (done) => {
const fakePreprocessor = sinon.spy((content, file, done) => {
done(null, content)
})
const injector = new di.Injector([{
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
}, emitterSetting])
pp = m.createPriorityPreprocessor({ '**/*': ['fake'] }, {}, null, injector)
const file = { originalPath: '/some/photo.png', path: 'path' }
pp(file, (err) => {
if (err) throw err
expect(fakePreprocessor).not.to.have.been.called
expect(file.content).to.be.an.instanceof(Buffer)
done()
})
})
it('should preprocess binary files if handleBinaryFiles=true', (done) => {
const fakePreprocessor = sinon.spy((content, file, done) => {
done(null, content)
})
fakePreprocessor.handleBinaryFiles = true
const injector = new di.Injector([{
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
}, emitterSetting])
pp = m.createPriorityPreprocessor({ '**/*': ['fake'] }, {}, null, injector)
const file = { originalPath: '/some/photo.png', path: 'path' }
pp(file, (err) => {
if (err) throw err
expect(fakePreprocessor).to.have.been.calledOnce
expect(file.content).to.be.an.instanceof(Buffer)
done()
})
})
it('should not preprocess binary files with capital letters in extension', (done) => {
const fakePreprocessor = sinon.spy((content, file, done) => {
done(null, content)
})
const injector = new di.Injector([{
'preprocessor:fake': ['factory', function () { fakePreprocessor }]
}, emitterSetting])
pp = m.createPriorityPreprocessor({ '**/*': ['fake'] }, {}, null, injector)
const file = { originalPath: '/some/CAM_PHOTO.JPG', path: 'path' }
pp(file, (err) => {
if (err) throw err
expect(fakePreprocessor).not.to.have.been.called
expect(file.content).to.be.an.instanceof(Buffer)
done()
})
})
it('should merge lists of preprocessors using default priority', (done) => {
const callOrder = []
const fakePreprocessorA = sinon.spy((content, file, done) => {
callOrder.push('a')
done(null, content)
})
const fakePreprocessorB = sinon.spy((content, file, done) => {
callOrder.push('b')
done(null, content)
})
const fakePreprocessorC = sinon.spy((content, file, done) => {
callOrder.push('c')
done(null, content)
})
const fakePreprocessorD = sinon.spy((content, file, done) => {
callOrder.push('d')
done(null, content)
})
const injector = new di.Injector([{
'preprocessor:fakeA': ['factory', function () { return fakePreprocessorA }],
'preprocessor:fakeB': ['factory', function () { return fakePreprocessorB }],
'preprocessor:fakeC': ['factory', function () { return fakePreprocessorC }],
'preprocessor:fakeD': ['factory', function () { return fakePreprocessorD }]
}, emitterSetting])
pp = m.createPriorityPreprocessor({
'/*/a.js': ['fakeA', 'fakeB'],
'/some/*': ['fakeB', 'fakeC'],
'/some/a.js': ['fakeD']
}, {}, null, injector)
const file = { originalPath: '/some/a.js', path: 'path' }
pp(file, (err) => {
if (err) throw err
expect(fakePreprocessorA).to.have.been.called
expect(fakePreprocessorB).to.have.been.called
expect(fakePreprocessorC).to.have.been.called
expect(fakePreprocessorD).to.have.been.called
expect(callOrder).to.eql(['a', 'b', 'c', 'd'])
done()
})
})
it('should merge lists of preprocessors obeying priority', (done) => {
const callOrder = []
const fakePreprocessorA = sinon.spy((content, file, done) => {
callOrder.push('a')
done(null, content)
})
const fakePreprocessorB = sinon.spy((content, file, done) => {
callOrder.push('b')
done(null, content)
})
const fakePreprocessorC = sinon.spy((content, file, done) => {
callOrder.push('c')
done(null, content)
})
const fakePreprocessorD = sinon.spy((content, file, done) => {
callOrder.push('d')
done(null, content)
})
const injector = new di.Injector([{
'preprocessor:fakeA': ['factory', function () { return fakePreprocessorA }],
'preprocessor:fakeB': ['factory', function () { return fakePreprocessorB }],
'preprocessor:fakeC': ['factory', function () { return fakePreprocessorC }],
'preprocessor:fakeD': ['factory', function () { return fakePreprocessorD }]
}, emitterSetting])
const priority = { 'fakeA': -1, 'fakeB': 1, 'fakeD': 100 }
pp = m.createPriorityPreprocessor({
'/*/a.js': ['fakeA', 'fakeB'],
'/some/*': ['fakeB', 'fakeC'],
'/some/a.js': ['fakeD']
}, priority, null, injector)
const file = { originalPath: '/some/a.js', path: 'path' }
pp(file, (err) => {
if (err) throw err
expect(fakePreprocessorA).to.have.been.called
expect(fakePreprocessorB).to.have.been.called
expect(fakePreprocessorC).to.have.been.called
expect(fakePreprocessorD).to.have.been.called
expect(callOrder).to.eql(['d', 'b', 'c', 'a'])
done()
})
})
})