From 5c6eec62ee33e5d5a286b4af2aaf33afa7f97bf1 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Sun, 3 May 2020 16:17:48 +0100 Subject: [PATCH] start testing get_files --- my/common.py | 2 +- tests/common.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 tests/common.py diff --git a/my/common.py b/my/common.py index 172af06..24dd0ce 100644 --- a/my/common.py +++ b/my/common.py @@ -107,7 +107,7 @@ from .kython.klogging import setup_logger, LazyLogger Paths = Union[Sequence[PathIsh], PathIsh] -def get_files(pp: Paths, glob: str, sort: bool=True) -> Tuple[Path, ...]: +def get_files(pp: Paths, glob: str='*', sort: bool=True) -> Tuple[Path, ...]: """ Helper function to avoid boilerplate. diff --git a/tests/common.py b/tests/common.py new file mode 100644 index 0000000..6486deb --- /dev/null +++ b/tests/common.py @@ -0,0 +1,45 @@ +from pathlib import Path +from my.common import get_files + +import pytest # type: ignore + + +def test_single_file(): + ''' + Regular file path is just returned as is. + ''' + + "Exception if it doesn't exist" + with pytest.raises(Exception): + get_files('/tmp/hpi_test/file.ext') + + + create('/tmp/hpi_test/file.ext') + + ''' + Couple of things: + 1. Return type is a tuple, it's friendlier for hashing/caching + 2. It always return pathlib.Path instead of plain strings + ''' + assert get_files('/tmp/hpi_test/file.ext') == ( + Path('/tmp/hpi_test/file.ext'), + ) + + + + +test_path = Path('/tmp/hpi_test') +def setup(): + teardown() + test_path.mkdir() + + +def teardown(): + import shutil + if test_path.is_dir(): + shutil.rmtree(test_path) + + +from my.common import PathIsh +def create(f: PathIsh) -> None: + Path(f).touch()