Skip to content

Commit

Permalink
repl: limit persistent history correctly on load
Browse files Browse the repository at this point in the history
Previously the wrong end of the history was limited on load.

PR-URL: #2356
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed By: Evan Lucas <evanlucas@me.com>
  • Loading branch information
Fishrock123 authored and jasnell committed Oct 29, 2015
1 parent 788106e commit a04408a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/internal/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
}

if (data) {
repl.history = data.split(/[\n\r]+/).slice(-repl.historySize);
repl.history = data.split(/[\n\r]+/, repl.historySize);
} else if (oldHistoryPath) {
// Grab data from the older pre-v3.0 JSON NODE_REPL_HISTORY_FILE format.
repl._writeToOutput(
Expand All @@ -123,7 +123,7 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
if (!Array.isArray(repl.history)) {
throw new Error('Expected array, got ' + typeof repl.history);
}
repl.history = repl.history.slice(-repl.historySize);
repl.history = repl.history.slice(0, repl.historySize);
} catch (err) {
if (err.code !== 'ENOENT') {
return ready(
Expand Down
12 changes: 12 additions & 0 deletions test/sequential/test-repl-persistent-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ const tests = [{
expected: [prompt, prompt + '\'42\'', prompt + '\'=^.^=\'', '\'=^.^=\'\n',
prompt]
},
{
env: { NODE_REPL_HISTORY: historyPath,
NODE_REPL_HISTORY_SIZE: 1 },
test: [UP, UP, CLEAR],
expected: [prompt, prompt + '\'you look fabulous today\'', prompt]
},
{
env: { NODE_REPL_HISTORY_FILE: oldHistoryPath,
NODE_REPL_HISTORY_SIZE: 1 },
test: [UP, UP, UP, CLEAR],
expected: [prompt, convertMsg, prompt, prompt + '\'=^.^=\'', prompt]
},
{ // Make sure this is always the last test, since we change os.homedir()
before: function mockHomedirFailure() {
// Mock os.homedir() failure
Expand Down

0 comments on commit a04408a

Please sign in to comment.