Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: try to stabilize test-child-process-fork-exec-path.js #27277

Closed
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 28 additions & 31 deletions test/parallel/test-child-process-fork-exec-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,42 @@

'use strict';
const common = require('../common');

// Test that `fork()` respects the `execPath` option.

const tmpdir = require('../common/tmpdir');
const { addLibraryPath } = require('../common/shared-lib-util');
const assert = require('assert');
const fs = require('fs');
const { COPYFILE_FICLONE } = fs.constants;
const path = require('path');
const tmpdir = require('../common/tmpdir');
const fs = require('fs');
const { fork } = require('child_process');

const msg = { test: 'this' };
const nodePath = process.execPath;
const copyPath = path.join(tmpdir.path, 'node-copy.exe');
const { addLibraryPath } = require('../common/shared-lib-util');

addLibraryPath(process.env);

// Child
if (process.env.FORK) {
assert(process.send);
assert.strictEqual(process.argv[0], copyPath);
assert.strictEqual(process.execPath, copyPath);
assert.ok(process.send);
process.send(msg);
process.exit();
} else {
tmpdir.refresh();
try {
fs.unlinkSync(copyPath);
} catch (e) {
if (e.code !== 'ENOENT') throw e;
}
fs.copyFileSync(nodePath, copyPath, COPYFILE_FICLONE);
fs.chmodSync(copyPath, '0755');

// slow but simple
const envCopy = JSON.parse(JSON.stringify(process.env));
envCopy.FORK = 'true';
const child = require('child_process').fork(__filename, {
execPath: copyPath,
env: envCopy
});
child.on('message', common.mustCall(function(recv) {
assert.deepStrictEqual(msg, recv);
}));
child.on('exit', common.mustCall(function(code) {
fs.unlinkSync(copyPath);
assert.strictEqual(code, 0);
}));
return process.exit();
}

// Parent
tmpdir.refresh();
assert.strictEqual(fs.existsSync(copyPath), false);
fs.copyFileSync(nodePath, copyPath, fs.constants.COPYFILE_FICLONE);
fs.chmodSync(copyPath, '0755');

// slow but simple
refack marked this conversation as resolved.
Show resolved Hide resolved
const envCopy = Object.assign({}, process.env, { 'FORK': 'true' });
const child = fork(__filename, { execPath: copyPath, env: envCopy });
child.on('message', common.mustCall(function(recv) {
assert.deepStrictEqual(recv, msg);
}));
child.on('exit', common.mustCall(function(code) {
assert.strictEqual(code, 0);
}));