diff options
author | 2023-07-03 18:08:49 -0300 | |
---|---|---|
committer | 2023-07-03 14:08:49 -0700 | |
commit | 034577c9dad2486324e3bd65907794dd9a3ee217 (patch) | |
tree | 0082ad9e3070976058e687290317219fc25085b0 | |
parent | 424717a9737321a3f37f7596f62067d72cd97e25 (diff) | |
download | bun-034577c9dad2486324e3bd65907794dd9a3ee217.tar.gz bun-034577c9dad2486324e3bd65907794dd9a3ee217.tar.zst bun-034577c9dad2486324e3bd65907794dd9a3ee217.zip |
[fix] patch fs.watch sym link (#3481)
* oopsie
* add tests
---------
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
-rw-r--r-- | src/bun.js/node/node_fs_watcher.zig | 2 | ||||
-rw-r--r-- | test/js/node/watch/fs.watch.test.js | 55 |
2 files changed, 56 insertions, 1 deletions
diff --git a/src/bun.js/node/node_fs_watcher.zig b/src/bun.js/node/node_fs_watcher.zig index b1f4ec8a9..d0af350c0 100644 --- a/src/bun.js/node/node_fs_watcher.zig +++ b/src/bun.js/node/node_fs_watcher.zig @@ -802,7 +802,7 @@ pub const FSWatcher = struct { result.fd = file.handle; const _stat = try file.stat(); - result.is_file = _stat.kind == .directory; + result.is_file = _stat.kind != .directory; }, .directory => { const dir = (try std.fs.openIterableDirAbsoluteZ(absolute_path_z, .{ diff --git a/test/js/node/watch/fs.watch.test.js b/test/js/node/watch/fs.watch.test.js index 33d05df29..faf6a8546 100644 --- a/test/js/node/watch/fs.watch.test.js +++ b/test/js/node/watch/fs.watch.test.js @@ -19,6 +19,8 @@ const testDir = tempDirWithFiles("watch", { "url.txt": "hello", "close.txt": "hello", "close-close.txt": "hello", + "sym-sync.txt": "hello", + "sym.txt": "hello", [encodingFileName]: "hello", }); @@ -341,6 +343,36 @@ describe("fs.watch", () => { watcher.once("close", () => reject()); }); }); + + test("should work with symlink", async () => { + const filepath = path.join(testDir, "sym-symlink2.txt"); + await fs.promises.symlink(path.join(testDir, "sym-sync.txt"), filepath); + + const interval = repeat(() => { + fs.writeFileSync(filepath, "hello"); + }); + + const promise = new Promise((resolve, reject) => { + let timeout = null; + const watcher = fs.watch(filepath, event => { + clearTimeout(timeout); + clearInterval(interval); + try { + resolve(event); + } catch (e) { + reject(e); + } finally { + watcher.close(); + } + }); + setTimeout(() => { + clearInterval(interval); + watcher?.close(); + reject("timeout"); + }, 3000); + }); + expect(promise).resolves.toBe("change"); + }); }); describe("fs.promises.watch", () => { @@ -464,4 +496,27 @@ describe("fs.promises.watch", () => { } })(); }); + + test("should work with symlink", async () => { + const filepath = path.join(testDir, "sym-symlink.txt"); + await fs.promises.symlink(path.join(testDir, "sym.txt"), filepath); + + const watcher = fs.promises.watch(filepath); + const interval = repeat(() => { + fs.writeFileSync(filepath, "hello"); + }); + + const promise = (async () => { + try { + for await (const event of watcher) { + return event.eventType; + } + } catch (e) { + expect("unreacheable").toBe(false); + } finally { + clearInterval(interval); + } + })(); + expect(promise).resolves.toBe("change"); + }); }); |