Add tests for saving to stdout

This commit is contained in:
Yutao Yuan 2021-08-10 17:56:52 +08:00
parent f55ccd9563
commit 0f11d22cce
2 changed files with 58 additions and 0 deletions

View File

@ -1,5 +1,6 @@
import os import os
import re import re
import sys
from io import BytesIO from io import BytesIO
import pytest import pytest
@ -870,6 +871,33 @@ class TestFileJpeg:
with Image.open("Tests/images/hopper.jpg") as im: with Image.open("Tests/images/hopper.jpg") as im:
assert im.getxmp() == {} assert im.getxmp() == {}
@pytest.mark.parametrize("buffer", (True, False))
def test_save_stdout(self, buffer):
old_stdout = sys.stdout
if buffer:
class MyStdOut:
buffer = BytesIO()
mystdout = MyStdOut()
else:
mystdout = BytesIO()
sys.stdout = mystdout
with Image.open(TEST_FILE) as im:
im.save(sys.stdout, "JPEG")
im_roundtrip = self.roundtrip(im)
# Reset stdout
sys.stdout = old_stdout
if buffer:
mystdout = mystdout.buffer
reloaded = Image.open(mystdout)
assert_image_equal(reloaded, im_roundtrip)
@pytest.mark.skipif(not is_win32(), reason="Windows only") @pytest.mark.skipif(not is_win32(), reason="Windows only")
@skip_unless_feature("jpg") @skip_unless_feature("jpg")

View File

@ -1,3 +1,6 @@
import sys
from io import BytesIO
import pytest import pytest
from PIL import Image from PIL import Image
@ -80,3 +83,30 @@ def test_mimetypes(tmp_path):
f.write("PyCMYK\n128 128\n255") f.write("PyCMYK\n128 128\n255")
with Image.open(path) as im: with Image.open(path) as im:
assert im.get_format_mimetype() == "image/x-portable-anymap" assert im.get_format_mimetype() == "image/x-portable-anymap"
@pytest.mark.parametrize("buffer", (True, False))
def test_save_stdout(buffer):
old_stdout = sys.stdout
if buffer:
class MyStdOut:
buffer = BytesIO()
mystdout = MyStdOut()
else:
mystdout = BytesIO()
sys.stdout = mystdout
with Image.open(TEST_FILE) as im:
im.save(sys.stdout, "PPM")
# Reset stdout
sys.stdout = old_stdout
if buffer:
mystdout = mystdout.buffer
reloaded = Image.open(mystdout)
assert_image_equal_tofile(reloaded, TEST_FILE)