File size: 1,772 Bytes
44459bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""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"])