Skip to content

Commit

Permalink
repl: support hidden history file on Windows
Browse files Browse the repository at this point in the history
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+'.

Fixes: #5261
PR-URL: #12207
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
bzoz authored and evanlucas committed May 2, 2017
1 parent 7d87edc commit b09bf51
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/internal/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,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);
}
Expand Down
Empty file.
15 changes: 15 additions & 0 deletions test/parallel/test-repl-persistent-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
{
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b09bf51

Please sign in to comment.