Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion esmvalcore/config/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def load_config_developer(cfg_file):
cfg['obs4MIPs'] = cfg.pop('obs4mips')

for project, settings in cfg.items():
for site, drs in settings['input_dir'].items():
for site, drs in settings.get('input_dir', {}).items():
# Since v2.8, 'version' can be used instead of 'latestversion'
if isinstance(drs, list):
drs = [d.replace('{latestversion}', '{version}') for d in drs]
Expand Down
7 changes: 4 additions & 3 deletions esmvalcore/config/_validated_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ def __init__(self, *args, **kwargs):

def __setitem__(self, key, val):
"""Map key to value."""
if key not in self._validate:
raise InvalidConfigParameter(
f"`{key}` is not a valid config parameter."
)
try:
cval = self._validate[key](val)
except ValidationError as verr:
raise InvalidConfigParameter(f"Key `{key}`: {verr}") from None
except KeyError:
raise InvalidConfigParameter(
f"`{key}` is not a valid config parameter.") from None

if key in self._deprecate:
self._deprecate[key](self, val, cval)
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,19 @@ def test_project_obs4mips_case_correction(tmp_path, monkeypatch, mocker):
assert _config.CFG['obs4MIPs'] == project_cfg


def test_load_config_developer_custom(tmp_path, monkeypatch, mocker):
monkeypatch.setattr(_config, 'CFG', {})
mocker.patch.object(_config, 'read_cmor_tables', autospec=True)
cfg_file = tmp_path / 'config-developer.yml'
cfg_dev = {'custom': {'cmor_path': '/path/to/tables'}}
with cfg_file.open('w') as file:
yaml.safe_dump(cfg_dev, file)

_config.load_config_developer(cfg_file)

assert 'custom' in _config.CFG


@pytest.mark.parametrize(
'project,step',
[
Expand Down
51 changes: 49 additions & 2 deletions tests/unit/config/test_config_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import numpy as np
import pytest
import yaml

import esmvalcore
from esmvalcore import __version__ as current_version
from esmvalcore.config._config_validators import (
_handle_deprecation,
_listify_validator,
validate_bool,
validate_bool_or_none,
validate_check_level,
validate_config_developer,
validate_diagnostics,
validate_float,
validate_int,
Expand Down Expand Up @@ -128,8 +131,17 @@ def generate_validator_testcases(valid):
},
{
'validator': validate_path_or_none,
'success': ((None, None), ),
'fail': (),
'success': (
('a/b/c', Path.cwd() / 'a' / 'b' / 'c'),
('/a/b/c/', Path('/', 'a', 'b', 'c')),
('~/', Path.home()),
(None, None),
),
'fail': (
(123, ValueError),
(False, ValueError),
([], ValueError),
),
},
{
'validator': validate_positive,
Expand Down Expand Up @@ -240,3 +252,38 @@ def test_handle_deprecation(remove_version):
_handle_deprecation(
option, deprecated_version, remove_version, more_info
)


def test_validate_config_developer_none():
"""Test ``validate_config_developer``."""
path = validate_config_developer(None)
assert path == Path(esmvalcore.__file__).parent / 'config-developer.yml'


def test_validate_config_developer(tmp_path):
"""Test ``validate_config_developer``."""
custom_table_path = (
Path(esmvalcore.__file__).parent / 'cmor' / 'tables' / 'custom'
)
cfg_dev = {
'custom': {'cmor_path': custom_table_path},
'CMIP3': {'input_dir': {'default': '/'}},
'CMIP5': {'input_dir': {'default': '/'}},
'CMIP6': {'input_dir': {'default': '/'}},
'CORDEX': {'input_dir': {'default': '/'}},
'OBS': {'input_dir': {'default': '/'}},
'OBS6': {'input_dir': {'default': '/'}},
'obs4MIPs': {'input_dir': {'default': '/'}},
'ana4mips': {'input_dir': {'default': '/'}},
'native6': {'input_dir': {'default': '/'}},
'EMAC': {'input_dir': {'default': '/'}},
'IPSLCM': {'input_dir': {'default': '/'}},
'ICON': {'input_dir': {'default': '/'}},
'CESM': {'input_dir': {'default': '/'}},
}
cfg_dev_file = tmp_path / 'cfg-developer.yml'
with open(cfg_dev_file, mode='w', encoding='utf-8') as file:
yaml.safe_dump(cfg_dev, file)

path = validate_config_developer(cfg_dev_file)
assert path == cfg_dev_file