Skip to content

Commit

Permalink
Config: better validation error for conda.environment (#10979)
Browse files Browse the repository at this point in the history
* Config: better validation error for `conda.environment`

Closes #9008

* Use `dedent` and `split`
  • Loading branch information
humitos authored Jan 11, 2024
1 parent 34ccbad commit 76cafd0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
11 changes: 9 additions & 2 deletions readthedocs/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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'):
Expand Down
1 change: 1 addition & 0 deletions readthedocs/config/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions readthedocs/config/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,18 @@
),
type=ERROR,
),
Message(
id=ConfigError.CONDA_KEY_REQUIRED,
header=_("Missing required key"),
body=_(
textwrap.dedent(
"""
The key <code>conda.environment</code> is required when using Conda or Mamba.
"""
).strip(),
),
type=ERROR,
),
]
registry.add(messages)

Expand Down
17 changes: 17 additions & 0 deletions readthedocs/config/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down

0 comments on commit 76cafd0

Please sign in to comment.