Skip to content

Commit

Permalink
lib: .load .save add proper error message when no file passed
Browse files Browse the repository at this point in the history
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: #52218

Signed-off-by: Thomas Mauran <[email protected]>
PR-URL: #52225
Reviewed-By: Kohei Ueno <[email protected]>
  • Loading branch information
thomas-mauran authored and marco-ippolito committed May 3, 2024
1 parent e3e0c68 commit ee11b53
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
23 changes: 19 additions & 4 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
},
Expand All @@ -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);
Expand All @@ -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();
},
Expand Down
25 changes: 24 additions & 1 deletion test/parallel/test-repl-save-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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']);

0 comments on commit ee11b53

Please sign in to comment.