Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rename event dispatch #142

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/emulation/promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
/**
* @todo Implement
*/
public readLines(options?: promises.CreateReadStreamOptions): ReadlineInterface {

Check warning on line 210 in src/emulation/promises.ts

View workflow job for this annotation

GitHub Actions / CI

'options' is defined but never used
throw ErrnoError.With('ENOSYS', this.file.path, 'FileHandle.readLines');
}

Expand Down Expand Up @@ -395,6 +395,7 @@
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));
Expand Down Expand Up @@ -889,7 +890,7 @@
*/
export async function realpath(path: fs.PathLike, options: fs.BufferEncodingOption): Promise<Buffer>;
export async function realpath(path: fs.PathLike, options?: fs.EncodingOption | BufferEncoding): Promise<string>;
export async function realpath(path: fs.PathLike, options?: fs.EncodingOption | BufferEncoding | fs.BufferEncodingOption): Promise<string | Buffer> {

Check warning on line 893 in src/emulation/promises.ts

View workflow job for this annotation

GitHub Actions / CI

'options' is defined but never used
path = normalizePath(path);
const { base, dir } = parse(path);
const lpath = join(dir == '/' ? '/' : await (cache.paths.get(dir) || realpath(dir)), base);
Expand Down Expand Up @@ -1055,7 +1056,7 @@
* @returns A `Dir` object representing the opened directory.
* @todo Use options
*/
export function opendir(path: fs.PathLike, options?: fs.OpenDirOptions): Promise<Dir> {

Check warning on line 1059 in src/emulation/promises.ts

View workflow job for this annotation

GitHub Actions / CI

'options' is defined but never used
path = normalizePath(path);
return Promise.resolve(new Dir(path));
}
Expand Down
19 changes: 15 additions & 4 deletions tests/fs/watch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>();
const newFileResolver = Promise.withResolvers<void>();

const fileResolvers: Record<string, { resolver: PromiseWithResolvers<void>; 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 () => {
Expand Down
Loading