|
1 |
| -import { createReadStream, lstatSync, promises, writeFileSync } from "fs"; |
| 1 | +import * as fs from "fs"; |
2 | 2 | import * as os from "os";
|
3 | 3 | import * as path from "path";
|
4 | 4 |
|
@@ -41,49 +41,52 @@ describe(calculateBodyLength.name, () => {
|
41 | 41 | });
|
42 | 42 |
|
43 | 43 | it("should handle a Readable from a file", async () => {
|
44 |
| - const tmpDir = await promises.mkdtemp(path.join(os.tmpdir(), "test1-")); |
| 44 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "test1-")); |
45 | 45 | const filePath = path.join(tmpDir, "foo");
|
46 |
| - writeFileSync(filePath, "foo"); |
47 |
| - const handle = await promises.open(filePath, "r"); |
48 |
| - const readStream = createReadStream(filePath, { fd: handle.fd }); |
| 46 | + fs.writeFileSync(filePath, "foo"); |
| 47 | + const handle = fs.openSync(filePath, "r"); |
| 48 | + const readStream = fs.createReadStream(filePath, { fd: handle }); |
49 | 49 | expect(calculateBodyLength(readStream)).toEqual(3);
|
50 | 50 | readStream.destroy();
|
51 |
| - await promises.unlink(filePath); |
52 |
| - await promises.rmdir(tmpDir); |
| 51 | + fs.unlinkSync(filePath); |
| 52 | + fs.rmdirSync(tmpDir); |
53 | 53 | });
|
54 | 54 |
|
55 | 55 | it("should handle Readable with start end from a file", async () => {
|
56 |
| - const tmpDir = await promises.mkdtemp(path.join(os.tmpdir(), "test2-")); |
| 56 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "test2-")); |
57 | 57 | const filePath = path.join(tmpDir, "foo");
|
58 |
| - writeFileSync(filePath, "foo"); |
59 |
| - const handle = await promises.open(filePath, "r"); |
60 |
| - const readStream = createReadStream(filePath, { fd: handle.fd, start: 1, end: 1 }); |
| 58 | + fs.writeFileSync(filePath, "foo"); |
| 59 | + const handle = fs.openSync(filePath, "r"); |
| 60 | + const readStream = fs.createReadStream(filePath, { fd: handle, start: 1, end: 1 }); |
61 | 61 | expect(calculateBodyLength(readStream)).toEqual(1);
|
62 | 62 | readStream.destroy();
|
63 |
| - await promises.unlink(filePath); |
64 |
| - await promises.rmdir(tmpDir); |
| 63 | + fs.unlinkSync(filePath); |
| 64 | + fs.rmdirSync(tmpDir); |
65 | 65 | });
|
66 | 66 |
|
67 | 67 | describe("fs.ReadStream", () => {
|
68 |
| - const fileSize = lstatSync(__filename).size; |
| 68 | + const fileSize = fs.lstatSync(__filename).size; |
69 | 69 |
|
70 | 70 | describe("should handle stream created using fs.createReadStream", () => {
|
71 | 71 | it("when path is a string", () => {
|
72 |
| - const fsReadStream = createReadStream(__filename); |
| 72 | + const fsReadStream = fs.createReadStream(__filename); |
73 | 73 | expect(calculateBodyLength(fsReadStream)).toEqual(fileSize);
|
| 74 | + fsReadStream.close(); |
74 | 75 | });
|
75 | 76 |
|
76 | 77 | it("when path is a Buffer", () => {
|
77 |
| - const fsReadStream = createReadStream(Buffer.from(__filename)); |
| 78 | + const fsReadStream = fs.createReadStream(Buffer.from(__filename)); |
78 | 79 | expect(calculateBodyLength(fsReadStream)).toEqual(fileSize);
|
| 80 | + fsReadStream.close(); |
79 | 81 | });
|
80 | 82 | });
|
81 | 83 |
|
82 | 84 | it("should handle stream created using fd.createReadStream", async () => {
|
83 |
| - const fd = await promises.open(__filename, "r"); |
| 85 | + const fd = await fs.promises.open(__filename, "r"); |
84 | 86 | if ((fd as any).createReadStream) {
|
85 | 87 | const fdReadStream = (fd as any).createReadStream();
|
86 | 88 | expect(calculateBodyLength(fdReadStream)).toEqual(fileSize);
|
| 89 | + fdReadStream.close(); |
87 | 90 | }
|
88 | 91 | });
|
89 | 92 | });
|
|
0 commit comments