fs.tap.js
26.6 KB
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
'use strict';
var createNamespace = require('../context.js').createNamespace
, fs = require('fs')
, path = require('path')
, exec = require('child_process').exec
, tap = require('tap')
, test = tap.test
;
// CONSTANTS
var FILENAME = '__testfile'
, DIRNAME = '__TESTDIR'
, LINKNAME = '__testlink'
, HARDLINKNAME = '__testhardlink'
;
function createFile(assert) {
var contents = new Buffer("UHOH")
, file = fs.openSync(FILENAME, 'w')
, written = fs.writeSync(file, contents, 0, contents.length, 0)
;
assert.equals(written, contents.length, "whole buffer was written");
var rc = fs.closeSync(file);
// need this here to avoid dealing with umask complications
fs.chmodSync(FILENAME, '0666');
return rc;
}
function deleteFile() { return fs.unlinkSync(FILENAME); }
function createLink(assert) {
createFile(assert);
fs.symlinkSync(FILENAME, LINKNAME);
if (fs.lchmodSync) {
// This function only exists on BSD systems (like OSX)
fs.lchmodSync(LINKNAME, '0777');
}
}
function deleteLink() {
fs.unlinkSync(LINKNAME);
return deleteFile();
}
function createDirectory(assert) {
fs.mkdirSync(DIRNAME);
assert.ok(fs.existsSync(DIRNAME), "directory was created");
}
function deleteDirectory() { return fs.rmdirSync(DIRNAME); }
function mapIds(username, groupname, callback) {
if (!callback) throw new Error("mapIds requires callback");
if (!username) return callback(new Error("mapIds requires username"));
if (!groupname) return callback(new Error("mapIds requires groupname"));
exec('id -u ' + username, function (error, stdout, stderr) {
if (error) return callback(error);
if (stderr) return callback(new Error(stderr));
var uid = +stdout;
exec('id -g ' + groupname, function (error, stdout, stderr) {
if (error) return callback(error);
if (stderr) return callback(new Error(stderr));
var gid = +stdout;
callback(null, uid, gid);
});
});
}
test("continuation-local state with MakeCallback and fs module", function (t) {
t.plan(33);
var namespace = createNamespace('fs');
namespace.run(function () {
namespace.set('test', 0xabad1dea);
t.test("fs.rename", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'rename');
t.equal(namespace.get('test'), 'rename', "state has been mutated");
fs.rename(FILENAME, '__renamed', function (error) {
t.notOk(error, "renaming shouldn't error");
t.equal(namespace.get('test'), 'rename',
"mutated state has persisted to fs.rename's callback");
fs.unlinkSync('__renamed');
t.end();
});
});
});
t.test("fs.truncate", function (t) {
// truncate -> ftruncate in Node > 0.8.x
if (!fs.ftruncate) return t.end();
createFile(t);
namespace.run(function () {
namespace.set('test', 'truncate');
t.equal(namespace.get('test'), 'truncate', "state has been mutated");
fs.truncate(FILENAME, 0, function (error) {
t.notOk(error, "truncation shouldn't error");
var stats = fs.statSync(FILENAME);
t.equal(stats.size, 0, "file has been truncated");
t.equal(namespace.get('test'), 'truncate',
"mutated state has persisted to fs.truncate's callback");
deleteFile();
t.end();
});
});
});
t.test("fs.ftruncate", function (t) {
createFile(t);
// truncate -> ftruncate in Node > 0.8.x
var truncate = fs.ftruncate ? fs.ftruncate : fs.truncate;
namespace.run(function () {
namespace.set('test', 'ftruncate');
t.equal(namespace.get('test'), 'ftruncate', "state has been mutated");
var file = fs.openSync(FILENAME, 'w');
truncate(file, 0, function (error) {
t.notOk(error, "truncation shouldn't error");
fs.closeSync(file);
var stats = fs.statSync(FILENAME);
t.equal(stats.size, 0, "file has been truncated");
t.equal(namespace.get('test'), 'ftruncate',
"mutated state has persisted to fs.ftruncate's callback");
deleteFile();
t.end();
});
});
});
t.test("fs.chown", function (t) {
createFile(t);
mapIds('daemon', 'daemon', function (error, uid, gid) {
t.notOk(error, "looking up uid & gid shouldn't error");
t.ok(uid, "uid for daemon was found");
t.ok(gid, "gid for daemon was found");
namespace.run(function () {
namespace.set('test', 'chown');
t.equal(namespace.get('test'), 'chown', "state has been mutated");
fs.chown(FILENAME, uid, gid, function (error) {
t.ok(error, "changing ownership will error for non-root users");
t.equal(namespace.get('test'), 'chown',
"mutated state has persisted to fs.chown's callback");
deleteFile();
t.end();
});
});
});
});
t.test("fs.fchown", function (t) {
createFile(t);
mapIds('daemon', 'daemon', function (error, uid, gid) {
t.notOk(error, "looking up uid & gid shouldn't error");
t.ok(uid, "uid for daemon was found");
t.ok(gid, "gid for daemon was found");
namespace.run(function () {
namespace.set('test', 'fchown');
t.equal(namespace.get('test'), 'fchown', "state has been mutated");
var file = fs.openSync(FILENAME, 'w');
fs.fchown(file, uid, gid, function (error) {
t.ok(error, "changing ownership will error for non-root users");
t.equal(namespace.get('test'), 'fchown',
"mutated state has persisted to fs.fchown's callback");
fs.closeSync(file);
deleteFile();
t.end();
});
});
});
});
t.test("fs.lchown", function (t) {
if (!fs.lchown) return t.end();
createLink(t);
mapIds('daemon', 'daemon', function (error, uid, gid) {
t.notOk(error, "looking up uid & gid shouldn't error");
t.ok(uid, "uid for daemon was found");
t.ok(gid, "gid for daemon was found");
namespace.run(function () {
namespace.set('test', 'lchown');
t.equal(namespace.get('test'), 'lchown', "state has been mutated");
fs.lchown(LINKNAME, uid, gid, function (error) {
t.ok(error, "changing ownership will error for non-root users");
t.equal(namespace.get('test'), 'lchown',
"mutated state has persisted to fs.lchown's callback");
deleteLink();
t.end();
});
});
});
});
t.test("fs.chmod", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'chmod');
t.equal(namespace.get('test'), 'chmod', "state has been mutated");
fs.chmod(FILENAME, '0700', function (error) {
t.notOk(error, "changing mode shouldn't error");
t.equal(namespace.get('test'), 'chmod',
"mutated state has persisted to fs.chmod's callback");
var stats = fs.statSync(FILENAME);
t.equal(stats.mode.toString(8), '100700', "extra access bits are stripped");
deleteFile();
t.end();
});
});
});
t.test("fs.fchmod", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'fchmod');
t.equal(namespace.get('test'), 'fchmod', "state has been mutated");
var file = fs.openSync(FILENAME, 'w+');
fs.fchmod(file, '0700', function (error) {
t.notOk(error, "changing mode shouldn't error");
t.equal(namespace.get('test'), 'fchmod',
"mutated state has persisted to fs.fchmod's callback");
fs.closeSync(file);
var stats = fs.statSync(FILENAME);
t.equal(stats.mode.toString(8), '100700', "extra access bits are stripped");
deleteFile();
t.end();
});
});
});
t.test("fs.lchmod", function (t) {
if (!fs.lchmod) return t.end();
createLink(t);
namespace.run(function () {
namespace.set('test', 'lchmod');
t.equal(namespace.get('test'), 'lchmod', "state has been mutated");
fs.lchmod(LINKNAME, '0700', function (error) {
t.notOk(error, "changing mode shouldn't error");
t.equal(namespace.get('test'), 'lchmod',
"mutated state has persisted to fs.lchmod's callback");
var stats = fs.lstatSync(LINKNAME);
t.equal(stats.mode.toString(8), '120700', "extra access bits are stripped");
deleteLink();
t.end();
});
});
});
t.test("fs.stat", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'stat');
t.equal(namespace.get('test'), 'stat', "state has been mutated");
fs.stat(FILENAME, function (error, stats) {
t.notOk(error, "reading stats shouldn't error");
t.equal(namespace.get('test'), 'stat',
"mutated state has persisted to fs.stat's callback");
t.equal(stats.mode.toString(8), '100666', "permissions should be as created");
deleteFile();
t.end();
});
});
});
t.test("fs.fstat", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'fstat');
t.equal(namespace.get('test'), 'fstat', "state has been mutated");
var file = fs.openSync(FILENAME, 'r');
fs.fstat(file, function (error, stats) {
t.notOk(error, "reading stats shouldn't error");
t.equal(namespace.get('test'), 'fstat',
"mutated state has persisted to fs.fstat's callback");
t.equal(stats.mode.toString(8), '100666', "permissions should be as created");
fs.closeSync(file);
deleteFile();
t.end();
});
});
});
t.test("fs.lstat", function (t) {
createLink(t);
namespace.run(function () {
namespace.set('test', 'lstat');
t.equal(namespace.get('test'), 'lstat', "state has been mutated");
fs.lstat(LINKNAME, function (error, stats) {
t.notOk(error, "reading stats shouldn't error");
t.equal(namespace.get('test'), 'lstat',
"mutated state has persisted to fs.lstat's callback");
t.equal(
stats.mode.toString(8),
'120777',
"permissions should be as created"
);
deleteLink();
t.end();
});
});
});
t.test("fs.link", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'link');
t.equal(namespace.get('test'), 'link', "state has been mutated");
fs.link(FILENAME, HARDLINKNAME, function (error) {
t.notOk(error, "creating a link shouldn't error");
t.equal(namespace.get('test'), 'link',
"mutated state has persisted to fs.link's callback");
var orig = fs.statSync(FILENAME)
, linked = fs.statSync(HARDLINKNAME)
;
t.equal(orig.ino, linked.ino, "entries should point to same file");
t.notOk(fs.unlinkSync(HARDLINKNAME), "link has been removed");
deleteFile();
t.end();
});
});
});
t.test("fs.symlink", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'symlink');
t.equal(namespace.get('test'), 'symlink', "state has been mutated");
fs.symlink(FILENAME, LINKNAME, function (error) {
t.notOk(error, "creating a symlink shouldn't error");
t.equal(namespace.get('test'), 'symlink',
"mutated state has persisted to fs.symlink's callback");
var pointed = fs.readlinkSync(LINKNAME);
t.equal(pointed, FILENAME, "symlink points back to original file");
t.notOk(fs.unlinkSync(LINKNAME), "symlink has been removed");
deleteFile();
t.end();
});
});
});
t.test("fs.readlink", function (t) {
createLink(t);
namespace.run(function () {
namespace.set('test', 'readlink');
t.equal(namespace.get('test'), 'readlink', "state has been mutated");
fs.readlink(LINKNAME, function (error, pointed) {
t.notOk(error, "reading symlink shouldn't error");
t.equal(namespace.get('test'), 'readlink',
"mutated state has persisted to fs.readlink's callback");
t.equal(pointed, FILENAME, "symlink points back to original file");
deleteLink();
t.end();
});
});
});
t.test("fs.unlink", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'unlink');
t.equal(namespace.get('test'), 'unlink', "state has been mutated");
fs.unlink(FILENAME, function (error) {
t.notOk(error, "deleting file shouldn't error");
t.equal(namespace.get('test'), 'unlink',
"mutated state has persisted to fs.unlink's callback");
t.notOk(fs.exists(FILENAME), "file should be gone");
t.end();
});
});
});
t.test("fs.realpath", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'realpath');
t.equal(namespace.get('test'), 'realpath', "state has been mutated");
fs.realpath(FILENAME, function (error, real) {
t.notOk(error, "deleting file shouldn't error");
t.equal(namespace.get('test'), 'realpath',
"mutated state has persisted to fs.realpath's callback");
t.equal(real, path.resolve(FILENAME), "no funny business with the real path");
deleteFile();
t.end();
});
});
});
t.test("fs.mkdir", function (t) {
namespace.run(function () {
namespace.set('test', 'mkdir');
t.equal(namespace.get('test'), 'mkdir', "state has been mutated");
fs.mkdir(DIRNAME, function (error) {
t.notOk(error, "creating directory shouldn't error");
t.equal(namespace.get('test'), 'mkdir',
"mutated state has persisted to fs.mkdir's callback");
t.ok(fs.existsSync(DIRNAME), "directory was created");
fs.rmdirSync(DIRNAME);
t.end();
});
});
});
t.test("fs.rmdir", function (t) {
createDirectory(t);
namespace.run(function () {
namespace.set('test', 'rmdir');
t.equal(namespace.get('test'), 'rmdir', "state has been mutated");
fs.rmdir(DIRNAME, function (error) {
t.notOk(error, "deleting directory shouldn't error");
t.equal(namespace.get('test'), 'rmdir',
"mutated state has persisted to fs.rmdir's callback");
t.notOk(fs.existsSync(DIRNAME), "directory was removed");
t.end();
});
});
});
t.test("fs.readdir", function (t) {
createDirectory(t);
var file1 = fs.openSync(path.join(DIRNAME, 'file1'), 'w');
fs.writeSync(file1, 'one');
fs.closeSync(file1);
var file2 = fs.openSync(path.join(DIRNAME, 'file2'), 'w');
fs.writeSync(file2, 'two');
fs.closeSync(file2);
var file3 = fs.openSync(path.join(DIRNAME, 'file3'), 'w');
fs.writeSync(file3, 'three');
fs.closeSync(file3);
namespace.run(function () {
namespace.set('test', 'readdir');
t.equal(namespace.get('test'), 'readdir', "state has been mutated");
fs.readdir(DIRNAME, function (error, contents) {
t.notOk(error, "reading directory shouldn't error");
t.equal(namespace.get('test'), 'readdir',
"mutated state has persisted to fs.readdir's callback");
t.equal(contents.length, 3, "3 files were found");
fs.unlinkSync(path.join(DIRNAME, 'file1'));
fs.unlinkSync(path.join(DIRNAME, 'file2'));
fs.unlinkSync(path.join(DIRNAME, 'file3'));
deleteDirectory();
t.end();
});
});
});
t.test("fs.watch", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'watch');
t.equal(namespace.get('test'), 'watch', "state has been mutated");
var watcher = fs.watch(FILENAME,
{persistent : false, interval : 200},
function (event) {
t.equal(namespace.get('test'), 'watch',
"mutated state has persisted to fs.watch's callback");
t.equal(event, 'change', "file was changed");
watcher.close();
process.nextTick(function cleanup() {
deleteFile();
t.end();
});
});
setTimeout(function poke() {
fs.writeFileSync(FILENAME, 'still a test');
}, 20);
});
});
t.test("fs.utimes", function (t) {
createFile(t);
/* utimes(2) takes seconds since the epoch, and Date() deals with
* milliseconds. I just want a date some time in the past.
*/
var PASTIME = new Date(Math.floor((Date.now() - 31337) / 1000) * 1000);
namespace.run(function () {
namespace.set('test', 'utimes');
t.equal(namespace.get('test'), 'utimes', "state has been mutated");
var before = fs.statSync(FILENAME);
t.ok(before.atime, "access time of newly-created file set");
t.ok(before.mtime, "modification time of newly-created file set");
fs.utimes(FILENAME, PASTIME, PASTIME, function (error) {
t.notOk(error, "setting utimes shouldn't error");
t.equal(namespace.get('test'), 'utimes',
"mutated state has persisted to fs.utimes's callback");
var after = fs.statSync(FILENAME);
t.deepEqual(after.atime, PASTIME, "access time of newly-created file is reset");
t.deepEqual(after.mtime, PASTIME, "mod time of newly-created file is reset");
t.notDeepEqual(before.atime, after.atime, "access time changed");
t.notDeepEqual(before.mtime, after.mtime, "mod time changed");
deleteFile();
t.end();
});
});
});
t.test("fs.futimes", function (t) {
createFile(t);
/* futimes(2) takes seconds since the epoch, and Date() deals with
* milliseconds. I just want a date some time in the past.
*/
var PASTIME = new Date(Math.floor((Date.now() - 0xb33fd) / 1000) * 1000);
namespace.run(function () {
namespace.set('test', 'futimes');
t.equal(namespace.get('test'), 'futimes', "state has been mutated");
var before = fs.statSync(FILENAME);
t.ok(before.atime, "access time of newly-created file set");
t.ok(before.mtime, "modification time of newly-created file set");
var file = fs.openSync(FILENAME, "w+");
fs.futimes(file, PASTIME, PASTIME, function (error) {
t.notOk(error, "setting futimes shouldn't error");
fs.closeSync(file);
t.equal(namespace.get('test'), 'futimes',
"mutated state has persisted to fs.futimes's callback");
var after = fs.statSync(FILENAME);
t.deepEqual(after.atime, PASTIME, "access time of newly-created file is reset");
t.deepEqual(after.mtime, PASTIME, "mod time of newly-created file is reset");
t.notDeepEqual(before.atime, after.atime, "access time changed");
t.notDeepEqual(before.mtime, after.mtime, "mod time changed");
deleteFile();
t.end();
});
});
});
t.test("fs.fsync", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'fsync');
t.equal(namespace.get('test'), 'fsync', "state has been mutated");
var file = fs.openSync(FILENAME, 'w+');
fs.fsync(file, function (error) {
t.notOk(error, "syncing the file shouldn't error");
t.equal(namespace.get('test'), 'fsync',
"mutated state has persisted to fs.fsync's callback");
fs.closeSync(file);
deleteFile();
t.end();
});
});
});
t.test("fs.open", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'open');
t.equal(namespace.get('test'), 'open', "state has been mutated");
fs.open(FILENAME, 'r', function (error, file) {
t.notOk(error, "opening the file shouldn't error");
t.equal(namespace.get('test'), 'open',
"mutated state has persisted to fs.open's callback");
var contents = new Buffer(4);
fs.readSync(file, contents, 0, 4, 0);
t.equal(contents.toString(), 'UHOH', "contents are still available");
fs.closeSync(file);
deleteFile();
t.end();
});
});
});
t.test("fs.close", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'close');
t.equal(namespace.get('test'), 'close', "state has been mutated");
var file = fs.openSync(FILENAME, 'r');
fs.close(file, function (error) {
t.notOk(error, "closing the file shouldn't error");
t.equal(namespace.get('test'), 'close',
"mutated state has persisted to fs.close's callback");
deleteFile();
t.end();
});
});
});
t.test("fs.read", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'read');
t.equal(namespace.get('test'), 'read', "state has been mutated");
var file = fs.openSync(FILENAME, 'r')
, contents = new Buffer(4)
;
fs.read(file, contents, 0, 4, 0, function (error) {
t.notOk(error, "reading from the file shouldn't error");
t.equal(namespace.get('test'), 'read',
"mutated state has persisted to fs.read's callback");
t.equal(contents.toString(), 'UHOH', "contents are still available");
fs.closeSync(file);
deleteFile();
t.end();
});
});
});
t.test("fs.write", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'write');
t.equal(namespace.get('test'), 'write', "state has been mutated");
var file = fs.openSync(FILENAME, 'w')
, contents = new Buffer('yeap')
;
fs.write(file, contents, 0, 4, 0, function (error) {
t.notOk(error, "writing to the file shouldn't error");
t.equal(namespace.get('test'), 'write',
"mutated state has persisted to fs.write's callback");
fs.closeSync(file);
var readback = fs.readFileSync(FILENAME);
t.equal(readback.toString(), 'yeap', "contents are still available");
deleteFile();
t.end();
});
});
});
t.test("fs.readFile", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'readFile');
t.equal(namespace.get('test'), 'readFile', "state has been mutated");
fs.readFile(FILENAME, function (error, contents) {
t.notOk(error, "reading from the file shouldn't error");
t.equal(namespace.get('test'), 'readFile',
"mutated state has persisted to fs.readFile's callback");
t.equal(contents.toString(), 'UHOH', "contents are still available");
deleteFile();
t.end();
});
});
});
t.test("fs.writeFile", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'writeFile');
t.equal(namespace.get('test'), 'writeFile', "state has been mutated");
fs.writeFile(FILENAME, 'woopwoop', function (error) {
t.notOk(error, "rewriting the file shouldn't error");
t.equal(namespace.get('test'), 'writeFile',
"mutated state has persisted to fs.writeFile's callback");
var readback = fs.readFileSync(FILENAME);
t.equal(readback.toString(), 'woopwoop', "rewritten contents are available");
deleteFile();
t.end();
});
});
});
t.test("fs.appendFile", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'appendFile');
t.equal(namespace.get('test'), 'appendFile', "state has been mutated");
fs.appendFile(FILENAME, '/jk', function (error) {
t.notOk(error, "appending to the file shouldn't error");
t.equal(namespace.get('test'), 'appendFile',
"mutated state has persisted to fs.appendFile's callback");
var readback = fs.readFileSync(FILENAME);
t.equal(readback.toString(), 'UHOH/jk',
"appended contents are still available");
deleteFile();
t.end();
});
});
});
t.test("fs.exists", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'exists');
t.equal(namespace.get('test'), 'exists', "state has been mutated");
fs.exists(FILENAME, function (yep) {
t.equal(namespace.get('test'), 'exists',
"mutated state has persisted to fs.exists's callback");
t.ok(yep, "precreated file does indeed exist.");
fs.exists('NOPENOWAY', function (nope) {
t.equal(namespace.get('test'), 'exists',
"mutated state continues to persist to fs.exists's second callback");
t.notOk(nope, "nonexistent file doesn't exist.");
deleteFile();
t.end();
});
});
});
});
t.test("fs.watchFile", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'watchFile');
t.equal(namespace.get('test'), 'watchFile', "state has been mutated");
fs.watchFile(FILENAME,
{persistent : false, interval : 20},
function (before, after) {
t.equal(namespace.get('test'), 'watchFile',
"mutated state has persisted to fs.watchFile's callback");
t.ok(before.ino, "file has an entry");
t.equal(before.ino, after.ino, "file is at the same location");
fs.unwatchFile(FILENAME);
process.nextTick(function () {
deleteFile();
t.end();
});
});
setTimeout(function poke() {
fs.appendFileSync(FILENAME, 'still a test');
}, 20);
});
});
});
});