-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests for excluding subfolders by pattern
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
__author__ = 'Lene Preuss <[email protected]>' | ||
|
||
from pathlib import Path | ||
from tempfile import TemporaryDirectory | ||
from typing import Generator | ||
|
||
import pytest | ||
|
||
from duplicate_images.duplicate import files_in_dirs, is_image_file | ||
|
||
NUM_NUMBERED_FILES = 3 | ||
|
||
|
||
@pytest.fixture(name='temp_dir', scope='session') | ||
def top_folder() -> Generator[Path, None, None]: | ||
with TemporaryDirectory() as temp_dir: | ||
yield Path(temp_dir) | ||
|
||
|
||
@pytest.fixture(name='filled_folder', scope='session') | ||
def filled_temp_dir(temp_dir: Path) -> Generator[Path, None, None]: | ||
for i in range(NUM_NUMBERED_FILES): | ||
(temp_dir / str(i)).mkdir() | ||
(temp_dir / str(i) / f'{i}.txt').open('w').close() | ||
yield temp_dir | ||
|
||
|
||
def test_files_in_dirs_finds_created_empty_files(filled_folder: Path) -> None: | ||
found = files_in_dirs([filled_folder]) | ||
assert NUM_NUMBERED_FILES == len(found) | ||
assert sorted(found) == sorted(filled_folder.glob('?/?.txt')) | ||
|
||
|
||
def test_files_in_dirs_ignores_empty_files_if_looking_for_images(filled_folder: Path) -> None: | ||
found = files_in_dirs([filled_folder], is_image_file) | ||
assert not found | ||
|
||
|
||
def test_files_in_dirs_ignores_subdir_matching_number_regex(filled_folder: Path) -> None: | ||
found = files_in_dirs([filled_folder], exclude_regexes=[r'/\d$']) | ||
assert not found | ||
|
||
|
||
def test_files_in_dirs_ignores_subdir_matching_explicit_name(filled_folder: Path) -> None: | ||
assert NUM_NUMBERED_FILES >= 1 | ||
found = files_in_dirs([filled_folder], exclude_regexes=['/1$']) | ||
assert NUM_NUMBERED_FILES - 1 == len(found) | ||
|
||
|
||
def test_files_in_dirs_ignores_subdirs_matching_multiple_names(filled_folder: Path) -> None: | ||
assert NUM_NUMBERED_FILES >= 2 | ||
found = files_in_dirs([filled_folder], exclude_regexes=['/1$', '/2$']) | ||
assert NUM_NUMBERED_FILES - 2 == len(found) | ||
|
||
|
||
def test_files_in_dirs_ignores_patterns_in_file_names(filled_folder: Path) -> None: | ||
assert NUM_NUMBERED_FILES >= 1 | ||
found = files_in_dirs([filled_folder], exclude_regexes=['/1.txt$']) | ||
assert NUM_NUMBERED_FILES == len(found) | ||
|
||
|
||
def test_files_in_dirs_with_arbitrary_condition(filled_folder: Path) -> None: | ||
assert NUM_NUMBERED_FILES >= 2 | ||
found = files_in_dirs([filled_folder], is_file=lambda f: '2.txt' == f.name) | ||
assert 1 == len(found) | ||
assert '2.txt' == found[0].name |