|
21 | 21 |
|
22 | 22 | 'use strict';
|
23 | 23 | 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'); |
24 | 29 | const assert = require('assert');
|
25 |
| -const fs = require('fs'); |
26 |
| -const { COPYFILE_FICLONE } = fs.constants; |
27 | 30 | const path = require('path');
|
28 |
| -const tmpdir = require('../common/tmpdir'); |
| 31 | +const fs = require('fs'); |
| 32 | +const { fork } = require('child_process'); |
| 33 | + |
29 | 34 | const msg = { test: 'this' };
|
30 | 35 | const nodePath = process.execPath;
|
31 | 36 | const copyPath = path.join(tmpdir.path, 'node-copy.exe');
|
32 |
| -const { addLibraryPath } = require('../common/shared-lib-util'); |
33 | 37 |
|
34 | 38 | addLibraryPath(process.env);
|
35 | 39 |
|
| 40 | +// Child |
36 | 41 | 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); |
39 | 44 | 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(); |
65 | 46 | }
|
| 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