From 76cafd0facbd0fef60d89fa6ab32fa6b9bdf8a91 Mon Sep 17 00:00:00 2001 From: Manuel Kaufmann Date: Thu, 11 Jan 2024 16:45:04 +0100 Subject: [PATCH] Config: better validation error for `conda.environment` (#10979) * Config: better validation error for `conda.environment` Closes #9008 * Use `dedent` and `split` --- readthedocs/config/config.py | 11 +++++++++-- readthedocs/config/exceptions.py | 1 + readthedocs/config/notifications.py | 12 ++++++++++++ readthedocs/config/tests/test_config.py | 17 +++++++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/readthedocs/config/config.py b/readthedocs/config/config.py index 8a6b20a48ed..b5e56c98d48 100644 --- a/readthedocs/config/config.py +++ b/readthedocs/config/config.py @@ -226,9 +226,11 @@ def settings(self): def validate(self): """Validates and process ``raw_config``.""" self._config['formats'] = self.validate_formats() + + # This should be called before ``validate_python`` and ``validate_conda`` + self._config["build"] = self.validate_build() + self._config['conda'] = self.validate_conda() - # This should be called before validate_python - self._config['build'] = self.validate_build() self._config['python'] = self.validate_python() # Call this before validate sphinx and mkdocs self.validate_doc_types() @@ -258,6 +260,11 @@ def validate_conda(self): """Validates the conda key.""" raw_conda = self._raw_config.get('conda') if raw_conda is None: + if self.is_using_conda: + raise ConfigError( + message_id=ConfigError.CONDA_KEY_REQUIRED, + format_values={"key": "conda"}, + ) return None with self.catch_validation_error('conda'): diff --git a/readthedocs/config/exceptions.py b/readthedocs/config/exceptions.py index 58dba2a8545..ea76ce0cbcf 100644 --- a/readthedocs/config/exceptions.py +++ b/readthedocs/config/exceptions.py @@ -22,6 +22,7 @@ class ConfigError(BuildUserError): SUBMODULES_INCLUDE_EXCLUDE_TOGETHER = "config:submodules:include-exclude-together" INVALID_KEY_NAME = "config:base:invalid-key-name" SYNTAX_INVALID = "config:base:invalid-syntax" + CONDA_KEY_REQUIRED = "config:conda:required" # TODO: improve these error messages shown to the user diff --git a/readthedocs/config/notifications.py b/readthedocs/config/notifications.py index 5084a6bf86d..f073dd1c6a2 100644 --- a/readthedocs/config/notifications.py +++ b/readthedocs/config/notifications.py @@ -237,6 +237,18 @@ ), type=ERROR, ), + Message( + id=ConfigError.CONDA_KEY_REQUIRED, + header=_("Missing required key"), + body=_( + textwrap.dedent( + """ + The key conda.environment is required when using Conda or Mamba. + """ + ).strip(), + ), + type=ERROR, + ), ] registry.add(messages) diff --git a/readthedocs/config/tests/test_config.py b/readthedocs/config/tests/test_config.py index c5963bf7ef4..64e9ccf14ad 100644 --- a/readthedocs/config/tests/test_config.py +++ b/readthedocs/config/tests/test_config.py @@ -282,6 +282,23 @@ def test_conda_check_valid(self, tmpdir): build.validate() assert build.conda.environment == "environment.yml" + def test_conda_key_required_for_conda_mamba(self): + build = get_build_config( + { + "build": { + "os": "ubuntu-22.04", + "tools": { + "python": "miniconda3-4.7", + }, + }, + } + ) + print(build) + with raises(ConfigError) as excinfo: + build.validate() + assert excinfo.value.message_id == ConfigError.CONDA_KEY_REQUIRED + assert excinfo.value.format_values.get("key") == "conda" + @pytest.mark.parametrize("value", [3, [], "invalid"]) def test_conda_check_invalid_value(self, value): build = get_build_config({"conda": value})