From 1a2c2ed43a6822cd4597bcd2ec15579c9a51322e Mon Sep 17 00:00:00 2001 From: Philip Hackstock <20710924+phackstock@users.noreply.github.com> Date: Fri, 9 Feb 2024 13:10:02 +0100 Subject: [PATCH] Disallow external repo double stacking (#324) * Add safeguard against external repo double stacking * Add test for double stacking repo * Improve error readability * Apply suggestions from code review Co-authored-by: Daniel Huppmann * Update test after suggestion from @danielhuppmann --------- Co-authored-by: Daniel Huppmann --- nomenclature/config.py | 14 ++++++++++++++ .../double_stacked_external_repo/nomenclature.yaml | 6 ++++++ tests/test_config.py | 12 ++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 tests/data/double_stacked_external_repo/nomenclature.yaml diff --git a/nomenclature/config.py b/nomenclature/config.py index 0bfb112f..c0372749 100644 --- a/nomenclature/config.py +++ b/nomenclature/config.py @@ -82,6 +82,20 @@ def fetch_repo(self, to_path): repo.git.clean("-xdf") if self.revision == "main": repo.remotes.origin.pull() + self.check_external_repo_double_stacking() + + def check_external_repo_double_stacking(self): + nomenclature_config = self.local_path / "nomenclature.yaml" + if nomenclature_config.is_file(): + with open(nomenclature_config, "r") as f: + config = yaml.safe_load(f) + if config.get("repositories"): + raise ValueError( + ( + "External repos cannot again refer to external repos, " + f"found in nomenclature.yaml in '{self.url}'" + ) + ) class DataStructureConfig(BaseModel): diff --git a/tests/data/double_stacked_external_repo/nomenclature.yaml b/tests/data/double_stacked_external_repo/nomenclature.yaml new file mode 100644 index 00000000..437e7ed0 --- /dev/null +++ b/tests/data/double_stacked_external_repo/nomenclature.yaml @@ -0,0 +1,6 @@ +repositories: + common-definitions: + url: https://github.com/IAMconsortium/common-definitions.git/ +definitions: + region: + repository: common-definitions diff --git a/tests/test_config.py b/tests/test_config.py index b03fd3e2..9674f08d 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -55,3 +55,15 @@ def test_multiple_mapping_repos(): assert nomenclature_config.repositories.keys() == exp_repos finally: clean_up_external_repos(nomenclature_config.repositories) + + +def test_double_stacked_external_repo_raises(monkeypatch): + repo = Repository(url="lorem ipsum") + monkeypatch.setitem( + repo.__dict__, + "local_path", + TEST_DATA_DIR / "double_stacked_external_repo", + ) + match = "External repos cannot again refer to external repos" + with raises(ValueError, match=match): + repo.check_external_repo_double_stacking()