| 1 |
from pathlib import Path |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from cyclopts import validators |
| 6 |
|
| 7 |
|
| 8 |
def test_path_type(tmp_path): |
| 9 |
validator = validators.Path() |
| 10 |
validator(Path, Path(tmp_path) / "does-not-exist") |
| 11 |
|
| 12 |
with pytest.raises(TypeError): |
| 13 |
validator(Path, "this is a string.") |
| 14 |
|
| 15 |
|
| 16 |
def test_path_exists(tmp_path): |
| 17 |
validator = validators.Path(exists=True) |
| 18 |
validator(Path, tmp_path) |
| 19 |
|
| 20 |
with pytest.raises(ValueError): |
| 21 |
validator(Path, tmp_path / "foo") |
| 22 |
|
| 23 |
|
| 24 |
def test_path_exists_sequence(tmp_path): |
| 25 |
validator = validators.Path() |
| 26 |
validator(tuple[Path, Path], (tmp_path, tmp_path)) |
| 27 |
validator(list[Path], [tmp_path, tmp_path]) |
| 28 |
|
| 29 |
|
| 30 |
def test_path_file_okay(tmp_path): |
| 31 |
validator = validators.Path(file_okay=False) |
| 32 |
|
| 33 |
folder = tmp_path / "directory" |
| 34 |
folder.mkdir() |
| 35 |
validator(Path, folder) |
| 36 |
|
| 37 |
file = tmp_path / "file" |
| 38 |
file.touch() |
| 39 |
|
| 40 |
with pytest.raises(ValueError): |
| 41 |
validator(Path, file) |
| 42 |
|
| 43 |
|
| 44 |
def test_path_dir_okay(tmp_path): |
| 45 |
validator = validators.Path(dir_okay=False) |
| 46 |
|
| 47 |
folder = tmp_path / "directory" |
| 48 |
folder.mkdir() |
| 49 |
with pytest.raises(ValueError): |
| 50 |
validator(Path, folder) |
| 51 |
|
| 52 |
file = tmp_path / "file" |
| 53 |
file.touch() |
| 54 |
validator(Path, file) |
| 55 |
|
| 56 |
|
| 57 |
def test_path_invalid_values(): |
| 58 |
with pytest.raises(ValueError): |
| 59 |
validators.Path(exists=True, dir_okay=False, file_okay=False) |
| 60 |
|
| 61 |
|
| 62 |
@pytest.mark.parametrize( |
| 63 |
"ext", |
| 64 |
["mp4", ".mp4", "Mp4", ".Mp4"], |
| 65 |
) |
| 66 |
def test_path_extension_match_single(ext): |
| 67 |
validator = validators.Path(ext=ext) |
| 68 |
validator(Path, Path("foo.mp4")) |
| 69 |
|
| 70 |
|
| 71 |
@pytest.mark.parametrize( |
| 72 |
"ext", |
| 73 |
["png", ".png"], |
| 74 |
) |
| 75 |
def test_path_extension_not_match_single(ext): |
| 76 |
validator = validators.Path(ext=ext) |
| 77 |
with pytest.raises(ValueError) as e: |
| 78 |
validator(Path, Path("foo.mp4")) |
| 79 |
assert str(e.value) == '"foo.mp4" must have extension "png".' |
| 80 |
|
| 81 |
|
| 82 |
@pytest.mark.parametrize( |
| 83 |
"ext", |
| 84 |
[ |
| 85 |
("mp4", "avi"), |
| 86 |
(".mp4", ".avi"), |
| 87 |
("Mp4", ".AVI"), |
| 88 |
(".Mp4", ".avi"), |
| 89 |
], |
| 90 |
) |
| 91 |
def test_path_extension_match_multiple(ext): |
| 92 |
validator = validators.Path(ext=ext) |
| 93 |
validator(Path, Path("foo.mp4")) |
| 94 |
validator(Path, Path("foo.avi")) |
| 95 |
|
| 96 |
|
| 97 |
@pytest.mark.parametrize( |
| 98 |
"ext", |
| 99 |
[(".png", ".jpg"), ("png", "jPG")], |
| 100 |
) |
| 101 |
def test_path_extension_not_match_multi(ext): |
| 102 |
validator = validators.Path(ext=ext) |
| 103 |
with pytest.raises(ValueError) as e: |
| 104 |
validator(Path, Path("foo.mp4")) |
| 105 |
assert str(e.value) == '"foo.mp4" does not match one of supported extensions {"png", "jpg"}.' |