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: https://github.com/nodejs/node/pull/25227 Refs: https://github.com/nodejs/node/issues/24921 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
9c2b14013b
commit
9b60bcb3b9
@ -28,10 +28,7 @@ const { execFileSync, execSync, spawnSync } = require('child_process');
|
|||||||
const TIMER = 200;
|
const TIMER = 200;
|
||||||
const SLEEP = 2000;
|
const SLEEP = 2000;
|
||||||
|
|
||||||
const start = Date.now();
|
|
||||||
const execOpts = { encoding: 'utf8', shell: true };
|
const execOpts = { encoding: 'utf8', shell: true };
|
||||||
let err;
|
|
||||||
let caught = false;
|
|
||||||
|
|
||||||
// Verify that stderr is not accessed when a bad shell is used
|
// Verify that stderr is not accessed when a bad shell is used
|
||||||
assert.throws(
|
assert.throws(
|
||||||
@ -43,9 +40,11 @@ assert.throws(
|
|||||||
/spawnSync bad_shell ENOENT/
|
/spawnSync bad_shell ENOENT/
|
||||||
);
|
);
|
||||||
|
|
||||||
let cmd, ret;
|
let caught = false;
|
||||||
|
let ret, err;
|
||||||
|
const start = Date.now();
|
||||||
try {
|
try {
|
||||||
cmd = `"${process.execPath}" -e "setTimeout(function(){}, ${SLEEP});"`;
|
const cmd = `"${process.execPath}" -e "setTimeout(function(){}, ${SLEEP});"`;
|
||||||
ret = execSync(cmd, { timeout: TIMER });
|
ret = execSync(cmd, { timeout: TIMER });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
caught = true;
|
caught = true;
|
||||||
@ -69,28 +68,32 @@ const msgBuf = Buffer.from(`${msg}\n`);
|
|||||||
|
|
||||||
// console.log ends every line with just '\n', even on Windows.
|
// console.log ends every line with just '\n', even on Windows.
|
||||||
|
|
||||||
cmd = `"${process.execPath}" -e "console.log('${msg}');"`;
|
const cmd = `"${process.execPath}" -e "console.log('${msg}');"`;
|
||||||
|
|
||||||
ret = execSync(cmd);
|
{
|
||||||
|
const ret = execSync(cmd);
|
||||||
|
assert.strictEqual(ret.length, msgBuf.length);
|
||||||
|
assert.deepStrictEqual(ret, msgBuf);
|
||||||
|
}
|
||||||
|
|
||||||
assert.strictEqual(ret.length, msgBuf.length);
|
{
|
||||||
assert.deepStrictEqual(ret, msgBuf);
|
const ret = execSync(cmd, { encoding: 'utf8' });
|
||||||
|
assert.strictEqual(ret, `${msg}\n`);
|
||||||
ret = execSync(cmd, { encoding: 'utf8' });
|
}
|
||||||
|
|
||||||
assert.strictEqual(ret, `${msg}\n`);
|
|
||||||
|
|
||||||
const args = [
|
const args = [
|
||||||
'-e',
|
'-e',
|
||||||
`console.log("${msg}");`
|
`console.log("${msg}");`
|
||||||
];
|
];
|
||||||
ret = execFileSync(process.execPath, args);
|
{
|
||||||
|
const ret = execFileSync(process.execPath, args);
|
||||||
|
assert.deepStrictEqual(ret, msgBuf);
|
||||||
|
}
|
||||||
|
|
||||||
assert.deepStrictEqual(ret, msgBuf);
|
{
|
||||||
|
const ret = execFileSync(process.execPath, args, { encoding: 'utf8' });
|
||||||
ret = execFileSync(process.execPath, args, { encoding: 'utf8' });
|
assert.strictEqual(ret, `${msg}\n`);
|
||||||
|
}
|
||||||
assert.strictEqual(ret, `${msg}\n`);
|
|
||||||
|
|
||||||
// Verify that the cwd option works.
|
// Verify that the cwd option works.
|
||||||
// See https://github.com/nodejs/node-v0.x-archive/issues/7824.
|
// See https://github.com/nodejs/node-v0.x-archive/issues/7824.
|
||||||
@ -133,10 +136,11 @@ assert.strictEqual(ret, `${msg}\n`);
|
|||||||
assert.strictEqual(err.message, msg);
|
assert.strictEqual(err.message, msg);
|
||||||
assert.strictEqual(err.status, 1);
|
assert.strictEqual(err.status, 1);
|
||||||
assert.strictEqual(typeof err.pid, 'number');
|
assert.strictEqual(typeof err.pid, 'number');
|
||||||
spawnSyncKeys.forEach((key) => {
|
spawnSyncKeys
|
||||||
if (key === 'pid') return;
|
.filter((key) => key !== 'pid')
|
||||||
assert.deepStrictEqual(err[key], spawnSyncResult[key]);
|
.forEach((key) => {
|
||||||
});
|
assert.deepStrictEqual(err[key], spawnSyncResult[key]);
|
||||||
|
});
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user