Skip to content

Commit a520a43

Browse files
authored
Merge pull request #4357 from radarhere/testing
Various test changes
2 parents 6c1553e + 760bc7d commit a520a43

7 files changed

Lines changed: 68 additions & 49 deletions

File tree

Tests/helper.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,8 @@ def assert_tuple_approx_equal(self, actuals, targets, threshold, msg):
205205

206206
def skipKnownBadTest(self, msg=None):
207207
# Skip if PILLOW_RUN_KNOWN_BAD is not true in the environment.
208-
if os.environ.get("PILLOW_RUN_KNOWN_BAD", False):
209-
print(os.environ.get("PILLOW_RUN_KNOWN_BAD", False))
210-
return
211-
212-
self.skipTest(msg or "Known Bad Test")
208+
if not os.environ.get("PILLOW_RUN_KNOWN_BAD", False):
209+
self.skipTest(msg or "Known Bad Test")
213210

214211
def tempfile(self, template):
215212
assert template[:5] in ("temp.", "temp_")
@@ -368,14 +365,6 @@ def is_pypy():
368365
IMCONVERT = "convert"
369366

370367

371-
def distro():
372-
if os.path.exists("/etc/os-release"):
373-
with open("/etc/os-release", "r") as f:
374-
for line in f:
375-
if "ID=" in line:
376-
return line.strip().split("=")[1]
377-
378-
379368
class cached_property:
380369
def __init__(self, func):
381370
self.func = func

Tests/test_file_spider.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ def test_nonstack_file(self):
132132
def test_nonstack_dos(self):
133133
with Image.open(TEST_FILE) as im:
134134
for i, frame in enumerate(ImageSequence.Iterator(im)):
135-
if i > 1:
136-
self.fail("Non-stack DOS file test failed")
135+
self.assertLessEqual(i, 1, "Non-stack DOS file test failed")
137136

138137
# for issue #4093
139138
def test_odd_size(self):

Tests/test_file_tga.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from PIL import Image
66

7-
from .helper import PillowTestCase
7+
from .helper import PillowTestCase, hopper
88

99
_TGA_DIR = os.path.join("Tests", "images", "tga")
1010
_TGA_DIR_COMMON = os.path.join(_TGA_DIR, "common")
@@ -112,6 +112,13 @@ def test_save(self):
112112
with Image.open(out) as test_im:
113113
self.assertEqual(test_im.size, (100, 100))
114114

115+
def test_save_wrong_mode(self):
116+
im = hopper("PA")
117+
out = self.tempfile("temp.tga")
118+
119+
with self.assertRaises(OSError):
120+
im.save(out)
121+
115122
def test_save_id_section(self):
116123
test_file = "Tests/images/rgb32rle.tga"
117124
with Image.open(test_file) as im:

Tests/test_file_webp_animated.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,11 @@ def test_seeking(self):
164164
self.assertEqual(im.info["duration"], dur)
165165
self.assertEqual(im.info["timestamp"], ts)
166166
ts -= dur
167+
168+
def test_seek_errors(self):
169+
with Image.open("Tests/images/iss634.webp") as im:
170+
with self.assertRaises(EOFError):
171+
im.seek(-1)
172+
173+
with self.assertRaises(EOFError):
174+
im.seek(42)

Tests/test_file_xbm.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from PIL import Image
44

5-
from .helper import PillowTestCase
5+
from .helper import PillowTestCase, hopper
66

