Skip to content

Commit

Permalink
fix test-fs-promises-writefile.js
Browse files Browse the repository at this point in the history
  • Loading branch information
pfgithub committed Feb 5, 2025
1 parent d63fe71 commit 8c90063
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/io/PipeWriter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -824,9 +824,11 @@ fn BaseWindowsPipeWriter(
.sync_file, .file => |file| {
// always cancel the current one
file.fs.cancel();
// always use close_fs here because we can have a operation in progress
file.close_fs.data = file;
_ = uv.uv_fs_close(uv.Loop.get(), &file.close_fs, file.file, onFileClose);
if (this.owns_fd) {
// always use close_fs here because we can have a operation in progress
file.close_fs.data = file;
_ = uv.uv_fs_close(uv.Loop.get(), &file.close_fs, file.file, onFileClose);
}
},
.pipe => |pipe| {
pipe.data = pipe;
Expand Down Expand Up @@ -1044,7 +1046,6 @@ pub fn WindowsBufferedWriter(
this.is_done = true;
if (this.pending_payload_size == 0) {
// will auto close when pending stuff get written
if (!this.owns_fd) return;
this.close();
}
}
Expand Down
15 changes: 15 additions & 0 deletions test/js/bun/util/bun-file.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { test, expect } from "bun:test";
import { tmpdirSync } from "harness";
import { join } from "path";
import fsPromises from "fs/promises";

test("delete() and stat() should work with unicode paths", async () => {
const testDir = tmpdirSync();
Expand All @@ -21,3 +22,17 @@ test("delete() and stat() should work with unicode paths", async () => {

expect(await Bun.file(filename).exists()).toBe(false);
});

test("writer.end() should not close the fd if it does not own the fd", async () => {
const testDir = tmpdirSync();
for (let i = 0; i < 30; i++) {
const fileHandle = await fsPromises.open(testDir + "/tmp.txt", "w", 0o666);
const fd = fileHandle.fd;

await Bun.file(fd).writer().end();
// @ts-ignore
await fsPromises.close(fd);
expect(await Bun.file(testDir + "/tmp.txt").text()).toBe("");
await Bun.sleep(50);
}
});

0 comments on commit 8c90063

Please sign in to comment.