From 547832fd592ee1392533be5bf935c19c690a665f Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 7 Sep 2024 19:49:21 +1000 Subject: [PATCH] Use tempfile.NamedTemporaryFile --- Tests/test_imagefont.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 32cd1db8a..e3d847567 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -461,14 +461,15 @@ def test_free_type_font_get_mask(font: ImageFont.FreeTypeFont) -> None: assert mask.size == (108, 13) -def test_load_when_image_not_found(tmp_path: Path) -> None: - tmpfile = tmp_path / "file.font" - tmpfile.write_bytes(b"") - tempfile = str(tmpfile) +def test_load_when_image_not_found() -> None: + with tempfile.NamedTemporaryFile(delete=False) as tmp: + pass with pytest.raises(OSError) as e: - ImageFont.load(tempfile) + ImageFont.load(tmp.name) - root = os.path.splitext(tempfile)[0] + os.unlink(tmp.name) + + root = os.path.splitext(tmp.name)[0] assert str(e.value) == f"cannot find glyph data file {root}.{{gif|pbm|png}}" @@ -492,8 +493,8 @@ def test_load_path_existing_path() -> None: with pytest.raises(OSError) as e: ImageFont.load_path(tmp.name) - # The file exists, so the error message suggests to use `load` instead - assert tmp.name in str(e.value) + # The file exists, so the error message suggests to use `load` instead + assert tmp.name in str(e.value) assert " did you mean" in str(e.value)