|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const { setTimeout } = require('timers/promises'); |
| 5 | + |
| 6 | +if (common.isIBMi) |
| 7 | + common.skip('IBMi does not support `fs.watch()`'); |
| 8 | + |
| 9 | +// fs-watch on folders have limited capability in AIX. |
| 10 | +// The testcase makes use of folder watching, and causes |
| 11 | +// hang. This behavior is documented. Skip this for AIX. |
| 12 | + |
| 13 | +if (common.isAIX) |
| 14 | + common.skip('folder watch capability is limited in AIX.'); |
| 15 | + |
| 16 | +const assert = require('assert'); |
| 17 | +const path = require('path'); |
| 18 | +const fs = require('fs'); |
| 19 | + |
| 20 | +const tmpdir = require('../common/tmpdir'); |
| 21 | +const testDir = tmpdir.path; |
| 22 | +tmpdir.refresh(); |
| 23 | + |
| 24 | +(async () => { |
| 25 | + // Add a file to subfolder of a watching folder |
| 26 | + |
| 27 | + const rootDirectory = fs.mkdtempSync(testDir + path.sep); |
| 28 | + const testDirectory = path.join(rootDirectory, 'test-4'); |
| 29 | + fs.mkdirSync(testDirectory); |
| 30 | + |
| 31 | + const file = 'folder-5'; |
| 32 | + const filePath = path.join(testDirectory, file); |
| 33 | + fs.mkdirSync(filePath); |
| 34 | + |
| 35 | + const subfolderPath = path.join(filePath, 'subfolder-6'); |
| 36 | + fs.mkdirSync(subfolderPath); |
| 37 | + |
| 38 | + const childrenFile = 'file-7.txt'; |
| 39 | + const childrenAbsolutePath = path.join(subfolderPath, childrenFile); |
| 40 | + const relativePath = path.join(file, path.basename(subfolderPath), childrenFile); |
| 41 | + |
| 42 | + const watcher = fs.watch(testDirectory, { recursive: true }); |
| 43 | + let watcherClosed = false; |
| 44 | + watcher.on('change', function(event, filename) { |
| 45 | + assert.strictEqual(event, 'rename'); |
| 46 | + |
| 47 | + if (filename === relativePath) { |
| 48 | + watcher.close(); |
| 49 | + watcherClosed = true; |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + await setTimeout(common.platformTimeout(100)); |
| 54 | + fs.writeFileSync(childrenAbsolutePath, 'world'); |
| 55 | + |
| 56 | + process.once('exit', function() { |
| 57 | + assert(watcherClosed, 'watcher Object was not closed'); |
| 58 | + }); |
| 59 | +})().then(common.mustCall()); |
0 commit comments