Skip to content

Commit e87f4b3

Browse files
greenheadHQaduh95
authored andcommitted
test: use common spawnSync helpers in more tests
The helpers report the command, both streams, and the exit condition on failure, so mismatches are diagnosed consistently across tests. Signed-off-by: greenhead <greenheadhq@gmail.com> PR-URL: #65552 Refs: #65377 Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 672edce commit e87f4b3

6 files changed

Lines changed: 46 additions & 49 deletions

‎test/message/node_run_list.js‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
'use strict';
22

33
require('../common');
4-
const assert = require('node:assert/strict');
5-
const childProcess = require('node:child_process');
4+
const { spawnSyncAndExit } = require('../common/child_process');
65
const fixtures = require('../common/fixtures');
76

8-
const child = childProcess.spawnSync(
7+
const { child } = spawnSyncAndExit(
98
process.execPath,
109
[ '--no-warnings', '--run'],
1110
{ cwd: fixtures.path('run-script'), encoding: 'utf8' },
11+
{ status: 9, signal: null },
1212
);
13-
assert.strictEqual(child.status, 9);
1413
console.log(child.stderr);

‎test/parallel/test-fs-glob.mjs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as common from '../common/index.mjs';
22
import tmpdir from '../common/tmpdir.js';
3-
import { spawnSync } from 'node:child_process';
3+
import { spawnSyncAndExitWithoutError } from '../common/child_process.js';
44
import { resolve, dirname, sep, relative, join, isAbsolute } from 'node:path';
55
import { mkdir, writeFile, symlink, glob as asyncGlob } from 'node:fs/promises';
66
import { glob, globSync, Dirent, chmodSync, writeFileSync, rmSync } from 'node:fs';
@@ -722,12 +722,11 @@ describe('glob - seen cache', function() {
722722
`;
723723

724724
const seenDir = tmpdir.resolve('glob-seen');
725-
const child = spawnSync(
725+
spawnSyncAndExitWithoutError(
726726
process.execPath,
727727
['--expose-internals', '-e', script, seenDir],
728728
{ encoding: 'utf8' },
729729
);
730-
assert.strictEqual(child.status, 0, child.stderr || child.stdout);
731730
});
732731
});
733732

‎test/parallel/test-module-unreadable-package-json.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const assert = require('assert');
1818
const fs = require('fs');
1919
const path = require('path');
2020
const { spawnSync } = require('child_process');
21+
const { spawnSyncAndAssert } = require('../common/child_process');
2122
const tmpdir = require('../common/tmpdir');
2223

2324
tmpdir.refresh();
@@ -38,11 +39,10 @@ const entry = tmpdir.resolve('main.mjs');
3839
fs.writeFileSync(entry, 'import { which } from "dep"; console.log(which);');
3940

4041
// Sanity check: the export resolves while the package config is readable.
41-
{
42-
const child = spawnSync(process.execPath, [entry], { encoding: 'utf8' });
43-
assert.strictEqual(child.stdout.trim(), 'real');
44-
assert.strictEqual(child.status, 0, child.stderr);
45-
}
42+
spawnSyncAndAssert(process.execPath, [entry], { encoding: 'utf8' }, {
43+
stdout: 'real',
44+
trim: true,
45+
});
4646

4747
fs.chmodSync(depPackageJson, 0o000);
4848

‎test/parallel/test-runner-coverage-default-exclusion.mjs‎

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import '../common/index.mjs';
22
import { before, describe, it } from 'node:test';
33
import assert from 'node:assert';
4-
import { spawnSync } from 'node:child_process';
54
import { cp } from 'node:fs/promises';
65
import tmpdir from '../common/tmpdir.js';
76
import fixtures from '../common/fixtures.js';
7+
import { spawnSyncAndAssert } from '../common/child_process.js';
88
const skipIfNoInspector = {
99
skip: !process.features.inspector ? 'inspector disabled' : false
1010
};
@@ -57,14 +57,13 @@ describe('test runner coverage default exclusion', skipIfNoInspector, () => {
5757
'--test-reporter=tap',
5858
'--no-experimental-strip-types',
5959
];
60-
const result = spawnSync(process.execPath, args, {
60+
spawnSyncAndAssert(process.execPath, args, {
6161
env: { ...process.env, NODE_TEST_TMPDIR: tmpdir.path },
6262
cwd: tmpdir.path
63+
}, {
64+
stderr: '',
65+
stdout: new RegExp(RegExp.escape(report)),
6366
});
64-
65-
assert.strictEqual(result.stderr.toString(), '');
66-
assert(result.stdout.toString().includes(report));
67-
assert.strictEqual(result.status, 0);
6867
});
6968

7069
it('should exclude test files from coverage by default', async () => {
@@ -74,14 +73,13 @@ describe('test runner coverage default exclusion', skipIfNoInspector, () => {
7473
'--experimental-test-coverage',
7574
'--test-reporter=tap',
7675
];
77-
const result = spawnSync(process.execPath, args, {
76+
spawnSyncAndAssert(process.execPath, args, {
7877
env: { ...process.env, NODE_TEST_TMPDIR: tmpdir.path },
7978
cwd: tmpdir.path
79+
}, {
80+
stderr: '',
81+
stdout: assertDefaultExclusions,
8082
});
81-
82-
assert.strictEqual(result.stderr.toString(), '');
83-
assertDefaultExclusions(result.stdout.toString());
84-
assert.strictEqual(result.status, 0);
8583
});
8684

8785
it('should exclude ts test files', async () => {
@@ -91,14 +89,13 @@ describe('test runner coverage default exclusion', skipIfNoInspector, () => {
9189
'--disable-warning=ExperimentalWarning',
9290
'--test-reporter=tap',
9391
];
94-
const result = spawnSync(process.execPath, args, {
92+
spawnSyncAndAssert(process.execPath, args, {
9593
env: { ...process.env, NODE_TEST_TMPDIR: tmpdir.path },
9694
cwd: tmpdir.path
95+
}, {
96+
stderr: '',
97+
stdout: assertDefaultExclusions,
9798
});
98-
99-
assert.strictEqual(result.stderr.toString(), '');
100-
assertDefaultExclusions(result.stdout.toString());
101-
assert.strictEqual(result.status, 0);
10299
});
103100

104101
it('should exclude dotfile test files from coverage by default', async () => {
@@ -109,14 +106,15 @@ describe('test runner coverage default exclusion', skipIfNoInspector, () => {
109106
'--test-reporter=tap',
110107
'test/.dotfile.cjs',
111108
];
112-
const result = spawnSync(process.execPath, args, {
109+
spawnSyncAndAssert(process.execPath, args, {
113110
env: { ...process.env, NODE_TEST_TMPDIR: tmpdir.path },
114111
cwd: tmpdir.path
112+
}, {
113+
stderr: '',
114+
stdout(output) {
115+
assertDefaultExclusions(output);
116+
assert.doesNotMatch(output, /#\s+\.dotfile\.cjs\s+\|/);
117+
},
115118
});
116-
117-
assert.strictEqual(result.stderr.toString(), '');
118-
assertDefaultExclusions(result.stdout.toString());
119-
assert.doesNotMatch(result.stdout.toString(), /#\s+\.dotfile\.cjs\s+\|/);
120-
assert.strictEqual(result.status, 0);
121119
});
122120
});

‎test/parallel/test-runner-coverage-thresholds.js‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const common = require('../common');
33
const assert = require('node:assert');
44
const { spawnSync } = require('node:child_process');
5+
const { spawnSyncAndExit } = require('../common/child_process');
56
const { readdirSync } = require('node:fs');
67
const { test } = require('node:test');
78
const fixtures = require('../common/fixtures');
@@ -172,23 +173,24 @@ for (const coverage of coverages) {
172173
});
173174

174175
test(`test failing ${coverage.flag} with dot reporter`, () => {
175-
const result = spawnSync(process.execPath, [
176+
const { child } = spawnSyncAndExit(process.execPath, [
176177
'--test',
177178
'--experimental-test-coverage',
178179
'--test-coverage-exclude=!test/**',
179180
`${coverage.flag}=99`,
180181
'--test-reporter', 'dot',
181182
fixture,
182-
]);
183-
184-
const stdout = result.stdout.toString();
185-
assert.match(
186-
stdout,
187-
RegExp(`Error: ${coverage.actual.toFixed(2)}% ${coverage.name} coverage does not meet threshold of 99%`)
188-
);
189-
assert.match(stdout, /start of coverage report/);
190-
assert.match(stdout, /end of coverage report/);
191-
assert.strictEqual(result.status, 1);
192-
assert(!findCoverageFileForPid(result.pid));
183+
], {
184+
status: 1,
185+
stdout(output) {
186+
assert.match(
187+
output,
188+
RegExp(`Error: ${coverage.actual.toFixed(2)}% ${coverage.name} coverage does not meet threshold of 99%`)
189+
);
190+
assert.match(output, /start of coverage report/);
191+
assert.match(output, /end of coverage report/);
192+
},
193+
});
194+
assert(!findCoverageFileForPid(child.pid));
193195
});
194196
}

‎test/pseudo-tty/test-set-raw-mode-modes.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
'use strict';
22
require('../common');
33
const assert = require('assert');
4-
const { spawnSync } = require('child_process');
4+
const { spawnSyncAndExitWithoutError } = require('../common/child_process');
55

66
function isOnlcrEnabled() {
7-
const { stdout, stderr, status } = spawnSync('stty', ['-a'], {
7+
const { child: { stdout } } = spawnSyncAndExitWithoutError('stty', ['-a'], {
88
encoding: 'utf8',
99
stdio: ['inherit', 'pipe', 'pipe'],
1010
});
1111

12-
assert.strictEqual(status, 0, stderr);
1312
return /(?:^|[\s;])onlcr(?:[\s;]|$)/.test(stdout);
1413
}
1514

0 commit comments

Comments
 (0)