From 82a256ac6740b7a6d29439af911b86a7a0b2e63f Mon Sep 17 00:00:00 2001 From: Richard Lau Date: Tue, 5 Mar 2019 08:56:16 -0500 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/26453 Reviewed-By: Ben Noordhuis Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- test/parallel/test-cli-eval.js | 7 +------ test/parallel/test-cli-node-options-disallowed.js | 5 +---- test/parallel/test-cli-node-options.js | 4 +--- test/parallel/test-preload-print-process-argv.js | 15 ++++++--------- test/parallel/test-preload.js | 7 +++---- test/parallel/test-tick-processor-arguments.js | 10 ++++------ test/parallel/test-trace-events-environment.js | 5 +---- test/parallel/test-worker-prof.js | 15 +++++++-------- 8 files changed, 24 insertions(+), 44 deletions(-) diff --git a/test/parallel/test-cli-eval.js b/test/parallel/test-cli-eval.js index 7a2c7a82aa797f..3fd00e7cdb4581 100644 --- a/test/parallel/test-cli-eval.js +++ b/test/parallel/test-cli-eval.js @@ -34,9 +34,6 @@ const path = require('path'); const fixtures = require('../common/fixtures'); const nodejs = `"${process.execPath}"`; -if (!common.isMainThread) - common.skip('process.chdir is not available in Workers'); - if (process.argv.length > 2) { console.log(process.argv.slice(2).join(' ')); process.exit(0); @@ -98,16 +95,14 @@ child.exec(`${nodejs} --print "os.platform()"`, })); // Module path resolve bug regression test. -const cwd = process.cwd(); -process.chdir(path.resolve(__dirname, '../../')); child.exec(`${nodejs} --eval "require('./test/parallel/test-cli-eval.js')"`, + { cwd: path.resolve(__dirname, '../../') }, common.mustCall((err, stdout, stderr) => { assert.strictEqual(err.code, 42); assert.strictEqual( stdout, 'Loaded as a module, exiting with status code 42.\n'); assert.strictEqual(stderr, ''); })); -process.chdir(cwd); // Missing argument should not crash. child.exec(`${nodejs} -e`, common.mustCall((err, stdout, stderr) => { diff --git a/test/parallel/test-cli-node-options-disallowed.js b/test/parallel/test-cli-node-options-disallowed.js index 733e5cd7c7b977..867e829a1d9fba 100644 --- a/test/parallel/test-cli-node-options-disallowed.js +++ b/test/parallel/test-cli-node-options-disallowed.js @@ -2,8 +2,6 @@ const common = require('../common'); if (process.config.variables.node_without_node_options) common.skip('missing NODE_OPTIONS support'); -if (!common.isMainThread) - common.skip('process.chdir is not available in Workers'); // Test options specified by env variable. @@ -12,7 +10,6 @@ const exec = require('child_process').execFile; const tmpdir = require('../common/tmpdir'); tmpdir.refresh(); -process.chdir(tmpdir.path); disallow('--version'); disallow('-v'); @@ -32,7 +29,7 @@ disallow('--'); function disallow(opt) { const env = Object.assign({}, process.env, { NODE_OPTIONS: opt }); - exec(process.execPath, { env }, common.mustCall(function(err) { + exec(process.execPath, { cwd: tmpdir.path, env }, common.mustCall((err) => { const message = err.message.split(/\r?\n/)[1]; const expect = `${process.execPath}: ${opt} is not allowed in NODE_OPTIONS`; diff --git a/test/parallel/test-cli-node-options.js b/test/parallel/test-cli-node-options.js index 8f65a8cb7e8121..7f6524c09a8c81 100644 --- a/test/parallel/test-cli-node-options.js +++ b/test/parallel/test-cli-node-options.js @@ -2,8 +2,6 @@ const common = require('../common'); if (process.config.variables.node_without_node_options) common.skip('missing NODE_OPTIONS support'); -if (!common.isMainThread) - common.skip('process.chdir is not available in Workers'); // Test options specified by env variable. @@ -12,7 +10,6 @@ const exec = require('child_process').execFile; const tmpdir = require('../common/tmpdir'); tmpdir.refresh(); -process.chdir(tmpdir.path); const printA = require.resolve('../fixtures/printA.js'); expect(`-r ${printA}`, 'A\nB\n'); @@ -64,6 +61,7 @@ expect('--stack-trace-limit=100', function expect(opt, want, command = 'console.log("B")', wantsError = false) { const argv = ['-e', command]; const opts = { + cwd: tmpdir.path, env: Object.assign({}, process.env, { NODE_OPTIONS: opt }), maxBuffer: 1e6, }; diff --git a/test/parallel/test-preload-print-process-argv.js b/test/parallel/test-preload-print-process-argv.js index ace20dfb3947c4..8baec6972ccd61 100644 --- a/test/parallel/test-preload-print-process-argv.js +++ b/test/parallel/test-preload-print-process-argv.js @@ -3,31 +3,28 @@ // This tests that process.argv is the same in the preloaded module // and the user module. -const common = require('../common'); +require('../common'); const tmpdir = require('../common/tmpdir'); const assert = require('assert'); +const { join } = require('path'); const { spawnSync } = require('child_process'); const fs = require('fs'); -if (!common.isMainThread) { - common.skip('Cannot chdir to the tmp directory in workers'); -} - tmpdir.refresh(); -process.chdir(tmpdir.path); fs.writeFileSync( - 'preload.js', + join(tmpdir.path, 'preload.js'), 'console.log(JSON.stringify(process.argv));', 'utf-8'); fs.writeFileSync( - 'main.js', + join(tmpdir.path, 'main.js'), 'console.log(JSON.stringify(process.argv));', 'utf-8'); -const child = spawnSync(process.execPath, ['-r', './preload.js', 'main.js']); +const child = spawnSync(process.execPath, ['-r', './preload.js', 'main.js'], + { cwd: tmpdir.path }); if (child.status !== 0) { console.log(child.stderr.toString()); diff --git a/test/parallel/test-preload.js b/test/parallel/test-preload.js index e54fa385fe3277..3d754accae8a8c 100644 --- a/test/parallel/test-preload.js +++ b/test/parallel/test-preload.js @@ -5,8 +5,6 @@ const fixtures = require('../common/fixtures'); // Refs: https://github.com/nodejs/node/pull/2253 if (common.isSunOS) common.skip('unreliable on SunOS'); -if (!common.isMainThread) - common.skip('process.chdir is not available in Workers'); const assert = require('assert'); const childProcess = require('child_process'); @@ -133,9 +131,9 @@ childProcess.exec( ); // Test that preloading with a relative path works -process.chdir(fixtures.fixturesDir); childProcess.exec( `"${nodeBinary}" ${preloadOption(['./printA.js'])} "${fixtureB}"`, + { cwd: fixtures.fixturesDir }, common.mustCall(function(err, stdout, stderr) { assert.ifError(err); assert.strictEqual(stdout, 'A\nB\n'); @@ -145,6 +143,7 @@ if (common.isWindows) { // https://github.com/nodejs/node/issues/21918 childProcess.exec( `"${nodeBinary}" ${preloadOption(['.\\printA.js'])} "${fixtureB}"`, + { cwd: fixtures.fixturesDir }, common.mustCall(function(err, stdout, stderr) { assert.ifError(err); assert.strictEqual(stdout, 'A\nB\n'); @@ -153,10 +152,10 @@ if (common.isWindows) { } // https://github.com/nodejs/node/issues/1691 -process.chdir(fixtures.fixturesDir); childProcess.exec( `"${nodeBinary}" --require ` + `"${fixtures.path('cluster-preload.js')}" cluster-preload-test.js`, + { cwd: fixtures.fixturesDir }, function(err, stdout, stderr) { assert.ifError(err); assert.ok(/worker terminated with code 43/.test(stdout)); diff --git a/test/parallel/test-tick-processor-arguments.js b/test/parallel/test-tick-processor-arguments.js index 606dbd03c8f6c4..3f0ac73596e677 100644 --- a/test/parallel/test-tick-processor-arguments.js +++ b/test/parallel/test-tick-processor-arguments.js @@ -5,20 +5,18 @@ const fs = require('fs'); const assert = require('assert'); const { spawnSync } = require('child_process'); -if (!common.isMainThread) - common.skip('chdir not available in workers'); if (!common.enoughTestMem) common.skip('skipped due to memory requirements'); if (common.isAIX) common.skip('does not work on AIX'); tmpdir.refresh(); -process.chdir(tmpdir.path); // Generate log file. -spawnSync(process.execPath, [ '--prof', '-p', '42' ]); +spawnSync(process.execPath, [ '--prof', '-p', '42' ], { cwd: tmpdir.path }); -const logfile = fs.readdirSync('.').filter((name) => name.endsWith('.log'))[0]; +const files = fs.readdirSync(tmpdir.path); +const logfile = files.filter((name) => /\.log$/.test(name))[0]; assert(logfile); // Make sure that the --preprocess argument is passed through correctly, @@ -28,7 +26,7 @@ assert(logfile); const { stdout } = spawnSync( process.execPath, [ '--prof-process', '--preprocess', logfile ], - { encoding: 'utf8' }); + { cwd: tmpdir.path, encoding: 'utf8' }); // Make sure that the result is valid JSON. JSON.parse(stdout); diff --git a/test/parallel/test-trace-events-environment.js b/test/parallel/test-trace-events-environment.js index 39218d0aac225e..14900dfc96ff46 100644 --- a/test/parallel/test-trace-events-environment.js +++ b/test/parallel/test-trace-events-environment.js @@ -10,9 +10,6 @@ const tmpdir = require('../common/tmpdir'); // This tests the emission of node.environment trace events -if (!common.isMainThread) - common.skip('process.chdir is not available in Workers'); - const names = new Set([ 'Environment', 'RunAndClearNativeImmediates', @@ -32,10 +29,10 @@ if (process.argv[2] === 'child') { setTimeout(() => { 1 + 1; }, 1); } else { tmpdir.refresh(); - process.chdir(tmpdir.path); const proc = cp.fork(__filename, [ 'child' ], { + cwd: tmpdir.path, execArgv: [ '--trace-event-categories', 'node.environment' diff --git a/test/parallel/test-worker-prof.js b/test/parallel/test-worker-prof.js index d4344a8057f06f..141b101a722a48 100644 --- a/test/parallel/test-worker-prof.js +++ b/test/parallel/test-worker-prof.js @@ -1,14 +1,12 @@ 'use strict'; -const common = require('../common'); +require('../common'); const tmpdir = require('../common/tmpdir'); const fs = require('fs'); const assert = require('assert'); +const { join } = require('path'); const { spawnSync } = require('child_process'); const { Worker } = require('worker_threads'); -if (!common.isMainThread) - common.skip('process.chdir is not available in Workers'); - // Test that --prof also tracks Worker threads. // Refs: https://github.com/nodejs/node/issues/24016 @@ -23,13 +21,14 @@ if (process.argv[2] === 'child') { } tmpdir.refresh(); -process.chdir(tmpdir.path); -spawnSync(process.execPath, ['--prof', __filename, 'child']); -const logfiles = fs.readdirSync('.').filter((name) => /\.log$/.test(name)); +spawnSync(process.execPath, ['--prof', __filename, 'child'], + { cwd: tmpdir.path }); +const files = fs.readdirSync(tmpdir.path); +const logfiles = files.filter((name) => /\.log$/.test(name)); assert.strictEqual(logfiles.length, 2); // Parent thread + child thread. for (const logfile of logfiles) { - const lines = fs.readFileSync(logfile, 'utf8').split('\n'); + const lines = fs.readFileSync(join(tmpdir.path, logfile), 'utf8').split('\n'); const ticks = lines.filter((line) => /^tick,/.test(line)).length; // Test that at least 15 ticks have been recorded for both parent and child