2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-04-05 07:15:57 -04:00
|
|
|
|
2024-08-20 15:14:37 -04:00
|
|
|
// deno-lint-ignore-file no-console
|
|
|
|
|
2023-04-05 07:15:57 -04:00
|
|
|
/**
|
|
|
|
* This script will run the test files specified in the configuration file.
|
|
|
|
*
|
|
|
|
* Each test file will be run independently (in a separate process as this is
|
|
|
|
* what Node.js is doing) and we wait until it completes. If the process reports
|
|
|
|
* an abnormal code, the test is reported and the test suite will fail
|
|
|
|
* immediately.
|
|
|
|
*
|
|
|
|
* Some tests check for presence of certain `process.exitCode`.
|
|
|
|
* Some tests depends on directories/files created by other tests - they must
|
|
|
|
* all share the same working directory.
|
|
|
|
*/
|
|
|
|
|
2024-07-25 01:30:28 -04:00
|
|
|
import { magenta } from "@std/fmt/colors";
|
|
|
|
import { pooledMap } from "@std/async/pool";
|
|
|
|
import { dirname, fromFileUrl, join } from "@std/path";
|
|
|
|
import { assertEquals, fail } from "@std/assert";
|
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
|
2024-03-05 15:49:21 -05:00
|
|
|
// deno test -A tests/node_compat/test.ts -- <test-names>
|
2023-04-05 07:15:57 -04:00
|
|
|
// Use the <test-names> as filters
|
2023-02-17 09:58:52 -05:00
|
|
|
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
|
|
|
const toolsPath = dirname(fromFileUrl(import.meta.url));
|
2023-03-14 03:56:06 -04:00
|
|
|
const testPaths = partitionParallelTestPaths(
|
2024-07-23 23:12:08 -04:00
|
|
|
getPathsFromTestSuites(config.tests).concat(
|
|
|
|
getPathsFromTestSuites(config.ignore),
|
|
|
|
),
|
2023-03-14 03:56:06 -04:00
|
|
|
);
|
2023-02-17 09:58:52 -05:00
|
|
|
const cwd = new URL(".", import.meta.url);
|
|
|
|
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
|
|
|
|
2024-08-06 08:52:53 -04:00
|
|
|
function parseFlags(source: string): string[] {
|
|
|
|
const line = /^\/\/ Flags: (.+)$/um.exec(source);
|
|
|
|
if (line == null) return [];
|
|
|
|
return line[1].split(" ");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
const ignore =
|
|
|
|
(Deno.build.os === "windows" && windowsIgnorePaths.has(path)) ||
|
2023-03-21 09:38:07 -04:00
|
|
|
(Deno.build.os === "darwin" && darwinIgnorePaths.has(path));
|
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);
|
2023-05-23 18:11:37 -04:00
|
|
|
const envVars: Record<string, string> = {};
|
2024-08-06 08:52:53 -04:00
|
|
|
const knownGlobals: string[] = [];
|
|
|
|
parseFlags(testSource).forEach((flag) => {
|
|
|
|
switch (flag) {
|
|
|
|
case "--expose_externalize_string":
|
|
|
|
v8Flags.push("--expose-externalize-string");
|
|
|
|
knownGlobals.push("createExternalizableString");
|
|
|
|
break;
|
|
|
|
case "--expose-gc":
|
|
|
|
v8Flags.push("--expose-gc");
|
|
|
|
knownGlobals.push("gc");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (knownGlobals.length > 0) {
|
|
|
|
envVars["NODE_TEST_KNOWN_GLOBALS"] = knownGlobals.join(",");
|
2023-02-17 09:58:52 -05:00
|
|
|
}
|
2024-04-02 19:20:48 -04:00
|
|
|
// TODO(nathanwhit): once we match node's behavior on executing
|
|
|
|
// `node:test` tests when we run a file, we can remove this
|
|
|
|
const usesNodeTest = testSource.includes("node:test");
|
2023-02-17 09:58:52 -05:00
|
|
|
const args = [
|
2024-04-02 19:20:48 -04:00
|
|
|
usesNodeTest ? "test" : "run",
|
2023-02-17 09:58:52 -05:00
|
|
|
"-A",
|
|
|
|
"--quiet",
|
2023-02-20 10:35:04 -05:00
|
|
|
//"--unsafely-ignore-certificate-errors",
|
2024-03-15 20:24:13 -04:00
|
|
|
"--unstable-unsafe-proto",
|
2023-11-29 01:42:58 -05:00
|
|
|
"--unstable-bare-node-builtins",
|
2024-07-02 22:33:32 -04:00
|
|
|
"--unstable-fs",
|
2023-02-17 09:58:52 -05:00
|
|
|
"--v8-flags=" + v8Flags.join(),
|
|
|
|
];
|
2024-04-02 19:20:48 -04:00
|
|
|
if (usesNodeTest) {
|
|
|
|
// deno test typechecks by default + we want to pass script args
|
|
|
|
args.push("--no-check", "runner.ts", "--", testCase);
|
|
|
|
} else {
|
|
|
|
args.push("runner.ts", testCase);
|
|
|
|
}
|
2023-02-17 09:58:52 -05:00
|
|
|
|
|
|
|
// 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: {
|
2023-03-14 03:56:06 -04:00
|
|
|
TEST_SERIAL_ID: String(testSerialId++),
|
2023-05-23 18:11:37 -04:00
|
|
|
...envVars,
|
2023-02-17 09:58:52 -05:00
|
|
|
},
|
|
|
|
cwd,
|
2024-05-22 22:27:24 -04:00
|
|
|
stdout: "piped",
|
|
|
|
stderr: "piped",
|
|
|
|
}).spawn();
|
|
|
|
const warner = setTimeout(() => {
|
|
|
|
console.error(`Test is running slow: ${testCase}`);
|
|
|
|
}, 2 * 60_000);
|
|
|
|
const killer = setTimeout(() => {
|
|
|
|
console.error(
|
|
|
|
`Test ran far too long, terminating with extreme prejudice: ${testCase}`,
|
|
|
|
);
|
|
|
|
command.kill();
|
|
|
|
}, 10 * 60_000);
|
2023-02-17 09:58:52 -05:00
|
|
|
const { code, stdout, stderr } = await command.output();
|
2024-05-22 22:27:24 -04:00
|
|
|
clearTimeout(warner);
|
|
|
|
clearTimeout(killer);
|
2023-02-17 09:58:52 -05:00
|
|
|
|
|
|
|
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.
|
2023-04-05 07:15:57 -04:00
|
|
|
if (stdout.length) {
|
|
|
|
console.log(decoder.decode(stdout));
|
|
|
|
}
|
|
|
|
const stderrOutput = decoder.decode(stderr);
|
|
|
|
const repeatCmd = magenta(
|
2024-06-11 07:41:44 -04:00
|
|
|
`./target/debug/deno test --config tests/config/deno.json -A tests/node_compat/test.ts -- ${path}`,
|
2023-02-17 09:58:52 -05:00
|
|
|
);
|
2023-04-05 07:15:57 -04:00
|
|
|
const msg = `"${magenta(path)}" failed:
|
|
|
|
|
|
|
|
${stderrOutput}
|
|
|
|
|
|
|
|
You can repeat only this test with the command:
|
2023-04-14 18:50:58 -04:00
|
|
|
|
2023-04-05 07:15:57 -04:00
|
|
|
${repeatCmd}
|
|
|
|
`;
|
|
|
|
console.log(msg);
|
|
|
|
fail(msg);
|
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);
|
|
|
|
}
|
2023-03-30 10:35:45 -04:00
|
|
|
const testPool = pooledMap(
|
|
|
|
navigator.hardwareConcurrency,
|
|
|
|
testPaths.parallel,
|
|
|
|
(path) => runTest(t, path),
|
|
|
|
);
|
|
|
|
const testCases = [];
|
|
|
|
for await (const testCase of testPool) {
|
|
|
|
testCases.push(testCase);
|
2023-03-14 03:56:06 -04:00
|
|
|
}
|
2023-03-30 10:35:45 -04:00
|
|
|
await Promise.all(testCases);
|
2023-03-14 03:56:06 -04:00
|
|
|
});
|
|
|
|
|
2023-02-17 09:58:52 -05:00
|
|
|
function checkConfigTestFilesOrder(testFileLists: Array<string[]>) {
|
2023-03-21 09:38:07 -04:00
|
|
|
for (const testFileList of testFileLists) {
|
2023-02-17 09:58:52 -05:00
|
|
|
const sortedTestList = JSON.parse(JSON.stringify(testFileList));
|
2024-06-24 05:47:12 -04:00
|
|
|
sortedTestList.sort((a: string, b: string) =>
|
|
|
|
a.toLowerCase().localeCompare(b.toLowerCase())
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
testFileList,
|
|
|
|
sortedTestList,
|
|
|
|
"File names in `config.json` are not correct order.",
|
|
|
|
);
|
2023-02-17 09:58:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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]),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
}
|