From 83887f35fa0dd71279410cb613c590c02822e29c Mon Sep 17 00:00:00 2001 From: Bartosz Sosnowski Date: Thu, 30 Mar 2017 12:57:12 +0200 Subject: [PATCH] repl: support hidden history file on Windows On Windows when REPL history file has the hidden attribute node will fail when trying to open it in 'w' mode. This changes the mode to 'r+'. The file is guaranteed to exists because of earlier open call with 'a+'. --- lib/internal/repl.js | 11 ++++++++++- test/fixtures/.empty-hidden-repl-history-file | 0 test/parallel/test-repl-persistent-history.js | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/.empty-hidden-repl-history-file diff --git a/lib/internal/repl.js b/lib/internal/repl.js index d81f56f5c96d33..c98fd00e35680f 100644 --- a/lib/internal/repl.js +++ b/lib/internal/repl.js @@ -164,10 +164,19 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) { } } - fs.open(historyPath, 'w', onhandle); + fs.open(historyPath, 'r+', onhandle); } function onhandle(err, hnd) { + if (err) { + return ready(err); + } + fs.ftruncate(hnd, 0, (err) => { + return onftruncate(err, hnd); + }); + } + + function onftruncate(err, hnd) { if (err) { return ready(err); } diff --git a/test/fixtures/.empty-hidden-repl-history-file b/test/fixtures/.empty-hidden-repl-history-file new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/test/parallel/test-repl-persistent-history.js b/test/parallel/test-repl-persistent-history.js index 2b0ceac530a65f..6d95f932ccae45 100644 --- a/test/parallel/test-repl-persistent-history.js +++ b/test/parallel/test-repl-persistent-history.js @@ -76,6 +76,8 @@ const oldHistoryPath = path.join(fixtures, 'old-repl-history-file.json'); const enoentHistoryPath = path.join(fixtures, 'enoent-repl-history-file.json'); const emptyHistoryPath = path.join(fixtures, '.empty-repl-history-file'); const defaultHistoryPath = path.join(common.tmpDir, '.node_repl_history'); +const emptyHiddenHistoryPath = path.join(fixtures, + '.empty-hidden-repl-history-file'); const tests = [ { @@ -163,6 +165,19 @@ const tests = [ test: [UP], expected: [prompt, replFailedRead, prompt, replDisabled, prompt] }, + { + before: function before() { + if (common.isWindows) { + const execSync = require('child_process').execSync; + execSync(`ATTRIB +H "${emptyHiddenHistoryPath}"`, (err) => { + assert.ifError(err); + }); + } + }, + env: { NODE_REPL_HISTORY: emptyHiddenHistoryPath }, + test: [UP], + expected: [prompt] + }, { // Make sure this is always the last test, since we change os.homedir() before: function before() { // Mock os.homedir() failure