77
PIL151 = b"""
88
#define basic_width 32
@@ -58,3 +58,20 @@ def test_open_filename_with_underscore(self):
5858
# Assert
5959
self.assertEqual(im.mode, "1")
6060
self.assertEqual(im.size, (128, 128))
61+
62+
def test_save_wrong_mode(self):
63+
im = hopper()
64+
out = self.tempfile("temp.xbm")
65+
66+
with self.assertRaises(OSError):
67+
im.save(out)
68+
69+
def test_hotspot(self):
70+
im = hopper("1")
71+
out = self.tempfile("temp.xbm")
72+
73+
hotspot = (0, 7)
74+
im.save(out, hotspot=hotspot)
75+
76+
with Image.open(out) as reloaded:
77+
self.assertEqual(reloaded.info["hotspot"], hotspot)

Tests/test_image_resample.py

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -502,13 +502,9 @@ def test_passthrough(self):
502502
((40, 50), (10, 0, 50, 50)),
503503
((40, 50), (10, 20, 50, 70)),
504504
]:
505-
try:
506-
res = im.resize(size, Image.LANCZOS, box)
507-
self.assertEqual(res.size, size)
508-
self.assert_image_equal(res, im.crop(box))
509-
except AssertionError:
510-
print(">>>", size, box)
511-
raise
505+
res = im.resize(size, Image.LANCZOS, box)
506+
self.assertEqual(res.size, size)
507+
self.assert_image_equal(res, im.crop(box), ">>> {} {}".format(size, box))
512508

513509
def test_no_passthrough(self):
514510
# When resize is required
@@ -520,15 +516,13 @@ def test_no_passthrough(self):
520516
((40, 50), (10.4, 0.4, 50.4, 50.4)),
521517
((40, 50), (10.4, 20.4, 50.4, 70.4)),
522518
]:
523-
try:
524-
res = im.resize(size, Image.LANCZOS, box)
525-
self.assertEqual(res.size, size)
526-
with self.assertRaisesRegex(AssertionError, r"difference \d"):
527-
# check that the difference at least that much
528-
self.assert_image_similar(res, im.crop(box), 20)
529-
except AssertionError:
530-
print(">>>", size, box)
531-
raise
519+
res = im.resize(size, Image.LANCZOS, box)
520+
self.assertEqual(res.size, size)
521+
with self.assertRaisesRegex(AssertionError, r"difference \d"):
522+
# check that the difference at least that much
523+
self.assert_image_similar(
524+
res, im.crop(box), 20, ">>> {} {}".format(size, box)
525+
)
532526

533527
def test_skip_horizontal(self):
534528
# Can skip resize for one dimension
@@ -541,14 +535,15 @@ def test_skip_horizontal(self):
541535
((40, 50), (10, 0, 50, 90)),
542536
((40, 50), (10, 20, 50, 90)),
543537
]:
544-
try:
545-
res = im.resize(size, flt, box)
546-
self.assertEqual(res.size, size)
547-
# Borders should be slightly different
548-
self.assert_image_similar(res, im.crop(box).resize(size, flt), 0.4)
549-
except AssertionError:
550-
print(">>>", size, box, flt)
551-
raise
538+
res = im.resize(size, flt, box)
539+
self.assertEqual(res.size, size)
540+
# Borders should be slightly different
541+
self.assert_image_similar(
542+
res,
543+
im.crop(box).resize(size, flt),
544+
0.4,
545+
">>> {} {} {}".format(size, box, flt),
546+
)
552547

553548
def test_skip_vertical(self):
554549
# Can skip resize for one dimension
@@ -561,11 +556,12 @@ def test_skip_vertical(self):
561556
((40, 50), (0, 10, 90, 60)),
562557
((40, 50), (20, 10, 90, 60)),
563558
]:
564-
try:
565-
res = im.resize(size, flt, box)
566-
self.assertEqual(res.size, size)
567-
# Borders should be slightly different
568-
self.assert_image_similar(res, im.crop(box).resize(size, flt), 0.4)
569-
except AssertionError:
570-
print(">>>", size, box, flt)
571-
raise
559+
res = im.resize(size, flt, box)
560+
self.assertEqual(res.size, size)
561+
# Borders should be slightly different
562+
self.assert_image_similar(
563+
res,
564+
im.crop(box).resize(size, flt),
565+
0.4,
566+
">>> {} {} {}".format(size, box, flt),
567+
)

Tests/test_imagegrab.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ def test_grab(self):
1515
]:
1616
self.assert_image(im, im.mode, im.size)
1717

18+
im = ImageGrab.grab(bbox=(10, 20, 50, 80))
19+
self.assert_image(im, im.mode, (40, 60))
20+
1821
def test_grabclipboard(self):
1922
if sys.platform == "darwin":
2023
subprocess.call(["screencapture", "-cx"])

0 commit comments

Comments
 (0)