Skip to content

Commit cbd586a

Browse files
BridgeARtargos
authored andcommitted
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: #28546 PR-URL: #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>
1 parent 506b50a commit cbd586a

File tree

3 files changed

+72
-67
lines changed

3 files changed

+72
-67
lines changed

lib/repl.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,10 +1535,10 @@ function defineDefaultCommands(repl) {
15351535
help: 'Save all evaluated commands in this REPL session to a file',
15361536
action: function(file) {
15371537
try {
1538-
fs.writeFileSync(file, this.lines.join('\n') + '\n');
1539-
this.outputStream.write('Session saved to: ' + file + '\n');
1538+
fs.writeFileSync(file, this.lines.join('\n'));
1539+
this.outputStream.write(`Session saved to: ${file}\n`);
15401540
} catch {
1541-
this.outputStream.write('Failed to save: ' + file + '\n');
1541+
this.outputStream.write(`Failed to save: ${file}\n`);
15421542
}
15431543
this.displayPrompt();
15441544
}
@@ -1548,23 +1548,20 @@ function defineDefaultCommands(repl) {
15481548
help: 'Load JS from a file into the REPL session',
15491549
action: function(file) {
15501550
try {
1551-
var stats = fs.statSync(file);
1551+
const stats = fs.statSync(file);
15521552
if (stats && stats.isFile()) {
15531553
_turnOnEditorMode(this);
1554-
var data = fs.readFileSync(file, 'utf8');
1555-
var lines = data.split('\n');
1556-
for (var n = 0; n < lines.length; n++) {
1557-
if (lines[n])
1558-
this.write(`${lines[n]}\n`);
1559-
}
1554+
const data = fs.readFileSync(file, 'utf8');
1555+
this.write(data);
15601556
_turnOffEditorMode(this);
15611557
this.write('\n');
15621558
} else {
1563-
this.outputStream.write('Failed to load: ' + file +
1564-
' is not a valid file\n');
1559+
this.outputStream.write(
1560+
`Failed to load: ${file} is not a valid file\n`
1561+
);
15651562
}
15661563
} catch {
1567-
this.outputStream.write('Failed to load: ' + file + '\n');
1564+
this.outputStream.write(`Failed to load: ${file}\n`);
15681565
}
15691566
this.displayPrompt();
15701567
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const expected = `${command}
1313
const getLunch = () =>
1414
placeOrder('tacos')
1515
.then(eat);
16+
1617
const placeOrder = (order) => Promise.resolve(order);
1718
const eat = (food) => '<nom nom nom>';
1819

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

Lines changed: 61 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
23+
const common = require('../common');
2424
const ArrayStream = require('../common/arraystream');
2525
const assert = require('assert');
2626
const join = require('path').join;
@@ -36,96 +36,103 @@ const works = [['inner.one'], 'inner.o'];
3636
const putIn = new ArrayStream();
3737
const testMe = repl.start('', putIn);
3838

39+
// Some errors might be passed to the domain.
40+
testMe._domain.on('error', function(reason) {
41+
const err = new Error('Test failed');
42+
err.reason = reason;
43+
throw err;
44+
});
3945

4046
const testFile = [
4147
'var top = function() {',
4248
'var inner = {one:1};'
4349
];
4450
const saveFileName = join(tmpdir.path, 'test.save.js');
4551

46-
// input some data
52+
// Add some data.
4753
putIn.run(testFile);
4854

49-
// save it to a file
55+
// Save it to a file.
5056
putIn.run([`.save ${saveFileName}`]);
5157

52-
// The file should have what I wrote
58+
// The file should have what I wrote.
5359
assert.strictEqual(fs.readFileSync(saveFileName, 'utf8'),
54-
`${testFile.join('\n')}\n`);
60+
testFile.join('\n'));
5561

56-
{
57-
// save .editor mode code
58-
const cmds = [
59-
'function testSave() {',
60-
'return "saved";',
61-
'}'
62-
];
63-
const putIn = new ArrayStream();
64-
const replServer = repl.start({ terminal: true, stream: putIn });
65-
66-
putIn.run(['.editor']);
67-
putIn.run(cmds);
68-
replServer.write('', { ctrl: true, name: 'd' });
69-
70-
putIn.run([`.save ${saveFileName}`]);
71-
replServer.close();
72-
assert.strictEqual(fs.readFileSync(saveFileName, 'utf8'),
73-
`${cmds.join('\n')}\n\n`);
74-
}
75-
76-
// Make sure that the REPL data is "correct"
77-
// so when I load it back I know I'm good
78-
testMe.complete('inner.o', function(error, data) {
62+
// Make sure that the REPL data is "correct".
63+
testMe.complete('inner.o', common.mustCall(function(error, data) {
64+
assert.ifError(error);
7965
assert.deepStrictEqual(data, works);
80-
});
66+
}));
8167

82-
// clear the REPL
68+
// Clear the REPL.
8369
putIn.run(['.clear']);
8470

85-
// Load the file back in
71+
// Load the file back in.
8672
putIn.run([`.load ${saveFileName}`]);
8773

88-
// Make sure that the REPL data is "correct"
89-
testMe.complete('inner.o', function(error, data) {
74+
// Make sure that the REPL data is "correct".
75+
testMe.complete('inner.o', common.mustCall(function(error, data) {
76+
assert.ifError(error);
9077
assert.deepStrictEqual(data, works);
91-
});
78+
}));
9279

93-
// clear the REPL
80+
// Clear the REPL.
9481
putIn.run(['.clear']);
9582

9683
let loadFile = join(tmpdir.path, 'file.does.not.exist');
9784

98-
// should not break
99-
putIn.write = function(data) {
100-
// Make sure I get a failed to load message and not some crazy error
101-
assert.strictEqual(data, `Failed to load:${loadFile}\n`);
102-
// Eat me to avoid work
85+
// Should not break.
86+
putIn.write = common.mustCall(function(data) {
87+
// Make sure I get a failed to load message and not some crazy error.
88+
assert.strictEqual(data, `Failed to load: ${loadFile}\n`);
89+
// Eat me to avoid work.
10390
putIn.write = () => {};
104-
};
91+
});
10592
putIn.run([`.load ${loadFile}`]);
10693

107-
// Throw error on loading directory
94+
// Throw error on loading directory.
10895
loadFile = tmpdir.path;
109-
putIn.write = function(data) {
110-
assert.strictEqual(data, `Failed to load:${loadFile} is not a valid file\n`);
96+
putIn.write = common.mustCall(function(data) {
97+
assert.strictEqual(data, `Failed to load: ${loadFile} is not a valid file\n`);
11198
putIn.write = () => {};
112-
};
99+
});
113100
putIn.run([`.load ${loadFile}`]);
114101

115-
// clear the REPL
102+
// Clear the REPL.
116103
putIn.run(['.clear']);
117104

118105
// NUL (\0) is disallowed in filenames in UNIX-like operating systems and
119-
// Windows so we can use that to test failed saves
106+
// Windows so we can use that to test failed saves.
120107
const invalidFileName = join(tmpdir.path, '\0\0\0\0\0');
121108

122-
// should not break
123-
putIn.write = function(data) {
124-
// Make sure I get a failed to save message and not some other error
125-
assert.strictEqual(data, `Failed to save:${invalidFileName}\n`);
126-
// reset to no-op
109+
// Should not break.
110+
putIn.write = common.mustCall(function(data) {
111+
// Make sure I get a failed to save message and not some other error.
112+
assert.strictEqual(data, `Failed to save: ${invalidFileName}\n`);
113+
// Reset to no-op.
127114
putIn.write = () => {};
128-
};
115+
});
129116

130-
// save it to a file
117+
// Save it to a file.
131118
putIn.run([`.save ${invalidFileName}`]);
119+
120+
{
121+
// Save .editor mode code.
122+
const cmds = [
123+
'function testSave() {',
124+
'return "saved";',
125+
'}'
126+
];
127+
const putIn = new ArrayStream();
128+
const replServer = repl.start({ terminal: true, stream: putIn });
129+
130+
putIn.run(['.editor']);
131+
putIn.run(cmds);
132+
replServer.write('', { ctrl: true, name: 'd' });
133+
134+
putIn.run([`.save ${saveFileName}`]);
135+
replServer.close();
136+
assert.strictEqual(fs.readFileSync(saveFileName, 'utf8'),
137+
`${cmds.join('\n')}\n`);
138+
}

0 commit comments

Comments
 (0)