Skip to content

Commit 76cafd0

Browse files
authored
Config: better validation error for conda.environment (#10979)
* Config: better validation error for `conda.environment` Closes #9008 * Use `dedent` and `split`
1 parent 34ccbad commit 76cafd0

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

readthedocs/config/config.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,11 @@ def settings(self):
226226
def validate(self):
227227
"""Validates and process ``raw_config``."""
228228
self._config['formats'] = self.validate_formats()
229+
230+
# This should be called before ``validate_python`` and ``validate_conda``
231+
self._config["build"] = self.validate_build()
232+
229233
self._config['conda'] = self.validate_conda()
230-
# This should be called before validate_python
231-
self._config['build'] = self.validate_build()
232234
self._config['python'] = self.validate_python()
233235
# Call this before validate sphinx and mkdocs
234236
self.validate_doc_types()
@@ -258,6 +260,11 @@ def validate_conda(self):
258260
"""Validates the conda key."""
259261
raw_conda = self._raw_config.get('conda')
260262
if raw_conda is None:
263+
if self.is_using_conda:
264+
raise ConfigError(
265+
message_id=ConfigError.CONDA_KEY_REQUIRED,
266+
format_values={"key": "conda"},
267+
)
261268
return None
262269

263270
with self.catch_validation_error('conda'):

readthedocs/config/exceptions.py

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class ConfigError(BuildUserError):
2222
SUBMODULES_INCLUDE_EXCLUDE_TOGETHER = "config:submodules:include-exclude-together"
2323
INVALID_KEY_NAME = "config:base:invalid-key-name"
2424
SYNTAX_INVALID = "config:base:invalid-syntax"
25+
CONDA_KEY_REQUIRED = "config:conda:required"
2526

2627

2728
# TODO: improve these error messages shown to the user

readthedocs/config/notifications.py

+12
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,18 @@
237237
),
238238
type=ERROR,
239239
),
240+
Message(
241+
id=ConfigError.CONDA_KEY_REQUIRED,
242+
header=_("Missing required key"),
243+
body=_(
244+
textwrap.dedent(
245+
"""
246+
The key <code>conda.environment</code> is required when using Conda or Mamba.
247+
"""
248+
).strip(),
249+
),
250+
type=ERROR,
251+
),
240252
]
241253
registry.add(messages)
242254

readthedocs/config/tests/test_config.py

+17
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,23 @@ def test_conda_check_valid(self, tmpdir):
282282
build.validate()
283283
assert build.conda.environment == "environment.yml"
284284

285+
def test_conda_key_required_for_conda_mamba(self):
286+
build = get_build_config(
287+
{
288+
"build": {
289+
"os": "ubuntu-22.04",
290+
"tools": {
291+
"python": "miniconda3-4.7",
292+
},
293+
},
294+
}
295+
)
296+
print(build)
297+
with raises(ConfigError) as excinfo:
298+
build.validate()
299+
assert excinfo.value.message_id == ConfigError.CONDA_KEY_REQUIRED
300+
assert excinfo.value.format_values.get("key") == "conda"
301+
285302
@pytest.mark.parametrize("value", [3, [], "invalid"])
286303
def test_conda_check_invalid_value(self, value):
287304
build = get_build_config({"conda": value})

0 commit comments

Comments
 (0)