import { magenta } from "../../fmt/colors.ts";import { dirname, fromFileUrl, join } from "../../path/mod.ts";import { fail } from "../../testing/asserts.ts";import { config, getPathsFromTestSuites } from "./common.ts";
const filters = Deno.args;
const toolsPath = dirname(fromFileUrl(import.meta.url));const stdRootUrl = new URL("../../", import.meta.url).href;const testPaths = getPathsFromTestSuites(config.tests);const cwd = fromFileUrl(new URL("./", import.meta.url));const requireTs = "require.ts";const windowsIgnorePaths = new Set( getPathsFromTestSuites(config.windowsIgnore),);const darwinIgnorePaths = new Set( getPathsFromTestSuites(config.darwinIgnore),);
const decoder = new TextDecoder();
for await (const path of testPaths) { if ( filters.length > 0 && filters.every((pattern) => !path.includes(pattern)) ) { continue; } const ignore = (Deno.build.os === "windows" && windowsIgnorePaths.has(path)) || (Deno.build.os === "darwin" && darwinIgnorePaths.has(path)); Deno.test({ name: `Node.js compatibility "${path}"`, ignore, fn: async () => { const targetTestPath = join(toolsPath, config.suitesFolder, path);
const v8Flags = ["--stack-size=4000"]; const testSourceCode = await Deno.readTextFile(targetTestPath); if (testSourceCode.includes("Flags: --expose_externalize_string")) { v8Flags.push("--expose-externalize-string"); }
const args = [ "run", "-A", "--quiet", "--unstable", "--no-check", "--v8-flags=" + v8Flags.join(), requireTs, targetTestPath, ];
const { status, stdout, stderr } = await Deno.spawn(Deno.execPath(), { args, env: { DENO_NODE_COMPAT_URL: stdRootUrl, }, cwd, });
const decodedStderr = decoder.decode(stderr); if (stderr.length) console.error(decodedStderr); if (stdout.length) console.log(decoder.decode(stdout));
if (status.code !== 0) { console.log(`Error: "${path}" failed`); console.log( "You can repeat only this test with the command:", magenta(`deno test -A node/_tools/test.ts -- ${path}`), ); fail(decodedStderr); } }, });}
function checkConfigTestFilesOrder(testFileLists: Array<string[]>) { for (const testFileList of testFileLists) { const sortedTestList = JSON.parse(JSON.stringify(testFileList)); sortedTestList.sort(); if (JSON.stringify(testFileList) !== JSON.stringify(sortedTestList)) { throw new Error( `File names in \`config.json\` are not correct order.`, ); } }}
Deno.test("checkConfigTestFilesOrder", function () { checkConfigTestFilesOrder([ ...Object.keys(config.ignore).map((suite) => config.ignore[suite]), ...Object.keys(config.tests).map((suite) => config.tests[suite]), ]);});