From 75d3a6396ca36b35411d162b739b377a3874ea0e Mon Sep 17 00:00:00 2001 From: Philip Hackstock <20710924+phackstock@users.noreply.github.com> Date: Wed, 11 Oct 2023 12:04:27 +0200 Subject: [PATCH] Fix for general config failing without definitions (#288) * Modify missing definition directory error condition * Add test for using only general config * Fix bug that broke tests * Parametrize external definitions repo test * Apply suggestions from code review Co-authored-by: Daniel Huppmann * Fix tests --------- Co-authored-by: Daniel Huppmann --- nomenclature/definition.py | 7 ++++--- tests/data/general-config-only/nomenclature.yaml | 10 ++++++++++ tests/test_definition.py | 8 +++++--- 3 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 tests/data/general-config-only/nomenclature.yaml diff --git a/nomenclature/definition.py b/nomenclature/definition.py index 6fbbbbdf..e1eda28e 100644 --- a/nomenclature/definition.py +++ b/nomenclature/definition.py @@ -41,13 +41,14 @@ def __init__(self, path, dimensions=None): if not isinstance(path, Path): path = Path(path) - if not path.is_dir(): - raise NotADirectoryError(f"Definitions directory not found: {path}") - if (file := path.parent / "nomenclature.yaml").exists(): self.config = NomenclatureConfig.from_file(file=file) else: self.config = None + + if not path.is_dir() and (self.config is None or not self.config.repositories): + raise NotADirectoryError(f"Definitions directory not found: {path}") + self.dimensions = dimensions or ["region", "variable"] for dim in self.dimensions: codelist_cls = SPECIAL_CODELIST.get(dim, CodeList) diff --git a/tests/data/general-config-only/nomenclature.yaml b/tests/data/general-config-only/nomenclature.yaml new file mode 100644 index 00000000..e9515d9e --- /dev/null +++ b/tests/data/general-config-only/nomenclature.yaml @@ -0,0 +1,10 @@ +repositories: + common-definitions: + url: https://github.com/IAMconsortium/common-definitions.git/ +definitions: + region: + repository: common-definitions + country: true + variable: + repository: common-definitions + repository_dimension_path: definitions/variable diff --git a/tests/test_definition.py b/tests/test_definition.py index 8b5369a8..cd90f4dd 100644 --- a/tests/test_definition.py +++ b/tests/test_definition.py @@ -41,14 +41,16 @@ def test_empty_codelist_raises(): DataStructureDefinition(TEST_DATA_DIR / "simple_codelist") -def test_definition_from_general_config(): +@pytest.mark.parametrize("workflow_folder", ["general-config-only", "general-config"]) +def test_definition_from_general_config(workflow_folder): obs = DataStructureDefinition( - TEST_DATA_DIR / "general-config" / "definitions", + TEST_DATA_DIR / workflow_folder / "definitions", dimensions=["region", "variable"], ) try: # explicitly defined in `general-config-definitions/region/regions.yaml` - assert "Region A" in obs.region + if workflow_folder == "general-config": + assert "Region A" in obs.region # imported from https://github.com/IAMconsortium/common-definitions repo assert "World" in obs.region # added via general-config definitions