1 | 'use strict';
|
2 |
|
3 | var createNamespace = require('../context.js').createNamespace
|
4 | , fs = require('fs')
|
5 | , path = require('path')
|
6 | , exec = require('child_process').exec
|
7 | , tap = require('tap')
|
8 | , test = tap.test
|
9 | ;
|
10 |
|
11 |
|
12 | var FILENAME = '__testfile'
|
13 | , DIRNAME = '__TESTDIR'
|
14 | , LINKNAME = '__testlink'
|
15 | , HARDLINKNAME = '__testhardlink'
|
16 | ;
|
17 |
|
18 | function createFile(assert) {
|
19 | var contents = new Buffer("UHOH")
|
20 | , file = fs.openSync(FILENAME, 'w')
|
21 | , written = fs.writeSync(file, contents, 0, contents.length, 0)
|
22 | ;
|
23 | assert.equals(written, contents.length, "whole buffer was written");
|
24 | var rc = fs.closeSync(file);
|
25 |
|
26 | fs.chmodSync(FILENAME, '0666');
|
27 | return rc;
|
28 | }
|
29 |
|
30 | function deleteFile() { return fs.unlinkSync(FILENAME); }
|
31 |
|
32 |
|
33 | function createLink(assert) {
|
34 | createFile(assert);
|
35 | fs.symlinkSync(FILENAME, LINKNAME);
|
36 | if (fs.lchmodSync) {
|
37 |
|
38 | fs.lchmodSync(LINKNAME, '0777');
|
39 | }
|
40 | }
|
41 |
|
42 | function deleteLink() {
|
43 | fs.unlinkSync(LINKNAME);
|
44 | return deleteFile();
|
45 | }
|
46 |
|
47 |
|
48 | function createDirectory(assert) {
|
49 | fs.mkdirSync(DIRNAME);
|
50 | assert.ok(fs.existsSync(DIRNAME), "directory was created");
|
51 | }
|
52 |
|
53 | function deleteDirectory() { return fs.rmdirSync(DIRNAME); }
|
54 |
|
55 |
|
56 | function mapIds(username, groupname, callback) {
|
57 | if (!callback) throw new Error("mapIds requires callback");
|
58 | if (!username) return callback(new Error("mapIds requires username"));
|
59 | if (!groupname) return callback(new Error("mapIds requires groupname"));
|
60 |
|
61 | exec('id -u ' + username, function (error, stdout, stderr) {
|
62 | if (error) return callback(error);
|
63 | if (stderr) return callback(new Error(stderr));
|
64 |
|
65 | var uid = +stdout;
|
66 | exec('id -g ' + groupname, function (error, stdout, stderr) {
|
67 | if (error) return callback(error);
|
68 | if (stderr) return callback(new Error(stderr));
|
69 |
|
70 | var gid = +stdout;
|
71 | callback(null, uid, gid);
|
72 | });
|
73 | });
|
74 | }
|
75 |
|
76 | test("continuation-local state with MakeCallback and fs module", function (t) {
|
77 | t.plan(33);
|
78 |
|
79 | var namespace = createNamespace('fs');
|
80 | namespace.run(function () {
|
81 | namespace.set('test', 0xabad1dea);
|
82 |
|
83 | t.test("fs.rename", function (t) {
|
84 | createFile(t);
|
85 |
|
86 | namespace.run(function () {
|
87 | namespace.set('test', 'rename');
|
88 | t.equal(namespace.get('test'), 'rename', "state has been mutated");
|
89 |
|
90 | fs.rename(FILENAME, '__renamed', function (error) {
|
91 | t.notOk(error, "renaming shouldn't error");
|
92 | t.equal(namespace.get('test'), 'rename',
|
93 | "mutated state has persisted to fs.rename's callback");
|
94 |
|
95 | fs.unlinkSync('__renamed');
|
96 | t.end();
|
97 | });
|
98 | });
|
99 | });
|
100 |
|
101 | t.test("fs.truncate", function (t) {
|
102 |
|
103 | if (!fs.ftruncate) return t.end();
|
104 |
|
105 | createFile(t);
|
106 |
|
107 | namespace.run(function () {
|
108 | namespace.set('test', 'truncate');
|
109 | t.equal(namespace.get('test'), 'truncate', "state has been mutated");
|
110 |
|
111 | fs.truncate(FILENAME, 0, function (error) {
|
112 | t.notOk(error, "truncation shouldn't error");
|
113 |
|
114 | var stats = fs.statSync(FILENAME);
|
115 | t.equal(stats.size, 0, "file has been truncated");
|
116 |
|
117 | t.equal(namespace.get('test'), 'truncate',
|
118 | "mutated state has persisted to fs.truncate's callback");
|
119 |
|
120 | deleteFile();
|
121 | t.end();
|
122 | });
|
123 | });
|
124 | });
|
125 |
|
126 | t.test("fs.ftruncate", function (t) {
|
127 | createFile(t);
|
128 |
|
129 |
|
130 | var truncate = fs.ftruncate ? fs.ftruncate : fs.truncate;
|
131 |
|
132 | namespace.run(function () {
|
133 | namespace.set('test', 'ftruncate');
|
134 | t.equal(namespace.get('test'), 'ftruncate', "state has been mutated");
|
135 |
|
136 | var file = fs.openSync(FILENAME, 'w');
|
137 | truncate(file, 0, function (error) {
|
138 | t.notOk(error, "truncation shouldn't error");
|
139 |
|
140 | fs.closeSync(file);
|
141 | var stats = fs.statSync(FILENAME);
|
142 | t.equal(stats.size, 0, "file has been truncated");
|
143 |
|
144 | t.equal(namespace.get('test'), 'ftruncate',
|
145 | "mutated state has persisted to fs.ftruncate's callback");
|
146 |
|
147 | deleteFile();
|
148 | t.end();
|
149 | });
|
150 | });
|
151 | });
|
152 |
|
153 | t.test("fs.chown", function (t) {
|
154 | createFile(t);
|
155 |
|
156 | mapIds('daemon', 'daemon', function (error, uid, gid) {
|
157 | t.notOk(error, "looking up uid & gid shouldn't error");
|
158 | t.ok(uid, "uid for daemon was found");
|
159 | t.ok(gid, "gid for daemon was found");
|
160 |
|
161 | namespace.run(function () {
|
162 | namespace.set('test', 'chown');
|
163 | t.equal(namespace.get('test'), 'chown', "state has been mutated");
|
164 |
|
165 | fs.chown(FILENAME, uid, gid, function (error) {
|
166 | t.ok(error, "changing ownership will error for non-root users");
|
167 |
|
168 | t.equal(namespace.get('test'), 'chown',
|
169 | "mutated state has persisted to fs.chown's callback");
|
170 |
|
171 | deleteFile();
|
172 | t.end();
|
173 | });
|
174 | });
|
175 | });
|
176 | });
|
177 |
|
178 | t.test("fs.fchown", function (t) {
|
179 | createFile(t);
|
180 |
|
181 | mapIds('daemon', 'daemon', function (error, uid, gid) {
|
182 | t.notOk(error, "looking up uid & gid shouldn't error");
|
183 | t.ok(uid, "uid for daemon was found");
|
184 | t.ok(gid, "gid for daemon was found");
|
185 |
|
186 | namespace.run(function () {
|
187 | namespace.set('test', 'fchown');
|
188 | t.equal(namespace.get('test'), 'fchown', "state has been mutated");
|
189 |
|
190 | var file = fs.openSync(FILENAME, 'w');
|
191 | fs.fchown(file, uid, gid, function (error) {
|
192 | t.ok(error, "changing ownership will error for non-root users");
|
193 |
|
194 | t.equal(namespace.get('test'), 'fchown',
|
195 | "mutated state has persisted to fs.fchown's callback");
|
196 |
|
197 | fs.closeSync(file);
|
198 | deleteFile();
|
199 | t.end();
|
200 | });
|
201 | });
|
202 | });
|
203 | });
|
204 |
|
205 | t.test("fs.lchown", function (t) {
|
206 | if (!fs.lchown) return t.end();
|
207 | createLink(t);
|
208 |
|
209 | mapIds('daemon', 'daemon', function (error, uid, gid) {
|
210 | t.notOk(error, "looking up uid & gid shouldn't error");
|
211 | t.ok(uid, "uid for daemon was found");
|
212 | t.ok(gid, "gid for daemon was found");
|
213 |
|
214 | namespace.run(function () {
|
215 | namespace.set('test', 'lchown');
|
216 | t.equal(namespace.get('test'), 'lchown', "state has been mutated");
|
217 |
|
218 | fs.lchown(LINKNAME, uid, gid, function (error) {
|
219 | t.ok(error, "changing ownership will error for non-root users");
|
220 |
|
221 | t.equal(namespace.get('test'), 'lchown',
|
222 | "mutated state has persisted to fs.lchown's callback");
|
223 |
|
224 | deleteLink();
|
225 | t.end();
|
226 | });
|
227 | });
|
228 | });
|
229 | });
|
230 |
|
231 | t.test("fs.chmod", function (t) {
|
232 | createFile(t);
|
233 |
|
234 | namespace.run(function () {
|
235 | namespace.set('test', 'chmod');
|
236 | t.equal(namespace.get('test'), 'chmod', "state has been mutated");
|
237 |
|
238 | fs.chmod(FILENAME, '0700', function (error) {
|
239 | t.notOk(error, "changing mode shouldn't error");
|
240 |
|
241 | t.equal(namespace.get('test'), 'chmod',
|
242 | "mutated state has persisted to fs.chmod's callback");
|
243 |
|
244 | var stats = fs.statSync(FILENAME);
|
245 | t.equal(stats.mode.toString(8), '100700', "extra access bits are stripped");
|
246 |
|
247 | deleteFile();
|
248 | t.end();
|
249 | });
|
250 | });
|
251 | });
|
252 |
|
253 | t.test("fs.fchmod", function (t) {
|
254 | createFile(t);
|
255 |
|
256 | namespace.run(function () {
|
257 | namespace.set('test', 'fchmod');
|
258 | t.equal(namespace.get('test'), 'fchmod', "state has been mutated");
|
259 |
|
260 | var file = fs.openSync(FILENAME, 'w+');
|
261 | fs.fchmod(file, '0700', function (error) {
|
262 | t.notOk(error, "changing mode shouldn't error");
|
263 |
|
264 | t.equal(namespace.get('test'), 'fchmod',
|
265 | "mutated state has persisted to fs.fchmod's callback");
|
266 |
|
267 | fs.closeSync(file);
|
268 | var stats = fs.statSync(FILENAME);
|
269 | t.equal(stats.mode.toString(8), '100700', "extra access bits are stripped");
|
270 |
|
271 | deleteFile();
|
272 | t.end();
|
273 | });
|
274 | });
|
275 | });
|
276 |
|
277 | t.test("fs.lchmod", function (t) {
|
278 | if (!fs.lchmod) return t.end();
|
279 | createLink(t);
|
280 |
|
281 | namespace.run(function () {
|
282 | namespace.set('test', 'lchmod');
|
283 | t.equal(namespace.get('test'), 'lchmod', "state has been mutated");
|
284 |
|
285 | fs.lchmod(LINKNAME, '0700', function (error) {
|
286 | t.notOk(error, "changing mode shouldn't error");
|
287 |
|
288 | t.equal(namespace.get('test'), 'lchmod',
|
289 | "mutated state has persisted to fs.lchmod's callback");
|
290 |
|
291 | var stats = fs.lstatSync(LINKNAME);
|
292 | t.equal(stats.mode.toString(8), '120700', "extra access bits are stripped");
|
293 |
|
294 | deleteLink();
|
295 | t.end();
|
296 | });
|
297 | });
|
298 | });
|
299 |
|
300 | t.test("fs.stat", function (t) {
|
301 | createFile(t);
|
302 |
|
303 | namespace.run(function () {
|
304 | namespace.set('test', 'stat');
|
305 | t.equal(namespace.get('test'), 'stat', "state has been mutated");
|
306 |
|
307 | fs.stat(FILENAME, function (error, stats) {
|
308 | t.notOk(error, "reading stats shouldn't error");
|
309 |
|
310 | t.equal(namespace.get('test'), 'stat',
|
311 | "mutated state has persisted to fs.stat's callback");
|
312 |
|
313 | t.equal(stats.mode.toString(8), '100666', "permissions should be as created");
|
314 |
|
315 | deleteFile();
|
316 | t.end();
|
317 | });
|
318 | });
|
319 | });
|
320 |
|
321 | t.test("fs.fstat", function (t) {
|
322 | createFile(t);
|
323 |
|
324 | namespace.run(function () {
|
325 | namespace.set('test', 'fstat');
|
326 | t.equal(namespace.get('test'), 'fstat', "state has been mutated");
|
327 |
|
328 | var file = fs.openSync(FILENAME, 'r');
|
329 | fs.fstat(file, function (error, stats) {
|
330 | t.notOk(error, "reading stats shouldn't error");
|
331 |
|
332 | t.equal(namespace.get('test'), 'fstat',
|
333 | "mutated state has persisted to fs.fstat's callback");
|
334 |
|
335 | t.equal(stats.mode.toString(8), '100666', "permissions should be as created");
|
336 |
|
337 | fs.closeSync(file);
|
338 | deleteFile();
|
339 | t.end();
|
340 | });
|
341 | });
|
342 | });
|
343 |
|
344 | t.test("fs.lstat", function (t) {
|
345 | createLink(t);
|
346 |
|
347 | namespace.run(function () {
|
348 | namespace.set('test', 'lstat');
|
349 | t.equal(namespace.get('test'), 'lstat', "state has been mutated");
|
350 |
|
351 | fs.lstat(LINKNAME, function (error, stats) {
|
352 | t.notOk(error, "reading stats shouldn't error");
|
353 |
|
354 | t.equal(namespace.get('test'), 'lstat',
|
355 | "mutated state has persisted to fs.lstat's callback");
|
356 |
|
357 | t.equal(
|
358 | (stats.mode ^ process.umask()).toString(8),
|
359 | '120777',
|
360 | "permissions should be as created"
|
361 | );
|
362 |
|
363 | deleteLink();
|
364 | t.end();
|
365 | });
|
366 | });
|
367 | });
|
368 |
|
369 | t.test("fs.link", function (t) {
|
370 | createFile(t);
|
371 |
|
372 | namespace.run(function () {
|
373 | namespace.set('test', 'link');
|
374 | t.equal(namespace.get('test'), 'link', "state has been mutated");
|
375 |
|
376 | fs.link(FILENAME, HARDLINKNAME, function (error) {
|
377 | t.notOk(error, "creating a link shouldn't error");
|
378 |
|
379 | t.equal(namespace.get('test'), 'link',
|
380 | "mutated state has persisted to fs.link's callback");
|
381 |
|
382 | var orig = fs.statSync(FILENAME)
|
383 | , linked = fs.statSync(HARDLINKNAME)
|
384 | ;
|
385 | t.equal(orig.ino, linked.ino, "entries should point to same file");
|
386 |
|
387 | t.notOk(fs.unlinkSync(HARDLINKNAME), "link has been removed");
|
388 | deleteFile();
|
389 | t.end();
|
390 | });
|
391 | });
|
392 | });
|
393 |
|
394 | t.test("fs.symlink", function (t) {
|
395 | createFile(t);
|
396 |
|
397 | namespace.run(function () {
|
398 | namespace.set('test', 'symlink');
|
399 | t.equal(namespace.get('test'), 'symlink', "state has been mutated");
|
400 |
|
401 | fs.symlink(FILENAME, LINKNAME, function (error) {
|
402 | t.notOk(error, "creating a symlink shouldn't error");
|
403 |
|
404 | t.equal(namespace.get('test'), 'symlink',
|
405 | "mutated state has persisted to fs.symlink's callback");
|
406 |
|
407 | var pointed = fs.readlinkSync(LINKNAME);
|
408 | t.equal(pointed, FILENAME, "symlink points back to original file");
|
409 |
|
410 | t.notOk(fs.unlinkSync(LINKNAME), "symlink has been removed");
|
411 | deleteFile();
|
412 | t.end();
|
413 | });
|
414 | });
|
415 | });
|
416 |
|
417 | t.test("fs.readlink", function (t) {
|
418 | createLink(t);
|
419 |
|
420 | namespace.run(function () {
|
421 | namespace.set('test', 'readlink');
|
422 | t.equal(namespace.get('test'), 'readlink', "state has been mutated");
|
423 |
|
424 | fs.readlink(LINKNAME, function (error, pointed) {
|
425 | t.notOk(error, "reading symlink shouldn't error");
|
426 |
|
427 | t.equal(namespace.get('test'), 'readlink',
|
428 | "mutated state has persisted to fs.readlink's callback");
|
429 |
|
430 | t.equal(pointed, FILENAME, "symlink points back to original file");
|
431 |
|
432 | deleteLink();
|
433 | t.end();
|
434 | });
|
435 | });
|
436 | });
|
437 |
|
438 | t.test("fs.unlink", function (t) {
|
439 | createFile(t);
|
440 |
|
441 | namespace.run(function () {
|
442 | namespace.set('test', 'unlink');
|
443 | t.equal(namespace.get('test'), 'unlink', "state has been mutated");
|
444 |
|
445 | fs.unlink(FILENAME, function (error) {
|
446 | t.notOk(error, "deleting file shouldn't error");
|
447 |
|
448 | t.equal(namespace.get('test'), 'unlink',
|
449 | "mutated state has persisted to fs.unlink's callback");
|
450 |
|
451 | t.notOk(fs.exists(FILENAME), "file should be gone");
|
452 | t.end();
|
453 | });
|
454 | });
|
455 | });
|
456 |
|
457 | t.test("fs.realpath", function (t) {
|
458 | createFile(t);
|
459 |
|
460 | namespace.run(function () {
|
461 | namespace.set('test', 'realpath');
|
462 | t.equal(namespace.get('test'), 'realpath', "state has been mutated");
|
463 |
|
464 | fs.realpath(FILENAME, function (error, real) {
|
465 | t.notOk(error, "deleting file shouldn't error");
|
466 |
|
467 | t.equal(namespace.get('test'), 'realpath',
|
468 | "mutated state has persisted to fs.realpath's callback");
|
469 |
|
470 | t.equal(real, path.resolve(FILENAME), "no funny business with the real path");
|
471 |
|
472 | deleteFile();
|
473 | t.end();
|
474 | });
|
475 | });
|
476 | });
|
477 |
|
478 | t.test("fs.mkdir", function (t) {
|
479 | namespace.run(function () {
|
480 | namespace.set('test', 'mkdir');
|
481 | t.equal(namespace.get('test'), 'mkdir', "state has been mutated");
|
482 |
|
483 | fs.mkdir(DIRNAME, function (error) {
|
484 | t.notOk(error, "creating directory shouldn't error");
|
485 |
|
486 | t.equal(namespace.get('test'), 'mkdir',
|
487 | "mutated state has persisted to fs.mkdir's callback");
|
488 |
|
489 | t.ok(fs.existsSync(DIRNAME), "directory was created");
|
490 |
|
491 | fs.rmdirSync(DIRNAME);
|
492 | t.end();
|
493 | });
|
494 | });
|
495 | });
|
496 |
|
497 | t.test("fs.rmdir", function (t) {
|
498 | createDirectory(t);
|
499 |
|
500 | namespace.run(function () {
|
501 | namespace.set('test', 'rmdir');
|
502 | t.equal(namespace.get('test'), 'rmdir', "state has been mutated");
|
503 |
|
504 | fs.rmdir(DIRNAME, function (error) {
|
505 | t.notOk(error, "deleting directory shouldn't error");
|
506 |
|
507 | t.equal(namespace.get('test'), 'rmdir',
|
508 | "mutated state has persisted to fs.rmdir's callback");
|
509 |
|
510 | t.notOk(fs.existsSync(DIRNAME), "directory was removed");
|
511 |
|
512 | t.end();
|
513 | });
|
514 | });
|
515 | });
|
516 |
|
517 | t.test("fs.readdir", function (t) {
|
518 | createDirectory(t);
|
519 |
|
520 | var file1 = fs.openSync(path.join(DIRNAME, 'file1'), 'w');
|
521 | fs.writeSync(file1, 'one');
|
522 | fs.closeSync(file1);
|
523 |
|
524 | var file2 = fs.openSync(path.join(DIRNAME, 'file2'), 'w');
|
525 | fs.writeSync(file2, 'two');
|
526 | fs.closeSync(file2);
|
527 |
|
528 | var file3 = fs.openSync(path.join(DIRNAME, 'file3'), 'w');
|
529 | fs.writeSync(file3, 'three');
|
530 | fs.closeSync(file3);
|
531 |
|
532 | namespace.run(function () {
|
533 | namespace.set('test', 'readdir');
|
534 | t.equal(namespace.get('test'), 'readdir', "state has been mutated");
|
535 |
|
536 | fs.readdir(DIRNAME, function (error, contents) {
|
537 | t.notOk(error, "reading directory shouldn't error");
|
538 |
|
539 | t.equal(namespace.get('test'), 'readdir',
|
540 | "mutated state has persisted to fs.readdir's callback");
|
541 |
|
542 | t.equal(contents.length, 3, "3 files were found");
|
543 |
|
544 | fs.unlinkSync(path.join(DIRNAME, 'file1'));
|
545 | fs.unlinkSync(path.join(DIRNAME, 'file2'));
|
546 | fs.unlinkSync(path.join(DIRNAME, 'file3'));
|
547 | deleteDirectory();
|
548 | t.end();
|
549 | });
|
550 | });
|
551 | });
|
552 |
|
553 | t.test("fs.watch", function (t) {
|
554 | createFile(t);
|
555 |
|
556 | namespace.run(function () {
|
557 | namespace.set('test', 'watch');
|
558 | t.equal(namespace.get('test'), 'watch', "state has been mutated");
|
559 |
|
560 | var watcher = fs.watch(FILENAME,
|
561 | {persistent : false, interval : 200},
|
562 | function (event) {
|
563 | t.equal(namespace.get('test'), 'watch',
|
564 | "mutated state has persisted to fs.watch's callback");
|
565 |
|
566 | t.equal(event, 'change', "file was changed");
|
567 |
|
568 | watcher.close();
|
569 | process.nextTick(function cleanup() {
|
570 | deleteFile();
|
571 | t.end();
|
572 | });
|
573 | });
|
574 |
|
575 | process.nextTick(function poke() {
|
576 | fs.writeFileSync(FILENAME, 'still a test');
|
577 | });
|
578 | });
|
579 | });
|
580 |
|
581 | t.test("fs.watchFile", function (t) {
|
582 | createFile(t);
|
583 |
|
584 | namespace.run(function () {
|
585 | namespace.set('test', 'watchFile');
|
586 | t.equal(namespace.get('test'), 'watchFile', "state has been mutated");
|
587 |
|
588 | fs.watchFile(FILENAME,
|
589 | {persistent : false, interval : 200},
|
590 | function (before, after) {
|
591 | t.equal(namespace.get('test'), 'watchFile',
|
592 | "mutated state has persisted to fs.watchFile's callback");
|
593 |
|
594 | t.ok(before.ino, "file has an entry");
|
595 | t.equal(before.ino, after.ino, "file is at the same location");
|
596 |
|
597 | fs.unwatchFile(FILENAME);
|
598 | process.nextTick(function () {
|
599 | deleteFile();
|
600 | t.end();
|
601 | });
|
602 | });
|
603 |
|
604 | process.nextTick(function poke() {
|
605 | fs.writeFileSync(FILENAME, 'still a test');
|
606 | });
|
607 | });
|
608 | });
|
609 |
|
610 | t.test("fs.utimes", function (t) {
|
611 | createFile(t);
|
612 |
|
613 | |
614 |
|
615 |
|
616 | var PASTIME = new Date(Math.floor((Date.now() - 31337) / 1000) * 1000);
|
617 |
|
618 | namespace.run(function () {
|
619 | namespace.set('test', 'utimes');
|
620 | t.equal(namespace.get('test'), 'utimes', "state has been mutated");
|
621 |
|
622 | var before = fs.statSync(FILENAME);
|
623 | t.ok(before.atime, "access time of newly-created file set");
|
624 | t.ok(before.mtime, "modification time of newly-created file set");
|
625 |
|
626 | fs.utimes(FILENAME, PASTIME, PASTIME, function (error) {
|
627 | t.notOk(error, "setting utimes shouldn't error");
|
628 |
|
629 | t.equal(namespace.get('test'), 'utimes',
|
630 | "mutated state has persisted to fs.utimes's callback");
|
631 |
|
632 | var after = fs.statSync(FILENAME);
|
633 | t.deepEqual(after.atime, PASTIME, "access time of newly-created file is reset");
|
634 | t.deepEqual(after.mtime, PASTIME, "mod time of newly-created file is reset");
|
635 | t.notDeepEqual(before.atime, after.atime, "access time changed");
|
636 | t.notDeepEqual(before.mtime, after.mtime, "mod time changed");
|
637 |
|
638 | deleteFile();
|
639 | t.end();
|
640 | });
|
641 | });
|
642 | });
|
643 |
|
644 | t.test("fs.futimes", function (t) {
|
645 | createFile(t);
|
646 |
|
647 | |
648 |
|
649 |
|
650 | var PASTIME = new Date(Math.floor((Date.now() - 0xb33fd) / 1000) * 1000);
|
651 |
|
652 | namespace.run(function () {
|
653 | namespace.set('test', 'futimes');
|
654 | t.equal(namespace.get('test'), 'futimes', "state has been mutated");
|
655 |
|
656 | var before = fs.statSync(FILENAME);
|
657 | t.ok(before.atime, "access time of newly-created file set");
|
658 | t.ok(before.mtime, "modification time of newly-created file set");
|
659 |
|
660 | var file = fs.openSync(FILENAME, "w+");
|
661 | fs.futimes(file, PASTIME, PASTIME, function (error) {
|
662 | t.notOk(error, "setting futimes shouldn't error");
|
663 | fs.closeSync(file);
|
664 |
|
665 | t.equal(namespace.get('test'), 'futimes',
|
666 | "mutated state has persisted to fs.futimes's callback");
|
667 |
|
668 | var after = fs.statSync(FILENAME);
|
669 | t.deepEqual(after.atime, PASTIME, "access time of newly-created file is reset");
|
670 | t.deepEqual(after.mtime, PASTIME, "mod time of newly-created file is reset");
|
671 | t.notDeepEqual(before.atime, after.atime, "access time changed");
|
672 | t.notDeepEqual(before.mtime, after.mtime, "mod time changed");
|
673 |
|
674 | deleteFile();
|
675 | t.end();
|
676 | });
|
677 | });
|
678 | });
|
679 |
|
680 | t.test("fs.fsync", function (t) {
|
681 | createFile(t);
|
682 |
|
683 | namespace.run(function () {
|
684 | namespace.set('test', 'fsync');
|
685 | t.equal(namespace.get('test'), 'fsync', "state has been mutated");
|
686 |
|
687 | var file = fs.openSync(FILENAME, 'w+');
|
688 | fs.fsync(file, function (error) {
|
689 | t.notOk(error, "syncing the file shouldn't error");
|
690 |
|
691 | t.equal(namespace.get('test'), 'fsync',
|
692 | "mutated state has persisted to fs.fsync's callback");
|
693 |
|
694 | fs.closeSync(file);
|
695 | deleteFile();
|
696 | t.end();
|
697 | });
|
698 | });
|
699 | });
|
700 |
|
701 | t.test("fs.open", function (t) {
|
702 | createFile(t);
|
703 |
|
704 | namespace.run(function () {
|
705 | namespace.set('test', 'open');
|
706 | t.equal(namespace.get('test'), 'open', "state has been mutated");
|
707 |
|
708 | fs.open(FILENAME, 'r', function (error, file) {
|
709 | t.notOk(error, "opening the file shouldn't error");
|
710 |
|
711 | t.equal(namespace.get('test'), 'open',
|
712 | "mutated state has persisted to fs.open's callback");
|
713 |
|
714 |
|
715 | var contents = new Buffer(4);
|
716 | fs.readSync(file, contents, 0, 4, 0);
|
717 | t.equal(contents.toString(), 'UHOH', "contents are still available");
|
718 |
|
719 | fs.closeSync(file);
|
720 | deleteFile();
|
721 | t.end();
|
722 | });
|
723 | });
|
724 | });
|
725 |
|
726 | t.test("fs.close", function (t) {
|
727 | createFile(t);
|
728 |
|
729 | namespace.run(function () {
|
730 | namespace.set('test', 'close');
|
731 | t.equal(namespace.get('test'), 'close', "state has been mutated");
|
732 |
|
733 | var file = fs.openSync(FILENAME, 'r');
|
734 | fs.close(file, function (error) {
|
735 | t.notOk(error, "closing the file shouldn't error");
|
736 |
|
737 | t.equal(namespace.get('test'), 'close',
|
738 | "mutated state has persisted to fs.close's callback");
|
739 |
|
740 | deleteFile();
|
741 | t.end();
|
742 | });
|
743 | });
|
744 | });
|
745 |
|
746 | t.test("fs.read", function (t) {
|
747 | createFile(t);
|
748 |
|
749 | namespace.run(function () {
|
750 | namespace.set('test', 'read');
|
751 | t.equal(namespace.get('test'), 'read', "state has been mutated");
|
752 |
|
753 | var file = fs.openSync(FILENAME, 'r')
|
754 | , contents = new Buffer(4)
|
755 | ;
|
756 | fs.read(file, contents, 0, 4, 0, function (error) {
|
757 | t.notOk(error, "reading from the file shouldn't error");
|
758 |
|
759 | t.equal(namespace.get('test'), 'read',
|
760 | "mutated state has persisted to fs.read's callback");
|
761 |
|
762 | t.equal(contents.toString(), 'UHOH', "contents are still available");
|
763 |
|
764 | fs.closeSync(file);
|
765 | deleteFile();
|
766 | t.end();
|
767 | });
|
768 | });
|
769 | });
|
770 |
|
771 | t.test("fs.write", function (t) {
|
772 | createFile(t);
|
773 |
|
774 | namespace.run(function () {
|
775 | namespace.set('test', 'write');
|
776 | t.equal(namespace.get('test'), 'write', "state has been mutated");
|
777 |
|
778 | var file = fs.openSync(FILENAME, 'w')
|
779 | , contents = new Buffer('yeap')
|
780 | ;
|
781 | fs.write(file, contents, 0, 4, 0, function (error) {
|
782 | t.notOk(error, "writing to the file shouldn't error");
|
783 |
|
784 | t.equal(namespace.get('test'), 'write',
|
785 | "mutated state has persisted to fs.write's callback");
|
786 |
|
787 | fs.closeSync(file);
|
788 |
|
789 | var readback = fs.readFileSync(FILENAME);
|
790 | t.equal(readback.toString(), 'yeap', "contents are still available");
|
791 |
|
792 | deleteFile();
|
793 | t.end();
|
794 | });
|
795 | });
|
796 | });
|
797 |
|
798 | t.test("fs.readFile", function (t) {
|
799 | createFile(t);
|
800 |
|
801 | namespace.run(function () {
|
802 | namespace.set('test', 'readFile');
|
803 | t.equal(namespace.get('test'), 'readFile', "state has been mutated");
|
804 |
|
805 | fs.readFile(FILENAME, function (error, contents) {
|
806 | t.notOk(error, "reading from the file shouldn't error");
|
807 |
|
808 | t.equal(namespace.get('test'), 'readFile',
|
809 | "mutated state has persisted to fs.readFile's callback");
|
810 |
|
811 | t.equal(contents.toString(), 'UHOH', "contents are still available");
|
812 |
|
813 | deleteFile();
|
814 | t.end();
|
815 | });
|
816 | });
|
817 | });
|
818 |
|
819 | t.test("fs.writeFile", function (t) {
|
820 | createFile(t);
|
821 |
|
822 | namespace.run(function () {
|
823 | namespace.set('test', 'writeFile');
|
824 | t.equal(namespace.get('test'), 'writeFile', "state has been mutated");
|
825 |
|
826 | fs.writeFile(FILENAME, 'woopwoop', function (error) {
|
827 | t.notOk(error, "rewriting the file shouldn't error");
|
828 |
|
829 | t.equal(namespace.get('test'), 'writeFile',
|
830 | "mutated state has persisted to fs.writeFile's callback");
|
831 |
|
832 | var readback = fs.readFileSync(FILENAME);
|
833 | t.equal(readback.toString(), 'woopwoop', "rewritten contents are available");
|
834 |
|
835 | deleteFile();
|
836 | t.end();
|
837 | });
|
838 | });
|
839 | });
|
840 |
|
841 | t.test("fs.appendFile", function (t) {
|
842 | createFile(t);
|
843 |
|
844 | namespace.run(function () {
|
845 | namespace.set('test', 'appendFile');
|
846 | t.equal(namespace.get('test'), 'appendFile', "state has been mutated");
|
847 |
|
848 | fs.appendFile(FILENAME, '/jk', function (error) {
|
849 | t.notOk(error, "appending to the file shouldn't error");
|
850 |
|
851 | t.equal(namespace.get('test'), 'appendFile',
|
852 | "mutated state has persisted to fs.appendFile's callback");
|
853 |
|
854 | var readback = fs.readFileSync(FILENAME);
|
855 | t.equal(readback.toString(), 'UHOH/jk',
|
856 | "appended contents are still available");
|
857 |
|
858 | deleteFile();
|
859 | t.end();
|
860 | });
|
861 | });
|
862 | });
|
863 |
|
864 | t.test("fs.exists", function (t) {
|
865 | createFile(t);
|
866 |
|
867 | namespace.run(function () {
|
868 | namespace.set('test', 'exists');
|
869 | t.equal(namespace.get('test'), 'exists', "state has been mutated");
|
870 |
|
871 | fs.exists(FILENAME, function (yep) {
|
872 | t.equal(namespace.get('test'), 'exists',
|
873 | "mutated state has persisted to fs.exists's callback");
|
874 |
|
875 | t.ok(yep, "precreated file does indeed exist.");
|
876 |
|
877 | fs.exists('NOPENOWAY', function (nope) {
|
878 | t.equal(namespace.get('test'), 'exists',
|
879 | "mutated state continues to persist to fs.exists's second callback");
|
880 |
|
881 | t.notOk(nope, "nonexistent file doesn't exist.");
|
882 |
|
883 | deleteFile();
|
884 | t.end();
|
885 | });
|
886 | });
|
887 | });
|
888 | });
|
889 | });
|
890 | });
|