|
1 | 1 | """Tests for all things related to the configuration
|
2 | 2 | """
|
3 | 3 |
|
| 4 | +import re |
4 | 5 | from unittest.mock import MagicMock
|
5 | 6 |
|
6 | 7 | import pytest
|
@@ -87,6 +88,25 @@ def test_environment_config_errors_if_malformed(
|
87 | 88 | err.value
|
88 | 89 | )
|
89 | 90 |
|
| 91 | + def test_no_such_key_error_message_no_command(self) -> None: |
| 92 | + self.configuration.load_only = kinds.GLOBAL |
| 93 | + self.configuration.load() |
| 94 | + expected_msg = ( |
| 95 | + "Key does not contain dot separated section and key. " |
| 96 | + "Perhaps you wanted to use 'global.index-url' instead?" |
| 97 | + ) |
| 98 | + pat = f"^{re.escape(expected_msg)}$" |
| 99 | + with pytest.raises(ConfigurationError, match=pat): |
| 100 | + self.configuration.get_value("index-url") |
| 101 | + |
| 102 | + def test_no_such_key_error_message_missing_option(self) -> None: |
| 103 | + self.configuration.load_only = kinds.GLOBAL |
| 104 | + self.configuration.load() |
| 105 | + expected_msg = "No such key - global.index-url" |
| 106 | + pat = f"^{re.escape(expected_msg)}$" |
| 107 | + with pytest.raises(ConfigurationError, match=pat): |
| 108 | + self.configuration.get_value("global.index-url") |
| 109 | + |
90 | 110 |
|
91 | 111 | class TestConfigurationPrecedence(ConfigurationMixin):
|
92 | 112 | # Tests for methods to that determine the order of precedence of
|
@@ -185,12 +205,8 @@ class TestConfigurationModification(ConfigurationMixin):
|
185 | 205 | def test_no_specific_given_modification(self) -> None:
|
186 | 206 | self.configuration.load()
|
187 | 207 |
|
188 |
| - try: |
| 208 | + with pytest.raises(ConfigurationError): |
189 | 209 | self.configuration.set_value("test.hello", "10")
|
190 |
| - except ConfigurationError: |
191 |
| - pass |
192 |
| - else: |
193 |
| - assert False, "Should have raised an error." |
194 | 210 |
|
195 | 211 | def test_site_modification(self) -> None:
|
196 | 212 | self.configuration.load_only = kinds.SITE
|
@@ -241,3 +257,16 @@ def test_global_modification(self) -> None:
|
241 | 257 | # get the path to user config file
|
242 | 258 | assert mymock.call_count == 1
|
243 | 259 | assert mymock.call_args[0][0] == (get_configuration_files()[kinds.GLOBAL][-1])
|
| 260 | + |
| 261 | + def test_normalization(self) -> None: |
| 262 | + # underscores and dashes can be used interchangeably. |
| 263 | + # internally, underscores get converted into dashes before reading/writing file |
| 264 | + self.configuration.load_only = kinds.GLOBAL |
| 265 | + self.configuration.load() |
| 266 | + self.configuration.set_value("global.index_url", "example.org") |
| 267 | + assert self.configuration.get_value("global.index_url") == "example.org" |
| 268 | + assert self.configuration.get_value("global.index-url") == "example.org" |
| 269 | + self.configuration.unset_value("global.index-url") |
| 270 | + pat = r"^No such key - global\.index-url$" |
| 271 | + with pytest.raises(ConfigurationError, match=pat): |
| 272 | + self.configuration.get_value("global.index-url") |
0 commit comments