-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest-utils.ts
More file actions
34 lines (28 loc) · 944 Bytes
/
test-utils.ts
File metadata and controls
34 lines (28 loc) · 944 Bytes
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
import { TestContext } from "node:test";
import os from "node:os";
import fs from "node:fs";
import path from "node:path";
export interface FileMap {
[key: string]: string | FileMap;
}
function writeFiles(fromPath: string, files: FileMap) {
for (const [filePath, content] of Object.entries(files)) {
const fullPath = path.join(fromPath, filePath);
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
if (typeof content === "string") {
fs.writeFileSync(fullPath, content, "utf8");
} else {
writeFiles(fullPath, content);
}
}
}
export function setupTempDirectory(context: TestContext, files: FileMap) {
const tempDirectoryPath = fs.realpathSync(
fs.mkdtempSync(path.join(os.tmpdir(), "react-native-node-api-test-"))
);
context.after(() => {
fs.rmSync(tempDirectoryPath, { recursive: true, force: true });
});
writeFiles(tempDirectoryPath, files);
return tempDirectoryPath;
}