|
| 1 | +import * as common from '../common/index.mjs'; |
| 2 | +import { describe, it } from 'node:test'; |
| 3 | +import assert from 'node:assert'; |
| 4 | +import tmpdir from '../common/tmpdir.js'; |
| 5 | +import watcher from 'internal/watch_mode/files_watcher'; |
| 6 | +import { writeFileSync } from 'node:fs'; |
| 7 | + |
| 8 | +if (common.isIBMi) |
| 9 | + common.skip('IBMi does not support `fs.watch()`'); |
| 10 | + |
| 11 | +if (common.isAIX) |
| 12 | + common.skip('folder watch capability is limited in AIX.'); |
| 13 | + |
| 14 | +tmpdir.refresh(); |
| 15 | + |
| 16 | +const { FilesWatcher } = watcher; |
| 17 | + |
| 18 | +tmpdir.refresh(); |
| 19 | + |
| 20 | +// Set up test files and dependencies |
| 21 | +const fixtureContent = { |
| 22 | + 'dependency.js': 'module.exports = {};', |
| 23 | + 'test.js': 'require(\'./dependency.js\');', |
| 24 | + 'test-2.js': 'require(\'./dependency.js\');', |
| 25 | +} |
| 26 | + |
| 27 | +const fixturePaths = Object.fromEntries(Object.keys(fixtureContent) |
| 28 | + .map((file) => [file, tmpdir.resolve(file)])); |
| 29 | + |
| 30 | +Object.entries(fixtureContent) |
| 31 | + .forEach(([file, content]) => writeFileSync(fixturePaths[file], content)); |
| 32 | + |
| 33 | +describe('watch file with shared dependency', () => { |
| 34 | + it('should not remove shared dependencies when unfiltering an owner', () => { |
| 35 | + const controller = new AbortController(); |
| 36 | + const watcher = new FilesWatcher({ signal: controller.signal, debounce: 200 }); |
| 37 | + |
| 38 | + watcher.on('changed', ({ owners }) => { |
| 39 | + assert.strictEqual(owners.size, 2); |
| 40 | + assert.ok(owners.has(fixturePaths['test.js'])); |
| 41 | + assert.ok(owners.has(fixturePaths['test-2.js'])); |
| 42 | + controller.abort(); |
| 43 | + }); |
| 44 | + watcher.filterFile(fixturePaths['test.js']); |
| 45 | + watcher.filterFile(fixturePaths['test-2.js']); |
| 46 | + watcher.filterFile(fixturePaths['dependency.js'], fixturePaths['test.js']); |
| 47 | + watcher.filterFile(fixturePaths['dependency.js'], fixturePaths['test-2.js']); |
| 48 | + watcher.unfilterFilesOwnedBy([fixturePaths['test.js']]); |
| 49 | + watcher.filterFile(fixturePaths['test.js']); |
| 50 | + watcher.filterFile(fixturePaths['dependency.js'], fixturePaths['test.js']); |
| 51 | + writeFileSync(fixturePaths['dependency.js'], 'module.exports = { modified: true };'); |
| 52 | + }); |
| 53 | +}); |
0 commit comments