From 73b7e052c04c25887b1aba4542d30a37e39fa60a Mon Sep 17 00:00:00 2001 From: Jeremiah Senkpiel Date: Tue, 6 Oct 2015 23:01:28 -0700 Subject: [PATCH] repl: limit persistent history correctly on load Previously the wrong end of the history was limited on load. PR-URL: https://github.com/nodejs/node/pull/2356 Reviewed-By: Roman Reiss Reviewed By: Evan Lucas --- lib/internal/repl.js | 4 ++-- test/sequential/test-repl-persistent-history.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/internal/repl.js b/lib/internal/repl.js index c1beb85cefd795..0318a36098e31a 100644 --- a/lib/internal/repl.js +++ b/lib/internal/repl.js @@ -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( @@ -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( diff --git a/test/sequential/test-repl-persistent-history.js b/test/sequential/test-repl-persistent-history.js index 7cebddbd6c20f5..7fd68d7d764a4f 100644 --- a/test/sequential/test-repl-persistent-history.js +++ b/test/sequential/test-repl-persistent-history.js @@ -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