Skip to content

Commit 17945a2

Browse files
MoLowtargos
authored andcommitted
test: migrate a pseudo_tty test to use assertSnapshot
PR-URL: #47803 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent e53e823 commit 17945a2

File tree

7 files changed

+83
-68
lines changed

7 files changed

+83
-68
lines changed

test/common/assertSnapshot.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
'use strict';
22
const common = require('.');
33
const path = require('node:path');
4+
const test = require('node:test');
45
const fs = require('node:fs/promises');
56
const assert = require('node:assert/strict');
67

7-
8-
const stackFramesRegexp = /(\s+)((.+?)\s+\()?(?:\(?(.+?):(\d+)(?::(\d+))?)\)?(\s+\{)?(\n|$)/g;
8+
const stackFramesRegexp = /(\s+)((.+?)\s+\()?(?:\(?(.+?):(\d+)(?::(\d+))?)\)?(\s+\{)?(\[\d+m)?(\n|$)/g;
99
const windowNewlineRegexp = /\r/g;
1010

11-
function replaceStackTrace(str, replacement = '$1*$7\n') {
11+
function replaceStackTrace(str, replacement = '$1*$7$8\n') {
1212
return str.replace(stackFramesRegexp, replacement);
1313
}
1414

@@ -50,11 +50,19 @@ async function assertSnapshot(actual, filename = process.argv[1]) {
5050
* assertSnapshot.transform(assertSnapshot.replaceStackTrace, assertSnapshot.replaceWindowsLineEndings)
5151
* @param {string} filename
5252
* @param {function(string): string} [transform]
53+
* @param {object} [options] - control how the child process is spawned
54+
* @param {boolean} [options.tty] - whether to spawn the process in a pseudo-tty
5355
* @returns {Promise<void>}
5456
*/
55-
async function spawnAndAssert(filename, transform = (x) => x) {
57+
async function spawnAndAssert(filename, transform = (x) => x, { tty = false } = {}) {
58+
if (tty && common.isWindows) {
59+
test({ skip: 'Skipping pseudo-tty tests, as pseudo terminals are not available on Windows.' });
60+
return;
61+
}
5662
const flags = common.parseTestFlags(filename);
57-
const { stdout, stderr } = await common.spawnPromisified(process.execPath, [...flags, filename]);
63+
const executable = tty ? 'tools/pseudo-tty.py' : process.execPath;
64+
const args = tty ? [process.execPath, ...flags, filename] : [...flags, filename];
65+
const { stdout, stderr } = await common.spawnPromisified(executable, args);
5866
await assertSnapshot(transform(`${stdout}${stderr}`), filename);
5967
}
6068

test/pseudo-tty/test_runner_default_reporter.js test/fixtures/test-runner/output/default_output.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ process.env.FORCE_COLOR = '1';
33
delete process.env.NODE_DISABLE_COLORS;
44
delete process.env.NO_COLOR;
55

6-
require('../common');
6+
require('../../../common');
77
const test = require('node:test');
88

99
test('should pass', () => {});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
[32m✔ should pass [90m(*ms)[39m[39m
2+
[31m✖ should fail [90m(*ms)[39m[39m
3+
Error: fail
4+
*[39m
5+
*[39m
6+
*[39m
7+
*[39m
8+
*[39m
9+
*[39m
10+
*[39m
11+
12+
[90m﹣ should skip [90m(*ms)[39m # SKIP[39m
13+
▶ parent
14+
[31m✖ should fail [90m(*ms)[39m[39m
15+
Error: fail
16+
*[39m
17+
*[39m
18+
*[39m
19+
*[39m
20+
*[39m
21+
22+
[31m✖ should pass but parent fail [90m(*ms)[39m[39m
23+
[32m'test did not finish before its parent and was cancelled'[39m
24+
25+
[31m▶ [39mparent [90m(*ms)[39m
26+
27+
[34mℹ tests 6[39m
28+
[34mℹ suites 0[39m
29+
[34mℹ pass 1[39m
30+
[34mℹ fail 3[39m
31+
[34mℹ cancelled 1[39m
32+
[34mℹ skipped 1[39m
33+
[34mℹ todo 0[39m
34+
[34mℹ duration_ms *[39m
35+
36+
[31m✖ failing tests:[39m
37+
38+
[31m✖ should fail [90m(*ms)[39m[39m
39+
Error: fail
40+
*[39m
41+
*[39m
42+
*[39m
43+
*[39m
44+
*[39m
45+
*[39m
46+
*[39m
47+
48+
[31m✖ should fail [90m(*ms)[39m[39m
49+
Error: fail
50+
*[39m
51+
*[39m
52+
*[39m
53+
*[39m
54+
*[39m
55+
56+
[31m✖ should pass but parent fail [90m(*ms)[39m[39m
57+
[32m'test did not finish before its parent and was cancelled'[39m

test/parallel/test-runner-output.mjs

+9-4
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@ function replaceTestDuration(str) {
1010
.replaceAll(/duration_ms [0-9.]+/g, 'duration_ms *');
1111
}
1212

13+
const color = '(\\[\\d+m)';
14+
const stackTraceBasePath = new RegExp(`${color}\\(${process.cwd()}/?${color}(.*)${color}\\)`, 'g');
15+
1316
function replaceSpecDuration(str) {
1417
return str
1518
.replaceAll(/\(0(\r?\n)ms\)/g, '(ZEROms)')
1619
.replaceAll(/[0-9.]+ms/g, '*ms')
17-
.replaceAll(/duration_ms [0-9.]+/g, 'duration_ms *');
20+
.replaceAll(/duration_ms [0-9.]+/g, 'duration_ms *')
21+
.replace(stackTraceBasePath, '$3');
1822
}
1923
const defaultTransform = snapshot
2024
.transform(snapshot.replaceWindowsLineEndings, snapshot.replaceStackTrace, replaceTestDuration);
2125
const specTransform = snapshot
22-
.transform(snapshot.replaceWindowsLineEndings, snapshot.replaceStackTrace, replaceSpecDuration);
26+
.transform(replaceSpecDuration, snapshot.replaceWindowsLineEndings, snapshot.replaceStackTrace);
2327

2428

2529
const tests = [
@@ -40,10 +44,11 @@ const tests = [
4044
{ name: 'test-runner/output/name_pattern.js' },
4145
{ name: 'test-runner/output/name_pattern_with_only.js' },
4246
{ name: 'test-runner/output/unresolved_promise.js' },
43-
].map(({ name, transform }) => ({
47+
{ name: 'test-runner/output/default_output.js', transform: specTransform, tty: true },
48+
].map(({ name, tty, transform }) => ({
4449
name,
4550
fn: common.mustCall(async () => {
46-
await snapshot.spawnAndAssert(fixtures.path(name), transform ?? defaultTransform);
51+
await snapshot.spawnAndAssert(fixtures.path(name), transform ?? defaultTransform, { tty });
4752
}),
4853
}));
4954

test/pseudo-tty/test_runner_default_reporter.out

-57
This file was deleted.

test/pseudo-tty/testcfg.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from functools import reduce
3737

3838
FLAGS_PATTERN = re.compile(r"//\s+Flags:(.*)")
39-
PTY_HELPER = join(dirname(__file__), 'pty_helper.py')
39+
PTY_HELPER = join(dirname(__file__), '../../tools/pseudo-tty.py')
4040

4141
class TTYTestCase(test.TestCase):
4242

test/pseudo-tty/pty_helper.py tools/pseudo-tty.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python3
2+
13
import errno
24
import os
35
import pty

0 commit comments

Comments
 (0)