-
-
Notifications
You must be signed in to change notification settings - Fork 157
/
fileOperations.js
917 lines (676 loc) · 21.6 KB
/
fileOperations.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
'use strict';
var expect = require('expect');
var os = require('os');
var fs = require('graceful-fs');
var del = require('del');
var path = require('path');
var File = require('vinyl');
var buffer = require('buffer');
var defaultResolution = require('default-resolution');
var fo = require('../lib/fileOperations');
var closeFd = fo.closeFd;
var isOwner = fo.isOwner;
var writeFile = fo.writeFile;
var getModeDiff = fo.getModeDiff;
var getTimesDiff = fo.getTimesDiff;
var updateMetadata = fo.updateMetadata;
var resolution = defaultResolution();
var MASK_MODE = parseInt('7777', 8);
function masked(mode) {
return mode & MASK_MODE;
}
function noop() {}
var isWindows = (os.platform() === 'win32');
describe('isOwner', function() {
var ownerStat = {
uid: 9001,
};
var nonOwnerStat = {
uid: 9002,
};
var getuidSpy;
var geteuidSpy;
beforeEach(function(done) {
if (typeof process.geteuid !== 'function') {
process.geteuid = noop;
}
// Windows :(
if (typeof process.getuid !== 'function') {
process.getuid = noop;
}
getuidSpy = expect.spyOn(process, 'getuid').andReturn(ownerStat.uid);
geteuidSpy = expect.spyOn(process, 'geteuid').andReturn(ownerStat.uid);
done();
});
afterEach(function(done) {
expect.restoreSpies();
if (process.geteuid === noop) {
delete process.geteuid;
}
// Windows :(
if (process.getuid === noop) {
delete process.getuid;
}
done();
});
// TODO: test for having neither
it('uses process.geteuid() when available', function(done) {
isOwner(ownerStat);
expect(getuidSpy.calls.length).toEqual(0);
expect(geteuidSpy.calls.length).toEqual(1);
done();
});
it('uses process.getuid() when geteuid() is not available', function(done) {
delete process.geteuid;
isOwner(ownerStat);
expect(getuidSpy.calls.length).toEqual(1);
done();
});
it('returns false when non-root and non-owner', function(done) {
var result = isOwner(nonOwnerStat);
expect(result).toEqual(false);
done();
});
it('returns true when owner and non-root', function(done) {
var result = isOwner(ownerStat);
expect(result).toEqual(true);
done();
});
it('returns true when non-owner but root', function(done) {
expect.spyOn(process, 'geteuid').andReturn(0); // 0 is root uid
var result = isOwner(nonOwnerStat);
expect(result).toEqual(true);
done();
});
});
describe('getModeDiff', function() {
it('returns 0 if both modes are the same', function(done) {
var fsMode = parseInt('777', 8);
var vfsMode = parseInt('777', 8);
var result = getModeDiff(fsMode, vfsMode);
expect(result).toEqual(0);
done();
});
it('returns 0 if vinyl mode is not a number', function(done) {
var fsMode = parseInt('777', 8);
var vfsMode = undefined;
var result = getModeDiff(fsMode, vfsMode);
expect(result).toEqual(0);
done();
});
it('returns a value greater than 0 if modes are different', function(done) {
var fsMode = parseInt('777', 8);
var vfsMode = parseInt('744', 8);
var result = getModeDiff(fsMode, vfsMode);
expect(result).toEqual(27);
done();
});
it('does not matter the order of diffing', function(done) {
var fsMode = parseInt('655', 8);
var vfsMode = parseInt('777', 8);
var result = getModeDiff(fsMode, vfsMode);
expect(result).toEqual(82);
done();
});
it('includes the sticky/setuid/setgid bits', function(done) {
var fsMode = parseInt('1777', 8);
var vfsMode = parseInt('4777', 8);
var result = getModeDiff(fsMode, vfsMode);
expect(result).toEqual(fsMode ^ vfsMode);
done();
});
});
describe('getTimesDiff', function() {
it('returns undefined if vinyl mtime is not a valid date', function(done) {
var fsStat = {
mtime: new Date(),
};
var vfsStat = {
mtime: new Date(undefined),
};
var result = getTimesDiff(fsStat, vfsStat);
expect(result).toEqual(undefined);
done();
});
it('returns undefined if vinyl mtime & atime are both equal to counterparts', function(done) {
var now = Date.now();
var fsStat = {
mtime: new Date(now),
atime: new Date(now),
};
var vfsStat = {
mtime: new Date(now),
atime: new Date(now),
};
var result = getTimesDiff(fsStat, vfsStat);
expect(result).toEqual(undefined);
done();
});
// TODO: is this proper/expected?
it('returns undefined if vinyl mtimes equals the counterpart and atimes are null', function(done) {
var now = Date.now();
var fsStat = {
mtime: new Date(now),
atime: null,
};
var vfsStat = {
mtime: new Date(now),
atime: null,
};
var result = getTimesDiff(fsStat, vfsStat);
expect(result).toEqual(undefined);
done();
});
it('returns a diff object if mtimes do not match', function(done) {
var now = Date.now();
var then = now - 1000;
var fsStat = {
mtime: new Date(now),
};
var vfsStat = {
mtime: new Date(then),
};
var expected = {
mtime: new Date(then),
atime: undefined,
};
var result = getTimesDiff(fsStat, vfsStat);
expect(result).toEqual(expected);
done();
});
it('returns a diff object if atimes do not match', function(done) {
var now = Date.now();
var then = now - 1000;
var fsStat = {
mtime: new Date(now),
atime: new Date(now),
};
var vfsStat = {
mtime: new Date(now),
atime: new Date(then),
};
var expected = {
mtime: new Date(now),
atime: new Date(then),
};
var result = getTimesDiff(fsStat, vfsStat);
expect(result).toEqual(expected);
done();
});
it('returns the fs atime if the vinyl atime is invalid', function(done) {
var now = Date.now();
var fsStat = {
mtime: new Date(now),
atime: new Date(now),
};
var vfsStat = {
mtime: new Date(now),
atime: new Date(undefined),
};
var expected = {
mtime: new Date(now),
atime: new Date(now),
};
var result = getTimesDiff(fsStat, vfsStat);
expect(result).toEqual(expected);
done();
});
// TODO: is this proper/expected?
it('makes atime diff undefined if fs and vinyl atime are invalid', function(done) {
var now = Date.now();
var fsStat = {
mtime: new Date(now),
atime: new Date(undefined),
};
var vfsStat = {
mtime: new Date(now),
atime: new Date(undefined),
};
var expected = {
mtime: new Date(now),
atime: undefined,
};
var result = getTimesDiff(fsStat, vfsStat);
expect(result).toEqual(expected);
done();
});
});
describe('closeFd', function() {
it('calls the callback with propagated error if fd is not a number', function(done) {
var propagatedError = new Error();
closeFd(propagatedError, null, function(err) {
expect(err).toEqual(propagatedError);
done();
});
});
it('calls the callback with close error if no error to propagate', function(done) {
closeFd(null, -1, function(err) {
expect(err).toExist();
done();
});
});
it('calls the callback with propagated error if close errors', function(done) {
var propagatedError = new Error();
closeFd(propagatedError, -1, function(err) {
expect(err).toEqual(propagatedError);
done();
});
});
it('calls the callback with propagated error if close succeeds', function(done) {
var propagatedError = new Error();
var fd = fs.openSync(path.join(__dirname, './fixtures/test.coffee'), 'r');
var spy = expect.spyOn(fs, 'close').andCallThrough();
closeFd(propagatedError, fd, function(err) {
spy.restore();
expect(spy.calls.length).toEqual(1);
expect(err).toEqual(propagatedError);
done();
});
});
it('calls the callback with no error if close succeeds & no propagated error', function(done) {
var fd = fs.openSync(path.join(__dirname, './fixtures/test.coffee'), 'r');
var spy = expect.spyOn(fs, 'close').andCallThrough();
closeFd(null, fd, function(err) {
spy.restore();
expect(spy.calls.length).toEqual(1);
expect(err).toEqual(undefined);
done();
});
});
});
describe('writeFile', function() {
var filepath;
beforeEach(function(done) {
filepath = path.join(__dirname, './fixtures/writeFile.txt');
done();
});
afterEach(function() {
// Async del to get sort-of-fix for https://github.com/isaacs/rimraf/issues/72
return del(filepath);
});
it('writes a file to the filesystem, does not close and returns the fd', function(done) {
var expected = 'test';
var content = new Buffer(expected);
writeFile(filepath, content, function(err, fd) {
expect(err).toNotExist();
expect(typeof fd === 'number').toEqual(true);
fs.close(fd, function() {
var written = fs.readFileSync(filepath, 'utf-8');
expect(written).toEqual(expected);
done();
});
});
});
it('defaults to writing files with 0666 mode', function(done) {
var expected = parseInt('0666', 8) & (~process.umask());
var content = new Buffer('test');
writeFile(filepath, content, function(err, fd) {
expect(err).toNotExist();
expect(typeof fd === 'number').toEqual(true);
fs.close(fd, function() {
var stats = fs.lstatSync(filepath);
expect(masked(stats.mode)).toEqual(expected);
done();
});
});
});
it('accepts a different mode in options', function(done) {
if (isWindows) {
console.log('Changing the mode of a file is not supported by node.js in Windows.');
this.skip();
return;
}
var expected = parseInt('0777', 8) & (~process.umask());
var content = new Buffer('test');
var options = {
mode: parseInt('0777', 8),
};
writeFile(filepath, content, options, function(err, fd) {
expect(err).toNotExist();
expect(typeof fd === 'number').toEqual(true);
fs.close(fd, function() {
var stats = fs.lstatSync(filepath);
expect(masked(stats.mode)).toEqual(expected);
done();
});
});
});
it('defaults to opening files with write flag', function(done) {
var content = new Buffer('test');
writeFile(filepath, content, function(err, fd) {
expect(err).toNotExist();
expect(typeof fd === 'number').toEqual(true);
fs.read(fd, new Buffer(4), 0, 4, 0, function(readErr) {
expect(readErr).toExist();
fs.close(fd, done);
});
});
});
it('accepts a different flag in options', function(done) {
var expected = 'test';
var content = new Buffer(expected);
var options = {
flag: 'w+',
};
writeFile(filepath, content, options, function(err, fd) {
expect(err).toNotExist();
expect(typeof fd === 'number').toEqual(true);
fs.read(fd, new Buffer(4), 0, 4, 0, function(readErr, _, written) {
expect(readErr).toNotExist();
expect(written.toString()).toEqual(expected);
fs.close(fd, done);
});
});
});
it('appends to a file if append flag is given', function(done) {
var initial = 'test';
var toWrite = '-a-thing';
fs.writeFileSync(filepath, initial, 'utf-8');
var expected = initial + toWrite;
var content = new Buffer(toWrite);
var options = {
flag: 'a',
};
writeFile(filepath, content, options, function(err, fd) {
expect(err).toNotExist();
expect(typeof fd === 'number').toEqual(true);
fs.close(fd, function() {
var written = fs.readFileSync(filepath, 'utf-8');
expect(written).toEqual(expected);
done();
});
});
});
it('does not pass a file descriptor if open call errors', function(done) {
filepath = path.join(__dirname, './not-exist-dir/writeFile.txt');
var content = new Buffer('test');
writeFile(filepath, content, function(err, fd) {
expect(err).toExist();
expect(typeof fd === 'number').toEqual(false);
done();
});
});
it('passes a file descriptor if write call errors', function(done) {
var existsFilepath = path.join(__dirname, './fixtures/test.coffee'); // File must exist
var content = new Buffer('test');
var options = {
flag: 'r',
};
writeFile(existsFilepath, content, options, function(err, fd) {
expect(err).toExist();
expect(typeof fd === 'number').toEqual(true);
fs.close(fd, done);
});
});
it('passes an error if called with string as data', function(done) {
writeFile(filepath, 'test', function(err) {
expect(err).toExist();
done();
});
});
it('does not error on SlowBuffer', function(done) {
if (!buffer.SlowBuffer) {
this.skip();
return;
}
var expected = 'test';
var buf = new Buffer(expected);
var content = new buffer.SlowBuffer(4);
buf.copy(content, 0, 0, 4);
writeFile(filepath, content, function(err, fd) {
expect(err).toNotExist();
expect(typeof fd === 'number').toEqual(true);
fs.close(fd, function() {
var written = fs.readFileSync(filepath, 'utf-8');
expect(written).toEqual(expected);
done();
});
});
});
it('does not error if options is falsey', function(done) {
var content = new Buffer('test');
writeFile(filepath, content, null, function(err, fd) {
expect(err).toNotExist();
expect(typeof fd === 'number').toEqual(true);
fs.close(fd, done);
});
});
});
describe('updateMetadata', function() {
var inputPath = path.join(__dirname, './fixtures/stats.txt');
var file;
beforeEach(function(done) {
file = new File({
base: __dirname,
cwd: __dirname,
path: inputPath,
contents: null,
stat: {
},
});
done();
});
afterEach(function(done) {
expect.restoreSpies();
del.sync(inputPath);
if (process.geteuid === noop) {
delete process.geteuid;
}
done();
});
it('passes the error and file descriptor if fstat fails', function(done) {
if (isWindows) {
console.log('Changing the time of a directory errors in Windows.');
console.log('Changing the mode of a file is not supported by node.js in Windows.');
console.log('Windows is treated as though it does not have permission to make these operations.');
this.skip();
return;
}
var fd = 9001;
updateMetadata(fd, file, function(err, fd2) {
expect(err).toExist();
expect(typeof fd === 'number').toEqual(true);
expect(fd2).toEqual(fd);
done();
});
});
it('updates the vinyl object with fs stats', function(done) {
if (isWindows) {
this.skip();
return;
}
var fd = fs.openSync(inputPath, 'w+');
var stats = fs.fstatSync(fd);
updateMetadata(fd, file, function(err, fd2) {
// Not sure why .toEqual doesn't match these
Object.keys(file.stat).forEach(function(key) {
expect(file.stat[key]).toEqual(stats[key]);
});
fs.close(fd2, done);
});
});
it('does not touch the fs if nothing to update', function(done) {
if (isWindows) {
this.skip();
return;
}
var fchmodSpy = expect.spyOn(fs, 'fchmod').andCallThrough();
var futimesSpy = expect.spyOn(fs, 'futimes').andCallThrough();
var fd = fs.openSync(inputPath, 'w+');
updateMetadata(fd, file, function(err, fd2) {
expect(fchmodSpy.calls.length).toEqual(0);
expect(futimesSpy.calls.length).toEqual(0);
fs.close(fd2, done);
});
});
it('does not touch the fs if process is not owner of the file', function(done) {
if (isWindows) {
this.skip();
return;
}
if (typeof process.geteuid !== 'function') {
process.geteuid = noop;
}
expect.spyOn(process, 'geteuid').andReturn(9002);
var fchmodSpy = expect.spyOn(fs, 'fchmod').andCallThrough();
var futimesSpy = expect.spyOn(fs, 'futimes').andCallThrough();
file.stat.mtime = new Date(Date.now() - 1000);
var fd = fs.openSync(inputPath, 'w+');
updateMetadata(fd, file, function(err, fd2) {
expect(fchmodSpy.calls.length).toEqual(0);
expect(futimesSpy.calls.length).toEqual(0);
fs.close(fd2, done);
});
});
it('updates times on fs and vinyl object if there is a diff', function(done) {
if (isWindows) {
this.skip();
return;
}
var futimesSpy = expect.spyOn(fs, 'futimes').andCallThrough();
var now = Date.now();
var then = now - 1000;
file.stat.mtime = new Date(then);
file.stat.atime = new Date(then);
var fd = fs.openSync(inputPath, 'w+');
updateMetadata(fd, file, function(err, fd2) {
expect(futimesSpy.calls.length).toEqual(1);
var stats = fs.fstatSync(fd);
var mtimeMs = Date.parse(file.stat.mtime);
var mtime = resolution ? mtimeMs - (mtimeMs % resolution) : mtimeMs;
var atimeMs = Date.parse(file.stat.atime);
var atime = resolution ? atimeMs - (atimeMs % resolution) : atimeMs;
expect(file.stat.mtime).toEqual(new Date(then));
expect(mtime).toEqual(Date.parse(stats.mtime));
expect(file.stat.atime).toEqual(new Date(then));
expect(atime).toEqual(Date.parse(stats.atime));
fs.close(fd2, done);
});
});
it('forwards futimes error and descriptor upon error', function(done) {
if (isWindows) {
this.skip();
return;
}
var futimesSpy = expect.spyOn(fs, 'futimes').andCall(function(fd, atime, mtime, cb) {
cb(new Error('mocked error'));
});
var now = Date.now();
var then = now - 1000;
file.stat.mtime = new Date(then);
file.stat.atime = new Date(then);
var fd = fs.openSync(inputPath, 'w+');
updateMetadata(fd, file, function(err, fd2) {
expect(err).toExist();
expect(futimesSpy.calls.length).toEqual(1);
expect(typeof fd2 === 'number').toEqual(true);
fs.close(fd2, done);
});
});
it('updates the mode on fs and vinyl object if there is a diff', function(done) {
if (isWindows) {
this.skip();
return;
}
var fchmodSpy = expect.spyOn(fs, 'fchmod').andCallThrough();
var mode = parseInt('777', 8);
file.stat.mode = mode;
var fd = fs.openSync(inputPath, 'w+');
updateMetadata(fd, file, function(err, fd2) {
expect(fchmodSpy.calls.length).toEqual(1);
var stats = fs.fstatSync(fd);
expect(file.stat.mode).toEqual(stats.mode);
fs.close(fd2, done);
});
});
it('updates the sticky bit on mode on fs and vinyl object if there is a diff', function(done) {
if (isWindows) {
this.skip();
return;
}
var fchmodSpy = expect.spyOn(fs, 'fchmod').andCallThrough();
var mode = parseInt('1777', 8);
file.stat.mode = mode;
var fd = fs.openSync(inputPath, 'w+');
updateMetadata(fd, file, function(err, fd2) {
expect(fchmodSpy.calls.length).toEqual(1);
var stats = fs.fstatSync(fd);
expect(file.stat.mode).toEqual(stats.mode);
fs.close(fd2, done);
});
});
it('forwards fchmod error and descriptor upon error', function(done) {
if (isWindows) {
this.skip();
return;
}
var mode = parseInt('777', 8);
file.stat.mode = mode;
var fd = fs.openSync(inputPath, 'w+');
var fchmodSpy = expect.spyOn(fs, 'fchmod').andCall(function(fd, mode, cb) {
cb(new Error('mocked error'));
});
updateMetadata(fd, file, function(err, fd2) {
expect(err).toExist();
expect(fchmodSpy.calls.length).toEqual(1);
expect(typeof fd2 === 'number').toEqual(true);
fs.close(fd2, done);
});
});
it('updates the mode & times on fs and vinyl object if there is a diff', function(done) {
if (isWindows) {
this.skip();
return;
}
var fchmodSpy = expect.spyOn(fs, 'fchmod').andCallThrough();
var futimesSpy = expect.spyOn(fs, 'futimes').andCallThrough();
var mode = parseInt('777', 8);
file.stat.mode = mode;
var now = Date.now();
var then = now - 1000;
file.stat.mtime = new Date(then);
file.stat.atime = new Date(then);
var fd = fs.openSync(inputPath, 'w+');
updateMetadata(fd, file, function(err, fd2) {
expect(fchmodSpy.calls.length).toEqual(1);
expect(futimesSpy.calls.length).toEqual(1);
var stats = fs.fstatSync(fd);
var mtimeMs = Date.parse(file.stat.mtime);
var mtime = resolution ? mtimeMs - (mtimeMs % resolution) : mtimeMs;
var atimeMs = Date.parse(file.stat.atime);
var atime = resolution ? atimeMs - (atimeMs % resolution) : atimeMs;
expect(file.stat.mtime).toEqual(new Date(then));
expect(mtime).toEqual(Date.parse(stats.mtime));
expect(file.stat.atime).toEqual(new Date(then));
expect(atime).toEqual(Date.parse(stats.atime));
expect(file.stat.mode).toEqual(stats.mode);
fs.close(fd2, done);
});
});
it('forwards fchmod error and descriptor through futimes if there is a time diff', function(done) {
if (isWindows) {
this.skip();
return;
}
var expectedErr = new Error('mocked error');
var fchmodSpy = expect.spyOn(fs, 'fchmod').andCall(function(fd, mode, cb) {
cb(expectedErr);
});
var futimesSpy = expect.spyOn(fs, 'futimes').andCallThrough();
var mode = parseInt('777', 8);
file.stat.mode = mode;
var now = Date.now();
var then = now - 1000;
file.stat.mtime = new Date(then);
file.stat.atime = new Date(then);
var fd = fs.openSync(inputPath, 'w');
updateMetadata(fd, file, function(err, fd2) {
expect(err).toExist();
expect(err).toEqual(expectedErr);
expect(fchmodSpy.calls.length).toEqual(1);
expect(futimesSpy.calls.length).toEqual(1);
expect(typeof fd2 === 'number').toEqual(true);
fs.close(fd2, done);
});
});
});