From 741f8d364c3fd2a8555db6be03b6bb72126d5fd5 Mon Sep 17 00:00:00 2001 From: Balint Haller Date: Tue, 20 Apr 2021 11:21:52 +0200 Subject: [PATCH] main: fix bug deleting all data for output path with trailing slash --- .../publisher/remove_deleted_publisher.py | 6 +++- pyproject.toml | 2 +- .../test_remove_deleted_publisher.py | 33 ++++++++++++++++--- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/carte_cli/publisher/remove_deleted_publisher.py b/carte_cli/publisher/remove_deleted_publisher.py index b8a65f5..f8a9cb1 100644 --- a/carte_cli/publisher/remove_deleted_publisher.py +++ b/carte_cli/publisher/remove_deleted_publisher.py @@ -17,8 +17,12 @@ def init(self, conf: ConfigTree) -> None: def _get_datasets_to_delete( self, datasets: Set[str], file_paths: List[str] ) -> Iterator[str]: + prefix_length = len(self.tables_path) + if not self.tables_path.endswith("/"): + prefix_length += 1 + file_ids = [ - path[(len(self.tables_path) + 1) : -(len(".md"))] for path in file_paths + path[prefix_length : -(len(".md"))] for path in file_paths ] for file_path, file_id in zip(file_paths, file_ids): diff --git a/pyproject.toml b/pyproject.toml index f98028a..e305e52 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "carte-cli" -version = "0.3.6" +version = "0.3.7" description = "A static site generator for data catalogs" authors = ["Balint Haller "] license = "GPL-3.0-or-later" diff --git a/tests/publisher/test_remove_deleted_publisher.py b/tests/publisher/test_remove_deleted_publisher.py index 9d5b615..67cdd63 100644 --- a/tests/publisher/test_remove_deleted_publisher.py +++ b/tests/publisher/test_remove_deleted_publisher.py @@ -9,11 +9,11 @@ def test_get_datasets_to_delete(): test_datasets = set( [ - f"db1/dataset1", - f"db1/dataset3", - f"db1/dataset4", - f"db1/dataset5", - f"db2/dataset1", + "db1/dataset1", + "db1/dataset3", + "db1/dataset4", + "db1/dataset5", + "db2/dataset1", ] ) @@ -34,3 +34,26 @@ def test_get_datasets_to_delete(): assert len(to_delete) == 2 assert "test-tables-path/db3/dataset1.md" in to_delete assert "test-tables-path/db2/dataset2.md" in to_delete + + +def test_output_path_with_trailing_slash(): + publisher = RemoveDeletedPublisher() + tables_path = "test-tables-path/" + + publisher.tables_path = tables_path + + test_datasets = set(["db1/dataset1", "db2/dataset1"]) + + test_file_paths = [ + f"{tables_path}db1/dataset1.md", + f"{tables_path}db2/dataset1.md", + f"{tables_path}db2/dataset2.md", + ] + + to_delete = [ + dataset + for dataset in publisher._get_datasets_to_delete(test_datasets, test_file_paths) + ] + + assert len(to_delete) == 1 + assert to_delete[0] == "test-tables-path/db2/dataset2.md"