Skip to content

Commit 95972aa

Browse files
committed
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 75d397e commit 95972aa

7 files changed

Lines changed: 83 additions & 68 deletions

File tree

‎test/common/assertSnapshot.js‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
'use strict';
22
constcommon=require('.');
33
constpath=require('node:path');
4+
consttest=require('node:test');
45
constfs=require('node:fs/promises');
56
constassert=require('node:assert/strict');
67

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

11-
functionreplaceStackTrace(str,replacement='$1*$7\n'){
11+
functionreplaceStackTrace(str,replacement='$1*$7$8\n'){
1212
returnstr.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-
asyncfunctionspawnAndAssert(filename,transform=(x)=>x){
57+
asyncfunctionspawnAndAssert(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
constflags=common.parseTestFlags(filename);
57-
const{ stdout, stderr }=awaitcommon.spawnPromisified(process.execPath,[...flags,filename]);
63+
constexecutable=tty ? 'tools/pseudo-tty.py' : process.execPath;
64+
constargs=tty ? [process.execPath, ...flags,filename] : [...flags,filename];
65+
const{ stdout, stderr }=awaitcommon.spawnPromisified(executable,args);
5866
awaitassertSnapshot(transform(`${stdout}${stderr}`),filename);
5967
}
6068

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
deleteprocess.env.NODE_DISABLE_COLORS;
44
deleteprocess.env.NO_COLOR;
55

6-
require('../common');
6+
require('../../../common');
77
consttest=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+
*[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‎

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+
constcolor='(\\[\\d+m)';
14+
conststackTraceBasePath=newRegExp(`${color}\\(${process.cwd()}/?${color}(.*)${color}\\)`,'g');
15+
1316
functionreplaceSpecDuration(str){
1417
returnstr
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
constdefaultTransform=snapshot
2024
.transform(snapshot.replaceWindowsLineEndings,snapshot.replaceStackTrace,replaceTestDuration);
2125
constspecTransform=snapshot
22-
.transform(snapshot.replaceWindowsLineEndings,snapshot.replaceStackTrace,replaceSpecDuration);
26+
.transform(replaceSpecDuration,snapshot.replaceWindowsLineEndings,snapshot.replaceStackTrace);
2327

2428

2529
consttests=[
@@ -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-
awaitsnapshot.spawnAndAssert(fixtures.path(name),transform??defaultTransform);
51+
awaitsnapshot.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
fromfunctoolsimportreduce
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
classTTYTestCase(test.TestCase):
4242

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
importerrno
24
importos
35
importpty

0 commit comments

Comments
 (0)