Skip to content

Commit 176b5ed

Browse files
committed
Add a node-tests package
1 parent f2e47cc commit 176b5ed

File tree

7 files changed

+267
-0
lines changed

7 files changed

+267
-0
lines changed

package-lock.json

Lines changed: 131 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"packages/ferric",
1111
"packages/host",
1212
"packages/node-addon-examples",
13+
"packages/node-tests",
1314
"packages/ferric-example"
1415
],
1516
"homepage": "https://github.com/callstackincubator/react-native-node-api#readme",

packages/node-tests/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node/
2+
tests/

packages/node-tests/package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "@react-native-node-api/node-tests",
3+
"description": "Harness for running the Node.js tests from https://github.com/nodejs/node/tree/main/test",
4+
"type": "commonjs",
5+
"main": "dist/index.js",
6+
"private": true,
7+
"homepage": "https://github.com/callstackincubator/react-native-node-api",
8+
"repository": {
9+
"type": "git",
10+
"url": "git+https://github.com/callstackincubator/react-native-node-api.git",
11+
"directory": "packages/node-tests"
12+
},
13+
"scripts": {
14+
"copy-tests": "tsx scripts/copy-tests.mts",
15+
"gyp-to-cmake": "gyp-to-cmake ./tests",
16+
"build": "tsx scripts/build-tests.mts",
17+
"copy-and-build": "npm run copy-tests && npm run gyp-to-cmake && npm run build",
18+
"test": "npm run copy-and-build"
19+
},
20+
"devDependencies": {
21+
"cmake-js": "^7.3.1",
22+
"cmake-rn": "*",
23+
"gyp-to-cmake": "*",
24+
"prebuildify": "^6.0.1",
25+
"read-pkg": "^9.0.1"
26+
}
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { execSync } from "node:child_process";
2+
3+
import { findCMakeProjects } from "./utils.mts";
4+
5+
const projectDirectories = findCMakeProjects();
6+
7+
for (const projectDirectory of projectDirectories) {
8+
console.log(
9+
`Running "cmake-rn" in ${projectDirectory} to build for React Native`
10+
);
11+
execSync("cmake-rn --out-to-build", {
12+
cwd: projectDirectory,
13+
stdio: "inherit",
14+
});
15+
console.log(`Running "cmake-js" in ${projectDirectory} to build for Node.js`);
16+
execSync("cmake-js", {
17+
cwd: projectDirectory,
18+
stdio: "inherit",
19+
});
20+
console.log();
21+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
import cp from "node:child_process";
4+
5+
import { TESTS_DIR } from "./utils.mts";
6+
7+
const NODE_REPO_URL = "[email protected]:nodejs/node.git";
8+
const NODE_REPO_DIR = path.resolve(import.meta.dirname, "../node");
9+
10+
const ALLOW_LIST = [
11+
"js-native-api/common.h",
12+
"js-native-api/common-inl.h",
13+
"js-native-api/entry_point.h",
14+
"js-native-api/2_function_arguments",
15+
];
16+
17+
console.log("Copying files to", TESTS_DIR);
18+
19+
// Clean up the destination directory before copying
20+
// fs.rmSync(EXAMPLES_DIR, { recursive: true, force: true });
21+
22+
if (!fs.existsSync(NODE_REPO_DIR)) {
23+
console.log(
24+
"Sparse and shallow cloning Node.js repository to",
25+
NODE_REPO_DIR
26+
);
27+
28+
// Init a new git repository
29+
cp.execFileSync("git", ["init", NODE_REPO_DIR], {
30+
stdio: "inherit",
31+
});
32+
// Set the remote origin to the Node.js repository
33+
cp.execFileSync("git", ["remote", "add", "origin", NODE_REPO_URL], {
34+
stdio: "inherit",
35+
cwd: NODE_REPO_DIR,
36+
});
37+
// Enable sparse checkout
38+
cp.execFileSync("git", ["sparse-checkout", "set", "test/js-native-api"], {
39+
stdio: "inherit",
40+
cwd: NODE_REPO_DIR,
41+
});
42+
// Pull the latest changes from the master branch
43+
console.log("Pulling latest changes from Node.js repository...");
44+
cp.execFileSync("git", ["pull", "--depth=1", "origin", "main"], {
45+
stdio: "inherit",
46+
cwd: NODE_REPO_DIR,
47+
});
48+
}
49+
const SRC_DIR = path.join(NODE_REPO_DIR, "test");
50+
console.log("Copying files from", SRC_DIR);
51+
52+
for (const src of ALLOW_LIST) {
53+
const srcPath = path.join(SRC_DIR, src);
54+
const destPath = path.join(TESTS_DIR, src);
55+
56+
if (fs.existsSync(destPath)) {
57+
console.warn(
58+
`Destination path ${destPath} already exists - skipping copy of ${src}.`
59+
);
60+
continue;
61+
}
62+
63+
console.log("Copying from", srcPath, "to", destPath);
64+
fs.cpSync(srcPath, destPath, { recursive: true });
65+
}

packages/node-tests/scripts/utils.mts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { readdirSync, statSync } from "node:fs";
2+
import path from "node:path";
3+
4+
export const TESTS_DIR = path.resolve(import.meta.dirname, "../tests");
5+
6+
export function findCMakeProjects(dir = TESTS_DIR): string[] {
7+
let results: string[] = [];
8+
const files = readdirSync(dir);
9+
10+
for (const file of files) {
11+
const fullPath = path.join(dir, file);
12+
if (statSync(fullPath).isDirectory()) {
13+
results = results.concat(findCMakeProjects(fullPath));
14+
} else if (file === "CMakeLists.txt") {
15+
results.push(dir);
16+
}
17+
}
18+
19+
return results;
20+
}

0 commit comments

Comments
 (0)