Skip to content

Commit 700cf06

Browse files
lundibundiBethGriggs
authored andcommitted
test: slightly refactor test-child-process-execsync
* move `start` time to the point of execution (avoids counting 'throws' tests towards 'timeout' test case) * scope cmd/ret values where possible * use `filter` instead of manual if/return PR-URL: #25227 Refs: #24921 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 5634319 commit 700cf06

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

test/sequential/test-child-process-execsync.js

+27-23
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ const { execFileSync, execSync, spawnSync } = require('child_process');
2828
const TIMER = 200;
2929
const SLEEP = 2000;
3030

31-
const start = Date.now();
3231
const execOpts = { encoding: 'utf8', shell: true };
33-
let err;
34-
let caught = false;
3532

3633
// Verify that stderr is not accessed when a bad shell is used
3734
assert.throws(
@@ -43,9 +40,11 @@ assert.throws(
4340
/spawnSync bad_shell ENOENT/
4441
);
4542

46-
let cmd, ret;
43+
let caught = false;
44+
let ret, err;
45+
const start = Date.now();
4746
try {
48-
cmd = `"${process.execPath}" -e "setTimeout(function(){}, ${SLEEP});"`;
47+
const cmd = `"${process.execPath}" -e "setTimeout(function(){}, ${SLEEP});"`;
4948
ret = execSync(cmd, { timeout: TIMER });
5049
} catch (e) {
5150
caught = true;
@@ -69,28 +68,32 @@ const msgBuf = Buffer.from(`${msg}\n`);
6968

7069
// console.log ends every line with just '\n', even on Windows.
7170

72-
cmd = `"${process.execPath}" -e "console.log('${msg}');"`;
71+
const cmd = `"${process.execPath}" -e "console.log('${msg}');"`;
7372

74-
ret = execSync(cmd);
75-
76-
assert.strictEqual(ret.length, msgBuf.length);
77-
assert.deepStrictEqual(ret, msgBuf);
78-
79-
ret = execSync(cmd, { encoding: 'utf8' });
73+
{
74+
const ret = execSync(cmd);
75+
assert.strictEqual(ret.length, msgBuf.length);
76+
assert.deepStrictEqual(ret, msgBuf);
77+
}
8078

81-
assert.strictEqual(ret, `${msg}\n`);
79+
{
80+
const ret = execSync(cmd, { encoding: 'utf8' });
81+
assert.strictEqual(ret, `${msg}\n`);
82+
}
8283

8384
const args = [
8485
'-e',
8586
`console.log("${msg}");`
8687
];
87-
ret = execFileSync(process.execPath, args);
88-
89-
assert.deepStrictEqual(ret, msgBuf);
90-
91-
ret = execFileSync(process.execPath, args, { encoding: 'utf8' });
88+
{
89+
const ret = execFileSync(process.execPath, args);
90+
assert.deepStrictEqual(ret, msgBuf);
91+
}
9292

93-
assert.strictEqual(ret, `${msg}\n`);
93+
{
94+
const ret = execFileSync(process.execPath, args, { encoding: 'utf8' });
95+
assert.strictEqual(ret, `${msg}\n`);
96+
}
9497

9598
// Verify that the cwd option works.
9699
// See https://github.com/nodejs/node-v0.x-archive/issues/7824.
@@ -133,10 +136,11 @@ assert.strictEqual(ret, `${msg}\n`);
133136
assert.strictEqual(err.message, msg);
134137
assert.strictEqual(err.status, 1);
135138
assert.strictEqual(typeof err.pid, 'number');
136-
spawnSyncKeys.forEach((key) => {
137-
if (key === 'pid') return;
138-
assert.deepStrictEqual(err[key], spawnSyncResult[key]);
139-
});
139+
spawnSyncKeys
140+
.filter((key) => key !== 'pid')
141+
.forEach((key) => {
142+
assert.deepStrictEqual(err[key], spawnSyncResult[key]);
143+
});
140144
return true;
141145
});
142146
}

0 commit comments

Comments
 (0)