Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl: make sure historyPath is trimmed #4539

Merged
merged 1 commit into from
Jan 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/api/repl.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ via the following environment variables:
- `NODE_REPL_HISTORY` - When a valid path is given, persistent REPL history
will be saved to the specified file rather than `.node_repl_history` in the
user's home directory. Setting this value to `""` will disable persistent
REPL history.
REPL history. Whitespace will be trimmed from the value.
- `NODE_REPL_HISTORY_SIZE` - defaults to `1000`. Controls how many lines of
history will be persisted if history is available. Must be a positive number.
- `NODE_REPL_MODE` - may be any of `sloppy`, `strict`, or `magic`. Defaults
Expand Down
13 changes: 12 additions & 1 deletion lib/internal/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,26 @@ function createRepl(env, opts, cb) {
}

const repl = REPL.start(opts);
if (opts.terminal && env.NODE_REPL_HISTORY !== '') {
if (opts.terminal) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it wouldn't be more DRY if the logic was moved out to here. We can make var historyPath = env.NODE_REPL_HISTORY up here so we don't access the env programmatically multiple times.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was how I had originally done this, but changed it at @cjihrig's request. I'm fine doing it either though

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My reasoning is that empty string is a magic value with setupHistory(), so it would make more sense to keep it there. Also, if this function ever gets called from anywhere else, the same logic would have to be duplicated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense to me and seems like it would be easier to maintain. That work for you @Fishrock123?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough, no strong opinion

return setupHistory(repl, env.NODE_REPL_HISTORY,
env.NODE_REPL_HISTORY_FILE, cb);
}

repl._historyPrev = _replHistoryMessage;
cb(null, repl);
}

function setupHistory(repl, historyPath, oldHistoryPath, ready) {
// Empty string disables persistent history.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want to do

if (typeof historyPath === 'string')
  historyPath = historyPath.trim();

Then drop the condition that is currently on line 74.


if (typeof historyPath === 'string')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait what else would it be? We don't export setupHistory().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if process.env.NODE_REPL_HISTORY is undefined, then it will be undefined

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, carry on.

historyPath = historyPath.trim();

if (historyPath === '') {
repl._historyPrev = _replHistoryMessage;
return ready(null, repl);
}

if (!historyPath) {
try {
historyPath = path.join(os.homedir(), '.node_repl_history');
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-repl-persistent-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ const tests = [
test: [UP],
expected: [prompt, replDisabled, prompt]
},
{
env: { NODE_REPL_HISTORY: ' ' },
test: [UP],
expected: [prompt, replDisabled, prompt]
},
{
env: { NODE_REPL_HISTORY: '',
NODE_REPL_HISTORY_FILE: enoentHistoryPath },
Expand Down