Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions test/parallel/test-cli-eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ const filename = __filename.replace(/\\/g, '/');
// assert that nothing is written to stdout
child.exec(nodejs + ' --eval 42',
function(err, stdout, stderr) {
assert.strictEqual(err, null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These will work and are totally fine, but it's probably more idiomatic to do this:

assert.ifError(err);

assert.strictEqual(stdout, '');
assert.strictEqual(stderr, '');
});

// assert that "42\n" is written to stderr
child.exec(nodejs + ' --eval "console.error(42)"',
function(err, stdout, stderr) {
assert.strictEqual(err, null);
assert.strictEqual(stdout, '');
assert.strictEqual(stderr, '42\n');
});
Expand All @@ -36,12 +38,14 @@ child.exec(nodejs + ' --eval "console.error(42)"',

child.exec(cmd + '42',
function(err, stdout, stderr) {
assert.strictEqual(typeof err, 'object');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is OK, but is probably better like this:

assert(err instanceof Error);

Even better if the type of error is known. For example, if it's expected to be a RangeError, then assert.(err instanceof RangeError) etc.

assert.strictEqual(stdout, '42\n');
assert.strictEqual(stderr, '');
});

child.exec(cmd + "'[]'", common.mustCall(
function(err, stdout, stderr) {
assert.strictEqual(typeof err, 'object');
assert.strictEqual(stdout, '[]\n');
assert.strictEqual(stderr, '');
}));
Expand All @@ -51,11 +55,15 @@ child.exec(nodejs + ' --eval "console.error(42)"',
child.exec(nodejs + ' --eval "require(\'' + filename + '\')"',
function(status, stdout, stderr) {
assert.strictEqual(status.code, 42);
assert.strictEqual(
stdout, 'Loaded as a module, exiting with status code 42.\n');
assert.strictEqual(stderr, '');
});

// Check that builtin modules are pre-defined.
child.exec(nodejs + ' --print "os.platform()"',
function(status, stdout, stderr) {
assert.strictEqual(status, null);
assert.strictEqual(stderr, '');
assert.strictEqual(stdout.trim(), require('os').platform());
});
Expand All @@ -64,38 +72,48 @@ child.exec(nodejs + ' --print "os.platform()"',
child.exec(nodejs + ' --eval "require(\'./test/parallel/test-cli-eval.js\')"',
function(status, stdout, stderr) {
assert.strictEqual(status.code, 42);
assert.strictEqual(
stdout, 'Loaded as a module, exiting with status code 42.\n');
assert.strictEqual(stderr, '');
});

// Missing argument should not crash
child.exec(nodejs + ' -e', common.mustCall(function(status, stdout, stderr) {
assert.notStrictEqual(status, null);
assert.strictEqual(status.code, 9);
assert.strictEqual(stdout, '');
assert(stderr.match(/node: -e requires an argument\n/));
}));

// empty program should do nothing
child.exec(nodejs + ' -e ""', function(status, stdout, stderr) {
assert.strictEqual(status, null);
assert.strictEqual(stdout, '');
assert.strictEqual(stderr, '');
});

// "\\-42" should be interpreted as an escaped expression, not a switch
child.exec(nodejs + ' -p "\\-42"',
function(err, stdout, stderr) {
assert.strictEqual(err, null);
assert.strictEqual(stdout, '-42\n');
assert.strictEqual(stderr, '');
});

child.exec(nodejs + ' --use-strict -p process.execArgv',
function(status, stdout, stderr) {
assert.strictEqual(status, null);
assert.strictEqual(
stdout, "[ '--use-strict', '-p', 'process.execArgv' ]\n"
);
assert.strictEqual(stderr, '');
});

// 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}")'`,
function(status, stdout, stderr) {
assert.strictEqual(status, null);
assert.strictEqual(stdout, '');
assert.strictEqual(stderr, '');
});
Expand Down