From b9b2f1a22fc283e173a71c2608f81f54020f9925 Mon Sep 17 00:00:00 2001 From: Evan Lucas Date: Mon, 2 Nov 2015 09:43:02 -0600 Subject: [PATCH] repl: don't crash if cannot open history file Previously, if we are unable to open the history file, an error would be thrown. Now, print an error message that we could not open the history file, but don't fail. Fixes: https://github.com/nodejs/node/issues/3610 PR-URL: https://github.com/nodejs/node/pull/3630 Reviewed-By: Jeremiah Senkpiel --- lib/internal/repl.js | 11 ++++++++++- test/sequential/test-repl-persistent-history.js | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/internal/repl.js b/lib/internal/repl.js index 0318a36098e31a..1a62414a49e95d 100644 --- a/lib/internal/repl.js +++ b/lib/internal/repl.js @@ -92,7 +92,16 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) { function oninit(err, hnd) { if (err) { - return ready(err); + // Cannot open history file. + // Don't crash, just don't persist history. + repl._writeToOutput('\nError: Could not open history file.\n' + + 'REPL session history will not be persisted.\n'); + repl._refreshLine(); + debug(err.stack); + + repl._historyPrev = _replHistoryMessage; + repl.resume(); + return ready(null, repl); } fs.close(hnd, onclose); } diff --git a/test/sequential/test-repl-persistent-history.js b/test/sequential/test-repl-persistent-history.js index 7fd68d7d764a4f..10e0dedf53ec3e 100644 --- a/test/sequential/test-repl-persistent-history.js +++ b/test/sequential/test-repl-persistent-history.js @@ -64,10 +64,13 @@ const convertMsg = '\nConverting old JSON repl history to line-separated ' + path.join(common.tmpDir, '.node_repl_history') + '.\n'; const homedirErr = '\nError: Could not get the home directory.\n' + 'REPL session history will not be persisted.\n'; +const replFailedRead = '\nError: Could not open history file.\n' + + 'REPL session history will not be persisted.\n'; // File paths const fixtures = path.join(common.testDir, 'fixtures'); const historyFixturePath = path.join(fixtures, '.node_repl_history'); const historyPath = path.join(common.tmpDir, '.fixture_copy_repl_history'); +const historyPathFail = path.join(common.tmpDir, '.node_repl\u0000_history'); const oldHistoryPath = path.join(fixtures, 'old-repl-history-file.json'); const enoentHistoryPath = path.join(fixtures, 'enoent-repl-history-file.json'); const defaultHistoryPath = path.join(common.tmpDir, '.node_repl_history'); @@ -147,6 +150,12 @@ const tests = [{ test: [UP, UP, UP, CLEAR], expected: [prompt, convertMsg, prompt, prompt + '\'=^.^=\'', prompt] }, +{ + env: { NODE_REPL_HISTORY: historyPathFail, + NODE_REPL_HISTORY_SIZE: 1 }, + test: [UP], + expected: [prompt, replFailedRead, prompt, replDisabled, prompt] +}, { // Make sure this is always the last test, since we change os.homedir() before: function mockHomedirFailure() { // Mock os.homedir() failure