11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures'); 51cb0ef41Sopenharmony_ci// Refs: https://github.com/nodejs/node/pull/2253 61cb0ef41Sopenharmony_ciif (common.isSunOS) 71cb0ef41Sopenharmony_ci common.skip('unreliable on SunOS'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciconst assert = require('assert'); 101cb0ef41Sopenharmony_ciconst childProcess = require('child_process'); 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciconst nodeBinary = process.argv[0]; 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciconst preloadOption = (preloads) => { 151cb0ef41Sopenharmony_ci let option = ''; 161cb0ef41Sopenharmony_ci preloads.forEach(function(preload, index) { 171cb0ef41Sopenharmony_ci option += `-r "${preload}" `; 181cb0ef41Sopenharmony_ci }); 191cb0ef41Sopenharmony_ci return option; 201cb0ef41Sopenharmony_ci}; 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ciconst fixtureA = fixtures.path('printA.js'); 231cb0ef41Sopenharmony_ciconst fixtureB = fixtures.path('printB.js'); 241cb0ef41Sopenharmony_ciconst fixtureC = fixtures.path('printC.js'); 251cb0ef41Sopenharmony_ciconst fixtureD = fixtures.path('define-global.js'); 261cb0ef41Sopenharmony_ciconst fixtureE = fixtures.path('intrinsic-mutation.js'); 271cb0ef41Sopenharmony_ciconst fixtureF = fixtures.path('print-intrinsic-mutation-name.js'); 281cb0ef41Sopenharmony_ciconst fixtureG = fixtures.path('worker-from-argv.js'); 291cb0ef41Sopenharmony_ciconst fixtureThrows = fixtures.path('throws_error4.js'); 301cb0ef41Sopenharmony_ciconst fixtureIsPreloading = fixtures.path('ispreloading.js'); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci// Assert that module.isPreloading is false here 331cb0ef41Sopenharmony_ciassert(!module.isPreloading); 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci// Test that module.isPreloading is set in preloaded module 361cb0ef41Sopenharmony_ci// Test preloading a single module works 371cb0ef41Sopenharmony_cichildProcess.exec( 381cb0ef41Sopenharmony_ci `"${nodeBinary}" ${preloadOption([fixtureIsPreloading])} "${fixtureB}"`, 391cb0ef41Sopenharmony_ci function(err, stdout, stderr) { 401cb0ef41Sopenharmony_ci assert.ifError(err); 411cb0ef41Sopenharmony_ci assert.strictEqual(stdout, 'B\n'); 421cb0ef41Sopenharmony_ci }); 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci// Test preloading a single module works 451cb0ef41Sopenharmony_cichildProcess.exec(`"${nodeBinary}" ${preloadOption([fixtureA])} "${fixtureB}"`, 461cb0ef41Sopenharmony_ci function(err, stdout, stderr) { 471cb0ef41Sopenharmony_ci assert.ifError(err); 481cb0ef41Sopenharmony_ci assert.strictEqual(stdout, 'A\nB\n'); 491cb0ef41Sopenharmony_ci }); 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci// Test preloading multiple modules works 521cb0ef41Sopenharmony_cichildProcess.exec( 531cb0ef41Sopenharmony_ci `"${nodeBinary}" ${preloadOption([fixtureA, fixtureB])} "${fixtureC}"`, 541cb0ef41Sopenharmony_ci function(err, stdout, stderr) { 551cb0ef41Sopenharmony_ci assert.ifError(err); 561cb0ef41Sopenharmony_ci assert.strictEqual(stdout, 'A\nB\nC\n'); 571cb0ef41Sopenharmony_ci } 581cb0ef41Sopenharmony_ci); 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci// Test that preloading a throwing module aborts 611cb0ef41Sopenharmony_cichildProcess.exec( 621cb0ef41Sopenharmony_ci `"${nodeBinary}" ${preloadOption([fixtureA, fixtureThrows])} "${fixtureB}"`, 631cb0ef41Sopenharmony_ci function(err, stdout, stderr) { 641cb0ef41Sopenharmony_ci if (err) { 651cb0ef41Sopenharmony_ci assert.strictEqual(stdout, 'A\n'); 661cb0ef41Sopenharmony_ci } else { 671cb0ef41Sopenharmony_ci throw new Error('Preload should have failed'); 681cb0ef41Sopenharmony_ci } 691cb0ef41Sopenharmony_ci } 701cb0ef41Sopenharmony_ci); 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ci// Test that preload can be used with --eval 731cb0ef41Sopenharmony_cichildProcess.exec( 741cb0ef41Sopenharmony_ci `"${nodeBinary}" ${preloadOption([fixtureA])}-e "console.log('hello');"`, 751cb0ef41Sopenharmony_ci function(err, stdout, stderr) { 761cb0ef41Sopenharmony_ci assert.ifError(err); 771cb0ef41Sopenharmony_ci assert.strictEqual(stdout, 'A\nhello\n'); 781cb0ef41Sopenharmony_ci } 791cb0ef41Sopenharmony_ci); 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci// Test that preload can be used with --frozen-intrinsics 821cb0ef41Sopenharmony_cichildProcess.exec( 831cb0ef41Sopenharmony_ci `"${nodeBinary}" --frozen-intrinsics ${ 841cb0ef41Sopenharmony_ci preloadOption([fixtureE]) 851cb0ef41Sopenharmony_ci } ${ 861cb0ef41Sopenharmony_ci fixtureF 871cb0ef41Sopenharmony_ci }`, 881cb0ef41Sopenharmony_ci function(err, stdout) { 891cb0ef41Sopenharmony_ci assert.ifError(err); 901cb0ef41Sopenharmony_ci assert.strictEqual(stdout, 'smoosh\n'); 911cb0ef41Sopenharmony_ci } 921cb0ef41Sopenharmony_ci); 931cb0ef41Sopenharmony_cichildProcess.exec( 941cb0ef41Sopenharmony_ci `"${ 951cb0ef41Sopenharmony_ci nodeBinary 961cb0ef41Sopenharmony_ci }" --frozen-intrinsics ${ 971cb0ef41Sopenharmony_ci preloadOption([fixtureE]) 981cb0ef41Sopenharmony_ci } ${ 991cb0ef41Sopenharmony_ci fixtureG 1001cb0ef41Sopenharmony_ci } ${fixtureF}`, 1011cb0ef41Sopenharmony_ci function(err, stdout) { 1021cb0ef41Sopenharmony_ci assert.ifError(err); 1031cb0ef41Sopenharmony_ci assert.strictEqual(stdout, 'smoosh\n'); 1041cb0ef41Sopenharmony_ci } 1051cb0ef41Sopenharmony_ci); 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci// Test that preload can be used with stdin 1081cb0ef41Sopenharmony_ciconst stdinProc = childProcess.spawn( 1091cb0ef41Sopenharmony_ci nodeBinary, 1101cb0ef41Sopenharmony_ci ['--require', fixtureA], 1111cb0ef41Sopenharmony_ci { stdio: 'pipe' } 1121cb0ef41Sopenharmony_ci); 1131cb0ef41Sopenharmony_cistdinProc.stdin.end("console.log('hello');"); 1141cb0ef41Sopenharmony_cilet stdinStdout = ''; 1151cb0ef41Sopenharmony_cistdinProc.stdout.on('data', function(d) { 1161cb0ef41Sopenharmony_ci stdinStdout += d; 1171cb0ef41Sopenharmony_ci}); 1181cb0ef41Sopenharmony_cistdinProc.on('close', function(code) { 1191cb0ef41Sopenharmony_ci assert.strictEqual(code, 0); 1201cb0ef41Sopenharmony_ci assert.strictEqual(stdinStdout, 'A\nhello\n'); 1211cb0ef41Sopenharmony_ci}); 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ci// Test that preload can be used with repl 1241cb0ef41Sopenharmony_ciconst replProc = childProcess.spawn( 1251cb0ef41Sopenharmony_ci nodeBinary, 1261cb0ef41Sopenharmony_ci ['-i', '--require', fixtureA], 1271cb0ef41Sopenharmony_ci { stdio: 'pipe' } 1281cb0ef41Sopenharmony_ci); 1291cb0ef41Sopenharmony_cireplProc.stdin.end('.exit\n'); 1301cb0ef41Sopenharmony_cilet replStdout = ''; 1311cb0ef41Sopenharmony_cireplProc.stdout.on('data', (d) => { 1321cb0ef41Sopenharmony_ci replStdout += d; 1331cb0ef41Sopenharmony_ci}); 1341cb0ef41Sopenharmony_cireplProc.on('close', function(code) { 1351cb0ef41Sopenharmony_ci assert.strictEqual(code, 0); 1361cb0ef41Sopenharmony_ci const output = [ 1371cb0ef41Sopenharmony_ci 'A', 1381cb0ef41Sopenharmony_ci '> ', 1391cb0ef41Sopenharmony_ci ]; 1401cb0ef41Sopenharmony_ci assert.ok(replStdout.startsWith(output[0])); 1411cb0ef41Sopenharmony_ci assert.ok(replStdout.endsWith(output[1])); 1421cb0ef41Sopenharmony_ci}); 1431cb0ef41Sopenharmony_ci 1441cb0ef41Sopenharmony_ci// Test that preload placement at other points in the cmdline 1451cb0ef41Sopenharmony_ci// also test that duplicated preload only gets loaded once 1461cb0ef41Sopenharmony_cichildProcess.exec( 1471cb0ef41Sopenharmony_ci `"${nodeBinary}" ${preloadOption([fixtureA])}-e "console.log('hello');" ${ 1481cb0ef41Sopenharmony_ci preloadOption([fixtureA, fixtureB])}`, 1491cb0ef41Sopenharmony_ci function(err, stdout, stderr) { 1501cb0ef41Sopenharmony_ci assert.ifError(err); 1511cb0ef41Sopenharmony_ci assert.strictEqual(stdout, 'A\nB\nhello\n'); 1521cb0ef41Sopenharmony_ci } 1531cb0ef41Sopenharmony_ci); 1541cb0ef41Sopenharmony_ci 1551cb0ef41Sopenharmony_ci// Test that preload works with -i 1561cb0ef41Sopenharmony_ciconst interactive = childProcess.exec( 1571cb0ef41Sopenharmony_ci `"${nodeBinary}" ${preloadOption([fixtureD])}-i`, 1581cb0ef41Sopenharmony_ci common.mustSucceed((stdout, stderr) => { 1591cb0ef41Sopenharmony_ci assert.ok(stdout.endsWith("> 'test'\n> ")); 1601cb0ef41Sopenharmony_ci }) 1611cb0ef41Sopenharmony_ci); 1621cb0ef41Sopenharmony_ci 1631cb0ef41Sopenharmony_ciinteractive.stdin.write('a\n'); 1641cb0ef41Sopenharmony_ciinteractive.stdin.write('process.exit()\n'); 1651cb0ef41Sopenharmony_ci 1661cb0ef41Sopenharmony_cichildProcess.exec( 1671cb0ef41Sopenharmony_ci `"${nodeBinary}" --require "${fixtures.path('cluster-preload.js')}" "${ 1681cb0ef41Sopenharmony_ci fixtures.path('cluster-preload-test.js')}"`, 1691cb0ef41Sopenharmony_ci function(err, stdout, stderr) { 1701cb0ef41Sopenharmony_ci assert.ifError(err); 1711cb0ef41Sopenharmony_ci assert.match(stdout, /worker terminated with code 43/); 1721cb0ef41Sopenharmony_ci } 1731cb0ef41Sopenharmony_ci); 1741cb0ef41Sopenharmony_ci 1751cb0ef41Sopenharmony_ci// Test that preloading with a relative path works 1761cb0ef41Sopenharmony_cichildProcess.exec( 1771cb0ef41Sopenharmony_ci `"${nodeBinary}" ${preloadOption(['./printA.js'])} "${fixtureB}"`, 1781cb0ef41Sopenharmony_ci { cwd: fixtures.fixturesDir }, 1791cb0ef41Sopenharmony_ci common.mustSucceed((stdout, stderr) => { 1801cb0ef41Sopenharmony_ci assert.strictEqual(stdout, 'A\nB\n'); 1811cb0ef41Sopenharmony_ci }) 1821cb0ef41Sopenharmony_ci); 1831cb0ef41Sopenharmony_ciif (common.isWindows) { 1841cb0ef41Sopenharmony_ci // https://github.com/nodejs/node/issues/21918 1851cb0ef41Sopenharmony_ci childProcess.exec( 1861cb0ef41Sopenharmony_ci `"${nodeBinary}" ${preloadOption(['.\\printA.js'])} "${fixtureB}"`, 1871cb0ef41Sopenharmony_ci { cwd: fixtures.fixturesDir }, 1881cb0ef41Sopenharmony_ci common.mustSucceed((stdout, stderr) => { 1891cb0ef41Sopenharmony_ci assert.strictEqual(stdout, 'A\nB\n'); 1901cb0ef41Sopenharmony_ci }) 1911cb0ef41Sopenharmony_ci ); 1921cb0ef41Sopenharmony_ci} 1931cb0ef41Sopenharmony_ci 1941cb0ef41Sopenharmony_ci// https://github.com/nodejs/node/issues/1691 1951cb0ef41Sopenharmony_cichildProcess.exec( 1961cb0ef41Sopenharmony_ci `"${nodeBinary}" --require ` + 1971cb0ef41Sopenharmony_ci `"${fixtures.path('cluster-preload.js')}" cluster-preload-test.js`, 1981cb0ef41Sopenharmony_ci { cwd: fixtures.fixturesDir }, 1991cb0ef41Sopenharmony_ci function(err, stdout, stderr) { 2001cb0ef41Sopenharmony_ci assert.ifError(err); 2011cb0ef41Sopenharmony_ci assert.match(stdout, /worker terminated with code 43/); 2021cb0ef41Sopenharmony_ci } 2031cb0ef41Sopenharmony_ci); 204