Skip to content

Commit 3f5ff8d

Browse files
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: #52218 Signed-off-by: Thomas Mauran <thomas.mauran@etu.umontpellier.fr> PR-URL: #52225 Reviewed-By: Kohei Ueno <kohei.ueno119@gmail.com>
1 parent 67b9dda commit 3f5ff8d

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

lib/repl.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ const {
147147
ERR_CANNOT_WATCH_SIGINT,
148148
ERR_INVALID_REPL_EVAL_CONFIG,
149149
ERR_INVALID_REPL_INPUT,
150+
ERR_MISSING_ARGS,
150151
ERR_SCRIPT_EXECUTION_INTERRUPTED,
151152
},
152153
isErrorStackTraceLimitWritable,
@@ -1788,10 +1789,17 @@ function defineDefaultCommands(repl) {
17881789
help: 'Save all evaluated commands in this REPL session to a file',
17891790
action: function(file) {
17901791
try {
1792+
if (file === '') {
1793+
throw new ERR_MISSING_ARGS('file');
1794+
}
17911795
fs.writeFileSync(file, ArrayPrototypeJoin(this.lines, '\n'));
17921796
this.output.write(`Session saved to: ${file}\n`);
1793-
} catch {
1794-
this.output.write(`Failed to save: ${file}\n`);
1797+
} catch (error) {
1798+
if (error instanceof ERR_MISSING_ARGS) {
1799+
this.output.write(`${error.message}\n`);
1800+
} else {
1801+
this.output.write(`Failed to save: ${file}\n`);
1802+
}
17951803
}
17961804
this.displayPrompt();
17971805
},
@@ -1801,6 +1809,9 @@ function defineDefaultCommands(repl) {
18011809
help: 'Load JS from a file into the REPL session',
18021810
action: function(file) {
18031811
try {
1812+
if (file === '') {
1813+
throw new ERR_MISSING_ARGS('file');
1814+
}
18041815
const stats = fs.statSync(file);
18051816
if (stats && stats.isFile()) {
18061817
_turnOnEditorMode(this);
@@ -1815,8 +1826,12 @@ function defineDefaultCommands(repl) {
18151826
`Failed to load: ${file} is not a valid file\n`,
18161827
);
18171828
}
1818-
} catch {
1819-
this.output.write(`Failed to load: ${file}\n`);
1829+
} catch (error) {
1830+
if (error instanceof ERR_MISSING_ARGS) {
1831+
this.output.write(`${error.message}\n`);
1832+
} else {
1833+
this.output.write(`Failed to load: ${file}\n`);
1834+
}
18201835
}
18211836
this.displayPrompt();
18221837
},

test/parallel/test-repl-save-load.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const common = require('../common');
2424
const ArrayStream = require('../common/arraystream');
2525
const assert = require('assert');
2626
const fs = require('fs');
27-
2827
const tmpdir = require('../common/tmpdir');
2928
tmpdir.refresh();
3029

@@ -139,3 +138,27 @@ putIn.run([`.save ${invalidFileName}`]);
139138
assert.strictEqual(fs.readFileSync(saveFileName, 'utf8'),
140139
`${cmds.join('\n')}\n`);
141140
}
141+
142+
// Check if the file is present when using save
143+
144+
// Clear the REPL.
145+
putIn.run(['.clear']);
146+
147+
// Error message when using save without a file
148+
putIn.write = common.mustCall(function(data) {
149+
assert.strictEqual(data, 'The "file" argument must be specified\n');
150+
putIn.write = () => {};
151+
});
152+
putIn.run(['.save']);
153+
154+
// Check if the file is present when using load
155+
156+
// Clear the REPL.
157+
putIn.run(['.clear']);
158+
159+
// Error message when using load without a file
160+
putIn.write = common.mustCall(function(data) {
161+
assert.strictEqual(data, 'The "file" argument must be specified\n');
162+
putIn.write = () => {};
163+
});
164+
putIn.run(['.load']);

0 commit comments

Comments
 (0)