Skip to content

Commit

Permalink
Merge pull request #9207 from NoahGorny/fix-redact-url-from-help
Browse files Browse the repository at this point in the history
  • Loading branch information
pradyunsg authored Dec 8, 2020
2 parents 7c00e6f + 8de94b5 commit 643217b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 2 additions & 0 deletions news/9191.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crash when logic for redacting authentication information from URLs
in ``--help`` is given a list of strings, instead of a single string.
18 changes: 13 additions & 5 deletions src/pip/_internal/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,23 @@ class UpdatingDefaultsHelpFormatter(PrettyHelpFormatter):
"""

def expand_default(self, option):
default_value = None
default_values = None
if self.parser is not None:
self.parser._update_defaults(self.parser.defaults)
default_value = self.parser.defaults.get(option.dest)
default_values = self.parser.defaults.get(option.dest)
help_text = optparse.IndentedHelpFormatter.expand_default(self, option)

if default_value and option.metavar == 'URL':
help_text = help_text.replace(
default_value, redact_auth_from_url(default_value))
if default_values and option.metavar == 'URL':
if isinstance(default_values, string_types):
default_values = [default_values]

# If its not a list, we should abort and just return the help text
if not isinstance(default_values, list):
default_values = []

for val in default_values:
help_text = help_text.replace(
val, redact_auth_from_url(val))

return help_text

Expand Down
11 changes: 11 additions & 0 deletions tests/functional/test_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ def test_help_command_redact_auth_from_url(script):
assert 'secret' not in result.stdout


def test_help_command_redact_auth_from_url_with_extra_index_url(script):
"""
Test `help` on various subcommands redact auth from url with extra index url
"""
script.environ['PIP_INDEX_URL'] = 'https://user:secret@example.com'
script.environ['PIP_EXTRA_INDEX_URL'] = 'https://user:secret@example2.com'
result = script.pip('install', '--help')
assert result.returncode == SUCCESS
assert 'secret' not in result.stdout


def test_help_commands_equally_functional(in_memory_pip):
"""
Test if `pip help` and 'pip --help' behave the same way.
Expand Down

0 comments on commit 643217b

Please sign in to comment.