repl: fix autocomplete while using .load
This makes sure that complete functions work as expected after using the REPL's `.load` command. It also fixes the corresponding test. So far the assertion where swallowed and the test passed even though it should not have. Fixes: https://github.com/nodejs/node/issues/28546 PR-URL: https://github.com/nodejs/node/pull/28608 Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Lance Ball <lball@redhat.com> Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
This commit is contained in:
parent
f9388580bd
commit
6874aa1fb1
23
lib/repl.js
23
lib/repl.js
@ -1535,10 +1535,10 @@ function defineDefaultCommands(repl) {
|
||||
help: 'Save all evaluated commands in this REPL session to a file',
|
||||
action: function(file) {
|
||||
try {
|
||||
fs.writeFileSync(file, this.lines.join('\n') + '\n');
|
||||
this.outputStream.write('Session saved to: ' + file + '\n');
|
||||
fs.writeFileSync(file, this.lines.join('\n'));
|
||||
this.outputStream.write(`Session saved to: ${file}\n`);
|
||||
} catch {
|
||||
this.outputStream.write('Failed to save: ' + file + '\n');
|
||||
this.outputStream.write(`Failed to save: ${file}\n`);
|
||||
}
|
||||
this.displayPrompt();
|
||||
}
|
||||
@ -1548,23 +1548,20 @@ function defineDefaultCommands(repl) {
|
||||
help: 'Load JS from a file into the REPL session',
|
||||
action: function(file) {
|
||||
try {
|
||||
var stats = fs.statSync(file);
|
||||
const stats = fs.statSync(file);
|
||||
if (stats && stats.isFile()) {
|
||||
_turnOnEditorMode(this);
|
||||
var data = fs.readFileSync(file, 'utf8');
|
||||
var lines = data.split('\n');
|
||||
for (var n = 0; n < lines.length; n++) {
|
||||
if (lines[n])
|
||||
this.write(`${lines[n]}\n`);
|
||||
}
|
||||
const data = fs.readFileSync(file, 'utf8');
|
||||
this.write(data);
|
||||
_turnOffEditorMode(this);
|
||||
this.write('\n');
|
||||
} else {
|
||||
this.outputStream.write('Failed to load: ' + file +
|
||||
' is not a valid file\n');
|
||||
this.outputStream.write(
|
||||
`Failed to load: ${file} is not a valid file\n`
|
||||
);
|
||||
}
|
||||
} catch {
|
||||
this.outputStream.write('Failed to load: ' + file + '\n');
|
||||
this.outputStream.write(`Failed to load: ${file}\n`);
|
||||
}
|
||||
this.displayPrompt();
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ const expected = `${command}
|
||||
const getLunch = () =>
|
||||
placeOrder('tacos')
|
||||
.then(eat);
|
||||
|
||||
const placeOrder = (order) => Promise.resolve(order);
|
||||
const eat = (food) => '<nom nom nom>';
|
||||
|
||||
|
@ -20,7 +20,7 @@
|
||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
'use strict';
|
||||
require('../common');
|
||||
const common = require('../common');
|
||||
const ArrayStream = require('../common/arraystream');
|
||||
const assert = require('assert');
|
||||
const join = require('path').join;
|
||||
@ -36,6 +36,12 @@ const works = [['inner.one'], 'inner.o'];
|
||||
const putIn = new ArrayStream();
|
||||
const testMe = repl.start('', putIn);
|
||||
|
||||
// Some errors might be passed to the domain.
|
||||
testMe._domain.on('error', function(reason) {
|
||||
const err = new Error('Test failed');
|
||||
err.reason = reason;
|
||||
throw err;
|
||||
});
|
||||
|
||||
const testFile = [
|
||||
'var top = function() {',
|
||||
@ -43,18 +49,76 @@ const testFile = [
|
||||
];
|
||||
const saveFileName = join(tmpdir.path, 'test.save.js');
|
||||
|
||||
// input some data
|
||||
// Add some data.
|
||||
putIn.run(testFile);
|
||||
|
||||
// save it to a file
|
||||
// Save it to a file.
|
||||
putIn.run([`.save ${saveFileName}`]);
|
||||
|
||||
// The file should have what I wrote
|
||||
// The file should have what I wrote.
|
||||
assert.strictEqual(fs.readFileSync(saveFileName, 'utf8'),
|
||||
`${testFile.join('\n')}\n`);
|
||||
testFile.join('\n'));
|
||||
|
||||
// Make sure that the REPL data is "correct".
|
||||
testMe.complete('inner.o', common.mustCall(function(error, data) {
|
||||
assert.ifError(error);
|
||||
assert.deepStrictEqual(data, works);
|
||||
}));
|
||||
|
||||
// Clear the REPL.
|
||||
putIn.run(['.clear']);
|
||||
|
||||
// Load the file back in.
|
||||
putIn.run([`.load ${saveFileName}`]);
|
||||
|
||||
// Make sure that the REPL data is "correct".
|
||||
testMe.complete('inner.o', common.mustCall(function(error, data) {
|
||||
assert.ifError(error);
|
||||
assert.deepStrictEqual(data, works);
|
||||
}));
|
||||
|
||||
// Clear the REPL.
|
||||
putIn.run(['.clear']);
|
||||
|
||||
let loadFile = join(tmpdir.path, 'file.does.not.exist');
|
||||
|
||||
// Should not break.
|
||||
putIn.write = common.mustCall(function(data) {
|
||||
// Make sure I get a failed to load message and not some crazy error.
|
||||
assert.strictEqual(data, `Failed to load: ${loadFile}\n`);
|
||||
// Eat me to avoid work.
|
||||
putIn.write = () => {};
|
||||
});
|
||||
putIn.run([`.load ${loadFile}`]);
|
||||
|
||||
// Throw error on loading directory.
|
||||
loadFile = tmpdir.path;
|
||||
putIn.write = common.mustCall(function(data) {
|
||||
assert.strictEqual(data, `Failed to load: ${loadFile} is not a valid file\n`);
|
||||
putIn.write = () => {};
|
||||
});
|
||||
putIn.run([`.load ${loadFile}`]);
|
||||
|
||||
// Clear the REPL.
|
||||
putIn.run(['.clear']);
|
||||
|
||||
// NUL (\0) is disallowed in filenames in UNIX-like operating systems and
|
||||
// Windows so we can use that to test failed saves.
|
||||
const invalidFileName = join(tmpdir.path, '\0\0\0\0\0');
|
||||
|
||||
// Should not break.
|
||||
putIn.write = common.mustCall(function(data) {
|
||||
// Make sure I get a failed to save message and not some other error.
|
||||
assert.strictEqual(data, `Failed to save: ${invalidFileName}\n`);
|
||||
// Reset to no-op.
|
||||
putIn.write = () => {};
|
||||
});
|
||||
|
||||
// Save it to a file.
|
||||
putIn.run([`.save ${invalidFileName}`]);
|
||||
|
||||
{
|
||||
// save .editor mode code
|
||||
// Save .editor mode code.
|
||||
const cmds = [
|
||||
'function testSave() {',
|
||||
'return "saved";',
|
||||
@ -70,62 +134,5 @@ assert.strictEqual(fs.readFileSync(saveFileName, 'utf8'),
|
||||
putIn.run([`.save ${saveFileName}`]);
|
||||
replServer.close();
|
||||
assert.strictEqual(fs.readFileSync(saveFileName, 'utf8'),
|
||||
`${cmds.join('\n')}\n\n`);
|
||||
`${cmds.join('\n')}\n`);
|
||||
}
|
||||
|
||||
// Make sure that the REPL data is "correct"
|
||||
// so when I load it back I know I'm good
|
||||
testMe.complete('inner.o', function(error, data) {
|
||||
assert.deepStrictEqual(data, works);
|
||||
});
|
||||
|
||||
// clear the REPL
|
||||
putIn.run(['.clear']);
|
||||
|
||||
// Load the file back in
|
||||
putIn.run([`.load ${saveFileName}`]);
|
||||
|
||||
// Make sure that the REPL data is "correct"
|
||||
testMe.complete('inner.o', function(error, data) {
|
||||
assert.deepStrictEqual(data, works);
|
||||
});
|
||||
|
||||
// clear the REPL
|
||||
putIn.run(['.clear']);
|
||||
|
||||
let loadFile = join(tmpdir.path, 'file.does.not.exist');
|
||||
|
||||
// should not break
|
||||
putIn.write = function(data) {
|
||||
// Make sure I get a failed to load message and not some crazy error
|
||||
assert.strictEqual(data, `Failed to load:${loadFile}\n`);
|
||||
// Eat me to avoid work
|
||||
putIn.write = () => {};
|
||||
};
|
||||
putIn.run([`.load ${loadFile}`]);
|
||||
|
||||
// Throw error on loading directory
|
||||
loadFile = tmpdir.path;
|
||||
putIn.write = function(data) {
|
||||
assert.strictEqual(data, `Failed to load:${loadFile} is not a valid file\n`);
|
||||
putIn.write = () => {};
|
||||
};
|
||||
putIn.run([`.load ${loadFile}`]);
|
||||
|
||||
// clear the REPL
|
||||
putIn.run(['.clear']);
|
||||
|
||||
// NUL (\0) is disallowed in filenames in UNIX-like operating systems and
|
||||
// Windows so we can use that to test failed saves
|
||||
const invalidFileName = join(tmpdir.path, '\0\0\0\0\0');
|
||||
|
||||
// should not break
|
||||
putIn.write = function(data) {
|
||||
// Make sure I get a failed to save message and not some other error
|
||||
assert.strictEqual(data, `Failed to save:${invalidFileName}\n`);
|
||||
// reset to no-op
|
||||
putIn.write = () => {};
|
||||
};
|
||||
|
||||
// save it to a file
|
||||
putIn.run([`.save ${invalidFileName}`]);
|
||||
|
Loading…
x
Reference in New Issue
Block a user