From 5580260fae207137e0d754cd065af04658bc4752 Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Mon, 1 May 2023 21:01:05 +0300 Subject: [PATCH] test: migrate pseudo_tty tests to use assertSnapshot --- test/common/assertSnapshot.js | 9 ++- .../test-runner/output/default_output.js} | 2 +- .../output/default_output.snapshot | 57 +++++++++++++++++++ test/parallel/test-runner-output.mjs | 10 ++-- .../test_runner_default_reporter.out | 57 ------------------- test/pseudo-tty/testcfg.py | 2 +- .../pty_helper.py => tools/pseudo-tty.py | 2 + 7 files changed, 73 insertions(+), 66 deletions(-) rename test/{pseudo-tty/test_runner_default_reporter.js => fixtures/test-runner/output/default_output.js} (93%) create mode 100644 test/fixtures/test-runner/output/default_output.snapshot delete mode 100644 test/pseudo-tty/test_runner_default_reporter.out rename test/pseudo-tty/pty_helper.py => tools/pseudo-tty.py (98%) mode change 100644 => 100755 diff --git a/test/common/assertSnapshot.js b/test/common/assertSnapshot.js index 4986103fe92245..f99c9de8ee8ed8 100644 --- a/test/common/assertSnapshot.js +++ b/test/common/assertSnapshot.js @@ -5,7 +5,8 @@ const fs = require('node:fs/promises'); const assert = require('node:assert/strict'); -const stackFramesRegexp = /(\s+)((.+?)\s+\()?(?:\(?(.+?):(\d+)(?::(\d+))?)\)?(\s+\{)?(\n|$)/g; +// eslint-disable-next-line no-control-regex +const stackFramesRegexp = /(\s+)((.+?)\s+\()?(?:\(?(.+?):(\d+)(?::(\d+))?)\)?(\s+\{)?(\u001b\[\d+m)?(\n|$)/g; const windowNewlineRegexp = /\r/g; function replaceStackTrace(str, replacement = '$1*$7\n') { @@ -53,9 +54,11 @@ async function assertSnapshot(actual, filename = process.argv[1]) { * @param {function(string): string} [transform] * @returns {Promise} */ -async function spawnAndAssert(filename, transform = (x) => x) { +async function spawnAndAssert(filename, transform = (x) => x, { tty = false }) { const flags = common.parseTestFlags(filename); - const { stdout, stderr } = await common.spawnPromisified(process.execPath, [...flags, filename]); + const executable = tty ? 'tools/pseudo-tty.py' : process.execPath; + const args = tty ? [process.execPath, ...flags, filename] : [...flags, filename]; + const { stdout, stderr } = await common.spawnPromisified(executable, args); await assertSnapshot(transform(`${stdout}${stderr}`), filename); } diff --git a/test/pseudo-tty/test_runner_default_reporter.js b/test/fixtures/test-runner/output/default_output.js similarity index 93% rename from test/pseudo-tty/test_runner_default_reporter.js rename to test/fixtures/test-runner/output/default_output.js index 1596603b217660..b0392f3737b1d6 100644 --- a/test/pseudo-tty/test_runner_default_reporter.js +++ b/test/fixtures/test-runner/output/default_output.js @@ -3,7 +3,7 @@ process.env.FORCE_COLOR = '1'; delete process.env.NODE_DISABLE_COLORS; delete process.env.NO_COLOR; -require('../common'); +require('../../../common'); const test = require('node:test'); test('should pass', () => {}); diff --git a/test/fixtures/test-runner/output/default_output.snapshot b/test/fixtures/test-runner/output/default_output.snapshot new file mode 100644 index 00000000000000..e75c88fbdd8a76 --- /dev/null +++ b/test/fixtures/test-runner/output/default_output.snapshot @@ -0,0 +1,57 @@ +[32m✔ should pass [90m(*ms)[39m[39m +[31m✖ should fail [90m(*ms)[39m[39m + Error: fail + at TestContext. [90m(/Users/moshe/repos/node/[39mtest/fixtures/test-runner/output/default_output.js:10:35[90m)[39m + [90m at Test.runInAsyncScope (node:async_hooks:206:9)[39m + [90m at Test.run (node:internal/test_runner/test:568:25)[39m + [90m at Test.processPendingSubtests (node:internal/test_runner/test:315:27)[39m + [90m at Test.postRun (node:internal/test_runner/test:637:19)[39m + [90m at Test.run (node:internal/test_runner/test:596:10)[39m + [90m at async startSubtest (node:internal/test_runner/harness:203:3)[39m + +[90m﹣ should skip [90m(*ms)[39m # SKIP[39m +▶ parent + [31m✖ should fail [90m(*ms)[39m[39m + Error: fail + at TestContext. [90m(/Users/moshe/repos/node/[39mtest/fixtures/test-runner/output/default_output.js:13:37[90m)[39m + [90m at Test.runInAsyncScope (node:async_hooks:206:9)[39m + [90m at Test.run (node:internal/test_runner/test:568:25)[39m + [90m at Test.start (node:internal/test_runner/test:481:17)[39m + [90m at startSubtest (node:internal/test_runner/harness:203:17)[39m + + [31m✖ should pass but parent fail [90m(*ms)[39m[39m + [32m'test did not finish before its parent and was cancelled'[39m + +[31m▶ [39mparent [90m(*ms)[39m + +[34mℹ tests 6[39m +[34mℹ suites 0[39m +[34mℹ pass 1[39m +[34mℹ fail 3[39m +[34mℹ cancelled 1[39m +[34mℹ skipped 1[39m +[34mℹ todo 0[39m +[34mℹ duration_ms *[39m + +[31m✖ failing tests:[39m + +[31m✖ should fail [90m(*ms)[39m[39m + Error: fail + at TestContext. [90m(/Users/moshe/repos/node/[39mtest/fixtures/test-runner/output/default_output.js:10:35[90m)[39m + [90m at Test.runInAsyncScope (node:async_hooks:206:9)[39m + [90m at Test.run (node:internal/test_runner/test:568:25)[39m + [90m at Test.processPendingSubtests (node:internal/test_runner/test:315:27)[39m + [90m at Test.postRun (node:internal/test_runner/test:637:19)[39m + [90m at Test.run (node:internal/test_runner/test:596:10)[39m + [90m at async startSubtest (node:internal/test_runner/harness:203:3)[39m + +[31m✖ should fail [90m(*ms)[39m[39m + Error: fail + at TestContext. [90m(/Users/moshe/repos/node/[39mtest/fixtures/test-runner/output/default_output.js:13:37[90m)[39m + [90m at Test.runInAsyncScope (node:async_hooks:206:9)[39m + [90m at Test.run (node:internal/test_runner/test:568:25)[39m + [90m at Test.start (node:internal/test_runner/test:481:17)[39m + [90m at startSubtest (node:internal/test_runner/harness:203:17)[39m + +[31m✖ should pass but parent fail [90m(*ms)[39m[39m + [32m'test did not finish before its parent and was cancelled'[39m diff --git a/test/parallel/test-runner-output.mjs b/test/parallel/test-runner-output.mjs index 977c20d9efed7b..8224581f9fd588 100644 --- a/test/parallel/test-runner-output.mjs +++ b/test/parallel/test-runner-output.mjs @@ -14,12 +14,13 @@ function replaceSpecDuration(str) { return str .replaceAll(/\(0(\r?\n)ms\)/g, '(ZEROms)') .replaceAll(/[0-9.]+ms/g, '*ms') - .replaceAll(/duration_ms [0-9.]+/g, 'duration_ms *'); + .replaceAll(/duration_ms [0-9.]+/g, 'duration_ms *') + .replace(new RegExp(`(\\u001b\\[\\d+m)\\(${process.cwd()}/?(\\u001b\\[\\d+m)(.*)(\\u001b\\[\\d+m)\\)`, 'g'), '$3'); } const defaultTransform = snapshot .transform(snapshot.replaceWindowsLineEndings, snapshot.replaceStackTrace, replaceTestDuration); const specTransform = snapshot - .transform(snapshot.replaceWindowsLineEndings, snapshot.replaceStackTrace, replaceSpecDuration); + .transform(replaceSpecDuration, snapshot.replaceWindowsLineEndings, snapshot.replaceStackTrace); const tests = [ @@ -40,10 +41,11 @@ const tests = [ { name: 'test-runner/output/name_pattern.js' }, { name: 'test-runner/output/name_pattern_with_only.js' }, { name: 'test-runner/output/unresolved_promise.js' }, -].map(({ name, transform }) => ({ + { name: 'test-runner/output/default_output.js', transform: specTransform, tty: true }, +].map(({ name, tty, transform }) => ({ name, fn: common.mustCall(async () => { - await snapshot.spawnAndAssert(fixtures.path(name), transform ?? defaultTransform); + await snapshot.spawnAndAssert(fixtures.path(name), transform ?? defaultTransform, { tty }); }), })); diff --git a/test/pseudo-tty/test_runner_default_reporter.out b/test/pseudo-tty/test_runner_default_reporter.out deleted file mode 100644 index 6787ceb6af708e..00000000000000 --- a/test/pseudo-tty/test_runner_default_reporter.out +++ /dev/null @@ -1,57 +0,0 @@ -[32m* should pass [90m(*ms)[39m[39m -[31m* should fail [90m(*ms)[39m[39m - Error: fail - at * [90m(*)[39m - [90m at *[39m - [90m at *[39m - [90m at *[39m - [90m at *[39m - [90m at *[39m - [90m at *[39m - -[90m* should skip [90m(*ms)[39m # SKIP[39m -* parent - [31m* should fail [90m(*ms)[39m[39m - Error: fail - at * [90m(*)[39m - [90m at *[39m - [90m at *[39m - [90m at *[39m - [90m at *[39m - - [31m* should pass but parent fail [90m(*ms)[39m[39m - [32m'test did not finish before its parent and was cancelled'[39m - -[31m* [39mparent [90m(*ms)[39m - -[34m* tests 6[39m -[34m* suites 0[39m -[34m* pass 1[39m -[34m* fail 3[39m -[34m* cancelled 1[39m -[34m* skipped 1[39m -[34m* todo 0[39m -[34m* duration_ms *[39m - -[31m* failing tests:[39m - -[31m* should fail [90m(*ms)[39m[39m - Error: fail - at * [90m(*)[39m - [90m at *[39m - [90m at *[39m - [90m at *[39m - [90m at *[39m - [90m at *[39m - [90m at *[39m - -[31m* should fail [90m(*ms)[39m[39m - Error: fail - at * [90m(*)[39m - [90m at *[39m - [90m at *[39m - [90m at *[39m - [90m at *[39m - -[31m* should pass but parent fail [90m(*ms)[39m[39m - [32m'test did not finish before its parent and was cancelled'[39m diff --git a/test/pseudo-tty/testcfg.py b/test/pseudo-tty/testcfg.py index df380ad31eff1e..4751188b44d30a 100644 --- a/test/pseudo-tty/testcfg.py +++ b/test/pseudo-tty/testcfg.py @@ -36,7 +36,7 @@ from functools import reduce FLAGS_PATTERN = re.compile(r"//\s+Flags:(.*)") -PTY_HELPER = join(dirname(__file__), 'pty_helper.py') +PTY_HELPER = join(dirname(__file__), '../../tools/pseudo-tty.py') class TTYTestCase(test.TestCase): diff --git a/test/pseudo-tty/pty_helper.py b/tools/pseudo-tty.py old mode 100644 new mode 100755 similarity index 98% rename from test/pseudo-tty/pty_helper.py rename to tools/pseudo-tty.py index a61ef1cc0eda0b..96300a679a58b3 --- a/test/pseudo-tty/pty_helper.py +++ b/tools/pseudo-tty.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + import errno import os import pty