Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: try using only sync fs in test #1139

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changeset/mighty-pugs-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
37 changes: 20 additions & 17 deletions packages/util-body-length-node/src/calculateBodyLength.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createReadStream, lstatSync, promises, writeFileSync } from "fs";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";

Expand Down Expand Up @@ -41,49 +41,52 @@ describe(calculateBodyLength.name, () => {
});

it("should handle a Readable from a file", async () => {
const tmpDir = await promises.mkdtemp(path.join(os.tmpdir(), "test1-"));
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "test1-"));
const filePath = path.join(tmpDir, "foo");
writeFileSync(filePath, "foo");
const handle = await promises.open(filePath, "r");
const readStream = createReadStream(filePath, { fd: handle.fd });
fs.writeFileSync(filePath, "foo");
const handle = fs.openSync(filePath, "r");
const readStream = fs.createReadStream(filePath, { fd: handle });
expect(calculateBodyLength(readStream)).toEqual(3);
readStream.destroy();
await promises.unlink(filePath);
await promises.rmdir(tmpDir);
fs.unlinkSync(filePath);
fs.rmdirSync(tmpDir);
});

it("should handle Readable with start end from a file", async () => {
const tmpDir = await promises.mkdtemp(path.join(os.tmpdir(), "test2-"));
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "test2-"));
const filePath = path.join(tmpDir, "foo");
writeFileSync(filePath, "foo");
const handle = await promises.open(filePath, "r");
const readStream = createReadStream(filePath, { fd: handle.fd, start: 1, end: 1 });
fs.writeFileSync(filePath, "foo");
const handle = fs.openSync(filePath, "r");
const readStream = fs.createReadStream(filePath, { fd: handle, start: 1, end: 1 });
expect(calculateBodyLength(readStream)).toEqual(1);
readStream.destroy();
await promises.unlink(filePath);
await promises.rmdir(tmpDir);
fs.unlinkSync(filePath);
fs.rmdirSync(tmpDir);
});

describe("fs.ReadStream", () => {
const fileSize = lstatSync(__filename).size;
const fileSize = fs.lstatSync(__filename).size;

describe("should handle stream created using fs.createReadStream", () => {
it("when path is a string", () => {
const fsReadStream = createReadStream(__filename);
const fsReadStream = fs.createReadStream(__filename);
expect(calculateBodyLength(fsReadStream)).toEqual(fileSize);
fsReadStream.close();
});

it("when path is a Buffer", () => {
const fsReadStream = createReadStream(Buffer.from(__filename));
const fsReadStream = fs.createReadStream(Buffer.from(__filename));
expect(calculateBodyLength(fsReadStream)).toEqual(fileSize);
fsReadStream.close();
});
});

it("should handle stream created using fd.createReadStream", async () => {
const fd = await promises.open(__filename, "r");
const fd = await fs.promises.open(__filename, "r");
if ((fd as any).createReadStream) {
const fdReadStream = (fd as any).createReadStream();
expect(calculateBodyLength(fdReadStream)).toEqual(fileSize);
fdReadStream.close();
}
});
});
Expand Down
Loading