From b1c4518795dd6aa0e8b32cd62081d8c4bd1ec32f Mon Sep 17 00:00:00 2001
From: "r.jaepel" <r.jaepel@fz-juelich.de>
Date: Tue, 6 Feb 2024 13:49:57 +0100
Subject: [PATCH] Improve handling of paths

---
 cadetrdm/repositories.py  | 20 +++++++++++++-------
 tests/test_git_adapter.py | 25 ++++++++++++++++---------
 2 files changed, 29 insertions(+), 16 deletions(-)

diff --git a/cadetrdm/repositories.py b/cadetrdm/repositories.py
index 1c5fed3..1b64226 100644
--- a/cadetrdm/repositories.py
+++ b/cadetrdm/repositories.py
@@ -1,13 +1,12 @@
-import os
-from pathlib import Path
+import contextlib
+import glob
 import json
+import os
+import shutil
 import sys
 import traceback
 from datetime import datetime
-import shutil
-import time
-import contextlib
-import glob
+from pathlib import Path
 from stat import S_IREAD, S_IWRITE
 from urllib.request import urlretrieve
 
@@ -176,8 +175,12 @@ class BaseRepo:
         :return:
         Path to the cloned repository
         """
+        if "://" in str(source_repo_location):
+            source_repo_name = source_repo_location.split("/")[-1]
+        else:
+            source_repo_name = Path(source_repo_location).name
         if target_repo_location is None:
-            target_repo_location = self.working_dir / "external_cache" / source_repo_location.split("/")[-1]
+            target_repo_location = self.working_dir / "external_cache" / source_repo_name
         else:
             target_repo_location = self.working_dir / target_repo_location
 
@@ -235,6 +238,9 @@ class BaseRepo:
 
         target_repo_location = str(self.ensure_relative_path(target_repo_location))
 
+        if isinstance(source_repo_location, Path):
+            source_repo_location = source_repo_location.as_posix()
+
         rdm_cache[target_repo_location] = {
             "source_repo_location": source_repo_location,
             "branch_name": source_repo_branch,
diff --git a/tests/test_git_adapter.py b/tests/test_git_adapter.py
index b917a52..89557ee 100644
--- a/tests/test_git_adapter.py
+++ b/tests/test_git_adapter.py
@@ -1,15 +1,15 @@
-from pathlib import Path
 import os
 import random
+from pathlib import Path
 
-import pytest
 import git
 import numpy as np
+import pytest
 
 from cadetrdm import initialize_repo, ProjectRepo, clone
 from cadetrdm.initialize_repo import init_lfs
-from cadetrdm.repositories import OutputRepo, BaseRepo
 from cadetrdm.io_utils import delete_path
+from cadetrdm.repositories import OutputRepo, BaseRepo
 
 
 @pytest.fixture(scope="module")
@@ -158,7 +158,7 @@ def test_init_over_existing_repo(monkeypatch):
 
 
 def test_cache_with_non_rdm_repo(monkeypatch):
-    path_to_repo = Path("test_repo_5")
+    path_to_repo = Path("non_rdm_repo")
     if path_to_repo.exists():
         delete_path(path_to_repo)
     os.makedirs(path_to_repo)
@@ -172,15 +172,22 @@ def test_cache_with_non_rdm_repo(monkeypatch):
     repo.git.add(".")
     repo.git.commit("-m", "Initial commit")
 
-    imported_repo = OutputRepo("../test_repo/results")
-    branch_name = imported_repo.active_branch.name
+    os.chdir("..")
+    if Path("test_repo_non_rdm_imports").exists():
+        delete_path("test_repo_non_rdm_imports")
+    initialize_repo("test_repo_non_rdm_imports")
+    os.chdir("test_repo_non_rdm_imports")
 
     repo = BaseRepo(".")
 
+    if Path("external_cache/non_rdm_repo").exists():
+        delete_path("external_cache/non_rdm_repo")
+    if Path("foo/bar/non_rdm_repo").exists():
+        delete_path("foo/bar/non_rdm_repo")
     # import two repos and confirm verify works.
-    repo.import_remote_repo(source_repo_location="../test_repo/results", source_repo_branch=branch_name)
-    repo.import_remote_repo(source_repo_location="../test_repo/results", source_repo_branch=branch_name,
-                            target_repo_location="foo/bar/repo")
+    repo.import_remote_repo(source_repo_location=".." / path_to_repo, source_repo_branch="master")
+    repo.import_remote_repo(source_repo_location=".." / path_to_repo, source_repo_branch="master",
+                            target_repo_location="foo/bar/non_rdm_repo")
     repo.verify_unchanged_cache()
 
 
-- 
GitLab