Skip to content

Commit 2b2471b

Browse files
richardlauBridgeAR
authored andcommitted
test: fix tests so they work in worker threads
Use the `cwd` option for child_process instead of `process.chdir()` to allow tests to work with worker threads. PR-URL: #26453 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent c661d8c commit 2b2471b

8 files changed

Lines changed: 24 additions & 44 deletions

‎test/parallel/test-cli-eval.js‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ const path = require('path');
3434
constfixtures=require('../common/fixtures');
3535
constnodejs=`"${process.execPath}"`;
3636

37-
if(!common.isMainThread)
38-
common.skip('process.chdir is not available in Workers');
39-
4037
if(process.argv.length>2){
4138
console.log(process.argv.slice(2).join(' '));
4239
process.exit(0);
@@ -98,16 +95,14 @@ child.exec(`${nodejs} --print "os.platform()"`,
9895
}));
9996

10097
// Module path resolve bug regression test.
101-
constcwd=process.cwd();
102-
process.chdir(path.resolve(__dirname,'../../'));
10398
child.exec(`${nodejs} --eval "require('./test/parallel/test-cli-eval.js')"`,
99+
{cwd: path.resolve(__dirname,'../../')},
104100
common.mustCall((err,stdout,stderr)=>{
105101
assert.strictEqual(err.code,42);
106102
assert.strictEqual(
107103
stdout,'Loaded as a module, exiting with status code 42.\n');
108104
assert.strictEqual(stderr,'');
109105
}));
110-
process.chdir(cwd);
111106

