Skip to content

Commit b6f9dfd

Browse files
committed
test: migrate pseudo_tty tests to use assertSnapshot
1 parent d225d95 commit b6f9dfd

File tree

7 files changed

+77
-67
lines changed

7 files changed

+77
-67
lines changed

test/common/assertSnapshot.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ const path = require('node:path');
44
const fs = require('node:fs/promises');
55
const assert = require('node:assert/strict');
66

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

1110
function replaceStackTrace(str, replacement = '$1*$7\n') {
@@ -51,11 +50,15 @@ async function assertSnapshot(actual, filename = process.argv[1]) {
5150
*
5251
* @param {string} filename
5352
* @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
5455
* @returns {Promise<void>}
5556
*/
56-
async function spawnAndAssert(filename, transform = (x) => x) {
57+
async function spawnAndAssert(filename, transform = (x) => x, { tty = false } = {}) {
5758
const flags = common.parseTestFlags(filename);
58-
const { stdout, stderr } = await common.spawnPromisified(process.execPath, [...flags, filename]);
59+
const executable = tty ? 'tools/pseudo-tty.py' : process.execPath;
60+
const args = tty ? [process.execPath, ...flags, filename] : [...flags, filename];
61+
const { stdout, stderr } = await common.spawnPromisified(executable, args);
5962
await assertSnapshot(transform(`${stdout}${stderr}`), filename);
6063
}
6164

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

Lines changed: 1 addition & 1 deletion
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', () => {});
Lines changed: 57 additions & 0 deletions
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+
*
5+
*
6+
*
7+
*
8+
*
9+
*
10+
*
11+
12+
[90m﹣ should skip [90m(*ms)[39m # SKIP[39m
13+
▶ parent
14+
[31m✖ should fail [90m(*ms)[39m[39m
15+
Error: fail
16+
*
17+
*
18+
*
19+
*
20+
*
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+
*
41+
*
42+
*
43+
*
44+
*
45+
*
46+
*
47+
48+
[31m✖ should fail [90m(*ms)[39m[39m
49+
Error: fail
50+
*
51+
*
52+
*
53+
*
54+
*
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

Lines changed: 9 additions & 4 deletions
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

Lines changed: 0 additions & 57 deletions
This file was deleted.

test/pseudo-tty/testcfg.py

Lines changed: 1 addition & 1 deletion
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 renamed to tools/pseudo-tty.py

Lines changed: 2 additions & 0 deletions
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)