Skip to content

Commit

Permalink
test: try using only sync fs in test (#1139)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe authored Jan 10, 2024
1 parent cb35507 commit 33bb5cd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
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

0 comments on commit 33bb5cd

Please sign in to comment.