Skip to content

Commit 4c80c02

Browse files
Fishrock123rvagg
authored andcommitted
repl: limit persistent history correctly on load
Previously the wrong end of the history was limited on load. PR-URL: #2356 Reviewed-By: Roman Reiss <[email protected]> Reviewed By: Evan Lucas <[email protected]>
1 parent 9e98155 commit 4c80c02

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/internal/repl.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
110110
}
111111

112112
if (data) {
113-
repl.history = data.split(/[\n\r]+/).slice(-repl.historySize);
113+
repl.history = data.split(/[\n\r]+/, repl.historySize);
114114
} else if (oldHistoryPath) {
115115
// Grab data from the older pre-v3.0 JSON NODE_REPL_HISTORY_FILE format.
116116
repl._writeToOutput(
@@ -123,7 +123,7 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
123123
if (!Array.isArray(repl.history)) {
124124
throw new Error('Expected array, got ' + typeof repl.history);
125125
}
126-
repl.history = repl.history.slice(-repl.historySize);
126+
repl.history = repl.history.slice(0, repl.historySize);
127127
} catch (err) {
128128
if (err.code !== 'ENOENT') {
129129
return ready(

test/sequential/test-repl-persistent-history.js

+12
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,18 @@ const tests = [{
135135
expected: [prompt, prompt + '\'42\'', prompt + '\'=^.^=\'', '\'=^.^=\'\n',
136136
prompt]
137137
},
138+
{
139+
env: { NODE_REPL_HISTORY: historyPath,
140+
NODE_REPL_HISTORY_SIZE: 1 },
141+
test: [UP, UP, CLEAR],
142+
expected: [prompt, prompt + '\'you look fabulous today\'', prompt]
143+
},
144+
{
145+
env: { NODE_REPL_HISTORY_FILE: oldHistoryPath,
146+
NODE_REPL_HISTORY_SIZE: 1 },
147+
test: [UP, UP, UP, CLEAR],
148+
expected: [prompt, convertMsg, prompt, prompt + '\'=^.^=\'', prompt]
149+
},
138150
{ // Make sure this is always the last test, since we change os.homedir()
139151
before: function mockHomedirFailure() {
140152
// Mock os.homedir() failure

0 commit comments

Comments
 (0)