|
"""Test path helpers.""" |
|
|
|
from pathlib import Path |
|
|
|
import pytest |
|
from folding_studio.utils.path_helpers import extract_files, validate_path |
|
|
|
|
|
def test_extract_files(tmp_path: Path): |
|
"""Test extract files.""" |
|
|
|
test_str_paths = [ |
|
tmp_path / "file.txt", |
|
tmp_path / "dir_1/file.txt", |
|
tmp_path / "dir_1/file_2.txt", |
|
tmp_path / "dir_1/dir_11/file.txt", |
|
tmp_path / "dir_2/file.txt", |
|
tmp_path / "dir_2/file_2.txt", |
|
] |
|
for path in test_str_paths: |
|
path.parent.mkdir(exist_ok=True, parents=True) |
|
path.touch() |
|
|
|
test_paths = [ |
|
tmp_path / "file.txt", |
|
tmp_path / "dir_1", |
|
tmp_path / "dir_2/file.txt", |
|
] |
|
|
|
extracted_paths = extract_files(test_paths) |
|
|
|
assert len(extracted_paths) == 4 |
|
|
|
|
|
def test_validate_path(tmp_directory: Path): |
|
with pytest.raises(FileNotFoundError): |
|
validate_path("unknonw_path") |
|
|
|
file_path = tmp_directory / "file.txt" |
|
file_path.touch() |
|
|
|
file_multi_suffix_path = tmp_directory / "file.tar.gz" |
|
file_multi_suffix_path.touch() |
|
|
|
dir_path = tmp_directory / "directory" |
|
dir_path.mkdir() |
|
|
|
with pytest.raises(FileNotFoundError): |
|
validate_path(dir_path, is_file=True) |
|
|
|
with pytest.raises(NotADirectoryError): |
|
validate_path(file_path, is_dir=True) |
|
|
|
with pytest.raises(ValueError): |
|
validate_path(file_path, is_file=True, file_suffix=[".csv"]) |
|
|
|
validate_path(file_path, is_file=True, file_suffix=[".txt"]) |
|
|
|
with pytest.raises(ValueError): |
|
validate_path(file_multi_suffix_path, is_file=True, file_suffix=[".tar"]) |
|
|
|
validate_path(file_multi_suffix_path, is_file=True, file_suffix=[".gz"]) |
|
validate_path(file_multi_suffix_path, is_file=True, file_suffix=[".tar.gz"]) |
|
|