|
| 1 | +// Flags: --expose-internals |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const common = require('../common'); |
| 5 | +const tmpdir = require('../common/tmpdir'); |
| 6 | +const assert = require('assert'); |
| 7 | +const fs = require('fs'); |
| 8 | +const path = require('path'); |
| 9 | +const { kFSWatchStart } = require('internal/fs/watchers'); |
| 10 | +const { FSWatcher } = require('internal/fs/recursive_watch'); |
| 11 | + |
| 12 | +if (common.isIBMi) |
| 13 | + common.skip('IBMi does not support `fs.watch()`'); |
| 14 | + |
| 15 | +tmpdir.refresh(); |
| 16 | + |
| 17 | +const parent = tmpdir.resolve('parent'); |
| 18 | +const child = path.join(parent, 'child'); |
| 19 | +fs.mkdirSync(child, { recursive: true }); |
| 20 | +fs.writeFileSync(path.join(child, 'test.tmp'), 'test'); |
| 21 | + |
| 22 | +const watch = fs.watch; |
| 23 | +let deletedChild = false; |
| 24 | +fs.watch = function(filename, ...args) { |
| 25 | + const watcher = Reflect.apply(watch, this, [filename, ...args]); |
| 26 | + |
| 27 | + if (filename === child) { |
| 28 | + fs.rmSync(child, { recursive: true }); |
| 29 | + deletedChild = true; |
| 30 | + } |
| 31 | + |
| 32 | + return watcher; |
| 33 | +}; |
| 34 | + |
| 35 | +const watcher = new FSWatcher({ recursive: true }); |
| 36 | +watcher.on('error', common.mustNotCall()); |
| 37 | + |
| 38 | +try { |
| 39 | + watcher[kFSWatchStart](parent); |
| 40 | + assert.strictEqual(deletedChild, true); |
| 41 | +} finally { |
| 42 | + watcher.close(); |
| 43 | + fs.watch = watch; |
| 44 | +} |
0 commit comments