2017-01-03 22:16:48 +01:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2016-12-31 00:38:06 +01:00
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
2012-06-21 02:25:56 +02:00
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
const FILENAME = path.join(common.tmpDir, 'watch-me');
|
|
|
|
const TIMEOUT = 1300;
|
2012-06-21 02:25:56 +02:00
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
let nevents = 0;
|
2012-06-21 02:25:56 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(FILENAME);
|
2016-07-09 02:17:47 +02:00
|
|
|
} catch (e) {
|
2012-06-21 02:25:56 +02:00
|
|
|
// swallow
|
|
|
|
}
|
|
|
|
|
2016-05-05 07:20:27 +02:00
|
|
|
fs.watchFile(FILENAME, {interval: TIMEOUT - 250}, function(curr, prev) {
|
2012-06-21 02:25:56 +02:00
|
|
|
console.log([curr, prev]);
|
|
|
|
switch (++nevents) {
|
2016-01-13 21:42:45 +01:00
|
|
|
case 1:
|
2017-01-08 16:36:25 +01:00
|
|
|
assert.strictEqual(common.fileExists(FILENAME), false);
|
2016-01-13 21:42:45 +01:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
case 3:
|
2017-01-08 16:36:25 +01:00
|
|
|
assert.strictEqual(common.fileExists(FILENAME), true);
|
2016-01-13 21:42:45 +01:00
|
|
|
break;
|
|
|
|
case 4:
|
2017-01-08 16:36:25 +01:00
|
|
|
assert.strictEqual(common.fileExists(FILENAME), false);
|
2016-01-13 21:42:45 +01:00
|
|
|
fs.unwatchFile(FILENAME);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
2012-06-21 02:25:56 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('exit', function() {
|
2017-01-08 16:36:25 +01:00
|
|
|
assert.strictEqual(nevents, 4);
|
2012-06-21 02:25:56 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
setTimeout(createFile, TIMEOUT);
|
|
|
|
|
|
|
|
function createFile() {
|
2014-02-25 01:54:04 +01:00
|
|
|
console.log('creating file');
|
2015-05-19 13:00:06 +02:00
|
|
|
fs.writeFileSync(FILENAME, 'test');
|
2012-06-21 02:25:56 +02:00
|
|
|
setTimeout(touchFile, TIMEOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
function touchFile() {
|
2014-02-25 01:54:04 +01:00
|
|
|
console.log('touch file');
|
2015-05-19 13:00:06 +02:00
|
|
|
fs.writeFileSync(FILENAME, 'test');
|
2012-06-21 02:25:56 +02:00
|
|
|
setTimeout(removeFile, TIMEOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeFile() {
|
2014-02-25 01:54:04 +01:00
|
|
|
console.log('remove file');
|
2012-06-21 02:25:56 +02:00
|
|
|
fs.unlinkSync(FILENAME);
|
|
|
|
}
|