2012-06-21 00:25:56 +00:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
|
|
|
var path = require('path');
|
|
|
|
var fs = require('fs');
|
|
|
|
|
|
|
|
var FILENAME = path.join(common.tmpDir, 'watch-me');
|
|
|
|
var TIMEOUT = 1300;
|
|
|
|
|
|
|
|
var nevents = 0;
|
|
|
|
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(FILENAME);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
// swallow
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.watchFile(FILENAME, {interval:TIMEOUT - 250}, function(curr, prev) {
|
|
|
|
console.log([curr, prev]);
|
|
|
|
switch (++nevents) {
|
|
|
|
case 1:
|
2014-12-15 18:58:37 +00:00
|
|
|
assert.equal(common.fileExists(FILENAME), false);
|
2014-02-25 00:54:04 +00:00
|
|
|
break;
|
2012-06-21 00:25:56 +00:00
|
|
|
case 2:
|
2014-02-25 00:54:04 +00:00
|
|
|
case 3:
|
2014-12-15 18:58:37 +00:00
|
|
|
assert.equal(common.fileExists(FILENAME), true);
|
2012-06-21 00:25:56 +00:00
|
|
|
break;
|
2014-02-25 00:54:04 +00:00
|
|
|
case 4:
|
2014-12-15 18:58:37 +00:00
|
|
|
assert.equal(common.fileExists(FILENAME), false);
|
2012-06-21 00:25:56 +00:00
|
|
|
fs.unwatchFile(FILENAME);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('exit', function() {
|
2014-02-25 00:54:04 +00:00
|
|
|
assert.equal(nevents, 4);
|
2012-06-21 00:25:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
setTimeout(createFile, TIMEOUT);
|
|
|
|
|
|
|
|
function createFile() {
|
2014-02-25 00:54:04 +00:00
|
|
|
console.log('creating file');
|
2012-06-21 00:25:56 +00:00
|
|
|
fs.writeFileSync(FILENAME, "test");
|
|
|
|
setTimeout(touchFile, TIMEOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
function touchFile() {
|
2014-02-25 00:54:04 +00:00
|
|
|
console.log('touch file');
|
2012-06-21 00:25:56 +00:00
|
|
|
fs.writeFileSync(FILENAME, "test");
|
|
|
|
setTimeout(removeFile, TIMEOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeFile() {
|
2014-02-25 00:54:04 +00:00
|
|
|
console.log('remove file');
|
2012-06-21 00:25:56 +00:00
|
|
|
fs.unlinkSync(FILENAME);
|
|
|
|
}
|