112107
// Missing argument should not crash.
113108
child.exec(`${nodejs} -e`,common.mustCall((err,stdout,stderr)=>{

‎test/parallel/test-cli-node-options-disallowed.js‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
constcommon=require('../common');
33
if(process.config.variables.node_without_node_options)
44
common.skip('missing NODE_OPTIONS support');
5-
if(!common.isMainThread)
6-
common.skip('process.chdir is not available in Workers');
75

86
// Test options specified by env variable.
97

@@ -12,7 +10,6 @@ const exec = require('child_process').execFile;
1210

1311
consttmpdir=require('../common/tmpdir');
1412
tmpdir.refresh();
15-
process.chdir(tmpdir.path);
1613

1714
disallow('--version');
1815
disallow('-v');
@@ -32,7 +29,7 @@ disallow('--');
3229

3330
functiondisallow(opt){
3431
constenv=Object.assign({},process.env,{NODE_OPTIONS: opt});
35-
exec(process.execPath,{ env },common.mustCall(function(err){
32+
exec(process.execPath,{cwd: tmpdir.path,env },common.mustCall((err)=>{
3633
constmessage=err.message.split(/\r?\n/)[1];
3734
constexpect=`${process.execPath}: ${opt} is not allowed in NODE_OPTIONS`;
3835

‎test/parallel/test-cli-node-options.js‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
constcommon=require('../common');
33
if(process.config.variables.node_without_node_options)
44
common.skip('missing NODE_OPTIONS support');
5-
if(!common.isMainThread)
6-
common.skip('process.chdir is not available in Workers');
75

86
// Test options specified by env variable.
97

@@ -12,7 +10,6 @@ const exec = require('child_process').execFile;
1210

1311
consttmpdir=require('../common/tmpdir');
1412
tmpdir.refresh();
15-
process.chdir(tmpdir.path);
1613

1714
constprintA=require.resolve('../fixtures/printA.js');
1815
expect(`-r ${printA}`,'A\nB\n');
@@ -64,6 +61,7 @@ expect('--stack-trace-limit=100',
6461
functionexpect(opt,want,command='console.log("B")',wantsError=false){
6562
constargv=['-e',command];
6663
constopts={
64+
cwd: tmpdir.path,
6765
env: Object.assign({},process.env,{NODE_OPTIONS: opt}),
6866
maxBuffer: 1e6,
6967
};

‎test/parallel/test-preload-print-process-argv.js‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,28 @@
33
// This tests that process.argv is the same in the preloaded module
44
// and the user module.
55

6-
constcommon=require('../common');
6+
require('../common');
77

88
consttmpdir=require('../common/tmpdir');
99
constassert=require('assert');
10+
const{ join }=require('path');
1011
const{ spawnSync }=require('child_process');
1112
constfs=require('fs');
1213

13-
if(!common.isMainThread){
14-
common.skip('Cannot chdir to the tmp directory in workers');
15-
}
16-
1714
tmpdir.refresh();
1815

19-
process.chdir(tmpdir.path);
2016
fs.writeFileSync(
21-
'preload.js',
17+
join(tmpdir.path,'preload.js'),
2218
'console.log(JSON.stringify(process.argv));',
2319
'utf-8');
2420

2521
fs.writeFileSync(
26-
'main.js',
22+
join(tmpdir.path,'main.js'),
2723
'console.log(JSON.stringify(process.argv));',
2824
'utf-8');
2925

30-
constchild=spawnSync(process.execPath,['-r','./preload.js','main.js']);
26+
constchild=spawnSync(process.execPath,['-r','./preload.js','main.js'],
27+
{cwd: tmpdir.path});
3128

3229
if(child.status!==0){
3330
console.log(child.stderr.toString());

‎test/parallel/test-preload.js‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ const fixtures = require('../common/fixtures');
55
// Refs: https://github.com/nodejs/node/pull/2253
66
if(common.isSunOS)
77
common.skip('unreliable on SunOS');
8-
if(!common.isMainThread)
9-
common.skip('process.chdir is not available in Workers');
108

119
constassert=require('assert');
1210
constchildProcess=require('child_process');
@@ -133,9 +131,9 @@ childProcess.exec(
133131
);
134132

135133
// Test that preloading with a relative path works
136-
process.chdir(fixtures.fixturesDir);
137134
childProcess.exec(
138135
`"${nodeBinary}" ${preloadOption(['./printA.js'])} "${fixtureB}"`,
136+
{cwd: fixtures.fixturesDir},
139137
common.mustCall(function(err,stdout,stderr){
140138
assert.ifError(err);
141139
assert.strictEqual(stdout,'A\nB\n');
@@ -145,6 +143,7 @@ if (common.isWindows) {
145143
// https://github.com/nodejs/node/issues/21918
146144
childProcess.exec(
147145
`"${nodeBinary}" ${preloadOption(['.\\printA.js'])} "${fixtureB}"`,
146+
{cwd: fixtures.fixturesDir},
148147
common.mustCall(function(err,stdout,stderr){
149148
assert.ifError(err);
150149
assert.strictEqual(stdout,'A\nB\n');
@@ -153,10 +152,10 @@ if (common.isWindows) {
153152
}
154153

155154
// https://github.com/nodejs/node/issues/1691
156-
process.chdir(fixtures.fixturesDir);
157155
childProcess.exec(
158156
`"${nodeBinary}" --require `+
159157
`"${fixtures.path('cluster-preload.js')}" cluster-preload-test.js`,
158+
{cwd: fixtures.fixturesDir},
160159
function(err,stdout,stderr){
161160
assert.ifError(err);
162161
assert.ok(/workerterminatedwithcode43/.test(stdout));

‎test/parallel/test-tick-processor-arguments.js‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@ const fs = require('fs');
55
constassert=require('assert');
66
const{ spawnSync }=require('child_process');
77

8-
if(!common.isMainThread)
9-
common.skip('chdir not available in workers');
108
if(!common.enoughTestMem)
119
common.skip('skipped due to memory requirements');
1210
if(common.isAIX)
1311
common.skip('does not work on AIX');
1412

1513
tmpdir.refresh();
16-
process.chdir(tmpdir.path);
1714

1815
// Generate log file.
19-
spawnSync(process.execPath,['--prof','-p','42']);
16+
spawnSync(process.execPath,['--prof','-p','42'],{cwd: tmpdir.path});
2017

21-
constlogfile=fs.readdirSync('.').filter((name)=>name.endsWith('.log'))[0];
18+
constfiles=fs.readdirSync(tmpdir.path);
19+
constlogfile=files.filter((name)=>/\.log$/.test(name))[0];
2220
assert(logfile);
2321

2422
// Make sure that the --preprocess argument is passed through correctly,
@@ -28,7 +26,7 @@ assert(logfile);
2826
const{ stdout }=spawnSync(
2927
process.execPath,
3028
['--prof-process','--preprocess',logfile],
31-
{encoding: 'utf8'});
29+
{cwd: tmpdir.path,encoding: 'utf8'});
3230

3331
// Make sure that the result is valid JSON.
3432
JSON.parse(stdout);

‎test/parallel/test-trace-events-environment.js‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ const tmpdir = require('../common/tmpdir');
1010

1111
// This tests the emission of node.environment trace events
1212

13-
if(!common.isMainThread)
14-
common.skip('process.chdir is not available in Workers');
15-
1613
constnames=newSet([
1714
'Environment',
1815
'RunAndClearNativeImmediates',
@@ -32,10 +29,10 @@ if (process.argv[2] === 'child') {
3229
setTimeout(()=>{1+1;},1);
3330
}else{
3431
tmpdir.refresh();
35-
process.chdir(tmpdir.path);
3632

3733
constproc=cp.fork(__filename,
3834
['child'],{
35+
cwd: tmpdir.path,
3936
execArgv: [
4037
'--trace-event-categories',
4138
'node.environment'

‎test/parallel/test-worker-prof.js‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
'use strict';
2-
constcommon=require('../common');
2+
require('../common');
33
consttmpdir=require('../common/tmpdir');
44
constfs=require('fs');
55
constassert=require('assert');
6+
const{ join }=require('path');
67
const{ spawnSync }=require('child_process');
78
const{ Worker }=require('worker_threads');
89

9-
if(!common.isMainThread)
10-
common.skip('process.chdir is not available in Workers');
11-
1210
// Test that --prof also tracks Worker threads.
1311
// Refs: https://github.com/nodejs/node/issues/24016
1412

@@ -23,13 +21,14 @@ if (process.argv[2] === 'child') {
2321
}
2422

2523
tmpdir.refresh();
26-
process.chdir(tmpdir.path);
27-
spawnSync(process.execPath,['--prof',__filename,'child']);
28-
constlogfiles=fs.readdirSync('.').filter((name)=>/\.log$/.test(name));
24+
spawnSync(process.execPath,['--prof',__filename,'child'],
25+
{cwd: tmpdir.path});
26+
constfiles=fs.readdirSync(tmpdir.path);
27+
constlogfiles=files.filter((name)=>/\.log$/.test(name));
2928
assert.strictEqual(logfiles.length,2);// Parent thread + child thread.
3029

3130
for(constlogfileoflogfiles){
32-
constlines=fs.readFileSync(logfile,'utf8').split('\n');
31+
constlines=fs.readFileSync(join(tmpdir.path,logfile),'utf8').split('\n');
3332
constticks=lines.filter((line)=>/^tick,/.test(line)).length;
3433

3534
// Test that at least 15 ticks have been recorded for both parent and child

0 commit comments

Comments
 (0)