From c59e4c9c9954d8968f8546b26e49062b1af1af57 Mon Sep 17 00:00:00 2001 From: gengjiawen Date: Wed, 19 Dec 2018 22:51:30 +0800 Subject: [PATCH] repl: add welcome message --- lib/internal/main/repl.js | 3 +++ test/parallel/test-force-repl-with-eval.js | 2 +- test/parallel/test-force-repl.js | 15 +++++++++++---- test/parallel/test-preload.js | 9 +++++---- test/parallel/test-repl-harmony.js | 10 +++++----- test/parallel/test-repl-inspect-defaults.js | 2 +- test/parallel/test-repl-require-after-write.js | 2 +- test/parallel/test-repl-require-context.js | 2 +- .../test-repl-unexpected-token-recoverable.js | 10 +++++----- 9 files changed, 33 insertions(+), 22 deletions(-) diff --git a/lib/internal/main/repl.js b/lib/internal/main/repl.js index c2bf54f8bb6981..a6c807945e5c58 100644 --- a/lib/internal/main/repl.js +++ b/lib/internal/main/repl.js @@ -19,6 +19,9 @@ if (require('internal/options').getOptionValue('--entry-type')) { process.exit(1); } +console.log(`Welcome to Node.js ${process.version}.\n` + + 'Type ".help" for more information.'); + const cliRepl = require('internal/repl'); cliRepl.createInternalRepl(process.env, (err, repl) => { if (err) { diff --git a/test/parallel/test-force-repl-with-eval.js b/test/parallel/test-force-repl-with-eval.js index 3f81d362d449b7..8b5b6db1c80e73 100644 --- a/test/parallel/test-force-repl-with-eval.js +++ b/test/parallel/test-force-repl-with-eval.js @@ -12,7 +12,7 @@ cp.stdout.setEncoding('utf8'); let output = ''; cp.stdout.on('data', function(b) { output += b; - if (output === '> 42\n') { + if (output.endsWith('> 42\n')) { gotToEnd = true; cp.kill(); } diff --git a/test/parallel/test-force-repl.js b/test/parallel/test-force-repl.js index 15475a7e30ed8d..702db24211e356 100644 --- a/test/parallel/test-force-repl.js +++ b/test/parallel/test-force-repl.js @@ -4,12 +4,19 @@ const assert = require('assert'); const spawn = require('child_process').spawn; // Spawn a node child process in interactive mode (enabling the REPL) and -// confirm the '> ' prompt is included in the output. +// confirm the '> ' prompt and welcome message is included in the output. const cp = spawn(process.execPath, ['-i']); cp.stdout.setEncoding('utf8'); -cp.stdout.once('data', common.mustCall(function(b) { - assert.strictEqual(b, '> '); - cp.kill(); +let out = ''; +cp.stdout.on('data', (d) => { + out += d; +}); + +cp.stdout.on('end', common.mustCall(() => { + assert.strictEqual(out, `Welcome to Node.js ${process.version}.\n` + + 'Type ".help" for more information.\n> '); })); + +cp.stdin.end(''); diff --git a/test/parallel/test-preload.js b/test/parallel/test-preload.js index 326eed030ca1bb..0fbe28faf3f799 100644 --- a/test/parallel/test-preload.js +++ b/test/parallel/test-preload.js @@ -86,7 +86,7 @@ const replProc = childProcess.spawn( ); replProc.stdin.end('.exit\n'); let replStdout = ''; -replProc.stdout.on('data', function(d) { +replProc.stdout.on('data', (d) => { replStdout += d; }); replProc.on('close', function(code) { @@ -94,8 +94,9 @@ replProc.on('close', function(code) { const output = [ 'A', '> ' - ].join('\n'); - assert.strictEqual(replStdout, output); + ]; + assert.ok(replStdout.startsWith(output[0])); + assert.ok(replStdout.endsWith(output[1])); }); // Test that preload placement at other points in the cmdline @@ -114,7 +115,7 @@ const interactive = childProcess.exec( `"${nodeBinary}" ${preloadOption([fixtureD])}-i`, common.mustCall(function(err, stdout, stderr) { assert.ifError(err); - assert.strictEqual(stdout, "> 'test'\n> "); + assert.ok(stdout.endsWith("> 'test'\n> ")); }) ); diff --git a/test/parallel/test-repl-harmony.js b/test/parallel/test-repl-harmony.js index 6686e63fe028ed..0aa8e56bdd6e2e 100644 --- a/test/parallel/test-repl-harmony.js +++ b/test/parallel/test-repl-harmony.js @@ -30,19 +30,19 @@ const child = spawn(process.execPath, args); const input = '(function(){"use strict"; const y=1;y=2})()\n'; // This message will vary based on JavaScript engine, so don't check the message // contents beyond confirming that the `Error` is a `TypeError`. -const expectOut = /^> Thrown:\nTypeError: /; +const expectOut = /> Thrown:\nTypeError: /; child.stderr.setEncoding('utf8'); -child.stderr.on('data', function(c) { +child.stderr.on('data', (d) => { throw new Error('child.stderr be silent'); }); child.stdout.setEncoding('utf8'); let out = ''; -child.stdout.on('data', function(c) { - out += c; +child.stdout.on('data', (d) => { + out += d; }); -child.stdout.on('end', function() { +child.stdout.on('end', () => { assert(expectOut.test(out)); console.log('ok'); }); diff --git a/test/parallel/test-repl-inspect-defaults.js b/test/parallel/test-repl-inspect-defaults.js index a73bc792fea8ab..9a033ce1f3dc13 100644 --- a/test/parallel/test-repl-inspect-defaults.js +++ b/test/parallel/test-repl-inspect-defaults.js @@ -11,7 +11,7 @@ child.stdout.on('data', (data) => { }); child.on('exit', common.mustCall(() => { - const results = output.replace(/^> /mg, '').split('\n'); + const results = output.replace(/^> /mg, '').split('\n').slice(2); assert.deepStrictEqual( results, [ diff --git a/test/parallel/test-repl-require-after-write.js b/test/parallel/test-repl-require-after-write.js index ed0a7076c183a0..83c464473589e0 100644 --- a/test/parallel/test-repl-require-after-write.js +++ b/test/parallel/test-repl-require-after-write.js @@ -25,7 +25,7 @@ child.stdout.on('data', (c) => { out += c; }); child.stdout.on('end', common.mustCall(() => { - assert.strictEqual(out, '> 1\n> '); + assert.ok(out.endsWith('> 1\n> ')); })); child.stdin.end(input); diff --git a/test/parallel/test-repl-require-context.js b/test/parallel/test-repl-require-context.js index 84896783211ab4..750235818b8bfc 100644 --- a/test/parallel/test-repl-require-context.js +++ b/test/parallel/test-repl-require-context.js @@ -13,7 +13,7 @@ child.stdout.on('data', (data) => { }); child.on('exit', common.mustCall(() => { - const results = output.replace(/^> /mg, '').split('\n'); + const results = output.replace(/^> /mg, '').split('\n').slice(2); assert.deepStrictEqual(results, ['undefined', 'true', 'true', '']); })); diff --git a/test/parallel/test-repl-unexpected-token-recoverable.js b/test/parallel/test-repl-unexpected-token-recoverable.js index c4b3a08e969000..888ee81bd4e62a 100644 --- a/test/parallel/test-repl-unexpected-token-recoverable.js +++ b/test/parallel/test-repl-unexpected-token-recoverable.js @@ -12,20 +12,20 @@ const child = spawn(process.execPath, args); const input = 'var foo = "bar\\\nbaz"'; // Match '...' as well since it marks a multi-line statement -const expectOut = /^> \.\.\. undefined\n/; +const expectOut = /> \.\.\. undefined\n/; child.stderr.setEncoding('utf8'); -child.stderr.on('data', function(c) { +child.stderr.on('data', (d) => { throw new Error('child.stderr be silent'); }); child.stdout.setEncoding('utf8'); let out = ''; -child.stdout.on('data', function(c) { - out += c; +child.stdout.on('data', (d) => { + out += d; }); -child.stdout.on('end', function() { +child.stdout.on('end', () => { assert(expectOut.test(out)); console.log('ok'); });