|
1 | 1 | import { expect } from "@std/expect"; |
2 | 2 | import { dirname, join } from "@std/path"; |
3 | 3 | import { beforeEach, describe, it } from "@effectionx/bdd"; |
4 | | -import { each, stream, until } from "effection"; |
5 | | -import { mkdir } from "node:fs"; |
6 | | -import { promisify } from "node:util"; |
| 4 | +import { each, until } from "effection"; |
| 5 | +import * as fsp from "node:fs/promises"; |
| 6 | +import { mkdtemp } from "node:fs/promises"; |
| 7 | +import { tmpdir } from "node:os"; |
7 | 8 |
|
8 | 9 | import { JSONLStore } from "./jsonl.ts"; |
9 | 10 | import type { Store } from "./types.ts"; |
10 | 11 |
|
11 | | -// using promisify there because Deno's ensure doesn't work |
12 | | -// correctly in Node. We should run these tests in Node |
13 | | -// to make sure that it'll work in Node too. |
14 | | - |
15 | 12 | describe("JSONLStore", () => { |
16 | 13 | let store: Store; |
17 | 14 | let tmpDir: string; |
18 | 15 |
|
19 | 16 | async function readTmpFile(fileName: string) { |
20 | | - return await Deno.readTextFile(`${tmpDir}/${fileName}`); |
| 17 | + return await fsp.readFile(`${tmpDir}/${fileName}`, "utf-8"); |
21 | 18 | } |
22 | 19 |
|
23 | 20 | async function writeTmpFile(fileName: string, data: string) { |
24 | | - await promisify(mkdir)(join(tmpDir, dirname(fileName)), { |
| 21 | + await fsp.mkdir(join(tmpDir, dirname(fileName)), { |
25 | 22 | recursive: true, |
26 | 23 | }); |
27 | | - await Deno.writeTextFile(join(tmpDir, fileName), data); |
| 24 | + await fsp.writeFile(join(tmpDir, fileName), data, "utf-8"); |
28 | 25 | } |
29 | 26 |
|
30 | 27 | async function appendTmpFile(fileName: string, data: string) { |
31 | 28 | const destination = join(tmpDir, fileName); |
32 | | - const file = await Deno.open(destination, { append: true }); |
33 | | - await file.write(new TextEncoder().encode(data)); |
34 | | - file.close(); |
| 29 | + await fsp.appendFile(destination, data, "utf-8"); |
35 | 30 | } |
36 | 31 |
|
37 | 32 | beforeEach(function* () { |
38 | | - tmpDir = yield* until(Deno.makeTempDir()); |
| 33 | + tmpDir = yield* until(mkdtemp(join(tmpdir(), "jsonl-test-"))); |
39 | 34 | store = JSONLStore.from({ location: tmpDir }); |
40 | 35 | }); |
41 | 36 |
|
@@ -69,18 +64,16 @@ describe("JSONLStore", () => { |
69 | 64 | }); |
70 | 65 | it("clears store when called clear", function* () { |
71 | 66 | yield* store.clear(); |
72 | | - const entries = []; |
73 | | - for (const dirEntry of yield* each(stream(Deno.readDir(tmpDir)))) { |
74 | | - entries.push(dirEntry); |
75 | | - yield* each.next(); |
76 | | - } |
| 67 | + const entries = yield* until(fsp.readdir(tmpDir)); |
77 | 68 | expect(entries).toHaveLength(0); |
78 | 69 | }); |
79 | 70 | }); |
80 | 71 |
|
81 | 72 | describe("reading content of a file", () => { |
82 | 73 | beforeEach(function* () { |
83 | | - yield* until(Deno.writeTextFile(join(tmpDir, "test.jsonl"), `1\n2\n3\n`)); |
| 74 | + yield* until( |
| 75 | + fsp.writeFile(join(tmpDir, "test.jsonl"), `1\n2\n3\n`, "utf-8"), |
| 76 | + ); |
84 | 77 | }); |
85 | 78 | it("streams multiple items", function* () { |
86 | 79 | const items: number[] = []; |
|
0 commit comments