From a5bf0cb31d2294e7cd7ee2339d8f3faa69ac006e Mon Sep 17 00:00:00 2001 From: simleo Date: Mon, 9 Dec 2024 15:33:25 +0100 Subject: [PATCH] fix dataset id when adding a local dir with trailing slash --- rocrate/model/file_or_dir.py | 2 +- test/test_model.py | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/rocrate/model/file_or_dir.py b/rocrate/model/file_or_dir.py index 5f192f0..8572e9b 100644 --- a/rocrate/model/file_or_dir.py +++ b/rocrate/model/file_or_dir.py @@ -47,5 +47,5 @@ def __init__(self, crate, source=None, dest_path=None, fetch_remote=False, if is_url(str(source)): identifier = os.path.basename(source) if fetch_remote else source else: - identifier = "./" if source == "./" else os.path.basename(source) + identifier = os.path.basename(str(source).rstrip(os.sep)) super().__init__(crate, identifier, properties) diff --git a/test/test_model.py b/test/test_model.py index 7835500..ad69283 100644 --- a/test/test_model.py +++ b/test/test_model.py @@ -41,7 +41,7 @@ RAW_REPO_URL = "https://raw.githubusercontent.com/ResearchObject/ro-crate-py" -def test_dereferencing(test_data_dir, helpers): +def test_dereferencing(test_data_dir, helpers, monkeypatch): crate = ROCrate(gen_preview=True) # verify default entities @@ -69,6 +69,12 @@ def test_dereferencing(test_data_dir, helpers): # alias assert crate.get(readme_url) is readme_entity + # dereference local dir added with trailing slash + monkeypatch.chdir(test_data_dir) + local_dir = "test_add_dir/" + local_dir_entity = crate.add_dataset(local_dir) + assert crate.dereference(local_dir) is local_dir_entity + @pytest.mark.parametrize("name", [".foo", "foo.", ".foo/", "foo./"]) def test_dereferencing_equivalent_id(test_data_dir, name):