From fd39443f8ffd1bca625ca4770a9cfb221690447e Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Tue, 14 Mar 2023 07:56:06 +0000 Subject: [PATCH] test: parallelize applicable node compat tests (#18161) --- cli/tests/node_compat/common.ts | 12 ++++++++++++ cli/tests/node_compat/test.ts | 32 +++++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/cli/tests/node_compat/common.ts b/cli/tests/node_compat/common.ts index 72f44e5101..0941e2c8f3 100644 --- a/cli/tests/node_compat/common.ts +++ b/cli/tests/node_compat/common.ts @@ -1,4 +1,5 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +import { partition } from "std/collections/mod.ts"; import { join } from "std/path/mod.ts"; /** @@ -52,3 +53,14 @@ export function getPathsFromTestSuites(suites: TestSuites): string[] { } return testPaths; } + +const PARALLEL_PATTERN = Deno.build.os == "windows" + ? /^parallel[/\/]/ + : /^parallel\//; + +export function partitionParallelTestPaths( + testPaths: string[], +): { parallel: string[]; sequential: string[] } { + const partitions = partition(testPaths, (p) => !!p.match(PARALLEL_PATTERN)); + return { parallel: partitions[0], sequential: partitions[1] }; +} diff --git a/cli/tests/node_compat/test.ts b/cli/tests/node_compat/test.ts index 213a279743..505a20f085 100644 --- a/cli/tests/node_compat/test.ts +++ b/cli/tests/node_compat/test.ts @@ -2,7 +2,11 @@ import { magenta } from "std/fmt/colors.ts"; import { dirname, fromFileUrl, join } from "std/path/mod.ts"; import { fail } from "std/testing/asserts.ts"; -import { config, getPathsFromTestSuites } from "./common.ts"; +import { + config, + getPathsFromTestSuites, + partitionParallelTestPaths, +} from "./common.ts"; // If the test case is invoked like // deno test -A cli/tests/node_compat/test.ts -- @@ -19,7 +23,9 @@ const hasFilters = filters.length > 0; const toolsPath = dirname(fromFileUrl(import.meta.url)); const stdRootUrl = new URL("../../", import.meta.url).href; -const testPaths = getPathsFromTestSuites(config.tests); +const testPaths = partitionParallelTestPaths( + getPathsFromTestSuites(config.tests), +); const cwd = new URL(".", import.meta.url); const importMap = "import_map.json"; const windowsIgnorePaths = new Set( @@ -30,23 +36,27 @@ const darwinIgnorePaths = new Set( ); const decoder = new TextDecoder(); +let testSerialId = 0; -for await (const path of testPaths) { +async function runTest(t: Deno.TestContext, path: string): Promise { // 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)) ) { - continue; + return; } const isTodo = path.includes("TODO"); const ignore = (Deno.build.os === "windows" && windowsIgnorePaths.has(path)) || (Deno.build.os === "darwin" && darwinIgnorePaths.has(path)) || isTodo; - Deno.test({ + await t.step({ name: `Node.js compatibility "${path}"`, ignore, + sanitizeOps: false, + sanitizeResources: false, + sanitizeExit: false, fn: async () => { const testCase = join(toolsPath, "test", path); @@ -74,6 +84,7 @@ for await (const path of testPaths) { args, env: { DENO_NODE_COMPAT_URL: stdRootUrl, + TEST_SERIAL_ID: String(testSerialId++), }, cwd, }); @@ -101,6 +112,17 @@ for await (const path of testPaths) { }); } +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); +}); + function checkConfigTestFilesOrder(testFileLists: Array) { for (let testFileList of testFileLists) { testFileList = testFileList.filter((name) => !name.startsWith("TODO:"));