GeezerVison is showing github.com — repl: don't crash if cannot open history file · nodejs/node@5073da0
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/repl.js +10-1Lines changed: 10 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -92,7 +92,16 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) { | 92 | 92 |
| 93 | 93 | function oninit(err, hnd) { | 94 | 94 | if (err) { | 95 | | - return ready(err); | | 95 | + // Cannot open history file. | | 96 | + // Don't crash, just don't persist history. | | 97 | + repl._writeToOutput('\nError: Could not open history file.\n' + | | 98 | + 'REPL session history will not be persisted.\n'); | | 99 | + repl._refreshLine(); | | 100 | + debug(err.stack); | | 101 | + | | 102 | + repl._historyPrev = _replHistoryMessage; | | 103 | + repl.resume(); | | 104 | + return ready(null, repl); | 96 | 105 | } | 97 | 106 | fs.close(hnd, onclose); | 98 | 107 | } | |
Collapse file Copy file name to clipboardExpand all lines: test/sequential/test-repl-persistent-history.js +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -64,10 +64,13 @@ const convertMsg = '\nConverting old JSON repl history to line-separated ' + | 64 | 64 | path.join(common.tmpDir, '.node_repl_history') + '.\n'; | 65 | 65 | const homedirErr = '\nError: Could not get the home directory.\n' + | 66 | 66 | 'REPL session history will not be persisted.\n'; | | 67 | +const replFailedRead = '\nError: Could not open history file.\n' + | | 68 | + 'REPL session history will not be persisted.\n'; | 67 | 69 | // File paths | 68 | 70 | const fixtures = path.join(common.testDir, 'fixtures'); | 69 | 71 | const historyFixturePath = path.join(fixtures, '.node_repl_history'); | 70 | 72 | const historyPath = path.join(common.tmpDir, '.fixture_copy_repl_history'); | | 73 | +const historyPathFail = path.join(common.tmpDir, '.node_repl\u0000_history'); | 71 | 74 | const oldHistoryPath = path.join(fixtures, 'old-repl-history-file.json'); | 72 | 75 | const enoentHistoryPath = path.join(fixtures, 'enoent-repl-history-file.json'); | 73 | 76 | const defaultHistoryPath = path.join(common.tmpDir, '.node_repl_history'); | @@ -147,6 +150,12 @@ const tests = [{ | 147 | 150 | test: [UP, UP, UP, CLEAR], | 148 | 151 | expected: [prompt, convertMsg, prompt, prompt + '\'=^.^=\'', prompt] | 149 | 152 | }, | | 153 | +{ | | 154 | + env: { NODE_REPL_HISTORY: historyPathFail, | | 155 | + NODE_REPL_HISTORY_SIZE: 1 }, | | 156 | + test: [UP], | | 157 | + expected: [prompt, replFailedRead, prompt, replDisabled, prompt] | | 158 | +}, | 150 | 159 | { // Make sure this is always the last test, since we change os.homedir() | 151 | 160 | before: function mockHomedirFailure() { | 152 | 161 | // Mock os.homedir() failure | |
|