forked from TypeCellOS/BlockNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryFileSnapshotUtil.ts
More file actions
73 lines (59 loc) · 2.12 KB
/
binaryFileSnapshotUtil.ts
File metadata and controls
73 lines (59 loc) · 2.12 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import fs from "fs";
import path from "path";
const NAME_WORKER_STATE = "__vitest_worker__";
// from https://github.com/vitest-dev/vitest/blob/main/packages/vitest/src/runtime/utils.ts#L8
export function getWorkerState(): any {
// @ts-expect-error untyped global
const workerState = globalThis[NAME_WORKER_STATE];
if (!workerState) {
const errorMsg =
"Vitest failed to access its internal state." +
"\n\nOne of the following is possible:" +
'\n- "vitest" is imported directly without running "vitest" command' +
'\n- "vitest" is imported inside "globalSetup" (to fix this, use "setupFiles" instead, because "globalSetup" runs in a different context)' +
"\n- Otherwise, it might be a Vitest bug. Please report it to https://github.com/vitest-dev/vitest/issues\n";
throw new Error(errorMsg);
}
return workerState;
}
function getSnapshotOptions() {
const option: "new" | "all" | "none" =
getWorkerState().ctx.config.snapshotOptions.updateSnapshot;
return option;
}
// async function createMD5FromBuffer(buffer: Buffer) {
// const hash = crypto.createHash("md5");
// hash.update(buffer);
// return hash.digest("hex");
// }
// async function createMD5FromFile(filePath: string) {
// const hash = crypto.createHash("md5");
// if (!fs.existsSync(filePath)) {
// return undefined;
// }
// await pipeline(fs.createReadStream(filePath), async function (source) {
// for await (const chunk of source) {
// hash.update(chunk);
// }
// });
// return hash.digest("hex");
// }
export async function toMatchBinaryFileSnapshot(
buffer: Buffer,
filepath: string,
) {
const fileBuffer = fs.existsSync(filepath)
? fs.readFileSync(filepath)
: undefined;
const same = fileBuffer && buffer.equals(fileBuffer); // && bufferMD5 === fileMD5;
const option = getSnapshotOptions();
if (same) {
return;
}
if (option === "none" || (option === "new" && fileBuffer !== undefined)) {
throw new Error(`${filepath} not matching `);
}
// create dir if not exists
fs.mkdirSync(path.dirname(filepath), { recursive: true });
fs.writeFileSync(filepath, buffer);
}