2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2012-07-28 14:00:27 -07:00
|
|
|
if (module.parent) {
|
|
|
|
// signal we've been loaded as a module
|
|
|
|
console.log('Loaded as a module, exiting with status code 42.');
|
|
|
|
process.exit(42);
|
|
|
|
}
|
|
|
|
|
2016-01-13 21:42:45 +01:00
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const child = require('child_process');
|
|
|
|
const path = require('path');
|
|
|
|
const nodejs = '"' + process.execPath + '"';
|
2011-08-23 23:42:23 -07:00
|
|
|
|
2012-07-28 14:00:27 -07:00
|
|
|
|
2011-09-11 03:52:44 +02:00
|
|
|
// replace \ by / because windows uses backslashes in paths, but they're still
|
|
|
|
// interpreted as the escape character when put between quotes.
|
2017-01-08 13:19:00 +00:00
|
|
|
const filename = __filename.replace(/\\/g, '/');
|
2010-11-16 15:44:06 +01:00
|
|
|
|
2011-07-25 23:54:44 +02:00
|
|
|
// assert that nothing is written to stdout
|
|
|
|
child.exec(nodejs + ' --eval 42',
|
2016-12-31 21:39:57 -08:00
|
|
|
function(err, stdout, stderr) {
|
|
|
|
assert.strictEqual(stdout, '');
|
|
|
|
assert.strictEqual(stderr, '');
|
|
|
|
});
|
2011-07-25 23:54:44 +02:00
|
|
|
|
2011-08-04 16:43:11 +02:00
|
|
|
// assert that "42\n" is written to stderr
|
2011-09-11 03:52:44 +02:00
|
|
|
child.exec(nodejs + ' --eval "console.error(42)"',
|
2016-12-31 21:39:57 -08:00
|
|
|
function(err, stdout, stderr) {
|
|
|
|
assert.strictEqual(stdout, '');
|
|
|
|
assert.strictEqual(stderr, '42\n');
|
|
|
|
});
|
2010-11-16 15:44:06 +01:00
|
|
|
|
2012-05-18 02:02:57 +02:00
|
|
|
// assert that the expected output is written to stdout
|
2012-09-04 14:19:59 +02:00
|
|
|
['--print', '-p -e', '-pe', '-p'].forEach(function(s) {
|
2017-01-08 13:19:00 +00:00
|
|
|
const cmd = nodejs + ' ' + s + ' ';
|
2011-12-01 17:21:00 +01:00
|
|
|
|
|
|
|
child.exec(cmd + '42',
|
2016-12-31 21:39:57 -08:00
|
|
|
function(err, stdout, stderr) {
|
|
|
|
assert.strictEqual(stdout, '42\n');
|
|
|
|
assert.strictEqual(stderr, '');
|
|
|
|
});
|
2011-12-01 17:21:00 +01:00
|
|
|
|
2016-05-23 16:14:02 -05:00
|
|
|
child.exec(cmd + "'[]'", common.mustCall(
|
2011-12-01 17:21:00 +01:00
|
|
|
function(err, stdout, stderr) {
|
2016-12-01 10:18:15 -06:00
|
|
|
assert.strictEqual(stdout, '[]\n');
|
|
|
|
assert.strictEqual(stderr, '');
|
2016-05-23 16:14:02 -05:00
|
|
|
}));
|
2011-12-01 17:21:00 +01:00
|
|
|
});
|
|
|
|
|
2010-11-16 15:44:06 +01:00
|
|
|
// assert that module loading works
|
2011-09-11 03:52:44 +02:00
|
|
|
child.exec(nodejs + ' --eval "require(\'' + filename + '\')"',
|
2016-12-31 21:39:57 -08:00
|
|
|
function(status, stdout, stderr) {
|
|
|
|
assert.strictEqual(status.code, 42);
|
|
|
|
});
|
2010-11-16 15:44:06 +01:00
|
|
|
|
2016-04-15 02:03:12 +02:00
|
|
|
// Check that builtin modules are pre-defined.
|
|
|
|
child.exec(nodejs + ' --print "os.platform()"',
|
2016-12-31 21:39:57 -08:00
|
|
|
function(status, stdout, stderr) {
|
|
|
|
assert.strictEqual(stderr, '');
|
|
|
|
assert.strictEqual(stdout.trim(), require('os').platform());
|
|
|
|
});
|
2016-04-15 02:03:12 +02:00
|
|
|
|
2010-11-16 15:44:06 +01:00
|
|
|
// module path resolve bug, regression test
|
2014-12-17 20:30:04 +07:00
|
|
|
child.exec(nodejs + ' --eval "require(\'./test/parallel/test-cli-eval.js\')"',
|
2016-12-31 21:39:57 -08:00
|
|
|
function(status, stdout, stderr) {
|
|
|
|
assert.strictEqual(status.code, 42);
|
|
|
|
});
|
2011-05-29 02:04:36 +09:00
|
|
|
|
2016-05-23 16:14:02 -05:00
|
|
|
// Missing argument should not crash
|
|
|
|
child.exec(nodejs + ' -e', common.mustCall(function(status, stdout, stderr) {
|
|
|
|
assert.notStrictEqual(status, null);
|
|
|
|
assert.strictEqual(status.code, 9);
|
|
|
|
}));
|
|
|
|
|
2011-05-29 02:04:36 +09:00
|
|
|
// empty program should do nothing
|
|
|
|
child.exec(nodejs + ' -e ""', function(status, stdout, stderr) {
|
2016-12-01 10:18:15 -06:00
|
|
|
assert.strictEqual(stdout, '');
|
|
|
|
assert.strictEqual(stderr, '');
|
2011-05-29 02:04:36 +09:00
|
|
|
});
|
2012-09-04 14:19:59 +02:00
|
|
|
|
|
|
|
// "\\-42" should be interpreted as an escaped expression, not a switch
|
|
|
|
child.exec(nodejs + ' -p "\\-42"',
|
2016-12-31 21:39:57 -08:00
|
|
|
function(err, stdout, stderr) {
|
|
|
|
assert.strictEqual(stdout, '-42\n');
|
|
|
|
assert.strictEqual(stderr, '');
|
|
|
|
});
|
2013-09-02 16:42:01 +02:00
|
|
|
|
|
|
|
child.exec(nodejs + ' --use-strict -p process.execArgv',
|
2016-12-31 21:39:57 -08:00
|
|
|
function(status, stdout, stderr) {
|
|
|
|
assert.strictEqual(
|
|
|
|
stdout, "[ '--use-strict', '-p', 'process.execArgv' ]\n"
|
|
|
|
);
|
|
|
|
});
|
2015-10-28 23:06:40 +01:00
|
|
|
|
|
|
|
// Regression test for https://github.com/nodejs/node/issues/3574
|
|
|
|
const emptyFile = path.join(common.fixturesDir, 'empty.js');
|
|
|
|
child.exec(nodejs + ` -e 'require("child_process").fork("${emptyFile}")'`,
|
2016-12-31 21:39:57 -08:00
|
|
|
function(status, stdout, stderr) {
|
|
|
|
assert.strictEqual(stdout, '');
|
|
|
|
assert.strictEqual(stderr, '');
|
|
|
|
});
|
2016-09-28 12:01:48 +02:00
|
|
|
|
|
|
|
// Regression test for https://github.com/nodejs/node/issues/8534.
|
|
|
|
{
|
|
|
|
const script = `
|
|
|
|
// console.log() can revive the event loop so we must be careful
|
|
|
|
// to write from a 'beforeExit' event listener only once.
|
|
|
|
process.once("beforeExit", () => console.log("beforeExit"));
|
|
|
|
process.on("exit", () => console.log("exit"));
|
|
|
|
console.log("start");
|
|
|
|
`;
|
|
|
|
const options = { encoding: 'utf8' };
|
|
|
|
const proc = child.spawnSync(process.execPath, ['-e', script], options);
|
|
|
|
assert.strictEqual(proc.stderr, '');
|
|
|
|
assert.strictEqual(proc.stdout, 'start\nbeforeExit\nexit\n');
|
|
|
|
}
|