2020-03-12 13:11:48 +01:00
|
|
|
|
'use strict';
|
|
|
|
|
const common = require('../common');
|
|
|
|
|
const fixtures = require('../common/fixtures');
|
2022-12-12 18:18:37 +01:00
|
|
|
|
const tmpdir = require('../common/tmpdir');
|
2020-03-12 13:11:48 +01:00
|
|
|
|
const assert = require('assert');
|
|
|
|
|
const child_process = require('child_process');
|
|
|
|
|
const path = require('path');
|
2022-12-12 18:18:37 +01:00
|
|
|
|
const fs = require('fs');
|
2020-03-12 13:11:48 +01:00
|
|
|
|
|
2022-12-12 18:18:37 +01:00
|
|
|
|
tmpdir.refresh();
|
2020-03-12 13:11:48 +01:00
|
|
|
|
common.allowGlobals(global.require);
|
2020-06-19 17:20:53 +02:00
|
|
|
|
common.allowGlobals(global.embedVars);
|
2022-12-12 18:18:37 +01:00
|
|
|
|
|
|
|
|
|
function resolveBuiltBinary(bin) {
|
|
|
|
|
let binary = `out/${common.buildType}/${bin}`;
|
|
|
|
|
if (common.isWindows) {
|
|
|
|
|
binary += '.exe';
|
|
|
|
|
}
|
|
|
|
|
return path.resolve(__dirname, '..', '..', binary);
|
2020-03-12 13:11:48 +01:00
|
|
|
|
}
|
2022-12-12 18:18:37 +01:00
|
|
|
|
|
|
|
|
|
const binary = resolveBuiltBinary('embedtest');
|
|
|
|
|
const standaloneNodeBinary = resolveBuiltBinary('node');
|
2020-03-12 13:11:48 +01:00
|
|
|
|
|
2020-06-19 17:20:53 +02:00
|
|
|
|
assert.strictEqual(
|
|
|
|
|
child_process.spawnSync(binary, ['console.log(42)'])
|
|
|
|
|
.stdout.toString().trim(),
|
|
|
|
|
'42');
|
|
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
|
child_process.spawnSync(binary, ['console.log(embedVars.nön_ascıı)'])
|
|
|
|
|
.stdout.toString().trim(),
|
|
|
|
|
'🏳️🌈');
|
|
|
|
|
|
2020-03-12 13:11:48 +01:00
|
|
|
|
assert.strictEqual(
|
|
|
|
|
child_process.spawnSync(binary, ['console.log(42)'])
|
|
|
|
|
.stdout.toString().trim(),
|
|
|
|
|
'42');
|
|
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
|
child_process.spawnSync(binary, ['throw new Error()']).status,
|
|
|
|
|
1);
|
|
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
|
child_process.spawnSync(binary, ['process.exitCode = 8']).status,
|
|
|
|
|
8);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const fixturePath = JSON.stringify(fixtures.path('exit.js'));
|
|
|
|
|
assert.strictEqual(
|
|
|
|
|
child_process.spawnSync(binary, [`require(${fixturePath})`, 92]).status,
|
|
|
|
|
92);
|
2022-12-12 18:18:37 +01:00
|
|
|
|
|
|
|
|
|
// Basic snapshot support
|
|
|
|
|
{
|
|
|
|
|
const snapshotFixture = fixtures.path('snapshot', 'echo-args.js');
|
|
|
|
|
const blobPath = path.join(tmpdir.path, 'embedder-snapshot.blob');
|
|
|
|
|
const buildSnapshotArgs = [snapshotFixture, 'arg1', 'arg2'];
|
|
|
|
|
const runEmbeddedArgs = ['--embedder-snapshot-blob', blobPath, 'arg3', 'arg4'];
|
|
|
|
|
|
|
|
|
|
fs.rmSync(blobPath, { force: true });
|
|
|
|
|
assert.strictEqual(child_process.spawnSync(standaloneNodeBinary, [
|
|
|
|
|
'--snapshot-blob', blobPath, '--build-snapshot', ...buildSnapshotArgs,
|
|
|
|
|
], {
|
|
|
|
|
cwd: tmpdir.path,
|
|
|
|
|
}).status, 0);
|
|
|
|
|
const spawnResult = child_process.spawnSync(binary, ['--', ...runEmbeddedArgs]);
|
|
|
|
|
assert.deepStrictEqual(JSON.parse(spawnResult.stdout), {
|
|
|
|
|
originalArgv: [standaloneNodeBinary, ...buildSnapshotArgs],
|
|
|
|
|
currentArgv: [binary, ...runEmbeddedArgs],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create workers and vm contexts after deserialization
|
|
|
|
|
{
|
|
|
|
|
const snapshotFixture = fixtures.path('snapshot', 'create-worker-and-vm.js');
|
|
|
|
|
const blobPath = path.join(tmpdir.path, 'embedder-snapshot.blob');
|
|
|
|
|
|
|
|
|
|
fs.rmSync(blobPath, { force: true });
|
|
|
|
|
assert.strictEqual(child_process.spawnSync(standaloneNodeBinary, [
|
|
|
|
|
'--snapshot-blob', blobPath, '--build-snapshot', snapshotFixture,
|
|
|
|
|
], {
|
|
|
|
|
cwd: tmpdir.path,
|
|
|
|
|
}).status, 0);
|
|
|
|
|
assert.strictEqual(
|
|
|
|
|
child_process.spawnSync(binary, ['--', '--embedder-snapshot-blob', blobPath]).status,
|
|
|
|
|
0);
|
|
|
|
|
}
|