2011-07-20 04:17:58 +02:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
|
|
|
var path = require('path');
|
|
|
|
var fs = require('fs');
|
|
|
|
|
2011-09-09 06:16:10 +02:00
|
|
|
var watchSeenOne = 0;
|
|
|
|
var watchSeenTwo = 0;
|
2012-07-08 16:31:07 +02:00
|
|
|
var watchSeenThree = 0;
|
2014-02-06 07:29:58 +01:00
|
|
|
var watchSeenFour = 0;
|
2011-07-20 04:17:58 +02:00
|
|
|
|
2011-09-09 06:16:10 +02:00
|
|
|
var startDir = process.cwd();
|
2012-07-08 16:37:45 +02:00
|
|
|
var testDir = common.tmpDir;
|
2011-09-09 06:16:10 +02:00
|
|
|
|
|
|
|
var filenameOne = 'watch.txt';
|
|
|
|
var filepathOne = path.join(testDir, filenameOne);
|
|
|
|
|
|
|
|
var filenameTwo = 'hasOwnProperty';
|
|
|
|
var filepathTwo = filenameTwo;
|
|
|
|
var filepathTwoAbs = path.join(testDir, filenameTwo);
|
|
|
|
|
2012-07-08 16:31:07 +02:00
|
|
|
var filenameThree = 'charm'; // because the third time is
|
|
|
|
|
2014-02-06 07:29:58 +01:00
|
|
|
var filenameFour = 'get';
|
2011-09-09 06:16:10 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
process.on('exit', function() {
|
2011-09-09 06:16:10 +02:00
|
|
|
fs.unlinkSync(filepathOne);
|
|
|
|
fs.unlinkSync(filepathTwoAbs);
|
2012-07-08 16:31:07 +02:00
|
|
|
fs.unlinkSync(filenameThree);
|
2014-02-06 07:29:58 +01:00
|
|
|
fs.unlinkSync(filenameFour);
|
2011-09-09 06:16:10 +02:00
|
|
|
assert.equal(1, watchSeenOne);
|
2012-07-08 16:31:07 +02:00
|
|
|
assert.equal(2, watchSeenTwo);
|
|
|
|
assert.equal(1, watchSeenThree);
|
2014-02-06 07:29:58 +01:00
|
|
|
assert.equal(1, watchSeenFour);
|
2011-09-09 06:16:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2011-10-05 00:08:18 +02:00
|
|
|
fs.writeFileSync(filepathOne, 'hello');
|
2011-07-20 04:17:58 +02:00
|
|
|
|
|
|
|
assert.throws(
|
2011-10-05 00:08:18 +02:00
|
|
|
function() {
|
|
|
|
fs.watchFile(filepathOne);
|
|
|
|
},
|
|
|
|
function(e) {
|
|
|
|
return e.message === 'watchFile requires a listener function';
|
|
|
|
}
|
2011-07-20 04:17:58 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert.doesNotThrow(
|
2011-10-05 00:08:18 +02:00
|
|
|
function() {
|
|
|
|
fs.watchFile(filepathOne, function(curr, prev) {
|
|
|
|
fs.unwatchFile(filepathOne);
|
|
|
|
++watchSeenOne;
|
|
|
|
});
|
|
|
|
}
|
2011-07-20 04:17:58 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
setTimeout(function() {
|
2011-10-05 00:08:18 +02:00
|
|
|
fs.writeFileSync(filepathOne, 'world');
|
2011-07-20 04:17:58 +02:00
|
|
|
}, 1000);
|
|
|
|
|
2011-09-09 06:16:10 +02:00
|
|
|
|
|
|
|
process.chdir(testDir);
|
|
|
|
|
2011-10-05 00:08:18 +02:00
|
|
|
fs.writeFileSync(filepathTwoAbs, 'howdy');
|
2011-09-09 06:16:10 +02:00
|
|
|
|
|
|
|
assert.throws(
|
2011-10-05 00:08:18 +02:00
|
|
|
function() {
|
|
|
|
fs.watchFile(filepathTwo);
|
|
|
|
},
|
|
|
|
function(e) {
|
|
|
|
return e.message === 'watchFile requires a listener function';
|
|
|
|
}
|
2011-09-09 06:16:10 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert.doesNotThrow(
|
2011-10-05 00:08:18 +02:00
|
|
|
function() {
|
2012-07-08 16:31:07 +02:00
|
|
|
function a(curr, prev) {
|
|
|
|
fs.unwatchFile(filepathTwo, a);
|
2011-10-05 00:08:18 +02:00
|
|
|
++watchSeenTwo;
|
2012-07-08 16:31:07 +02:00
|
|
|
}
|
|
|
|
function b(curr, prev) {
|
|
|
|
fs.unwatchFile(filepathTwo, b);
|
|
|
|
++watchSeenTwo;
|
|
|
|
}
|
|
|
|
fs.watchFile(filepathTwo, a);
|
|
|
|
fs.watchFile(filepathTwo, b);
|
2011-10-05 00:08:18 +02:00
|
|
|
}
|
2011-09-09 06:16:10 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
setTimeout(function() {
|
2011-10-05 00:08:18 +02:00
|
|
|
fs.writeFileSync(filepathTwoAbs, 'pardner');
|
2011-09-09 06:16:10 +02:00
|
|
|
}, 1000);
|
2012-07-08 16:31:07 +02:00
|
|
|
|
|
|
|
assert.doesNotThrow(
|
|
|
|
function() {
|
|
|
|
function a(curr, prev) {
|
|
|
|
assert.ok(0); // should not run
|
|
|
|
}
|
|
|
|
function b(curr, prev) {
|
|
|
|
fs.unwatchFile(filenameThree, b);
|
|
|
|
++watchSeenThree;
|
|
|
|
}
|
|
|
|
fs.watchFile(filenameThree, a);
|
|
|
|
fs.watchFile(filenameThree, b);
|
|
|
|
fs.unwatchFile(filenameThree, a);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
fs.writeFileSync(filenameThree, 'pardner');
|
|
|
|
}, 1000);
|
2014-02-06 07:29:58 +01:00
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
fs.writeFileSync(filenameFour, 'hey');
|
|
|
|
}, 200);
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
fs.writeFileSync(filenameFour, 'hey');
|
|
|
|
}, 500);
|
|
|
|
|
|
|
|
assert.doesNotThrow(
|
|
|
|
function() {
|
|
|
|
function a(curr, prev) {
|
|
|
|
++watchSeenFour;
|
|
|
|
assert.equal(1, watchSeenFour);
|
|
|
|
fs.unwatchFile("." + path.sep + filenameFour, a);
|
|
|
|
}
|
|
|
|
fs.watchFile(filenameFour, a);
|
|
|
|
}
|
|
|
|
);
|