Skip to content

Commit

Permalink
fix fs-promises-file-handle-write.test.js (#14315)
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro authored Oct 8, 2024
1 parent b0b38b4 commit c41ff9d
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/bun.js/node/node_fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2941,7 +2941,8 @@ pub const Arguments = struct {

if (exception.* != null) return null;

const buffer = StringOrBuffer.fromJS(ctx.ptr(), bun.default_allocator, arguments.next() orelse {
const buffer_value = arguments.next();
const buffer = StringOrBuffer.fromJS(ctx.ptr(), bun.default_allocator, buffer_value orelse {
if (exception.* == null) {
JSC.throwInvalidArguments(
"data is required",
Expand All @@ -2953,16 +2954,15 @@ pub const Arguments = struct {
return null;
}) orelse {
if (exception.* == null) {
JSC.throwInvalidArguments(
"data must be a string or TypedArray",
.{},
ctx,
exception,
);
_ = ctx.throwInvalidArgumentTypeValue("buffer", "string or TypedArray", buffer_value.?);
}
return null;
};
if (exception.* != null) return null;
if (buffer_value.?.isString() and !buffer_value.?.isStringLiteral()) {
_ = ctx.throwInvalidArgumentTypeValue("buffer", "string or TypedArray", buffer_value.?);
return null;
}

var args = Write{
.fd = fd,
Expand Down
93 changes: 93 additions & 0 deletions test/js/node/test/parallel/fs-promises-file-handle-write.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//#FILE: test-fs-promises-file-handle-write.js
//#SHA1: 6ca802494e0ce0ee3187b1661322f115cfd7340c
//-----------------
"use strict";

const fs = require("fs");
const { open } = fs.promises;
const path = require("path");
const os = require("os");

const tmpDir = path.join(os.tmpdir(), "test-fs-promises-file-handle-write");

beforeAll(() => {
if (fs.existsSync(tmpDir)) {
fs.rmSync(tmpDir, { recursive: true, force: true });
}
fs.mkdirSync(tmpDir, { recursive: true });
});

afterAll(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});

test("validateWrite", async () => {
const filePathForHandle = path.resolve(tmpDir, "tmp-write.txt");
const fileHandle = await open(filePathForHandle, "w+");
const buffer = Buffer.from("Hello world".repeat(100), "utf8");

await fileHandle.write(buffer, 0, buffer.length);
const readFileData = fs.readFileSync(filePathForHandle);
expect(readFileData).toEqual(buffer);

await fileHandle.close();
});

test("validateEmptyWrite", async () => {
const filePathForHandle = path.resolve(tmpDir, "tmp-empty-write.txt");
const fileHandle = await open(filePathForHandle, "w+");
const buffer = Buffer.from(""); // empty buffer

await fileHandle.write(buffer, 0, buffer.length);
const readFileData = fs.readFileSync(filePathForHandle);
expect(readFileData).toEqual(buffer);

await fileHandle.close();
});

test("validateNonUint8ArrayWrite", async () => {
const filePathForHandle = path.resolve(tmpDir, "tmp-data-write.txt");
const fileHandle = await open(filePathForHandle, "w+");
const buffer = Buffer.from("Hello world", "utf8").toString("base64");

await fileHandle.write(buffer, 0, buffer.length);
const readFileData = fs.readFileSync(filePathForHandle);
expect(readFileData).toEqual(Buffer.from(buffer, "utf8"));

await fileHandle.close();
});

test("validateNonStringValuesWrite", async () => {
const filePathForHandle = path.resolve(tmpDir, "tmp-non-string-write.txt");
const fileHandle = await open(filePathForHandle, "w+");
const nonStringValues = [
123,
{},
new Map(),
null,
undefined,
0n,
() => {},
Symbol(),
true,
new String("notPrimitive"),
{
toString() {
return "amObject";
},
},
{ [Symbol.toPrimitive]: hint => "amObject" },
];
for (const nonStringValue of nonStringValues) {
await expect(fileHandle.write(nonStringValue)).rejects.toThrow(
expect.objectContaining({
message: expect.stringMatching(/"buffer"/),
code: "ERR_INVALID_ARG_TYPE",
}),
);
}

await fileHandle.close();
});

//<#END_FILE: test-fs-promises-file-handle-write.js

0 comments on commit c41ff9d

Please sign in to comment.