Browse Source

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 <me@silverwind.io>
Reviewed By: Evan Lucas <evanlucas@me.com>
process-exit-stdio-flushing
Jeremiah Senkpiel 9 years ago
parent
commit
73b7e052c0
  1. 4
      lib/internal/repl.js
  2. 12
      test/sequential/test-repl-persistent-history.js

4
lib/internal/repl.js

@ -110,7 +110,7 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
} }
if (data) { if (data) {
repl.history = data.split(/[\n\r]+/).slice(-repl.historySize); repl.history = data.split(/[\n\r]+/, repl.historySize);
} else if (oldHistoryPath) { } else if (oldHistoryPath) {
// Grab data from the older pre-v3.0 JSON NODE_REPL_HISTORY_FILE format. // Grab data from the older pre-v3.0 JSON NODE_REPL_HISTORY_FILE format.
repl._writeToOutput( repl._writeToOutput(
@ -123,7 +123,7 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
if (!Array.isArray(repl.history)) { if (!Array.isArray(repl.history)) {
throw new Error('Expected array, got ' + typeof 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) { } catch (err) {
if (err.code !== 'ENOENT') { if (err.code !== 'ENOENT') {
return ready( return ready(

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

@ -135,6 +135,18 @@ const tests = [{
expected: [prompt, prompt + '\'42\'', prompt + '\'=^.^=\'', '\'=^.^=\'\n', expected: [prompt, prompt + '\'42\'', prompt + '\'=^.^=\'', '\'=^.^=\'\n',
prompt] 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() { // Make sure this is always the last test, since we change os.homedir()
before: function mockHomedirFailure() { before: function mockHomedirFailure() {
// Mock os.homedir() failure // Mock os.homedir() failure

Loading…
Cancel
Save