11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ciconst common = require('../common'); 241cb0ef41Sopenharmony_ciif (common.isIBMi) 251cb0ef41Sopenharmony_ci common.skip('IBMi does not support fs.watch()'); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ciconst assert = require('assert'); 281cb0ef41Sopenharmony_ciconst fs = require('fs'); 291cb0ef41Sopenharmony_ciconst path = require('path'); 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir'); 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ciif (!common.isMainThread) 341cb0ef41Sopenharmony_ci common.skip('process.chdir is not available in Workers'); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ciconst expectFilePath = common.isWindows || 371cb0ef41Sopenharmony_ci common.isLinux || 381cb0ef41Sopenharmony_ci common.isOSX || 391cb0ef41Sopenharmony_ci common.isAIX; 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ciconst testDir = tmpdir.path; 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_citmpdir.refresh(); 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci// Because macOS (and possibly other operating systems) can return a watcher 461cb0ef41Sopenharmony_ci// before it is actually watching, we need to repeat the operation to avoid 471cb0ef41Sopenharmony_ci// a race condition. 481cb0ef41Sopenharmony_cifunction repeat(fn) { 491cb0ef41Sopenharmony_ci setImmediate(fn); 501cb0ef41Sopenharmony_ci const interval = setInterval(fn, 5000); 511cb0ef41Sopenharmony_ci return interval; 521cb0ef41Sopenharmony_ci} 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci{ 551cb0ef41Sopenharmony_ci const filepath = path.join(testDir, 'watch.txt'); 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci fs.writeFileSync(filepath, 'hello'); 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci const watcher = fs.watch(filepath); 601cb0ef41Sopenharmony_ci watcher.on('change', common.mustCall(function(event, filename) { 611cb0ef41Sopenharmony_ci assert.strictEqual(event, 'change'); 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci if (expectFilePath) { 641cb0ef41Sopenharmony_ci assert.strictEqual(filename, 'watch.txt'); 651cb0ef41Sopenharmony_ci } 661cb0ef41Sopenharmony_ci clearInterval(interval); 671cb0ef41Sopenharmony_ci watcher.close(); 681cb0ef41Sopenharmony_ci })); 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci const interval = repeat(() => { fs.writeFileSync(filepath, 'world'); }); 711cb0ef41Sopenharmony_ci} 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci{ 741cb0ef41Sopenharmony_ci const filepathAbs = path.join(testDir, 'hasOwnProperty'); 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci process.chdir(testDir); 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci fs.writeFileSync(filepathAbs, 'howdy'); 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci const watcher = 811cb0ef41Sopenharmony_ci fs.watch('hasOwnProperty', common.mustCall(function(event, filename) { 821cb0ef41Sopenharmony_ci assert.strictEqual(event, 'change'); 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci if (expectFilePath) { 851cb0ef41Sopenharmony_ci assert.strictEqual(filename, 'hasOwnProperty'); 861cb0ef41Sopenharmony_ci } 871cb0ef41Sopenharmony_ci clearInterval(interval); 881cb0ef41Sopenharmony_ci watcher.close(); 891cb0ef41Sopenharmony_ci })); 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci const interval = repeat(() => { fs.writeFileSync(filepathAbs, 'pardner'); }); 921cb0ef41Sopenharmony_ci} 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ci{ 951cb0ef41Sopenharmony_ci const testsubdir = fs.mkdtempSync(testDir + path.sep); 961cb0ef41Sopenharmony_ci const filepath = path.join(testsubdir, 'newfile.txt'); 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ci const watcher = 991cb0ef41Sopenharmony_ci fs.watch(testsubdir, common.mustCall(function(event, filename) { 1001cb0ef41Sopenharmony_ci const renameEv = common.isSunOS || common.isAIX ? 'change' : 'rename'; 1011cb0ef41Sopenharmony_ci assert.strictEqual(event, renameEv); 1021cb0ef41Sopenharmony_ci if (expectFilePath) { 1031cb0ef41Sopenharmony_ci assert.strictEqual(filename, 'newfile.txt'); 1041cb0ef41Sopenharmony_ci } else { 1051cb0ef41Sopenharmony_ci assert.strictEqual(filename, null); 1061cb0ef41Sopenharmony_ci } 1071cb0ef41Sopenharmony_ci clearInterval(interval); 1081cb0ef41Sopenharmony_ci watcher.close(); 1091cb0ef41Sopenharmony_ci })); 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ci const interval = repeat(() => { 1121cb0ef41Sopenharmony_ci fs.rmSync(filepath, { force: true }); 1131cb0ef41Sopenharmony_ci const fd = fs.openSync(filepath, 'w'); 1141cb0ef41Sopenharmony_ci fs.closeSync(fd); 1151cb0ef41Sopenharmony_ci }); 1161cb0ef41Sopenharmony_ci} 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ci// https://github.com/joyent/node/issues/2293 - non-persistent watcher should 1191cb0ef41Sopenharmony_ci// not block the event loop 1201cb0ef41Sopenharmony_ci{ 1211cb0ef41Sopenharmony_ci fs.watch(__filename, { persistent: false }, common.mustNotCall()); 1221cb0ef41Sopenharmony_ci} 1231cb0ef41Sopenharmony_ci 1241cb0ef41Sopenharmony_ci// Whitebox test to ensure that wrapped FSEvent is safe 1251cb0ef41Sopenharmony_ci// https://github.com/joyent/node/issues/6690 1261cb0ef41Sopenharmony_ci{ 1271cb0ef41Sopenharmony_ci let oldhandle; 1281cb0ef41Sopenharmony_ci assert.throws( 1291cb0ef41Sopenharmony_ci () => { 1301cb0ef41Sopenharmony_ci const w = fs.watch(__filename, common.mustNotCall()); 1311cb0ef41Sopenharmony_ci oldhandle = w._handle; 1321cb0ef41Sopenharmony_ci w._handle = { close: w._handle.close }; 1331cb0ef41Sopenharmony_ci w.close(); 1341cb0ef41Sopenharmony_ci }, 1351cb0ef41Sopenharmony_ci { 1361cb0ef41Sopenharmony_ci name: 'Error', 1371cb0ef41Sopenharmony_ci code: 'ERR_INTERNAL_ASSERTION', 1381cb0ef41Sopenharmony_ci message: /^handle must be a FSEvent/, 1391cb0ef41Sopenharmony_ci } 1401cb0ef41Sopenharmony_ci ); 1411cb0ef41Sopenharmony_ci oldhandle.close(); // clean up 1421cb0ef41Sopenharmony_ci} 1431cb0ef41Sopenharmony_ci 1441cb0ef41Sopenharmony_ci{ 1451cb0ef41Sopenharmony_ci let oldhandle; 1461cb0ef41Sopenharmony_ci assert.throws( 1471cb0ef41Sopenharmony_ci () => { 1481cb0ef41Sopenharmony_ci const w = fs.watch(__filename, common.mustNotCall()); 1491cb0ef41Sopenharmony_ci oldhandle = w._handle; 1501cb0ef41Sopenharmony_ci const protoSymbols = 1511cb0ef41Sopenharmony_ci Object.getOwnPropertySymbols(Object.getPrototypeOf(w)); 1521cb0ef41Sopenharmony_ci const kFSWatchStart = 1531cb0ef41Sopenharmony_ci protoSymbols.find((val) => val.toString() === 'Symbol(kFSWatchStart)'); 1541cb0ef41Sopenharmony_ci w._handle = {}; 1551cb0ef41Sopenharmony_ci w[kFSWatchStart](); 1561cb0ef41Sopenharmony_ci }, 1571cb0ef41Sopenharmony_ci { 1581cb0ef41Sopenharmony_ci name: 'Error', 1591cb0ef41Sopenharmony_ci code: 'ERR_INTERNAL_ASSERTION', 1601cb0ef41Sopenharmony_ci message: /^handle must be a FSEvent/, 1611cb0ef41Sopenharmony_ci } 1621cb0ef41Sopenharmony_ci ); 1631cb0ef41Sopenharmony_ci oldhandle.close(); // clean up 1641cb0ef41Sopenharmony_ci} 165