From 4f89ad799b864422d37fc4c9f281cf12312df7dc Mon Sep 17 00:00:00 2001 From: Mauran Date: Tue, 26 Mar 2024 22:48:45 +0100 Subject: [PATCH 1/2] lib: .load .save add proper error message when no file passed This commit adds a proper error message using ERR_MISSING_ARGS('file') when a .save or .load REPL command is runned. This commit also adds test for both of this cases. Fixes: https://github.com/nodejs/node/issues/52218 Signed-off-by: Thomas Mauran --- lib/repl.js | 23 +++++++++++++++++++---- test/parallel/test-repl-save-load.js | 25 ++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index d16f8882211a42..a06ca0dd990f29 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -147,6 +147,7 @@ const { ERR_CANNOT_WATCH_SIGINT, ERR_INVALID_REPL_EVAL_CONFIG, ERR_INVALID_REPL_INPUT, + ERR_MISSING_ARGS, ERR_SCRIPT_EXECUTION_INTERRUPTED, }, isErrorStackTraceLimitWritable, @@ -1788,10 +1789,17 @@ function defineDefaultCommands(repl) { help: 'Save all evaluated commands in this REPL session to a file', action: function(file) { try { + if (file === '') { + throw new ERR_MISSING_ARGS('file'); + } fs.writeFileSync(file, ArrayPrototypeJoin(this.lines, '\n')); this.output.write(`Session saved to: ${file}\n`); - } catch { - this.output.write(`Failed to save: ${file}\n`); + } catch (error) { + if (error instanceof ERR_MISSING_ARGS) { + this.output.write(`${error.message}\n`); + } else { + this.output.write(`Failed to save: ${file}\n`); + } } this.displayPrompt(); }, @@ -1801,6 +1809,9 @@ function defineDefaultCommands(repl) { help: 'Load JS from a file into the REPL session', action: function(file) { try { + if (file === '') { + throw new ERR_MISSING_ARGS('file'); + } const stats = fs.statSync(file); if (stats && stats.isFile()) { _turnOnEditorMode(this); @@ -1815,8 +1826,12 @@ function defineDefaultCommands(repl) { `Failed to load: ${file} is not a valid file\n`, ); } - } catch { - this.output.write(`Failed to load: ${file}\n`); + } catch (error) { + if (error instanceof ERR_MISSING_ARGS) { + this.output.write(`${error.message}\n`); + } else { + this.output.write(`Failed to load: ${file}\n`); + } } this.displayPrompt(); }, diff --git a/test/parallel/test-repl-save-load.js b/test/parallel/test-repl-save-load.js index 365bad8260bfc6..34b6c2901450ef 100644 --- a/test/parallel/test-repl-save-load.js +++ b/test/parallel/test-repl-save-load.js @@ -24,7 +24,6 @@ const common = require('../common'); const ArrayStream = require('../common/arraystream'); const assert = require('assert'); const fs = require('fs'); - const tmpdir = require('../common/tmpdir'); tmpdir.refresh(); @@ -139,3 +138,27 @@ putIn.run([`.save ${invalidFileName}`]); assert.strictEqual(fs.readFileSync(saveFileName, 'utf8'), `${cmds.join('\n')}\n`); } + +// Check if the file is present when using save + +// Clear the REPL. +putIn.run(['.clear']); + +// Error message when using save without a file +putIn.write = common.mustCall(function(data) { + assert.strictEqual(data, 'The "file" argument must be specified\n'); + putIn.write = () => {}; +}); +putIn.run(['.save']); + +// Check if the file is present when using load + +// Clear the REPL. +putIn.run(['.clear']); + +// Error message when using load without a file +putIn.write = common.mustCall(function(data) { + assert.strictEqual(data, 'The "file" argument must be specified\n'); + putIn.write = () => {}; +}); +putIn.run(['.load']); From 93ed866544e8776577bff915593c4397af1ad1d3 Mon Sep 17 00:00:00 2001 From: Thomas Mauran Date: Sat, 30 Mar 2024 11:34:23 +0100 Subject: [PATCH 2/2] test: lint Signed-off-by: Thomas Mauran --- test/parallel/test-repl-save-load.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-repl-save-load.js b/test/parallel/test-repl-save-load.js index 34b6c2901450ef..bb5130d1d71bbf 100644 --- a/test/parallel/test-repl-save-load.js +++ b/test/parallel/test-repl-save-load.js @@ -144,7 +144,7 @@ putIn.run([`.save ${invalidFileName}`]); // Clear the REPL. putIn.run(['.clear']); -// Error message when using save without a file +// Error message when using save without a file putIn.write = common.mustCall(function(data) { assert.strictEqual(data, 'The "file" argument must be specified\n'); putIn.write = () => {}; @@ -156,7 +156,7 @@ putIn.run(['.save']); // Clear the REPL. putIn.run(['.clear']); -// Error message when using load without a file +// Error message when using load without a file putIn.write = common.mustCall(function(data) { assert.strictEqual(data, 'The "file" argument must be specified\n'); putIn.write = () => {};