Skip to content

Commit a4fc5e9

Browse files
authored
Fix readFile bindings (#32)
* Fix readFile binding and add test Test stays self-contained and read-only by reading the test file itself. * Fix readFileWith type signature and add test When an encoding is specified the output type is no longer a buffer, but instead a string.
1 parent 8c91a32 commit a4fc5e9

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

src/Fs.re

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,10 @@ module FileHandle = {
227227
(t, Buffer.t, ~offset: int, ~length: int, ~position: int) =>
228228
Js.Promise.t(readInfo) =
229229
"read";
230-
[@bs.send] external readFile: t => Js.Promise.t(Buffer.t) = "read";
230+
[@bs.send] external readFile: t => Js.Promise.t(Buffer.t) = "readFile";
231231
[@bs.send]
232-
external readFileWith: (t, readFileOptions) => Js.Promise.t(Buffer.t) =
233-
"read";
232+
external readFileWith: (t, ~encoding: string) => Js.Promise.t(string) =
233+
"readFile";
234234

235235
[@bs.send] external stat: t => Js.Promise.t(Stats.t) = "stat";
236236
[@bs.send] external sync: t => Js.Promise.t(unit) = "sync";

test/__tests__/Fs_test.re

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
open Fs;
2+
open Jest;
3+
open Js.Promise;
4+
5+
describe("Fs", () => {
6+
testPromise("readFile should read entire file", () => {
7+
open_(Global.filename, Flag.read)
8+
|> then_(fh =>
9+
Fs.FileHandle.readFile(fh)
10+
|> then_(buffer =>
11+
FileHandle.close(fh) |> then_(_ => resolve(buffer))
12+
)
13+
)
14+
|> then_(buffer => {
15+
let needle = "Random string: Gh2e71pdHhPxU";
16+
Expect.(
17+
expect(buffer->Buffer.indexOfString(needle))
18+
|> toBeGreaterThan(0)
19+
|> resolve
20+
);
21+
})
22+
});
23+
testPromise("readFileWith should read entire file as a string", () => {
24+
open_(Global.filename, Flag.read)
25+
|> then_(fh =>
26+
Fs.FileHandle.readFileWith(fh, ~encoding="UTF-8")
27+
|> then_(buffer =>
28+
FileHandle.close(fh) |> then_(_ => resolve(buffer))
29+
)
30+
)
31+
|> then_(content => {
32+
let needle = "Random string: uCF6c5f3Arrq";
33+
Expect.(
34+
expect(Js.String.indexOf(needle, content))
35+
|> toBeGreaterThan(0)
36+
|> resolve
37+
);
38+
})
39+
});
40+
});

0 commit comments

Comments
 (0)