Skip to content

Commit 358dc16

Browse files
committed
Refactor setupTempDirectory to accept objects for dirs
1 parent f1720e2 commit 358dc16

File tree

2 files changed

+42
-27
lines changed

2 files changed

+42
-27
lines changed

packages/host/src/node/path-utils.test.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -268,24 +268,30 @@ describe("getLibraryName", () => {
268268
describe("findPackageDependencyPaths", () => {
269269
it("should find package dependency paths", (context) => {
270270
const tempDir = setupTempDirectory(context, {
271-
"node_modules/lib-a/package.json": JSON.stringify({
272-
name: "lib-a",
273-
main: "index.js",
274-
}),
275-
"node_modules/lib-a/index.js": "",
276-
"test-package/node_modules/lib-b/package.json": JSON.stringify({
277-
name: "lib-b",
278-
main: "index.js",
279-
}),
280-
"test-package/node_modules/lib-b/index.js": "",
281-
"test-package/package.json": JSON.stringify({
282-
name: "test-package",
283-
dependencies: {
284-
"lib-a": "^1.0.0",
285-
"lib-b": "^1.0.0",
271+
"node_modules/lib-a": {
272+
"package.json": JSON.stringify({
273+
name: "lib-a",
274+
main: "index.js",
275+
}),
276+
"index.js": "",
277+
},
278+
"test-package": {
279+
"package.json": JSON.stringify({
280+
name: "test-package",
281+
dependencies: {
282+
"lib-a": "^1.0.0",
283+
"lib-b": "^1.0.0",
284+
},
285+
}),
286+
"src/index.js": "console.log('Hello, world!')",
287+
"node_modules/lib-b": {
288+
"package.json": JSON.stringify({
289+
name: "lib-b",
290+
main: "index.js",
291+
}),
292+
"index.js": "",
286293
},
287-
}),
288-
"test-package/src/index.js": "console.log('Hello, world!')",
294+
},
289295
});
290296

291297
const result = findPackageDependencyPaths(

packages/host/src/node/test-utils.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,32 @@ import os from "node:os";
33
import fs from "node:fs";
44
import path from "node:path";
55

6-
export function setupTempDirectory(
7-
context: TestContext,
8-
files: Record<string, string>
9-
) {
6+
export interface FileMap {
7+
[key: string]: string | FileMap;
8+
}
9+
10+
function writeFiles(fromPath: string, files: FileMap) {
11+
for (const [filePath, content] of Object.entries(files)) {
12+
const fullPath = path.join(fromPath, filePath);
13+
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
14+
if (typeof content === "string") {
15+
fs.writeFileSync(fullPath, content, "utf8");
16+
} else {
17+
writeFiles(fullPath, content);
18+
}
19+
}
20+
}
21+
22+
export function setupTempDirectory(context: TestContext, files: FileMap) {
1023
const tempDirectoryPath = fs.realpathSync(
11-
fs.mkdtempSync(path.join(os.tmpdir(), "babel-transform-test-"))
24+
fs.mkdtempSync(path.join(os.tmpdir(), "react-native-node-api-test-"))
1225
);
1326

1427
context.after(() => {
1528
fs.rmSync(tempDirectoryPath, { recursive: true, force: true });
1629
});
1730

18-
for (const [filePath, content] of Object.entries(files)) {
19-
const fullPath = path.join(tempDirectoryPath, filePath);
20-
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
21-
fs.writeFileSync(fullPath, content, "utf8");
22-
}
31+
writeFiles(tempDirectoryPath, files);
2332

2433
return tempDirectoryPath;
2534
}

0 commit comments

Comments
 (0)