repl: limit persistent history correctly on load
authorJeremiah Senkpiel <fishrock123@rocketmail.com>
Wed, 7 Oct 2015 06:01:28 +0000 (23:01 -0700)
committerJames M Snell <jasnell@gmail.com>
Thu, 29 Oct 2015 15:38:42 +0000 (08:38 -0700)
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>

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

index c1beb85..0318a36 100644 (file)
@@ -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(
index 7cebddb..7fd68d7 100644 (file)
@@ -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