Add more tests for Node FS

This commit is contained in:
Jarred Sumner
2022-04-02 05:14:13 -07:00
parent 51e5d3ea70
commit c2ed8d0240
4 changed files with 60 additions and 1 deletions

View File

@@ -5,6 +5,11 @@ import {
readFileSync,
writeFileSync,
readFile,
read,
openSync,
readSync,
closeSync,
writeSync,
} from "node:fs";
const Buffer = globalThis.Buffer || Uint8Array;
@@ -24,6 +29,57 @@ describe("mkdirSync", () => {
});
});
describe("readSync", () => {
const firstFourBytes = new Uint32Array(
new TextEncoder().encode("File").buffer
)[0];
it("works with a position set to 0", () => {
const fd = openSync(import.meta.dir + "/readFileSync.txt", "r");
const four = new Uint8Array(4);
{
const count = readSync(fd, four, 0, 4, 0);
const u32 = new Uint32Array(four.buffer)[0];
expect(u32).toBe(firstFourBytes);
expect(count).toBe(4);
}
closeSync(fd);
});
it("works without position set", () => {
const fd = openSync(import.meta.dir + "/readFileSync.txt", "r");
const four = new Uint8Array(4);
{
const count = readSync(fd, four);
const u32 = new Uint32Array(four.buffer)[0];
expect(u32).toBe(firstFourBytes);
expect(count).toBe(4);
}
closeSync(fd);
});
});
describe("writeSync", () => {
it("works with a position set to 0", () => {
const fd = openSync(import.meta.dir + "/writeFileSync.txt", "w+");
const four = new Uint8Array(4);
{
const count = writeSync(fd, new TextEncoder().encode("File"), 0, 4, 0);
expect(count).toBe(4);
}
closeSync(fd);
});
it("works without position set", () => {
const fd = openSync(import.meta.dir + "/writeFileSync.txt", "w+");
const four = new Uint8Array(4);
{
const count = writeSync(fd, new TextEncoder().encode("File"));
expect(count).toBe(4);
}
closeSync(fd);
});
});
describe("readFileSync", () => {
it("works", () => {
const text = readFileSync(import.meta.dir + "/readFileSync.txt", "utf8");

View File

@@ -0,0 +1 @@
File

View File

@@ -51,9 +51,10 @@ pub const AllocatorConfiguration = struct {
long_running: bool = false,
};
pub const Mimalloc = @import("./allocators/mimalloc.zig");
pub inline fn mimalloc_cleanup(force: bool) void {
if (comptime use_mimalloc) {
const Mimalloc = @import("./allocators/mimalloc.zig");
Mimalloc.mi_collect(force);
}
}

View File

@@ -173,6 +173,7 @@ typedef struct {
uint32_t len;
uint32_t byte_len;
uint8_t cell_type;
uint64_t _value;
} Bun__ArrayBuffer;
#ifndef STRING_POINTER