Skip to content

Commit 4f8b497

Browse files
refackdanbev
authored andcommittedApr 18, 2019
test: try to stabalize test-child-process-fork-exec-path.js
PR-URL: #27277 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent 002dacb commit 4f8b497

File tree

1 file changed

+27
-31
lines changed

1 file changed

+27
-31
lines changed
 

‎test/parallel/test-child-process-fork-exec-path.js

+27-31
Original file line numberDiff line numberDiff line change
@@ -21,45 +21,41 @@
2121

2222
'use strict';
2323
const common = require('../common');
24+
25+
// Test that `fork()` respects the `execPath` option.
26+
27+
const tmpdir = require('../common/tmpdir');
28+
const { addLibraryPath } = require('../common/shared-lib-util');
2429
const assert = require('assert');
25-
const fs = require('fs');
26-
const { COPYFILE_FICLONE } = fs.constants;
2730
const path = require('path');
28-
const tmpdir = require('../common/tmpdir');
31+
const fs = require('fs');
32+
const { fork } = require('child_process');
33+
2934
const msg = { test: 'this' };
3035
const nodePath = process.execPath;
3136
const copyPath = path.join(tmpdir.path, 'node-copy.exe');
32-
const { addLibraryPath } = require('../common/shared-lib-util');
3337

3438
addLibraryPath(process.env);
3539

40+
// Child
3641
if (process.env.FORK) {
37-
assert(process.send);
38-
assert.strictEqual(process.argv[0], copyPath);
42+
assert.strictEqual(process.execPath, copyPath);
43+
assert.ok(process.send);
3944
process.send(msg);
40-
process.exit();
41-
} else {
42-
tmpdir.refresh();
43-
try {
44-
fs.unlinkSync(copyPath);
45-
} catch (e) {
46-
if (e.code !== 'ENOENT') throw e;
47-
}
48-
fs.copyFileSync(nodePath, copyPath, COPYFILE_FICLONE);
49-
fs.chmodSync(copyPath, '0755');
50-
51-
// slow but simple
52-
const envCopy = JSON.parse(JSON.stringify(process.env));
53-
envCopy.FORK = 'true';
54-
const child = require('child_process').fork(__filename, {
55-
execPath: copyPath,
56-
env: envCopy
57-
});
58-
child.on('message', common.mustCall(function(recv) {
59-
assert.deepStrictEqual(msg, recv);
60-
}));
61-
child.on('exit', common.mustCall(function(code) {
62-
fs.unlinkSync(copyPath);
63-
assert.strictEqual(code, 0);
64-
}));
45+
return process.exit();
6546
}
47+
48+
// Parent
49+
tmpdir.refresh();
50+
assert.strictEqual(fs.existsSync(copyPath), false);
51+
fs.copyFileSync(nodePath, copyPath, fs.constants.COPYFILE_FICLONE);
52+
fs.chmodSync(copyPath, '0755');
53+
54+
const envCopy = Object.assign({}, process.env, { 'FORK': 'true' });
55+
const child = fork(__filename, { execPath: copyPath, env: envCopy });
56+
child.on('message', common.mustCall(function(recv) {
57+
assert.deepStrictEqual(recv, msg);
58+
}));
59+
child.on('exit', common.mustCall(function(code) {
60+
assert.strictEqual(code, 0);
61+
}));

0 commit comments

Comments
 (0)
Please sign in to comment.