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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent c661d8c commit 2b2471b

8 files changed

+24
-44
lines changed

Diff for: test/parallel/test-cli-eval.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ const path = require('path');
3434
const fixtures = require('../common/fixtures');
3535
const nodejs = `"${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-
const cwd = 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) => {

Diff for: test/parallel/test-cli-node-options-disallowed.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
const common = 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
const tmpdir = 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
function disallow(opt) {
3431
const env = 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
const message = err.message.split(/\r?\n/)[1];
3734
const expect = `${process.execPath}: ${opt} is not allowed in NODE_OPTIONS`;
3835

Diff for: test/parallel/test-cli-node-options.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
const common = 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
const tmpdir = require('../common/tmpdir');
1412
tmpdir.refresh();
15-
process.chdir(tmpdir.path);
1613

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

Diff for: test/parallel/test-preload-print-process-argv.js

+6-9
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-
const common = require('../common');
6+
require('../common');
77

88
const tmpdir = require('../common/tmpdir');
99
const assert = require('assert');
10+
const { join } = require('path');
1011
const { spawnSync } = require('child_process');
1112
const fs = 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-
const child = spawnSync(process.execPath, ['-r', './preload.js', 'main.js']);
26+
const child = spawnSync(process.execPath, ['-r', './preload.js', 'main.js'],
27+
{ cwd: tmpdir.path });
3128

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

Diff for: test/parallel/test-preload.js

+3-4
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
const assert = require('assert');
1210
const childProcess = 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(/worker terminated with code 43/.test(stdout));

Diff for: test/parallel/test-tick-processor-arguments.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@ const fs = require('fs');
55
const assert = 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-
const logfile = fs.readdirSync('.').filter((name) => name.endsWith('.log'))[0];
18+
const files = fs.readdirSync(tmpdir.path);
19+
const logfile = 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);

Diff for: test/parallel/test-trace-events-environment.js

+1-4
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
const names = new Set([
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
const proc = cp.fork(__filename,
3834
[ 'child' ], {
35+
cwd: tmpdir.path,
3936
execArgv: [
4037
'--trace-event-categories',
4138
'node.environment'

Diff for: test/parallel/test-worker-prof.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
'use strict';
2-
const common = require('../common');
2+
require('../common');
33
const tmpdir = require('../common/tmpdir');
44
const fs = require('fs');
55
const assert = 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-
const logfiles = fs.readdirSync('.').filter((name) => /\.log$/.test(name));
24+
spawnSync(process.execPath, ['--prof', __filename, 'child'],
25+
{ cwd: tmpdir.path });
26+
const files = fs.readdirSync(tmpdir.path);
27+
const logfiles = files.filter((name) => /\.log$/.test(name));
2928
assert.strictEqual(logfiles.length, 2); // Parent thread + child thread.
3029

3130
for (const logfile of logfiles) {
32-
const lines = fs.readFileSync(logfile, 'utf8').split('\n');
31+
const lines = fs.readFileSync(join(tmpdir.path, logfile), 'utf8').split('\n');
3332
const ticks = 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)