Skip to content

Commit

Permalink
Use monkeypatch
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Dec 28, 2024
1 parent b285979 commit 1c97cc9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 12 deletions.
8 changes: 2 additions & 6 deletions Tests/test_file_png.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,23 +772,19 @@ def test_seek(self) -> None:
im.seek(1)

@pytest.mark.parametrize("buffer", (True, False))
def test_save_stdout(self, buffer: bool) -> None:
old_stdout = sys.stdout
def test_save_stdout(self, buffer: bool, monkeypatch: pytest.MonkeyPatch) -> None:
b = BytesIO()

class MyStdOut:
buffer = b

mystdout = cast(BytesIO, MyStdOut()) if buffer else b

sys.stdout = mystdout
monkeypatch.setattr(sys, "stdout", mystdout)

with Image.open(TEST_PNG_FILE) as im:
im.save(sys.stdout, "PNG")

# Reset stdout
sys.stdout = old_stdout

with Image.open(b) as reloaded:
assert_image_equal_tofile(reloaded, TEST_PNG_FILE)

Expand Down
8 changes: 2 additions & 6 deletions Tests/test_file_ppm.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,22 +368,18 @@ def test_mimetypes(tmp_path: Path) -> None:


@pytest.mark.parametrize("buffer", (True, False))
def test_save_stdout(buffer: bool) -> None:
old_stdout = sys.stdout
def test_save_stdout(buffer: bool, monkeypatch: pytest.MonkeyPatch) -> None:
b = BytesIO()

class MyStdOut:
buffer = b

mystdout = cast(BytesIO, MyStdOut()) if buffer else b

sys.stdout = mystdout
monkeypatch.setattr(sys, "stdout", mystdout)

with Image.open(TEST_FILE) as im:
im.save(sys.stdout, "PPM")

# Reset stdout
sys.stdout = old_stdout

with Image.open(b) as reloaded:
assert_image_equal_tofile(reloaded, TEST_FILE)

0 comments on commit 1c97cc9

Please sign in to comment.