-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
file-list.spec.js
973 lines (808 loc) · 28.9 KB
/
file-list.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
'use strict'
const Promise = require('bluebird')
const EventEmitter = require('events').EventEmitter
const mocks = require('mocks')
const proxyquire = require('proxyquire')
const pathLib = require('path')
const _ = require('lodash')
const helper = require('../../lib/helper')
const config = require('../../lib/config')
// create an array of pattern objects from given strings
function patterns () {
return Array.from(arguments).map((str) => new config.Pattern(str))
}
function pathsFrom (files) {
return Array.from(files).map((file) => file.path)
}
function findFile (path, files) {
return Array.from(files).find((file) => file.path === path)
}
const PATTERN_LIST = {
'/some/*.js': ['/some/a.js', '/some/b.js'],
'*.txt': ['/c.txt', '/a.txt', '/b.txt'],
'/a.*': ['/a.txt']
}
const MG = {
statCache: {
'/some/a.js': {mtime: new Date()},
'/some/b.js': {mtime: new Date()},
'/a.txt': {mtime: new Date()},
'/b.txt': {mtime: new Date()},
'/c.txt': {mtime: new Date()}
}
}
const mockFs = mocks.fs.create({
some: {
'0.js': mocks.fs.file('2012-04-04'),
'a.js': mocks.fs.file('2012-04-04'),
'b.js': mocks.fs.file('2012-05-05'),
'd.js': mocks.fs.file('2012-05-05')
},
folder: {
'x.js': mocks.fs.file(0)
},
'a.txt': mocks.fs.file(0),
'b.txt': mocks.fs.file(0),
'c.txt': mocks.fs.file(0),
'a.js': mocks.fs.file('2012-01-01')
})
describe('FileList', () => {
let list
let emitter
let preprocess
let patternList
let mg
let modified
let glob
let List = list = emitter = preprocess = patternList = mg = modified = glob = null
beforeEach(() => {})
describe('files', () => {
beforeEach(() => {
patternList = PATTERN_LIST
mg = MG
preprocess = sinon.spy((file, done) => process.nextTick(done))
emitter = new EventEmitter()
glob = {
Glob: function (pattern, opts) {
return {
found: patternList[pattern],
statCache: mg.statCache
}
}
}
List = proxyquire('../../lib/file-list', {
helper: helper,
glob: glob,
path: pathLib.posix,
'graceful-fs': mockFs
})
})
it('returns a flat array of served files', () => {
list = new List(patterns('/some/*.js'), [], emitter, preprocess)
return list.refresh().then(() => {
expect(list.files.served).to.have.length(2)
})
})
it('returns a unique set', () => {
list = new List(patterns('/a.*', '*.txt'), [], emitter, preprocess)
return list.refresh().then(() => {
expect(list.files.served).to.have.length(3)
expect(pathsFrom(list.files.served)).to.contain('/a.txt', '/b.txt', '/c.txt')
})
})
it('returns only served files', () => {
const files = [
new config.Pattern('/a.*', true), // served: true
new config.Pattern('/some/*.js', false) // served: false
]
list = new List(files, [], emitter, preprocess)
return list.refresh().then(() => {
expect(pathsFrom(list.files.served)).to.eql(['/a.txt'])
})
})
it('marks no cache files', () => {
const files = [
new config.Pattern('/a.*'), // nocach: false
new config.Pattern('/some/*.js', true, true, true, true) // nocache: true
]
list = new List(files, [], emitter, preprocess)
return list.refresh().then(() => {
expect(pathsFrom(list.files.served)).to.deep.equal([
'/a.txt',
'/some/a.js',
'/some/b.js'
])
expect(preprocess).to.have.been.calledOnce
expect(list.files.served[0].doNotCache).to.be.false
expect(list.files.served[1].doNotCache).to.be.true
expect(list.files.served[2].doNotCache).to.be.true
})
})
it('returns a flat array of included files', () => {
const files = [
new config.Pattern('/a.*', true, false), // included: false
new config.Pattern('/some/*.js') // included: true
]
list = new List(files, [], emitter, preprocess)
return list.refresh().then(() => {
expect(pathsFrom(list.files.included)).not.to.contain('/a.txt')
expect(pathsFrom(list.files.included)).to.deep.equal([
'/some/a.js',
'/some/b.js'
])
})
})
})
describe('_findExcluded', () => {
beforeEach(() => {
preprocess = sinon.spy((file, done) => process.nextTick(done))
emitter = new EventEmitter()
})
it('returns undefined when no match is found', () => {
list = new List([], ['hello.js', 'world.js'], emitter, preprocess)
expect(list._findExcluded('hello.txt')).to.be.undefined
expect(list._findExcluded('/hello/world/i.js')).to.be.undefined
})
it('returns the first match if it finds one', () => {
list = new List([], ['*.js', '**/*.js'], emitter, preprocess)
expect(list._findExcluded('world.js')).to.be.eql('*.js')
expect(list._findExcluded('/hello/world/i.js')).to.be.eql('**/*.js')
})
})
describe('_findIncluded', () => {
beforeEach(() => {
preprocess = sinon.spy((file, done) => process.nextTick(done))
emitter = new EventEmitter()
})
it('returns undefined when no match is found', () => {
list = new List(patterns('*.js'), [], emitter, preprocess)
expect(list._findIncluded('hello.txt')).to.be.undefined
expect(list._findIncluded('/hello/world/i.js')).to.be.undefined
})
it('returns the first match if it finds one', () => {
list = new List(patterns('*.js', '**/*.js'), [], emitter, preprocess)
expect(list._findIncluded('world.js').pattern).to.be.eql('*.js')
expect(list._findIncluded('/hello/world/i.js').pattern).to.be.eql('**/*.js')
})
})
describe('_exists', () => {
beforeEach(() => {
patternList = _.cloneDeep(PATTERN_LIST)
mg = _.cloneDeep(MG)
preprocess = sinon.spy((file, done) => process.nextTick(done))
emitter = new EventEmitter()
glob = {
Glob: function (pattern, opts) {
return {
found: patternList[pattern],
statCache: mg.statCache
}
}
}
List = proxyquire('../../lib/file-list', {
helper: helper,
glob: glob,
path: pathLib.posix,
'graceful-fs': mockFs
})
list = new List(patterns('/some/*.js', '*.txt'), [], emitter, preprocess)
return list.refresh()
})
it('returns false when no match is found', () => {
expect(list._exists('/some/s.js')).to.be.false
expect(list._exists('/hello/world.ex')).to.be.false
})
it('returns true when a match is found', () => {
expect(list._exists('/some/a.js')).to.be.true
expect(list._exists('/some/b.js')).to.be.true
})
})
describe('refresh', () => {
beforeEach(() => {
patternList = _.cloneDeep(PATTERN_LIST)
mg = _.cloneDeep(MG)
preprocess = sinon.spy((file, done) => process.nextTick(done))
emitter = new EventEmitter()
glob = {
Glob: function (pattern, opts) {
return {
found: patternList[pattern],
statCache: mg.statCache
}
}
}
List = proxyquire('../../lib/file-list', {
helper: helper,
glob: glob,
path: pathLib.posix,
'graceful-fs': mockFs
})
list = new List(patterns('/some/*.js', '*.txt'), [], emitter, preprocess, 100)
})
it('resolves patterns', () => {
return list.refresh().then((files) => {
expect(list.buckets.size).to.equal(2)
const first = pathsFrom(list.buckets.get('/some/*.js'))
const second = pathsFrom(list.buckets.get('*.txt'))
expect(first).to.contain('/some/a.js', '/some/b.js')
expect(second).to.contain('/a.txt', '/b.txt', '/c.txt')
})
})
it('uses the file from the first matcher if two matchers match the same file', () => {
list = new List(patterns('/a.*', '*.txt'), [], emitter, preprocess, 100)
return list.refresh().then(() => {
const first = pathsFrom(list.buckets.get('/a.*'))
const second = pathsFrom(list.buckets.get('*.txt'))
expect(first).to.contain('/a.txt')
expect(second).not.to.contain('/a.txt')
})
})
it('cancels refreshs', () => {
const checkResult = (files) => {
expect(pathsFrom(files.served)).to.contain('/some/a.js', '/some/b.js', '/some/c.js')
}
const p1 = list.refresh().then(checkResult)
patternList['/some/*.js'].push('/some/c.js')
mg.statCache['/some/c.js'] = {mtime: new Date(Date.now() + 5000)}
const p2 = list.refresh().then(checkResult)
let called = false
const callback = (data) => {
expect(called).to.be.false
expect(data.served[0].mtime.toString()).to.not.equal(data.served[2].mtime.toString())
expect(data.served[0].mtime.toString()).to.equal(data.served[1].mtime.toString())
called = true
}
list._emitter.on('file_list_modified', callback)
return Promise.all([p1, p2]).then(() => {
list._emitter.removeListener('file_list_modified', callback)
})
})
it('sets the mtime for all files', () => {
return list.refresh().then((files) => {
const bucket = list.buckets.get('/some/*.js')
const file1 = findFile('/some/a.js', bucket)
const file2 = findFile('/some/b.js', bucket)
expect(file1.mtime).to.be.eql(mg.statCache['/some/a.js'].mtime)
expect(file2.mtime).to.be.eql(mg.statCache['/some/b.js'].mtime)
})
})
it('sets the mtime for relative patterns', () => {
list = new List(patterns('/some/world/../*.js', '*.txt'), [], emitter, preprocess)
return list.refresh().then((files) => {
const bucket = list.buckets.get('/some/world/../*.js')
const file1 = findFile('/some/a.js', bucket)
const file2 = findFile('/some/b.js', bucket)
expect(file1.mtime).to.be.eql(mg.statCache['/some/a.js'].mtime)
expect(file2.mtime).to.be.eql(mg.statCache['/some/b.js'].mtime)
})
})
it('should sort files within buckets and keep order of patterns (buckets)', () => {
// /(a.* ) => /a.txt [MATCH in *.txt as well]
// /some/*.(js) => /some/a.js, /some/b.js [/some/b.js EXCLUDED]
// *.(txt ) => /c.txt, a.txt, b.txt [UNSORTED]
list = new List(patterns('/a.*', '/some/*.js', '*.txt'), ['**/b.js'], emitter, preprocess)
return list.refresh().then((files) => {
expect(pathsFrom(files.served)).to.deep.equal([
'/a.txt',
'/some/a.js',
'/b.txt',
'/c.txt'
])
})
})
it('ingores excluded files', () => {
list = new List(patterns('*.txt'), ['/a.*', '**/b.txt'], emitter, preprocess)
return list.refresh().then((files) => {
const bucket = pathsFrom(list.buckets.get('*.txt'))
expect(bucket).to.contain('/c.txt')
expect(bucket).not.to.contain('/a.txt')
expect(bucket).not.to.contain('/b.txt')
})
})
it('does not glob urls and sets the isUrl flag', () => {
list = new List(patterns('http://some.com'), [], emitter, preprocess)
return list.refresh()
.then((files) => {
const bucket = list.buckets.get('http://some.com')
const file = findFile('http://some.com', bucket)
expect(file).to.have.property('isUrl', true)
})
})
it('preprocesses all files', () => {
return list.refresh().then((files) => {
expect(preprocess.callCount).to.be.eql(5)
})
})
it('fails when a preprocessor fails', () => {
preprocess = sinon.spy((file, next) => {
next(new Error('failing'), null)
})
list = new List(patterns('/some/*.js'), [], emitter, preprocess)
return list.refresh().catch((err) => {
expect(err.message).to.be.eql('failing')
})
})
it('fires modified before resolving promise after subsequent calls', () => {
const modified = sinon.stub()
emitter.on('file_list_modified', modified)
return list.refresh().then(() => {
expect(modified).to.have.been.calledOnce
})
.then(() => {
list.refresh().then(() => {
expect(modified).to.have.been.calledTwice
})
})
})
})
describe('reload', () => {
beforeEach(() => {
preprocess = sinon.spy((file, done) => process.nextTick(done))
emitter = new EventEmitter()
list = new List(patterns('/some/*.js', '*.txt'), [], emitter, preprocess)
})
it('refreshes, even when a refresh is already happening', () => {
sinon.spy(list, '_refresh')
return Promise.all([
list.refresh(),
list.reload(patterns('*.txt'), [])
])
.then(() => {
expect(list._refresh).to.have.been.calledTwice
})
})
})
describe('addFile', () => {
let clock = null
beforeEach(() => {
patternList = PATTERN_LIST
mg = MG
preprocess = sinon.spy((file, done) => process.nextTick(done))
emitter = new EventEmitter()
glob = {
Glob: function (pattern, opts) {
return {
found: patternList[pattern],
statCache: mg.statCache
}
}
}
clock = sinon.useFakeTimers()
// This hack is needed to ensure lodash is using the fake timers
// from sinon
List = proxyquire('../../lib/file-list', {
lodash: _.runInContext(),
helper: helper,
glob: glob,
'graceful-fs': mockFs,
path: pathLib.posix,
bluebird: Promise
})
list = new List(patterns('/some/*.js', '*.txt'), ['/secret/*.txt'], emitter, preprocess, 100)
})
afterEach(() => {
clock.restore()
Promise.setScheduler((fn) => process.nextTick(fn))
})
it('does not add excluded files', () => {
return list.refresh().then((before) => {
return list.addFile('/secret/hello.txt').then((files) => {
expect(files.served).to.be.eql(before.served)
})
})
})
it('does not add already existing files', () => {
return list.refresh().then((before) => {
return list.addFile('/some/a.js').then((files) => {
expect(files.served).to.be.eql(before.served)
})
})
})
it('does not add unmatching files', () => {
return list.refresh().then((before) => {
return list.addFile('/some/a.ex').then((files) => {
expect(files.served).to.be.eql(before.served)
})
})
})
it('adds the file to the correct bucket', () => {
return list.refresh().then((before) => {
return list.addFile('/some/d.js').then((files) => {
expect(pathsFrom(files.served)).to.contain('/some/d.js')
const bucket = list.buckets.get('/some/*.js')
expect(pathsFrom(bucket)).to.contain('/some/d.js')
})
})
})
it('fires "file_list_modified"', () => {
const modified = sinon.stub()
emitter.on('file_list_modified', modified)
return list.refresh().then(() => {
modified.resetHistory()
return list.addFile('/some/d.js').then(() => {
clock.tick(101)
expect(modified).to.have.been.calledOnce
})
})
})
it('ignores quick double "add"', () => {
// On linux fs.watch (chokidar with usePolling: false) fires "add" event twice.
// This checks that we only stat and preprocess the file once.
return list.refresh().then(() => {
preprocess.resetHistory()
sinon.spy(mockFs, 'stat')
return Promise.all([
list.addFile('/some/d.js'),
list.addFile('/some/d.js')
]).then(() => {
expect(preprocess).to.have.been.calledOnce
expect(mockFs.stat).to.have.been.calledOnce
})
})
})
it('sets the proper mtime of the new file', () => {
list = new List(patterns('/a.*'), [], emitter, preprocess)
return list.refresh().then(() => {
return list.addFile('/a.js').then((files) => {
expect(findFile('/a.js', files.served).mtime).to.eql(new Date('2012-01-01'))
})
})
})
it('preprocesses the added file', () => {
// MATCH: /a.txt
list = new List(patterns('/a.*'), [], emitter, preprocess)
return list.refresh().then((files) => {
preprocess.resetHistory()
return list.addFile('/a.js').then(() => {
expect(preprocess).to.have.been.calledOnce
expect(preprocess.args[0][0].originalPath).to.eql('/a.js')
})
})
})
})
describe('changeFile', () => {
let clock = null
beforeEach(() => {
patternList = PATTERN_LIST
mg = MG
Promise.setScheduler((fn) => fn())
preprocess = sinon.spy((file, done) => process.nextTick(done))
emitter = new EventEmitter()
glob = {
Glob: function (pattern, opts) {
return {
found: patternList[pattern],
statCache: mg.statCache
}
}
}
clock = sinon.useFakeTimers()
// This hack is needed to ensure lodash is using the fake timers
// from sinon
List = proxyquire('../../lib/file-list', {
lodash: _.runInContext(),
helper: helper,
glob: glob,
'graceful-fs': mockFs,
path: pathLib.posix,
bluebird: Promise
})
mockFs._touchFile('/some/a.js', '2012-04-04')
mockFs._touchFile('/some/b.js', '2012-05-05')
})
afterEach(() => {
clock.restore()
Promise.setScheduler((fn) => process.nextTick(fn))
})
it('updates mtime and fires "file_list_modified"', () => {
// MATCH: /some/a.js, /some/b.js
list = new List(patterns('/some/*.js', '/a.*'), [], emitter, preprocess, 100)
const modified = sinon.stub()
emitter.on('file_list_modified', modified)
return list.refresh().then((files) => {
mockFs._touchFile('/some/b.js', '2020-01-01')
modified.resetHistory()
return list.changeFile('/some/b.js').then((files) => {
clock.tick(101)
expect(modified).to.have.been.calledOnce
expect(findFile('/some/b.js', files.served).mtime).to.be.eql(new Date('2020-01-01'))
})
})
})
it('does not fire "file_list_modified" if no matching file is found', () => {
// MATCH: /some/a.js
list = new List(patterns('/some/*.js', '/a.*'), ['/some/b.js'], emitter, preprocess)
const modified = sinon.stub()
emitter.on('file_list_modified', modified)
return list.refresh().then((files) => {
mockFs._touchFile('/some/b.js', '2020-01-01')
modified.resetHistory()
return list.changeFile('/some/b.js').then(() => {
expect(modified).to.not.have.been.called
})
})
})
it('fire "file_list_modified" if force is true even if mtime has not changed', () => {
// MATCH: /some/a.js, /some/b.js, /a.txt
list = new List(patterns('/some/*.js', '/a.*'), [], emitter, preprocess)
var modified = sinon.stub()
emitter.on('file_list_modified', modified)
return list.refresh().then((files) => {
// not touching the file, stat will return still the same
modified.reset()
return list.changeFile('/some/b.js', true).then(() => {
expect(modified).to.have.been.calledOnce
})
})
})
it('does not fire "file_list_modified" if mtime has not changed', () => {
// chokidar on fucking windows sometimes fires event multiple times
// MATCH: /some/a.js, /some/b.js, /a.txt
list = new List(patterns('/some/*.js', '/a.*'), [], emitter, preprocess)
const modified = sinon.stub()
emitter.on('file_list_modified', modified)
return list.refresh().then((files) => {
// not touching the file, stat will return still the same
modified.resetHistory()
return list.changeFile('/some/b.js').then(() => {
expect(modified).not.to.have.been.called
})
})
})
it('preprocesses the changed file', () => {
// MATCH: /some/a.js, /some/b.js
list = new List(patterns('/some/*.js', '/a.*'), [], emitter, preprocess)
return list.refresh().then((files) => {
preprocess.resetHistory()
mockFs._touchFile('/some/a.js', '2020-01-01')
return list.changeFile('/some/a.js').then(() => {
expect(preprocess).to.have.been.called
expect(preprocess.lastCall.args[0]).to.have.property('path', '/some/a.js')
})
})
})
})
describe('removeFile', () => {
let clock = null
beforeEach(() => {
patternList = PATTERN_LIST
mg = MG
Promise.setScheduler((fn) => fn())
preprocess = sinon.spy((file, done) => process.nextTick(done))
emitter = new EventEmitter()
glob = {
Glob: function (pattern, opts) {
return {
found: patternList[pattern],
statCache: mg.statCache
}
}
}
clock = sinon.useFakeTimers()
// This hack is needed to ensure lodash is using the fake timers
// from sinon
List = proxyquire('../../lib/file-list', {
lodash: _.runInContext(),
helper: helper,
glob: glob,
'graceful-fs': mockFs,
path: pathLib.posix,
bluebird: Promise
})
modified = sinon.stub()
emitter.on('file_list_modified', modified)
})
afterEach(() => {
clock.restore()
Promise.setScheduler((fn) => process.nextTick(fn))
})
it('removes the file from the list and fires "file_list_modified"', () => {
// MATCH: /some/a.js, /some/b.js, /a.txt
list = new List(patterns('/some/*.js', '/a.*'), [], emitter, preprocess, 100)
const modified = sinon.stub()
emitter.on('file_list_modified', modified)
return list.refresh().then((files) => {
modified.resetHistory()
return list.removeFile('/some/a.js')
}).then((files) => {
expect(pathsFrom(files.served)).to.be.eql([
'/some/b.js',
'/a.txt'
])
clock.tick(101)
expect(modified).to.have.been.calledOnce
})
})
it('does not fire "file_list_modified" if the file is not in the list', () => {
// MATCH: /some/a.js, /some/b.js, /a.txt
list = new List(patterns('/some/*.js', '/a.*'), [], emitter, preprocess)
return list.refresh().then((files) => {
modified.resetHistory()
return list.removeFile('/a.js').then(() => {
expect(modified).to.not.have.been.called
})
})
})
})
describe('batch interval', () => {
// IMPORTANT: When writing tests for debouncing behaviour, you must wait for the promise
// returned by list.changeFile or list.addFile. list.removeFile calls self._emitModified()
// in a different manner and doesn't *need* to be waited on. If you use this behaviour
// in your tests it can can lead to very confusing results when they are modified or
// extended.
//
// Rule of thumb: Always wait on the promises returned by list.addFile, list.changeFile,
// and list.removeFile.
let clock = null
beforeEach(() => {
patternList = PATTERN_LIST
mg = MG
Promise.setScheduler((fn) => { fn() })
preprocess = sinon.spy((file, done) => process.nextTick(done))
emitter = new EventEmitter()
glob = {
Glob: function (pattern, opts) {
return {
found: patternList[pattern],
statCache: mg.statCache
}
}
}
modified = sinon.stub()
emitter.on('file_list_modified', modified)
clock = sinon.useFakeTimers()
// This hack is needed to ensure lodash is using the fake timers
// from sinon
List = proxyquire('../../lib/file-list', {
lodash: _.runInContext(),
helper: helper,
glob: glob,
'graceful-fs': mockFs,
path: pathLib.posix,
bluebird: Promise
})
})
afterEach(() => {
clock.restore()
Promise.setScheduler((fn) => process.nextTick(fn))
})
it('debounces calls to emitModified', () => {
list = new List(patterns(), [], emitter, preprocess, 100)
return list.refresh().then(() => {
modified.resetHistory()
list._emitModified()
clock.tick(99)
expect(modified).to.not.have.been.called
list._emitModified()
clock.tick(2)
expect(modified).to.not.have.been.called
clock.tick(97)
expect(modified).to.not.have.been.called
clock.tick(2)
expect(modified).to.have.been.calledOnce
clock.tick(1000)
expect(modified).to.have.been.calledOnce
list._emitModified()
clock.tick(99)
expect(modified).to.have.been.calledOnce
clock.tick(2)
expect(modified).to.have.been.calledTwice
})
})
it('debounces a single file change', () => {
list = new List(patterns('/some/*.js', '/a.*'), [], emitter, preprocess, 100)
return list.refresh().then((files) => {
modified.resetHistory()
// Even with no changes, all these files are served
list.addFile('/some/0.js').then(() => {
clock.tick(99)
expect(modified).to.not.have.been.called
clock.tick(2)
expect(modified).to.have.been.calledOnce
files = modified.lastCall.args[0]
expect(pathsFrom(files.served)).to.be.eql([
'/some/0.js',
'/some/a.js',
'/some/b.js',
'/a.txt'
])
})
})
})
it('debounces several changes to a file', () => {
list = new List(patterns('/some/*.js', '/a.*'), [], emitter, preprocess, 100)
return list.refresh().then((files) => {
modified.resetHistory()
list.addFile('/some/0.js').then(() => {
clock.tick(99)
expect(modified).to.not.have.been.called
// Modify file, must change mtime too, or change is ignored
mockFs._touchFile('/some/0.js', '2020-01-01')
list.changeFile('/some/0.js').then(() => {
// Ensure that the debounce timer was reset
clock.tick(2)
expect(modified).to.not.have.been.called
// Ensure that debounce timer fires after 100ms
clock.tick(99)
expect(modified).to.have.been.calledOnce
// Make sure there aren't any lingering debounce calls
clock.tick(1000)
// Modify file (one hour later mtime)
expect(modified).to.have.been.calledOnce
mockFs._touchFile('/some/0.js', '2020-01-02')
list.changeFile('/some/0.js').then(() => {
clock.tick(99)
expect(modified).to.have.been.calledOnce
clock.tick(2)
expect(modified).to.have.been.calledTwice
// Make sure there aren't any lingering calls
clock.tick(1000)
expect(modified).to.have.been.calledTwice
})
})
})
})
})
it('debounces multiple changes until there is quiescence', () => {
// MATCH: /some/a.js, /some/b.js, /a.txt
list = new List(patterns('/some/*.js', '/a.*'), [], emitter, preprocess, 100)
return list.refresh().then((files) => {
modified.resetHistory()
mockFs._touchFile('/some/b.js', '2020-01-01')
list.changeFile('/some/b.js')
list.removeFile('/some/a.js') // /some/b.js, /a.txt
list.removeFile('/a.txt') // /some/b.js
list.addFile('/a.txt') // /some/b.js, /a.txt
list.addFile('/some/0.js').then(() => {
// /some/0.js, /some/b.js, /a.txt
clock.tick(99)
expect(modified).to.not.have.been.called
mockFs._touchFile('/a.txt', '2020-01-01')
list.changeFile('/a.txt').then(() => {
clock.tick(2)
expect(modified).to.not.have.been.called
clock.tick(100)
expect(modified).to.have.been.calledOnce
clock.tick(1000)
expect(modified).to.have.been.calledOnce
files = modified.lastCall.args[0]
expect(pathsFrom(files.served)).to.be.eql([
'/some/0.js',
'/some/b.js',
'/a.txt'
])
})
})
})
})
it('waits while file preprocessing, if the file was deleted and immediately added', () => {
list = new List(patterns('/a.*'), [], emitter, preprocess, 100)
list.refresh().then((files) => {
preprocess.resetHistory()
modified.resetHistory()
// Remove and then immediately add file to the bucket
list.removeFile('/a.txt')
list.addFile('/a.txt')
clock.tick(99)
expect(preprocess).to.not.have.been.called
const promise = new Promise((resolve) => {
emitter.once('file_list_modified', () => _.defer(() => {
resolve()
}))
})
clock.tick(2)
return promise
.then(() => {
return new Promise((resolve) => {
_.defer(() => {
resolve()
})
})
})
.then(() => {
expect(preprocess).to.have.been.calledOnce
})
})
})
})
})