0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00

test: refactor test-fs-watchfile.js

The test no longer waits about 5 seconds between callback invocations.

It now writes to the tmp directory rather than the fixtures directory.

Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/2393
This commit is contained in:
Rich Trott 2015-08-15 16:17:36 -07:00
parent 90a2671889
commit 522f8a8071

View File

@ -4,7 +4,6 @@ const common = require('../common');
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const fixtures = path.join(__dirname, '..', 'fixtures');
// Basic usage tests.
assert.throws(function() {
@ -19,7 +18,7 @@ assert.throws(function() {
fs.watchFile(new Object(), function() {});
}, /Path must be a string/);
const enoentFile = path.join(fixtures, 'non-existent-file');
const enoentFile = path.join(common.tmpDir, 'non-existent-file');
const expectedStatObject = new fs.Stats(
0, // dev
0, // mode
@ -37,24 +36,13 @@ const expectedStatObject = new fs.Stats(
Date.UTC(1970, 0, 1, 0, 0, 0) // birthtime
);
function removeTestFile() {
try {
fs.unlinkSync(enoentFile);
} catch (ex) {
if (ex.code !== 'ENOENT') {
throw ex;
}
}
}
// Make sure that the file does not exist, when the test starts
removeTestFile();
common.refreshTmpDir();
// If the file initially didn't exist, and gets created at a later point of
// time, the callback should be invoked again with proper values in stat object
var fileExists = false;
fs.watchFile(enoentFile, common.mustCall(function(curr, prev) {
fs.watchFile(enoentFile, {interval: 0}, common.mustCall(function(curr, prev) {
if (!fileExists) {
// If the file does not exist, all the fields should be zero and the date
// fields should be UNIX EPOCH time
@ -71,8 +59,7 @@ fs.watchFile(enoentFile, common.mustCall(function(curr, prev) {
// As the file just got created, previous ino value should be lesser than
// or equal to zero (non-existent file).
assert(prev.ino <= 0);
// Stop watching the file and delete it
// Stop watching the file
fs.unwatchFile(enoentFile);
removeTestFile();
}
}, 2));