2017-01-03 21:16:48 +00: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 11:00:06 +00:00
|
|
|
'use strict';
|
2018-08-06 13:47:11 +00:00
|
|
|
require('../common');
|
2016-12-30 23:38:06 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const fs = require('fs');
|
2012-06-21 00:25:56 +00:00
|
|
|
|
2017-12-25 06:38:11 +00:00
|
|
|
const tmpdir = require('../common/tmpdir');
|
|
|
|
|
2019-07-30 06:50:21 +00:00
|
|
|
tmpdir.refresh();
|
2023-08-21 16:41:53 +00:00
|
|
|
const FILENAME = tmpdir.resolve('watch-me');
|
2017-01-08 13:19:00 +00:00
|
|
|
const TIMEOUT = 1300;
|
2012-06-21 00:25:56 +00:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
let nevents = 0;
|
2012-06-21 00:25:56 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(FILENAME);
|
2018-10-12 17:35:04 +00:00
|
|
|
} catch {
|
2012-06-21 00:25:56 +00:00
|
|
|
// swallow
|
|
|
|
}
|
|
|
|
|
2017-07-11 00:55:21 +00:00
|
|
|
fs.watchFile(FILENAME, { interval: TIMEOUT - 250 }, function(curr, prev) {
|
2012-06-21 00:25:56 +00:00
|
|
|
console.log([curr, prev]);
|
|
|
|
switch (++nevents) {
|
2016-01-13 20:42:45 +00:00
|
|
|
case 1:
|
2018-08-06 13:47:11 +00:00
|
|
|
assert.strictEqual(fs.existsSync(FILENAME), false);
|
2016-01-13 20:42:45 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
case 3:
|
2018-08-06 13:47:11 +00:00
|
|
|
assert.strictEqual(fs.existsSync(FILENAME), true);
|
2016-01-13 20:42:45 +00:00
|
|
|
break;
|
|
|
|
case 4:
|
2018-08-06 13:47:11 +00:00
|
|
|
assert.strictEqual(fs.existsSync(FILENAME), false);
|
2016-01-13 20:42:45 +00:00
|
|
|
fs.unwatchFile(FILENAME);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
2012-06-21 00:25:56 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('exit', function() {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(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');
|
2015-05-19 11:00:06 +00:00
|
|
|
fs.writeFileSync(FILENAME, 'test');
|
2012-06-21 00:25:56 +00:00
|
|
|
setTimeout(touchFile, TIMEOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
function touchFile() {
|
2014-02-25 00:54:04 +00:00
|
|
|
console.log('touch file');
|
2015-05-19 11:00:06 +00:00
|
|
|
fs.writeFileSync(FILENAME, 'test');
|
2012-06-21 00:25:56 +00:00
|
|
|
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);
|
|
|
|
}
|