|
5 | 5 | import re
|
6 | 6 | import shutil
|
7 | 7 | import sys
|
| 8 | +import tempfile |
8 | 9 | from io import BytesIO
|
9 | 10 | from pathlib import Path
|
10 | 11 | from typing import Any, BinaryIO
|
@@ -460,42 +461,40 @@ def test_free_type_font_get_mask(font: ImageFont.FreeTypeFont) -> None:
|
460 | 461 | assert mask.size == (108, 13)
|
461 | 462 |
|
462 | 463 |
|
463 |
| -def test_load_raises_if_image_not_found(tmp_path) -> None: |
464 |
| - font_path = tmp_path / "file.font" |
465 |
| - font_path.write_bytes(b"") |
466 |
| - with pytest.raises(OSError) as excinfo: |
467 |
| - ImageFont.load(font_path) |
| 464 | +def test_load_when_image_not_found(tmp_path: Path) -> None: |
| 465 | + tmpfile = tmp_path / "file.font" |
| 466 | + tmpfile.write_bytes(b"") |
| 467 | + tempfile = str(tmpfile) |
| 468 | + with pytest.raises(OSError) as e: |
| 469 | + ImageFont.load(tempfile) |
468 | 470 |
|
469 |
| - pre = tmp_path / "file" |
470 |
| - msg = f"cannot find glyph data file {pre}.{{png|gif|pbm}}" |
471 |
| - assert msg in str(excinfo.value) |
| 471 | + root = os.path.splitext(tempfile)[0] |
| 472 | + assert str(e.value) == f"cannot find glyph data file {root}.{{png|gif|pbm}}" |
472 | 473 |
|
473 | 474 |
|
474 | 475 | def test_load_path_not_found() -> None:
|
475 | 476 | # Arrange
|
476 | 477 | filename = "somefilenamethatdoesntexist.ttf"
|
477 | 478 |
|
478 | 479 | # Act/Assert
|
479 |
| - with pytest.raises(OSError): |
| 480 | + with pytest.raises(OSError) as e: |
480 | 481 | ImageFont.load_path(filename)
|
| 482 | + |
| 483 | + # The file doesn't exist, so don't suggest `load` |
| 484 | + assert filename in str(e.value) |
| 485 | + assert "did you mean" not in str(e.value) |
481 | 486 | with pytest.raises(OSError):
|
482 | 487 | ImageFont.truetype(filename)
|
483 | 488 |
|
484 | 489 |
|
485 |
| -def test_load_path_exisitng_path(tmp_path) -> None: |
486 |
| - # First, the file doens't exist, so we don't suggest `load` |
487 |
| - some_path = tmp_path / "file.ttf" |
488 |
| - with pytest.raises(OSError) as excinfo: |
489 |
| - ImageFont.load_path(str(some_path)) |
490 |
| - assert str(some_path) in str(excinfo.value) |
491 |
| - assert "did you mean" not in str(excinfo.value) |
492 |
| - |
493 |
| - # The file exists, so the error message suggests to use `load` instead |
494 |
| - some_path.write_bytes(b"") |
495 |
| - with pytest.raises(OSError) as excinfo: |
496 |
| - ImageFont.load_path(str(some_path)) |
497 |
| - assert str(some_path) in str(excinfo.value) |
498 |
| - assert " did you mean" in str(excinfo.value) |
| 490 | +def test_load_path_existing_path() -> None: |
| 491 | + with tempfile.NamedTemporaryFile() as tmp: |
| 492 | + with pytest.raises(OSError) as e: |
| 493 | + ImageFont.load_path(tmp.name) |
| 494 | + |
| 495 | + # The file exists, so the error message suggests to use `load` instead |
| 496 | + assert tmp.name in str(e.value) |
| 497 | + assert " did you mean" in str(e.value) |
499 | 498 |
|
500 | 499 |
|
501 | 500 | def test_load_non_font_bytes() -> None:
|
|
0 commit comments