Skip to content

Commit

Permalink
Disallow external repo double stacking (#324)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Update test after suggestion from @danielhuppmann

---------

Co-authored-by: Daniel Huppmann <[email protected]>
  • Loading branch information
phackstock and danielhuppmann authored Feb 9, 2024
1 parent 8f39de1 commit 1a2c2ed
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
14 changes: 14 additions & 0 deletions nomenclature/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions tests/data/double_stacked_external_repo/nomenclature.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
repositories:
common-definitions:
url: https://github.com/IAMconsortium/common-definitions.git/
definitions:
region:
repository: common-definitions
12 changes: 12 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 1a2c2ed

Please sign in to comment.