Add node:test:cp package.json script

This commit is contained in:
Jarred Sumner
2025-05-29 16:48:37 -07:00
parent da87890532
commit 316c8d6c48
2 changed files with 113 additions and 0 deletions

View File

@@ -76,6 +76,7 @@
"zig-format:check": "bun run analysis:no-llvm --target zig-format-check",
"prettier": "bunx prettier@latest --plugin=prettier-plugin-organize-imports --config .prettierrc --write scripts packages src docs 'test/**/*.{test,spec}.{ts,tsx,js,jsx,mts,mjs,cjs,cts}' '!test/**/*fixture*.*'",
"node:test": "node ./scripts/runner.node.mjs --quiet --exec-path=$npm_execpath --node-tests ",
"node:test:cp": "bun ./scripts/fetch-node-test.ts ",
"clean:zig": "rm -rf build/debug/cache/zig build/debug/CMakeCache.txt 'build/debug/*.o' .zig-cache zig-out || true",
"sync-webkit-source": "bun ./scripts/sync-webkit-source.ts"
}

112
scripts/fetch-node-test.ts Normal file
View File

@@ -0,0 +1,112 @@
import { mkdirSync, writeFileSync } from "fs";
import path, { dirname, join } from "path";
const options: RequestInit = {};
if (process.env.GITHUB_TOKEN) {
options.headers = {
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
};
}
async function fetchNodeTest(testName: string) {
const nodeRepoUrl = "https://raw.githubusercontent.com/nodejs/node/main";
const extensions = ["js", "mjs", "ts"];
const testDirs = ["test/parallel", "test/sequential"];
// Try different combinations of test name patterns
const testNameVariations = [
testName,
testName.startsWith("test-") ? testName : `test-${testName}`,
testName.replace(/^test-/, ""),
];
for (const testDir of testDirs) {
for (const nameVariation of testNameVariations) {
// Try with extensions
for (const ext of extensions) {
const testPath = `${testDir}/${nameVariation}.${ext}`;
const url = `${nodeRepoUrl}/${testPath}`;
try {
console.log(`Trying: ${url}`);
const response = await fetch(url, options);
if (response.ok) {
const content = await response.text();
const localPath = join("test/js/node", testPath);
// Create directory if it doesn't exist
mkdirSync(dirname(localPath), { recursive: true });
// Write the file
writeFileSync(localPath, content);
console.log(
`✅ Successfully fetched and saved: ${localPath} (${new Intl.NumberFormat("en-US", {
notation: "compact",
unit: "kilobyte",
}).format(Buffer.byteLength(content, "utf-8"))})`,
);
return localPath;
}
} catch (error) {
// Continue to next variation
}
}
// Try without extension
const testPath = `${testDir}/${nameVariation}`;
const url = `${nodeRepoUrl}/${testPath}`;
try {
console.log(`Trying: ${url}`);
const response = await fetch(url, options);
if (response.ok) {
const content = await response.text();
const localPath = join("test/js/node", testPath);
// Create directory if it doesn't exist
mkdirSync(dirname(localPath), { recursive: true });
// Write the file
writeFileSync(localPath, content);
console.log(
`✅ Successfully fetched and saved: ${localPath} (${new Intl.NumberFormat("en-US", {
notation: "compact",
unit: "kilobyte",
}).format(Buffer.byteLength(content, "utf-8"))})`,
);
return localPath;
}
} catch (error) {
// Continue to next variation
}
}
}
throw new Error(`❌ Could not find test: ${testName}`);
}
// Get test name from command line arguments
let testName = process.argv[2];
if (testName.startsWith(path.join(import.meta.dirname, ".."))) {
testName = testName.slice(path.join(import.meta.dirname, "..").length);
}
if (testName.startsWith("test/parallel/")) {
testName = testName.replace("test/parallel/", "");
} else if (testName.startsWith("test/sequential/")) {
testName = testName.replace("test/sequential/", "");
}
if (!testName) {
console.error("Usage: bun scripts/fetch-node-test.ts <test-name>");
process.exit(1);
}
try {
await fetchNodeTest(testName);
} catch (error) {
console.error(error.message);
process.exit(1);
}