Skip to content

Commit 9eb677b

Browse files
lundibundiMylesBorins
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 <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent fcc03c1 commit 9eb677b

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

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

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ const{execFileSync, execSync, spawnSync } = require('child_process');
2828
constTIMER=200;
2929
constSLEEP=2000;
3030

31-
conststart=Date.now();
3231
constexecOpts={encoding: 'utf8',shell: true};
33-
leterr;
34-
letcaught=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
/spawnSyncbad_shellENOENT/
4441
);
4542

46-
letcmd,ret;
43+
letcaught=false;
44+
letret,err;
45+
conststart=Date.now();
4746
try{
48-
cmd=`"${process.execPath}" -e "setTimeout(function(){}, ${SLEEP});"`;
47+
constcmd=`"${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+
constcmd=`"${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+
constret=execSync(cmd);
75+
assert.strictEqual(ret.length,msgBuf.length);
76+
assert.deepStrictEqual(ret,msgBuf);
77+
}
8078

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

8384
constargs=[
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+
constret=execFileSync(process.execPath,args);
90+
assert.deepStrictEqual(ret,msgBuf);
91+
}
9292

93-
assert.strictEqual(ret,`${msg}\n`);
93+
{
94+
constret=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(typeoferr.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
returntrue;
141145
});
142146
}

0 commit comments

Comments
(0)