From f076263993cd4b555c192754fb6b44c4e285af37 Mon Sep 17 00:00:00 2001 From: Sean Breckenridge Date: Thu, 1 Jul 2021 06:55:41 -0700 Subject: [PATCH] raise error if passed base is not a zipfile or dir --- my/core/structure.py | 4 ++++ tests/core/test_structure.py | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/my/core/structure.py b/my/core/structure.py index 1c04420..b273f2b 100644 --- a/my/core/structure.py +++ b/my/core/structure.py @@ -118,6 +118,10 @@ def match_structure( zf = zipfile.ZipFile(base) zf.extractall(path=sd) + else: + if not searchdir.is_dir(): + raise NotADirectoryError(f"Expected either a zipfile or a directory, received {searchdir}") + matches: List[Path] = [] possible_targets: List[Path] = [searchdir] diff --git a/tests/core/test_structure.py b/tests/core/test_structure.py index 9250957..dfcef43 100644 --- a/tests/core/test_structure.py +++ b/tests/core/test_structure.py @@ -1,7 +1,10 @@ -from my.core.structure import match_structure +import pytest from pathlib import Path +from my.core.structure import match_structure + + structure_data: Path = Path(__file__).parent / "structure_data" gdpr_expected = ("comments", "messages/index.csv", "profile") @@ -25,3 +28,10 @@ def test_gdpr_unzip() -> None: # make sure the temporary directory this created no longer exists assert not extracted.exists() + +def test_not_directory() -> None: + with pytest.raises(NotADirectoryError, match=r"Expected either a zipfile or a directory"): + with match_structure( + structure_data / "messages/index.csv", expected=gdpr_expected + ): + pass