2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2016-07-19 02:03:42 +05:30
|
|
|
const common = require('../common');
|
2016-12-30 18:38:06 -05:00
|
|
|
const assert = require('assert');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2017-01-08 13:19:00 +00:00
|
|
|
const msg = {test: 'this'};
|
|
|
|
const nodePath = process.execPath;
|
|
|
|
const copyPath = path.join(common.tmpDir, 'node-copy.exe');
|
2013-01-04 13:58:35 -06:00
|
|
|
|
|
|
|
if (process.env.FORK) {
|
|
|
|
assert(process.send);
|
2016-12-01 09:01:51 -08:00
|
|
|
assert.strictEqual(process.argv[0], copyPath);
|
2013-01-04 13:58:35 -06:00
|
|
|
process.send(msg);
|
|
|
|
process.exit();
|
2016-07-08 17:17:47 -07:00
|
|
|
} else {
|
2015-06-09 11:40:55 -07:00
|
|
|
common.refreshTmpDir();
|
2013-01-04 13:58:35 -06:00
|
|
|
try {
|
2013-03-08 08:29:36 -08:00
|
|
|
fs.unlinkSync(copyPath);
|
2016-07-08 17:17:47 -07:00
|
|
|
} catch (e) {
|
2013-01-04 13:58:35 -06:00
|
|
|
if (e.code !== 'ENOENT') throw e;
|
|
|
|
}
|
2013-03-08 08:29:36 -08:00
|
|
|
fs.writeFileSync(copyPath, fs.readFileSync(nodePath));
|
|
|
|
fs.chmodSync(copyPath, '0755');
|
2013-01-04 13:58:35 -06:00
|
|
|
|
2013-04-10 15:38:02 +02:00
|
|
|
// slow but simple
|
2017-01-08 13:19:00 +00:00
|
|
|
const envCopy = JSON.parse(JSON.stringify(process.env));
|
2013-04-10 15:38:02 +02:00
|
|
|
envCopy.FORK = 'true';
|
2016-12-30 18:38:06 -05:00
|
|
|
const child = require('child_process').fork(__filename, {
|
2013-03-08 08:29:36 -08:00
|
|
|
execPath: copyPath,
|
2013-04-10 15:38:02 +02:00
|
|
|
env: envCopy
|
2013-01-04 13:58:35 -06:00
|
|
|
});
|
|
|
|
child.on('message', common.mustCall(function(recv) {
|
2016-04-19 15:37:45 -07:00
|
|
|
assert.deepStrictEqual(msg, recv);
|
2013-01-04 13:58:35 -06:00
|
|
|
}));
|
|
|
|
child.on('exit', common.mustCall(function(code) {
|
2013-03-08 08:29:36 -08:00
|
|
|
fs.unlinkSync(copyPath);
|
2016-12-01 09:01:51 -08:00
|
|
|
assert.strictEqual(code, 0);
|
2013-01-04 13:58:35 -06:00
|
|
|
}));
|
|
|
|
}
|