2023-02-17 09:58:52 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import { magenta } from "std/fmt/colors.ts";
|
|
|
|
import { dirname, fromFileUrl, join } from "std/path/mod.ts";
|
|
|
|
import { fail } from "std/testing/asserts.ts";
|
2023-03-14 03:56:06 -04:00
|
|
|
import {
|
|
|
|
config,
|
|
|
|
getPathsFromTestSuites,
|
|
|
|
partitionParallelTestPaths,
|
|
|
|
} from "./common.ts";
|
2023-02-17 09:58:52 -05:00
|
|
|
|
|
|
|
// If the test case is invoked like
|
|
|
|
// deno test -A cli/tests/node_compat/test.ts -- <test-names>
|
|
|
|
// Use the test-names as filters
|
|
|
|
const filters = Deno.args;
|
2023-02-27 05:39:33 -05:00
|
|
|
const hasFilters = filters.length > 0;
|
2023-02-17 09:58:52 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This script will run the test files specified in the configuration file
|
|
|
|
*
|
|
|
|
* Each test file will be run independently and wait until completion, if an abnormal
|
|
|
|
* code for the test is reported, the test suite will fail immediately
|
|
|
|
*/
|
|
|
|
|
|
|
|
const toolsPath = dirname(fromFileUrl(import.meta.url));
|
|
|
|
const stdRootUrl = new URL("../../", import.meta.url).href;
|
2023-03-14 03:56:06 -04:00
|
|
|
const testPaths = partitionParallelTestPaths(
|
|
|
|
getPathsFromTestSuites(config.tests),
|
|
|
|
);
|
2023-02-17 09:58:52 -05:00
|
|
|
const cwd = new URL(".", import.meta.url);
|
|
|
|
const importMap = "import_map.json";
|
|
|
|
const windowsIgnorePaths = new Set(
|
|
|
|
getPathsFromTestSuites(config.windowsIgnore),
|
|
|
|
);
|
|
|
|
const darwinIgnorePaths = new Set(
|
|
|
|
getPathsFromTestSuites(config.darwinIgnore),
|
|
|
|
);
|
|
|
|
|
|
|
|
const decoder = new TextDecoder();
|
2023-03-14 03:56:06 -04:00
|
|
|
let testSerialId = 0;
|
2023-02-17 09:58:52 -05:00
|
|
|
|
2023-03-14 03:56:06 -04:00
|
|
|
async function runTest(t: Deno.TestContext, path: string): Promise<void> {
|
2023-02-17 09:58:52 -05:00
|
|
|
// If filter patterns are given and any pattern doesn't match
|
|
|
|
// to the file path, then skip the case
|
|
|
|
if (
|
|
|
|
filters.length > 0 &&
|
|
|
|
filters.every((pattern) => !path.includes(pattern))
|
|
|
|
) {
|
2023-03-14 03:56:06 -04:00
|
|
|
return;
|
2023-02-17 09:58:52 -05:00
|
|
|
}
|
2023-02-20 10:35:04 -05:00
|
|
|
const isTodo = path.includes("TODO");
|
2023-02-17 09:58:52 -05:00
|
|
|
const ignore =
|
|
|
|
(Deno.build.os === "windows" && windowsIgnorePaths.has(path)) ||
|
2023-02-20 10:35:04 -05:00
|
|
|
(Deno.build.os === "darwin" && darwinIgnorePaths.has(path)) || isTodo;
|
2023-03-14 03:56:06 -04:00
|
|
|
await t.step({
|
2023-02-17 09:58:52 -05:00
|
|
|
name: `Node.js compatibility "${path}"`,
|
|
|
|
ignore,
|
2023-03-14 03:56:06 -04:00
|
|
|
sanitizeOps: false,
|
|
|
|
sanitizeResources: false,
|
|
|
|
sanitizeExit: false,
|
2023-02-17 09:58:52 -05:00
|
|
|
fn: async () => {
|
|
|
|
const testCase = join(toolsPath, "test", path);
|
|
|
|
|
|
|
|
const v8Flags = ["--stack-size=4000"];
|
|
|
|
const testSource = await Deno.readTextFile(testCase);
|
|
|
|
// TODO(kt3k): Parse `Flags` directive correctly
|
|
|
|
if (testSource.includes("Flags: --expose_externalize_string")) {
|
|
|
|
v8Flags.push("--expose-externalize-string");
|
|
|
|
}
|
|
|
|
|
|
|
|
const args = [
|
|
|
|
"run",
|
|
|
|
"-A",
|
|
|
|
"--quiet",
|
|
|
|
"--unstable",
|
2023-02-20 10:35:04 -05:00
|
|
|
//"--unsafely-ignore-certificate-errors",
|
2023-02-17 09:58:52 -05:00
|
|
|
"--v8-flags=" + v8Flags.join(),
|
|
|
|
testCase.endsWith(".mjs") ? "--import-map=" + importMap : "runner.ts",
|
|
|
|
testCase,
|
|
|
|
];
|
|
|
|
|
|
|
|
// Pipe stdout in order to output each test result as Deno.test output
|
|
|
|
// That way the tests will respect the `--quiet` option when provided
|
|
|
|
const command = new Deno.Command(Deno.execPath(), {
|
|
|
|
args,
|
|
|
|
env: {
|
|
|
|
DENO_NODE_COMPAT_URL: stdRootUrl,
|
2023-03-14 03:56:06 -04:00
|
|
|
TEST_SERIAL_ID: String(testSerialId++),
|
2023-02-17 09:58:52 -05:00
|
|
|
},
|
|
|
|
cwd,
|
|
|
|
});
|
|
|
|
const { code, stdout, stderr } = await command.output();
|
|
|
|
|
|
|
|
if (code !== 0) {
|
2023-02-27 05:39:33 -05:00
|
|
|
// If the test case failed, show the stdout, stderr, and instruction
|
|
|
|
// for repeating the single test case.
|
|
|
|
if (stdout.length) console.log(decoder.decode(stdout));
|
2023-02-17 09:58:52 -05:00
|
|
|
console.log(`Error: "${path}" failed`);
|
|
|
|
console.log(
|
|
|
|
"You can repeat only this test with the command:",
|
|
|
|
magenta(
|
|
|
|
`./target/debug/deno test -A --import-map cli/tests/node_compat/import_map.json cli/tests/node_compat/test.ts -- ${path}`,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
fail(decoder.decode(stderr));
|
2023-02-27 05:39:33 -05:00
|
|
|
} else if (hasFilters) {
|
|
|
|
// Even if the test case is successful, shows the stdout and stderr
|
|
|
|
// when test case filtering is specified.
|
|
|
|
if (stdout.length) console.log(decoder.decode(stdout));
|
|
|
|
if (stderr.length) console.log(decoder.decode(stderr));
|
2023-02-17 09:58:52 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-14 03:56:06 -04:00
|
|
|
Deno.test("Node.js compatibility", async (t) => {
|
|
|
|
for (const path of testPaths.sequential) {
|
|
|
|
await runTest(t, path);
|
|
|
|
}
|
|
|
|
const pending = [];
|
|
|
|
for (const path of testPaths.parallel) {
|
|
|
|
pending.push(runTest(t, path));
|
|
|
|
}
|
|
|
|
await Promise.all(pending);
|
|
|
|
});
|
|
|
|
|
2023-02-17 09:58:52 -05:00
|
|
|
function checkConfigTestFilesOrder(testFileLists: Array<string[]>) {
|
2023-02-20 10:35:04 -05:00
|
|
|
for (let testFileList of testFileLists) {
|
|
|
|
testFileList = testFileList.filter((name) => !name.startsWith("TODO:"));
|
2023-02-17 09:58:52 -05:00
|
|
|
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.`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-27 05:39:33 -05:00
|
|
|
if (!hasFilters) {
|
2023-02-17 09:58:52 -05:00
|
|
|
Deno.test("checkConfigTestFilesOrder", function () {
|
|
|
|
checkConfigTestFilesOrder([
|
|
|
|
...Object.keys(config.ignore).map((suite) => config.ignore[suite]),
|
|
|
|
...Object.keys(config.tests).map((suite) => config.tests[suite]),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
}
|