-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-utils.ts
More file actions
40 lines (34 loc) · 1.27 KB
/
test-utils.ts
File metadata and controls
40 lines (34 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import type { PathLike } from "node:fs";
import { mkdtemp, mkdir, writeFile, rm, readFile as fsReadFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { sep } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { onTestFinished } from "vitest";
export interface Fixture {
root: URL;
resolve: (...segments: string[]) => URL;
readFile: (file: PathLike) => Promise<string>;
cleanup: () => Promise<void>;
}
export async function createFixture(files: Record<string, string>): Promise<Fixture> {
const root = new URL(`bsh-`, `file://${tmpdir()}/`);
const path = await mkdtemp(fileURLToPath(root));
const base = pathToFileURL(path + sep);
for (const [name, content] of Object.entries(files)) {
const url = new URL(name, base);
const dir = new URL("./", url);
await mkdir(dir, { recursive: true });
await writeFile(url, content, "utf8");
}
const cleanup = () => rm(path, { recursive: true, force: true });
onTestFinished(cleanup);
const resolve = (...segments: string[]) => new URL(`./${segments.join("/")}`, base);
const readFile = (file: PathLike) =>
fsReadFile(new URL(`./${file}`, base), { encoding: "utf-8" });
return {
root: pathToFileURL(path),
resolve,
readFile,
cleanup,
};
}