Skip to content

Commit

Permalink
test: refactor test-fs-watchfile.js
Browse files Browse the repository at this point in the history
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: #2393
  • Loading branch information
Trott committed Aug 18, 2015
1 parent 90a2671 commit 522f8a8
Showing 1 changed file with 4 additions and 17 deletions.
21 changes: 4 additions & 17 deletions test/parallel/test-fs-watchfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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));

0 comments on commit 522f8a8

Please sign in to comment.