mirror of
https://github.com/oven-sh/bun
synced 2026-02-02 15:08:46 +00:00
Support flag parameter in readFileSync (#15595)
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
This commit is contained in:
@@ -4771,8 +4771,8 @@ pub const NodeFS = struct {
|
||||
|
||||
break :brk switch (bun.sys.open(
|
||||
path,
|
||||
bun.O.RDONLY | bun.O.NOCTTY,
|
||||
0,
|
||||
@intFromEnum(args.flag) | bun.O.NOCTTY,
|
||||
default_permission,
|
||||
)) {
|
||||
.err => |err| return .{
|
||||
.err = err.withPath(if (args.path == .path) args.path.path.slice() else ""),
|
||||
|
||||
@@ -597,7 +597,7 @@ describe("mkdirSync", () => {
|
||||
// @ts-expect-error
|
||||
{ recursive: "lalala" },
|
||||
),
|
||||
).toThrow("The \"recursive\" property must be of type boolean, got string");
|
||||
).toThrow('The "recursive" property must be of type boolean, got string');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1406,6 +1406,36 @@ describe("readFile", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it("works with flags", async () => {
|
||||
const mydir = tempDirWithFiles("fs-read", {});
|
||||
console.log(mydir);
|
||||
|
||||
for (const [flag, code] of [
|
||||
["a", "EBADF"],
|
||||
["ax", "EBADF"],
|
||||
["a+", undefined],
|
||||
["as", "EBADF"],
|
||||
["as+", undefined],
|
||||
["r", "ENOENT"],
|
||||
["rs", "ENOENT"],
|
||||
["r+", "ENOENT"],
|
||||
["rs+", "ENOENT"],
|
||||
["w", "EBADF"],
|
||||
["wx", "EBADF"],
|
||||
["w+", undefined],
|
||||
["wx+", undefined],
|
||||
]) {
|
||||
const name = flag!.replace("+", "_plus") + ".txt";
|
||||
if (code == null) {
|
||||
expect(readFileSync(mydir + "/" + name, { encoding: "utf8", flag })).toBe("");
|
||||
expect(readFileSync(mydir + "/" + name, { encoding: "utf8" })).toBe("");
|
||||
} else {
|
||||
expect.toThrowWithCode(() => readFileSync(mydir + "/" + name, { encoding: "utf8", flag }), code);
|
||||
expect.toThrowWithCode(() => readFileSync(mydir + "/" + name, { encoding: "utf8" }), "ENOENT");
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("writeFileSync", () => {
|
||||
|
||||
Reference in New Issue
Block a user