From ec3a07e4617d99bd9b5208894d566e61de2f4250 Mon Sep 17 00:00:00 2001 From: Marcos Candeia Date: Thu, 21 Nov 2024 11:49:25 -0300 Subject: [PATCH] Fix rename event dispatch Signed-off-by: Marcos Candeia --- src/emulation/promises.ts | 1 + tests/fs/watch.test.ts | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/emulation/promises.ts b/src/emulation/promises.ts index 933704c4..dd389631 100644 --- a/src/emulation/promises.ts +++ b/src/emulation/promises.ts @@ -395,6 +395,7 @@ export async function rename(oldPath: fs.PathLike, newPath: fs.PathLike): Promis if (src.mountPoint == dst.mountPoint) { await src.fs.rename(src.path, dst.path); emitChange('rename', oldPath.toString()); + emitChange('change', newPath.toString()); return; } await writeFile(newPath, await readFile(oldPath)); diff --git a/tests/fs/watch.test.ts b/tests/fs/watch.test.ts index 43dc6d83..f00f3965 100644 --- a/tests/fs/watch.test.ts +++ b/tests/fs/watch.test.ts @@ -71,18 +71,29 @@ suite('Watch Features', () => { }); test('fs.watch should detect file renames', async () => { - const oldFile = `${testDir}/oldFile.txt`; - const newFile = `${testDir}/newFile.txt`; + const oldFileName = `oldFile.txt`; + const newFileName = `newFile.txt`; + const oldFile = `${testDir}/${oldFileName}`; + const newFile = `${testDir}/${newFileName}`; await fs.promises.writeFile(oldFile, 'Some content'); + const oldFileResolver = Promise.withResolvers(); + const newFileResolver = Promise.withResolvers(); + const fileResolvers: Record; eventType: string }> = { + [oldFileName]: { resolver: oldFileResolver, eventType: 'rename' }, + [newFileName]: { resolver: newFileResolver, eventType: 'change' }, + }; using watcher = fs.watch(testDir, (eventType, filename) => { - assert.strictEqual(eventType, 'rename'); - assert.strictEqual(filename, 'oldFile.txt'); + const resolver = fileResolvers[filename]; + assert.notEqual(resolver, undefined); // should have a resolver so file is expected + assert.strictEqual(eventType, resolver.eventType); + resolver.resolver.resolve(); }); // Rename the file to trigger the event await fs.promises.rename(oldFile, newFile); + await Promise.all([newFileResolver.promise, oldFileResolver.promise]); }); test('fs.watch should detect file deletions', async () => {