File: test_validator_path.py

package info (click to toggle)
python-cyclopts 3.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,288 kB
  • sloc: python: 11,445; makefile: 24
file content (105 lines) | stat: -rw-r--r-- 2,598 bytes parent folder | download | duplicates (2)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from pathlib import Path

import pytest

from cyclopts import validators


def test_path_type(tmp_path):
    validator = validators.Path()
    validator(Path, Path(tmp_path) / "does-not-exist")  # default configuration doesn't really check much.

    with pytest.raises(TypeError):
        validator(Path, "this is a string.")  # pyright: ignore[reportArgumentType]


def test_path_exists(tmp_path):
    validator = validators.Path(exists=True)
    validator(Path, tmp_path)

    with pytest.raises(ValueError):
        validator(Path, tmp_path / "foo")


def test_path_exists_sequence(tmp_path):
    validator = validators.Path()
    validator(tuple[Path, Path], (tmp_path, tmp_path))
    validator(list[Path], [tmp_path, tmp_path])


def test_path_file_okay(tmp_path):
    validator = validators.Path(file_okay=False)

    folder = tmp_path / "directory"
    folder.mkdir()
    validator(Path, folder)

    file = tmp_path / "file"
    file.touch()

    with pytest.raises(ValueError):
        validator(Path, file)


def test_path_dir_okay(tmp_path):
    validator = validators.Path(dir_okay=False)

    folder = tmp_path / "directory"
    folder.mkdir()
    with pytest.raises(ValueError):
        validator(Path, folder)

    file = tmp_path / "file"
    file.touch()
    validator(Path, file)


def test_path_invalid_values():
    with pytest.raises(ValueError):
        validators.Path(exists=True, dir_okay=False, file_okay=False)


@pytest.mark.parametrize(
    "ext",
    ["mp4", ".mp4", "Mp4", ".Mp4"],
)
def test_path_extension_match_single(ext):
    validator = validators.Path(ext=ext)
    validator(Path, Path("foo.mp4"))


@pytest.mark.parametrize(
    "ext",
    ["png", ".png"],
)
def test_path_extension_not_match_single(ext):
    validator = validators.Path(ext=ext)
    with pytest.raises(ValueError) as e:
        validator(Path, Path("foo.mp4"))
    assert str(e.value) == '"foo.mp4" must have extension "png".'


@pytest.mark.parametrize(
    "ext",
    [
        ("mp4", "avi"),
        (".mp4", ".avi"),
        ("Mp4", ".AVI"),
        (".Mp4", ".avi"),
    ],
)
def test_path_extension_match_multiple(ext):
    validator = validators.Path(ext=ext)
    validator(Path, Path("foo.mp4"))
    validator(Path, Path("foo.avi"))


@pytest.mark.parametrize(
    "ext",
    [(".png", ".jpg"), ("png", "jPG")],
)
def test_path_extension_not_match_multi(ext):
    validator = validators.Path(ext=ext)
    with pytest.raises(ValueError) as e:
        validator(Path, Path("foo.mp4"))
    assert str(e.value) == '"foo.mp4" does not match one of supported extensions {"png", "jpg"}.'