provide a better error message for "pip config get index-url"

This commit is contained in:
wim glenn 2022-04-29 22:51:16 -05:00
parent 6efe70858c
commit 32f642d123
2 changed files with 23 additions and 0 deletions

View File

@ -147,6 +147,9 @@ class Configuration:
try: try:
return self._dictionary[key] return self._dictionary[key]
except KeyError: 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}") raise ConfigurationError(f"No such key - {orig_key}")
def set_value(self, key: str, value: Any) -> None: def set_value(self, key: str, value: Any) -> None:

View File

@ -1,6 +1,7 @@
"""Tests for all things related to the configuration """Tests for all things related to the configuration
""" """
import re
from unittest.mock import MagicMock from unittest.mock import MagicMock
import pytest import pytest
@ -87,6 +88,25 @@ class TestConfigurationLoading(ConfigurationMixin):
err.value 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): class TestConfigurationPrecedence(ConfigurationMixin):
# Tests for methods to that determine the order of precedence of # Tests for methods to that determine the order of precedence of