From 481d8f50198eebced2b2f4d183b2f0b2e9cce6f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=B6nch?= Date: Wed, 22 Jun 2022 17:03:43 +0200 Subject: [PATCH] fix traverser-error on unresolved top_level_dir paths This commit fixes insufficient path handling in the case of an unresolved top_level_dir path, e.g. '/home/dataset0/../dataset0/subdataset1', and adds a regression test for the proper handling of unresolved paths --- .../pipeline/provider/datasettraverse.py | 2 +- .../provider/tests/test_datasettraverse.py | 14 +++++++++++++- datalad_metalad/pipeline/tests/__init__.py | 0 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 datalad_metalad/pipeline/tests/__init__.py diff --git a/datalad_metalad/pipeline/provider/datasettraverse.py b/datalad_metalad/pipeline/provider/datasettraverse.py index e3be42c7..e20b429e 100644 --- a/datalad_metalad/pipeline/provider/datasettraverse.py +++ b/datalad_metalad/pipeline/provider/datasettraverse.py @@ -103,7 +103,7 @@ def __init__(self, raise ValueError(f"{item_type.lower()} is not a known item_type. " f"Known types are: {', '.join(known_types)}") - self.top_level_dir = Path(top_level_dir).absolute() + self.top_level_dir = Path(top_level_dir).absolute().resolve() self.item_set = self.name_to_item_set[item_type.lower()] self.traverse_sub_datasets = traverse_sub_datasets self.root_dataset = require_dataset(self.top_level_dir, diff --git a/datalad_metalad/pipeline/provider/tests/test_datasettraverse.py b/datalad_metalad/pipeline/provider/tests/test_datasettraverse.py index 4545c442..604a07cb 100644 --- a/datalad_metalad/pipeline/provider/tests/test_datasettraverse.py +++ b/datalad_metalad/pipeline/provider/tests/test_datasettraverse.py @@ -11,8 +11,9 @@ @with_tempfile(mkdir=True) -def test_relative_top_level_dir(temp_dir: str): +def test_relative_and_unresolved_top_level_dir(temp_dir: str): relative_path_str = "./some/path/dataset_0" + unresolved_path = "./some/path/../path/dataset_0" dataset_path = Path(temp_dir) / relative_path_str dataset_path.mkdir(parents=True) @@ -20,11 +21,22 @@ def test_relative_top_level_dir(temp_dir: str): old_path = Path.cwd() os.chdir(str(temp_dir)) + + # check relative paths traverser = DatasetTraverser( top_level_dir=relative_path_str, item_type="both" ) assert_equal(traverser.fs_base_path, dataset_path) + # check unresolved paths + traverser = DatasetTraverser( + top_level_dir=Path(temp_dir) / unresolved_path, + item_type="both" + ) + + tuple(traverser.next_object()) + assert_equal(traverser.fs_base_path, dataset_path.resolve()) + # prevent teardown error due to modified working directory os.chdir(str(old_path)) diff --git a/datalad_metalad/pipeline/tests/__init__.py b/datalad_metalad/pipeline/tests/__init__.py new file mode 100644 index 00000000..e69de29b