test: migrate a pseudo_tty test to use assertSnapshot

PR-URL: https://github.com/nodejs/node/pull/47803
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
Moshe Atlow 2023-05-04 11:34:23 +03:00 committed by GitHub
parent 76ae7be78d
commit 9e5e2f1dc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 83 additions and 68 deletions

View File

@ -1,14 +1,14 @@
'use strict';
const common = require('.');
const path = require('node:path');
const test = require('node:test');
const fs = require('node:fs/promises');
const assert = require('node:assert/strict');
const stackFramesRegexp = /(\s+)((.+?)\s+\()?(?:\(?(.+?):(\d+)(?::(\d+))?)\)?(\s+\{)?(\n|$)/g;
const stackFramesRegexp = /(\s+)((.+?)\s+\()?(?:\(?(.+?):(\d+)(?::(\d+))?)\)?(\s+\{)?(\[\d+m)?(\n|$)/g;
const windowNewlineRegexp = /\r/g;
function replaceStackTrace(str, replacement = '$1*$7\n') {
function replaceStackTrace(str, replacement = '$1*$7$8\n') {
return str.replace(stackFramesRegexp, replacement);
}
@ -50,11 +50,19 @@ async function assertSnapshot(actual, filename = process.argv[1]) {
* assertSnapshot.transform(assertSnapshot.replaceStackTrace, assertSnapshot.replaceWindowsLineEndings)
* @param {string} filename
* @param {function(string): string} [transform]
* @param {object} [options] - control how the child process is spawned
* @param {boolean} [options.tty] - whether to spawn the process in a pseudo-tty
* @returns {Promise<void>}
*/
async function spawnAndAssert(filename, transform = (x) => x) {
async function spawnAndAssert(filename, transform = (x) => x, { tty = false } = {}) {
if (tty && common.isWindows) {
test({ skip: 'Skipping pseudo-tty tests, as pseudo terminals are not available on Windows.' });
return;
}
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);
}

View File

@ -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', () => {});

View File

@ -0,0 +1,57 @@
[32m✔ should pass [90m(*ms)[39m[39m
[31m✖ should fail [90m(*ms)[39m[39m
Error: fail
*[39m
*[39m
*[39m
*[39m
*[39m
*[39m
*[39m
[90m﹣ should skip [90m(*ms)[39m # SKIP[39m
▶ parent
[31m✖ should fail [90m(*ms)[39m[39m
Error: fail
*[39m
*[39m
*[39m
*[39m
*[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
*[39m
*[39m
*[39m
*[39m
*[39m
*[39m
*[39m
[31m✖ should fail [90m(*ms)[39m[39m
Error: fail
*[39m
*[39m
*[39m
*[39m
*[39m
[31m✖ should pass but parent fail [90m(*ms)[39m[39m
[32m'test did not finish before its parent and was cancelled'[39m

View File

@ -10,16 +10,20 @@ function replaceTestDuration(str) {
.replaceAll(/duration_ms [0-9.]+/g, 'duration_ms *');
}
const color = '(\\[\\d+m)';
const stackTraceBasePath = new RegExp(`${color}\\(${process.cwd()}/?${color}(.*)${color}\\)`, 'g');
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(stackTraceBasePath, '$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 +44,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 });
}),
}));

View File

@ -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

View File

@ -36,7 +36,7 @@ import utils
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):

2
test/pseudo-tty/pty_helper.py → tools/pseudo-tty.py Normal file → Executable file
View File

@ -1,3 +1,5 @@
#!/usr/bin/env python3
import errno
import os
import pty