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