|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const { skipIfNoWatch } = require('../common/watch.js'); |
| 5 | + |
| 6 | +skipIfNoWatch(); |
| 7 | + |
| 8 | +// if (common.isSunOS) |
| 9 | +// common.skip('`fs.watch()` is not reliable on SunOS.'); |
| 10 | + |
| 11 | +const assert = require('assert'); |
| 12 | +const path = require('path'); |
| 13 | +const fs = require('fs'); |
| 14 | + |
| 15 | +const tmpdir = require('../common/tmpdir'); |
| 16 | + |
| 17 | +tmpdir.refresh(); |
| 18 | + |
| 19 | +const testFileName = 'keep.txt'; |
| 20 | +const testFilePath = path.join(tmpdir.path, testFileName); |
| 21 | +const ignoredLogName = 'debug.log'; |
| 22 | +const ignoredLogPath = path.join(tmpdir.path, ignoredLogName); |
| 23 | +const ignoredTmpName = 'temp.tmp'; |
| 24 | +const ignoredTmpPath = path.join(tmpdir.path, ignoredTmpName); |
| 25 | +const ignoredHiddenName = '.secret'; |
| 26 | +const ignoredHiddenPath = path.join(tmpdir.path, ignoredHiddenName); |
| 27 | + |
| 28 | +const watcher = fs.watch(tmpdir.path, { |
| 29 | + ignore: [ |
| 30 | + '*.log', |
| 31 | + /\.tmp$/, |
| 32 | + (filename) => filename.startsWith('.'), |
| 33 | + ], |
| 34 | +}); |
| 35 | + |
| 36 | +watcher.on('change', common.mustCallAtLeast((event, filename) => { |
| 37 | + assert.notStrictEqual(filename, ignoredLogName); |
| 38 | + assert.notStrictEqual(filename, ignoredTmpName); |
| 39 | + assert.notStrictEqual(filename, ignoredHiddenName); |
| 40 | + |
| 41 | + if (filename === testFileName) { |
| 42 | + watcher.close(); |
| 43 | + } |
| 44 | +}, 1)); |
| 45 | + |
| 46 | +// Do the write with a delay to ensure that the OS is ready to notify us. See |
| 47 | +// https://github.com/nodejs/node/issues/52601. |
| 48 | +setTimeout(() => { |
| 49 | + fs.writeFileSync(ignoredLogPath, 'ignored'); |
| 50 | + fs.writeFileSync(ignoredTmpPath, 'ignored'); |
| 51 | + fs.writeFileSync(ignoredHiddenPath, 'ignored'); |
| 52 | + fs.writeFileSync(testFilePath, 'content'); |
| 53 | +}, common.platformTimeout(100)); |
0 commit comments