| 1 |
from pathlib import Path |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from cyclopts import types as ct |
| 6 |
from cyclopts.exceptions import ValidationError |
| 7 |
|
| 8 |
|
| 9 |
@pytest.fixture |
| 10 |
def tmp_file(tmp_path): |
| 11 |
file_path = tmp_path / "file.bin" |
| 12 |
file_path.touch() |
| 13 |
return file_path |
| 14 |
|
| 15 |
|
| 16 |
def test_types_existing_path(convert, tmp_file): |
| 17 |
assert tmp_file == convert(ct.ExistingPath, tmp_file) |
| 18 |
|
| 19 |
|
| 20 |
def test_types_existing_path_validation_error(convert, tmp_path): |
| 21 |
with pytest.raises(ValidationError): |
| 22 |
convert(ct.ExistingPath, tmp_path / "foo") |
| 23 |
|
| 24 |
|
| 25 |
def test_types_existing_file(convert, tmp_file): |
| 26 |
assert tmp_file == convert(ct.ExistingFile, tmp_file) |
| 27 |
|
| 28 |
|
| 29 |
def test_types_existing_file_app(app): |
| 30 |
"""https://github.com/BrianPugh/cyclopts/issues/287""" |
| 31 |
|
| 32 |
@app.default |
| 33 |
def main(f: ct.ExistingFile): |
| 34 |
pass |
| 35 |
|
| 36 |
with pytest.raises(ValidationError): |
| 37 |
app(["this-file-does-not-exist"], exit_on_error=False) |
| 38 |
|
| 39 |
|
| 40 |
def test_types_existing_file_app_signature_default(app): |
| 41 |
@app.default |
| 42 |
def main(f: ct.ExistingFile = Path("this-file-does-not-exist")): |
| 43 |
pass |
| 44 |
|
| 45 |
with pytest.raises(ValidationError): |
| 46 |
app([""], exit_on_error=False) |
| 47 |
|
| 48 |
|
| 49 |
def test_types_optional_existing_file_app_signature_default(app): |
| 50 |
@app.default |
| 51 |
def main(f: ct.ExistingFile | None = Path("this-file-does-not-exist")): |
| 52 |
pass |
| 53 |
|
| 54 |
with pytest.raises(ValidationError): |
| 55 |
app([""], exit_on_error=False) |
| 56 |
|
| 57 |
|
| 58 |
def test_types_optional_existing_file_app_signature_default_none(app, assert_parse_args): |
| 59 |
@app.default |
| 60 |
def main(f: ct.ExistingFile | None = None): |
| 61 |
pass |
| 62 |
|
| 63 |
assert_parse_args(main, "") |
| 64 |
|
| 65 |
|
| 66 |
def test_types_existing_file_app_list(app): |
| 67 |
"""https://github.com/BrianPugh/cyclopts/issues/287""" |
| 68 |
|
| 69 |
@app.default |
| 70 |
def main(f: list[ct.ExistingFile]): |
| 71 |
pass |
| 72 |
|
| 73 |
with pytest.raises(ValidationError): |
| 74 |
app(["this-file-does-not-exist"], exit_on_error=False) |
| 75 |
|
| 76 |
|
| 77 |
def test_types_existing_file_validation_error(convert, tmp_path): |
| 78 |
with pytest.raises(ValidationError): |
| 79 |
convert(ct.ExistingFile, tmp_path) |
| 80 |
|
| 81 |
|
| 82 |
def test_types_existing_directory(convert, tmp_path): |
| 83 |
assert tmp_path == convert(ct.ExistingDirectory, tmp_path) |
| 84 |
|
| 85 |
|
| 86 |
def test_types_existing_directory_validation_error(convert, tmp_file): |
| 87 |
with pytest.raises(ValidationError): |
| 88 |
convert(ct.ExistingDirectory, tmp_file) |
| 89 |
|
| 90 |
|
| 91 |
def test_types_directory(convert, tmp_path): |
| 92 |
assert tmp_path == convert(ct.Directory, tmp_path) |
| 93 |
|
| 94 |
|
| 95 |
def test_types_directory_validation_error(convert, tmp_file): |
| 96 |
with pytest.raises(ValidationError): |
| 97 |
convert(ct.Directory, tmp_file) |
| 98 |
|
| 99 |
|
| 100 |
def test_types_file(convert, tmp_file): |
| 101 |
assert tmp_file == convert(ct.File, tmp_file) |
| 102 |
|
| 103 |
|
| 104 |
def test_types_file_validation_error(convert, tmp_path): |
| 105 |
with pytest.raises(ValidationError): |
| 106 |
convert(ct.File, tmp_path) |
| 107 |
|
| 108 |
|
| 109 |
@pytest.mark.parametrize("action", ["touch", "mkdir"]) |
| 110 |
def test_types_resolved_existing_path(convert, tmp_path, action): |
| 111 |
src = tmp_path / ".." / tmp_path.name / "foo" |
| 112 |
getattr(src, action)() |
| 113 |
assert src.resolve() == convert(ct.ResolvedExistingPath, src) |
| 114 |
|
| 115 |
|
| 116 |
def test_types_resolved_existing_path_list(app, assert_parse_args): |
| 117 |
@app.default |
| 118 |
def main(f: list[ct.ResolvedFile]): |
| 119 |
pass |
| 120 |
|
| 121 |
expected = Path("foo.bin").resolve() |
| 122 |
assert_parse_args(main, "foo.bin", [expected]) |
| 123 |
|
| 124 |
|
| 125 |
def test_types_resolved_existing_path_validation_error(convert, tmp_path): |
| 126 |
with pytest.raises(ValidationError): |
| 127 |
convert(ct.ResolvedExistingPath, tmp_path / "foo") |
| 128 |
|
| 129 |
|
| 130 |
def test_types_resolved_existing_file(convert, tmp_path): |
| 131 |
src = tmp_path / ".." / tmp_path.name / "foo" |
| 132 |
src.touch() |
| 133 |
assert src.resolve() == convert(ct.ResolvedExistingFile, src) |
| 134 |
|
| 135 |
|
| 136 |
def test_types_resolved_existing_file_validation_error(convert, tmp_path): |
| 137 |
with pytest.raises(ValidationError): |
| 138 |
convert(ct.ResolvedExistingFile, tmp_path / "foo") |
| 139 |
|
| 140 |
|
| 141 |
def test_types_resolved_existing_directory(convert, tmp_path): |
| 142 |
src = tmp_path / ".." / tmp_path.name / "foo" |
| 143 |
src.mkdir() |
| 144 |
assert src.resolve() == convert(ct.ResolvedExistingDirectory, src) |
| 145 |
|
| 146 |
|
| 147 |
def test_types_resolved_existing_directory_validation_error(convert, tmp_path): |
| 148 |
src = tmp_path / ".." / tmp_path.name / "foo" |
| 149 |
|
| 150 |
with pytest.raises(ValidationError): |
| 151 |
convert(ct.ResolvedExistingDirectory, src) |
| 152 |
|
| 153 |
|
| 154 |
def test_types_resolved_directory(convert, tmp_path): |
| 155 |
assert tmp_path == convert(ct.ResolvedDirectory, tmp_path / "foo" / "..") |
| 156 |
|
| 157 |
|
| 158 |
def test_types_resolved_directory_validation_error(convert, tmp_file): |
| 159 |
with pytest.raises(ValidationError): |
| 160 |
convert(ct.ResolvedDirectory, tmp_file) |
| 161 |
|
| 162 |
|
| 163 |
def test_types_resolved_file(convert, tmp_path): |
| 164 |
src = tmp_path / ".." / tmp_path.name / "foo" |
| 165 |
src.touch() |
| 166 |
assert src.resolve() == convert(ct.ResolvedFile, src) |
| 167 |
|
| 168 |
|
| 169 |
def test_types_resolved_file_validation_error(convert, tmp_path): |
| 170 |
with pytest.raises(ValidationError): |
| 171 |
convert(ct.ResolvedFile, tmp_path) |
| 172 |
|
| 173 |
|
| 174 |
def test_types_path_resolve_converter(convert, tmp_path): |
| 175 |
"""Tests that ``_path_resolve_converter`` handles things like tuples correctly.""" |
| 176 |
dir1 = tmp_path / "foo" |
| 177 |
dir2 = tmp_path / "bar" |
| 178 |
|
| 179 |
dir1.mkdir() |
| 180 |
dir2.mkdir() |
| 181 |
|
| 182 |
actual = convert(tuple[ct.ResolvedDirectory, ct.ResolvedDirectory], [dir1.as_posix(), dir2.as_posix()]) |
| 183 |
assert (dir1, dir2) == actual |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
@pytest.mark.parametrize( |
| 188 |
"annotation, ext", |
| 189 |
[ |
| 190 |
(ct.BinPath, "bin"), |
| 191 |
(ct.CsvPath, "csv"), |
| 192 |
(ct.TxtPath, "txt"), |
| 193 |
(ct.ImagePath, "jpg"), |
| 194 |
(ct.ImagePath, "jpeg"), |
| 195 |
(ct.ImagePath, "png"), |
| 196 |
(ct.Mp4Path, "mp4"), |
| 197 |
(ct.JsonPath, "json"), |
| 198 |
(ct.TomlPath, "toml"), |
| 199 |
(ct.YamlPath, "yaml"), |
| 200 |
], |
| 201 |
) |
| 202 |
def test_types_file_extensions_good(annotation, ext, convert, tmp_path): |
| 203 |
path = tmp_path / f"foo.{ext}" |
| 204 |
convert(annotation, path) |
| 205 |
|
| 206 |
|
| 207 |
@pytest.mark.parametrize( |
| 208 |
"annotation, ext", |
| 209 |
[ |
| 210 |
(ct.ExistingBinPath, "bin"), |
| 211 |
(ct.ExistingCsvPath, "csv"), |
| 212 |
(ct.ExistingTxtPath, "txt"), |
| 213 |
(ct.ExistingImagePath, "jpg"), |
| 214 |
(ct.ExistingImagePath, "jpeg"), |
| 215 |
(ct.ExistingImagePath, "png"), |
| 216 |
(ct.ExistingMp4Path, "mp4"), |
| 217 |
(ct.ExistingJsonPath, "json"), |
| 218 |
(ct.ExistingTomlPath, "toml"), |
| 219 |
(ct.ExistingYamlPath, "yaml"), |
| 220 |
], |
| 221 |
) |
| 222 |
def test_types_file_extensions_exist_good(annotation, ext, convert, tmp_path): |
| 223 |
path = tmp_path / f"foo.{ext}" |
| 224 |
with pytest.raises(ValidationError): |
| 225 |
|
| 226 |
convert(annotation, path) |
| 227 |
|
| 228 |
path.touch() |
| 229 |
convert(annotation, path) |
| 230 |
|
| 231 |
|
| 232 |
@pytest.mark.parametrize( |
| 233 |
"annotation", |
| 234 |
[ |
| 235 |
ct.BinPath, |
| 236 |
ct.CsvPath, |
| 237 |
ct.TxtPath, |
| 238 |
ct.ImagePath, |
| 239 |
ct.Mp4Path, |
| 240 |
ct.JsonPath, |
| 241 |
ct.TomlPath, |
| 242 |
ct.YamlPath, |
| 243 |
], |
| 244 |
) |
| 245 |
def test_types_file_extensions_bad(annotation, convert, tmp_path): |
| 246 |
path = tmp_path / "foo.bar" |
| 247 |
with pytest.raises(ValidationError): |
| 248 |
convert(annotation, path) |
| 249 |
|
| 250 |
|
| 251 |
def test_types_nonexistent_path(convert, tmp_path): |
| 252 |
nonexistent_path = tmp_path / "nonexistent" |
| 253 |
assert nonexistent_path == convert(ct.NonExistentPath, nonexistent_path) |
| 254 |
|
| 255 |
|
| 256 |
def test_types_nonexistent_path_validation_error_file(convert, tmp_file): |
| 257 |
with pytest.raises(ValidationError): |
| 258 |
convert(ct.NonExistentPath, tmp_file) |
| 259 |
|
| 260 |
|
| 261 |
def test_types_nonexistent_path_validation_error_directory(convert, tmp_path): |
| 262 |
with pytest.raises(ValidationError): |
| 263 |
convert(ct.NonExistentPath, tmp_path) |
| 264 |
|
| 265 |
|
| 266 |
def test_types_nonexistent_file(convert, tmp_path): |
| 267 |
nonexistent_file = tmp_path / "nonexistent.txt" |
| 268 |
assert nonexistent_file == convert(ct.NonExistentFile, nonexistent_file) |
| 269 |
|
| 270 |
|
| 271 |
def test_types_nonexistent_file_validation_error(convert, tmp_file): |
| 272 |
with pytest.raises(ValidationError): |
| 273 |
convert(ct.NonExistentFile, tmp_file) |
| 274 |
|
| 275 |
|
| 276 |
def test_types_nonexistent_directory(convert, tmp_path): |
| 277 |
nonexistent_dir = tmp_path / "nonexistent_dir" |
| 278 |
assert nonexistent_dir == convert(ct.NonExistentDirectory, nonexistent_dir) |
| 279 |
|
| 280 |
|
| 281 |
def test_types_nonexistent_directory_validation_error(convert, tmp_path): |
| 282 |
with pytest.raises(ValidationError): |
| 283 |
convert(ct.NonExistentDirectory, tmp_path) |
| 284 |
|
| 285 |
|
| 286 |
@pytest.mark.parametrize( |
| 287 |
"annotation, ext", |
| 288 |
[ |
| 289 |
(ct.NonExistentBinPath, "bin"), |
| 290 |
(ct.NonExistentCsvPath, "csv"), |
| 291 |
(ct.NonExistentTxtPath, "txt"), |
| 292 |
(ct.NonExistentImagePath, "jpg"), |
| 293 |
(ct.NonExistentImagePath, "jpeg"), |
| 294 |
(ct.NonExistentImagePath, "png"), |
| 295 |
(ct.NonExistentMp4Path, "mp4"), |
| 296 |
(ct.NonExistentJsonPath, "json"), |
| 297 |
(ct.NonExistentTomlPath, "toml"), |
| 298 |
(ct.NonExistentYamlPath, "yaml"), |
| 299 |
], |
| 300 |
) |
| 301 |
def test_types_nonexistent_file_extensions_good(annotation, ext, convert, tmp_path): |
| 302 |
path = tmp_path / f"nonexistent.{ext}" |
| 303 |
|
| 304 |
assert path == convert(annotation, path) |
| 305 |
|
| 306 |
|
| 307 |
@pytest.mark.parametrize( |
| 308 |
"annotation, ext", |
| 309 |
[ |
| 310 |
(ct.NonExistentBinPath, "bin"), |
| 311 |
(ct.NonExistentCsvPath, "csv"), |
| 312 |
(ct.NonExistentTxtPath, "txt"), |
| 313 |
(ct.NonExistentImagePath, "jpg"), |
| 314 |
(ct.NonExistentImagePath, "jpeg"), |
| 315 |
(ct.NonExistentImagePath, "png"), |
| 316 |
(ct.NonExistentMp4Path, "mp4"), |
| 317 |
(ct.NonExistentJsonPath, "json"), |
| 318 |
(ct.NonExistentTomlPath, "toml"), |
| 319 |
(ct.NonExistentYamlPath, "yaml"), |
| 320 |
], |
| 321 |
) |
| 322 |
def test_types_nonexistent_file_extensions_validation_error(annotation, ext, convert, tmp_path): |
| 323 |
path = tmp_path / f"existing.{ext}" |
| 324 |
path.touch() |
| 325 |
|
| 326 |
with pytest.raises(ValidationError): |
| 327 |
convert(annotation, path) |
| 328 |
|
| 329 |
|
| 330 |
@pytest.mark.parametrize( |
| 331 |
"annotation", |
| 332 |
[ |
| 333 |
ct.NonExistentBinPath, |
| 334 |
ct.NonExistentCsvPath, |
| 335 |
ct.NonExistentTxtPath, |
| 336 |
ct.NonExistentImagePath, |
| 337 |
ct.NonExistentMp4Path, |
| 338 |
ct.NonExistentJsonPath, |
| 339 |
ct.NonExistentTomlPath, |
| 340 |
ct.NonExistentYamlPath, |
| 341 |
], |
| 342 |
) |
| 343 |
def test_types_nonexistent_file_extensions_bad_extension(annotation, convert, tmp_path): |
| 344 |
path = tmp_path / "nonexistent.bar" |
| 345 |
|
| 346 |
with pytest.raises(ValidationError): |
| 347 |
convert(annotation, path) |