Skip to content

Commit c7fd4b6

Browse files
committed
Fix fs.promises.watch context stuff
Added tests to prevent regression
1 parent be3f663 commit c7fd4b6

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/emulation/promises.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,9 +951,10 @@ export function watch(
951951
options?: fs.WatchOptions | string
952952
): AsyncIterable<promises.FileChangeInfo<string>> | AsyncIterable<promises.FileChangeInfo<Buffer>>;
953953
export function watch<T extends string | Buffer>(this: V_Context, filename: fs.PathLike, options: fs.WatchOptions | string = {}): AsyncIterable<promises.FileChangeInfo<T>> {
954+
const context = this;
954955
return {
955956
[Symbol.asyncIterator](): AsyncIterator<promises.FileChangeInfo<T>> {
956-
const watcher = new FSWatcher<T>(this, filename.toString(), typeof options !== 'string' ? options : { encoding: options as BufferEncoding | 'buffer' });
957+
const watcher = new FSWatcher<T>(context, filename.toString(), typeof options !== 'string' ? options : { encoding: options as BufferEncoding | 'buffer' });
957958

958959
// A queue to hold change events, since we need to resolve them in the async iterator
959960
const eventQueue: ((value: IteratorResult<promises.FileChangeInfo<T>>) => void)[] = [];

tests/common/context.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,21 @@ suite('Context', () => {
1616
test('break-out fails', () => {
1717
assert.deepEqual(c_fs.readdirSync('/../../'), ['example.txt']);
1818
});
19+
20+
test('watch should consider context', async () => {
21+
let lastFile: string,
22+
events = 0;
23+
const watcher = c_fs.promises.watch('/', { recursive: true });
24+
25+
(async () => {
26+
for await (const event of watcher) {
27+
lastFile = event.filename!;
28+
if (++events == 2) return;
29+
}
30+
})();
31+
await c_fs.promises.writeFile('/xpto.txt', 'in real root');
32+
assert.strictEqual(lastFile!, 'xpto.txt');
33+
await c_fs.promises.unlink('/xpto.txt');
34+
assert.strictEqual(lastFile, 'xpto.txt');
35+
});
1936
});

0 commit comments

Comments
 (0)