Skip to content

Commit

Permalink
provide a better error message for "pip config get index-url"
Browse files Browse the repository at this point in the history
  • Loading branch information
wimglenn committed Apr 30, 2022
1 parent 6efe708 commit 32f642d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/pip/_internal/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ def get_value(self, key: str) -> Any:
try:
return self._dictionary[key]
except KeyError:
# disassembling triggers a more useful error message than simply
# "No such key" in the case that the key isn't in the form command.option
_disassemble_key(key)
raise ConfigurationError(f"No such key - {orig_key}")

def set_value(self, key: str, value: Any) -> None:
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/test_configuration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Tests for all things related to the configuration
"""

import re
from unittest.mock import MagicMock

import pytest
Expand Down Expand Up @@ -87,6 +88,25 @@ def test_environment_config_errors_if_malformed(
err.value
)

def test_no_such_key_error_message_no_command(self) -> None:
self.configuration.load_only = kinds.GLOBAL
self.configuration.load()
expected_msg = (
"Key does not contain dot separated section and key. "
"Perhaps you wanted to use 'global.index-url' instead?"
)
pat = f"^{re.escape(expected_msg)}$"
with pytest.raises(ConfigurationError, match=pat):
self.configuration.get_value("index-url")

def test_no_such_key_error_message_missing_option(self) -> None:
self.configuration.load_only = kinds.GLOBAL
self.configuration.load()
expected_msg = "No such key - global.index-url"
pat = f"^{re.escape(expected_msg)}$"
with pytest.raises(ConfigurationError, match=pat):
self.configuration.get_value("global.index-url")


class TestConfigurationPrecedence(ConfigurationMixin):
# Tests for methods to that determine the order of precedence of
Expand Down

0 comments on commit 32f642d

Please sign in to comment.