From 316c8d6c489747f1f4a4f8e30b04c608c475670b Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Thu, 29 May 2025 16:48:37 -0700 Subject: [PATCH] Add `node:test:cp` package.json script --- package.json | 1 + scripts/fetch-node-test.ts | 112 +++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 scripts/fetch-node-test.ts diff --git a/package.json b/package.json index 2d58e39f1c..b6e7d28a7b 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/scripts/fetch-node-test.ts b/scripts/fetch-node-test.ts new file mode 100644 index 0000000000..dbf38ac56d --- /dev/null +++ b/scripts/fetch-node-test.ts @@ -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 "); + process.exit(1); +} + +try { + await fetchNodeTest(testName); +} catch (error) { + console.error(error.message); + process.exit(1); +}