Skip to content

Commit 478ccb4

Browse files
authored
Merge pull request #8823 from radarhere/test_fp
Simplified test code
2 parents e66ebb6 + c7e3158 commit 478ccb4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+324
-314
lines changed

Tests/check_j2k_overflow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
def test_j2k_overflow(tmp_path: Path) -> None:
1111
im = Image.new("RGBA", (1024, 131584))
12-
target = str(tmp_path / "temp.jpc")
12+
target = tmp_path / "temp.jpc"
1313
with pytest.raises(OSError):
1414
im.save(target)

Tests/check_large_memory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333

3434
def _write_png(tmp_path: Path, xdim: int, ydim: int) -> None:
35-
f = str(tmp_path / "temp.png")
35+
f = tmp_path / "temp.png"
3636
im = Image.new("L", (xdim, ydim), 0)
3737
im.save(f)
3838

Tests/check_large_memory_numpy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
def _write_png(tmp_path: Path, xdim: int, ydim: int) -> None:
2929
dtype = np.uint8
3030
a = np.zeros((xdim, ydim), dtype=dtype)
31-
f = str(tmp_path / "temp.png")
31+
f = tmp_path / "temp.png"
3232
im = Image.fromarray(a, "L")
3333
im.save(f)
3434

Tests/helper.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from collections.abc import Sequence
1414
from functools import lru_cache
1515
from io import BytesIO
16+
from pathlib import Path
1617
from typing import Any, Callable
1718

1819
import pytest
@@ -95,7 +96,10 @@ def assert_image_equal(a: Image.Image, b: Image.Image, msg: str | None = None) -
9596

9697

9798
def assert_image_equal_tofile(
98-
a: Image.Image, filename: str, msg: str | None = None, mode: str | None = None
99+
a: Image.Image,
100+
filename: str | Path,
101+
msg: str | None = None,
102+
mode: str | None = None,
99103
) -> None:
100104
with Image.open(filename) as img:
101105
if mode:
@@ -136,7 +140,7 @@ def assert_image_similar(
136140

137141
def assert_image_similar_tofile(
138142
a: Image.Image,
139-
filename: str,
143+
filename: str | Path,
140144
epsilon: float,
141145
msg: str | None = None,
142146
) -> None:

Tests/test_file_apng.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ def test_apng_sequence_errors(test_file: str) -> None:
345345

346346
def test_apng_save(tmp_path: Path) -> None:
347347
with Image.open("Tests/images/apng/single_frame.png") as im:
348-
test_file = str(tmp_path / "temp.png")
348+
test_file = tmp_path / "temp.png"
349349
im.save(test_file, save_all=True)
350350

351351
with Image.open(test_file) as im:
@@ -375,7 +375,7 @@ def test_apng_save(tmp_path: Path) -> None:
375375

376376

377377
def test_apng_save_alpha(tmp_path: Path) -> None:
378-
test_file = str(tmp_path / "temp.png")
378+
test_file = tmp_path / "temp.png"
379379

380380
im = Image.new("RGBA", (1, 1), (255, 0, 0, 255))
381381
im2 = Image.new("RGBA", (1, 1), (255, 0, 0, 127))
@@ -393,7 +393,7 @@ def test_apng_save_split_fdat(tmp_path: Path) -> None:
393393
# frames with image data spanning multiple fdAT chunks (in this case
394394
# both the default image and first animation frame will span multiple
395395
# data chunks)
396-
test_file = str(tmp_path / "temp.png")
396+
test_file = tmp_path / "temp.png"
397397
with Image.open("Tests/images/old-style-jpeg-compression.png") as im:
398398
frames = [im.copy(), Image.new("RGBA", im.size, (255, 0, 0, 255))]
399399
im.save(
@@ -408,7 +408,7 @@ def test_apng_save_split_fdat(tmp_path: Path) -> None:
408408

409409

410410
def test_apng_save_duration_loop(tmp_path: Path) -> None:
411-
test_file = str(tmp_path / "temp.png")
411+
test_file = tmp_path / "temp.png"
412412
with Image.open("Tests/images/apng/delay.png") as im:
413413
frames = []
414414
durations = []
@@ -471,7 +471,7 @@ def test_apng_save_duration_loop(tmp_path: Path) -> None:
471471

472472

473473
def test_apng_save_disposal(tmp_path: Path) -> None:
474-
test_file = str(tmp_path / "temp.png")
474+
test_file = tmp_path / "temp.png"
475475
size = (128, 64)
476476
red = Image.new("RGBA", size, (255, 0, 0, 255))
477477
green = Image.new("RGBA", size, (0, 255, 0, 255))
@@ -572,7 +572,7 @@ def test_apng_save_disposal(tmp_path: Path) -> None:
572572

573573

574574
def test_apng_save_disposal_previous(tmp_path: Path) -> None:
575-
test_file = str(tmp_path / "temp.png")
575+
test_file = tmp_path / "temp.png"
576576
size = (128, 64)
577577
blue = Image.new("RGBA", size, (0, 0, 255, 255))
578578
red = Image.new("RGBA", size, (255, 0, 0, 255))
@@ -594,7 +594,7 @@ def test_apng_save_disposal_previous(tmp_path: Path) -> None:
594594

595595

596596
def test_apng_save_blend(tmp_path: Path) -> None:
597-
test_file = str(tmp_path / "temp.png")
597+
test_file = tmp_path / "temp.png"
598598
size = (128, 64)
599599
red = Image.new("RGBA", size, (255, 0, 0, 255))
600600
green = Image.new("RGBA", size, (0, 255, 0, 255))
@@ -662,7 +662,7 @@ def test_apng_save_blend(tmp_path: Path) -> None:
662662

663663

664664
def test_apng_save_size(tmp_path: Path) -> None:
665-
test_file = str(tmp_path / "temp.png")
665+
test_file = tmp_path / "temp.png"
666666

667667
im = Image.new("L", (100, 100))
668668
im.save(test_file, save_all=True, append_images=[Image.new("L", (200, 200))])
@@ -686,7 +686,7 @@ def test_seek_after_close() -> None:
686686
def test_different_modes_in_later_frames(
687687
mode: str, default_image: bool, duplicate: bool, tmp_path: Path
688688
) -> None:
689-
test_file = str(tmp_path / "temp.png")
689+
test_file = tmp_path / "temp.png"
690690

691691
im = Image.new("L", (1, 1))
692692
im.save(
@@ -700,7 +700,7 @@ def test_different_modes_in_later_frames(
700700

701701

702702
def test_different_durations(tmp_path: Path) -> None:
703-
test_file = str(tmp_path / "temp.png")
703+
test_file = tmp_path / "temp.png"
704704

705705
with Image.open("Tests/images/apng/different_durations.png") as im:
706706
for _ in range(3):

Tests/test_file_blp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_invalid_file() -> None:
4646

4747

4848
def test_save(tmp_path: Path) -> None:
49-
f = str(tmp_path / "temp.blp")
49+
f = tmp_path / "temp.blp"
5050

5151
for version in ("BLP1", "BLP2"):
5252
im = hopper("P")
@@ -56,7 +56,7 @@ def test_save(tmp_path: Path) -> None:
5656
assert_image_equal(im.convert("RGB"), reloaded)
5757

5858
with Image.open("Tests/images/transparent.png") as im:
59-
f = str(tmp_path / "temp.blp")
59+
f = tmp_path / "temp.blp"
6060
im.convert("P").save(f, blp_version=version)
6161

6262
with Image.open(f) as reloaded:

Tests/test_file_bmp.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
def test_sanity(tmp_path: Path) -> None:
1919
def roundtrip(im: Image.Image) -> None:
20-
outfile = str(tmp_path / "temp.bmp")
20+
outfile = tmp_path / "temp.bmp"
2121

2222
im.save(outfile, "BMP")
2323

@@ -66,15 +66,15 @@ def test_small_palette(tmp_path: Path) -> None:
6666
colors = [0, 0, 0, 125, 125, 125, 255, 255, 255]
6767
im.putpalette(colors)
6868

69-
out = str(tmp_path / "temp.bmp")
69+
out = tmp_path / "temp.bmp"
7070
im.save(out)
7171

7272
with Image.open(out) as reloaded:
7373
assert reloaded.getpalette() == colors
7474

7575

7676
def test_save_too_large(tmp_path: Path) -> None:
77-
outfile = str(tmp_path / "temp.bmp")
77+
outfile = tmp_path / "temp.bmp"
7878
with Image.new("RGB", (1, 1)) as im:
7979
im._size = (37838, 37838)
8080
with pytest.raises(ValueError):
@@ -96,7 +96,7 @@ def test_dpi() -> None:
9696
def test_save_bmp_with_dpi(tmp_path: Path) -> None:
9797
# Test for #1301
9898
# Arrange
99-
outfile = str(tmp_path / "temp.jpg")
99+
outfile = tmp_path / "temp.jpg"
100100
with Image.open("Tests/images/hopper.bmp") as im:
101101
assert im.info["dpi"] == (95.98654816726399, 95.98654816726399)
102102

@@ -112,7 +112,7 @@ def test_save_bmp_with_dpi(tmp_path: Path) -> None:
112112

113113

114114
def test_save_float_dpi(tmp_path: Path) -> None:
115-
outfile = str(tmp_path / "temp.bmp")
115+
outfile = tmp_path / "temp.bmp"
116116
with Image.open("Tests/images/hopper.bmp") as im:
117117
im.save(outfile, dpi=(72.21216100543306, 72.21216100543306))
118118
with Image.open(outfile) as reloaded:
@@ -152,7 +152,7 @@ def test_dib_header_size(header_size: int, path: str) -> None:
152152

153153

154154
def test_save_dib(tmp_path: Path) -> None:
155-
outfile = str(tmp_path / "temp.dib")
155+
outfile = tmp_path / "temp.dib"
156156

157157
with Image.open("Tests/images/clipboard.dib") as im:
158158
im.save(outfile)

Tests/test_file_bufrstub.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_load() -> None:
4343
def test_save(tmp_path: Path) -> None:
4444
# Arrange
4545
im = hopper()
46-
tmpfile = str(tmp_path / "temp.bufr")
46+
tmpfile = tmp_path / "temp.bufr"
4747

4848
# Act / Assert: stub cannot save without an implemented handler
4949
with pytest.raises(OSError):
@@ -79,7 +79,7 @@ def save(self, im: Image.Image, fp: IO[bytes], filename: str) -> None:
7979
im.load()
8080
assert handler.is_loaded()
8181

82-
temp_file = str(tmp_path / "temp.bufr")
82+
temp_file = tmp_path / "temp.bufr"
8383
im.save(temp_file)
8484
assert handler.saved
8585

Tests/test_file_dds.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def test_sanity_ati1_bc4u(image_path: str) -> None:
116116

117117

118118
def test_dx10_bc2(tmp_path: Path) -> None:
119-
out = str(tmp_path / "temp.dds")
119+
out = tmp_path / "temp.dds"
120120
with Image.open(TEST_FILE_DXT3) as im:
121121
im.save(out, pixel_format="BC2")
122122

@@ -129,7 +129,7 @@ def test_dx10_bc2(tmp_path: Path) -> None:
129129

130130

131131
def test_dx10_bc3(tmp_path: Path) -> None:
132-
out = str(tmp_path / "temp.dds")
132+
out = tmp_path / "temp.dds"
133133
with Image.open(TEST_FILE_DXT5) as im:
134134
im.save(out, pixel_format="BC3")
135135

@@ -400,7 +400,7 @@ def test_not_implemented(test_file: str) -> None:
400400

401401

402402
def test_save_unsupported_mode(tmp_path: Path) -> None:
403-
out = str(tmp_path / "temp.dds")
403+
out = tmp_path / "temp.dds"
404404
im = hopper("HSV")
405405
with pytest.raises(OSError, match="cannot write mode HSV as DDS"):
406406
im.save(out)
@@ -416,7 +416,7 @@ def test_save_unsupported_mode(tmp_path: Path) -> None:
416416
],
417417
)
418418
def test_save(mode: str, test_file: str, tmp_path: Path) -> None:
419-
out = str(tmp_path / "temp.dds")
419+
out = tmp_path / "temp.dds"
420420
with Image.open(test_file) as im:
421421
assert im.mode == mode
422422
im.save(out)
@@ -425,15 +425,15 @@ def test_save(mode: str, test_file: str, tmp_path: Path) -> None:
425425

426426

427427
def test_save_unsupported_pixel_format(tmp_path: Path) -> None:
428-
out = str(tmp_path / "temp.dds")
428+
out = tmp_path / "temp.dds"
429429
im = hopper()
430430
with pytest.raises(OSError, match="cannot write pixel format UNKNOWN"):
431431
im.save(out, pixel_format="UNKNOWN")
432432

433433

434434
def test_save_dxt1(tmp_path: Path) -> None:
435435
# RGB
436-
out = str(tmp_path / "temp.dds")
436+
out = tmp_path / "temp.dds"
437437
with Image.open(TEST_FILE_DXT1) as im:
438438
im.convert("RGB").save(out, pixel_format="DXT1")
439439
assert_image_similar_tofile(im, out, 1.84)
@@ -458,7 +458,7 @@ def test_save_dxt1(tmp_path: Path) -> None:
458458

459459
def test_save_dxt3(tmp_path: Path) -> None:
460460
# RGB
461-
out = str(tmp_path / "temp.dds")
461+
out = tmp_path / "temp.dds"
462462
with Image.open(TEST_FILE_DXT3) as im:
463463
im_rgb = im.convert("RGB")
464464
im_rgb.save(out, pixel_format="DXT3")
@@ -481,7 +481,7 @@ def test_save_dxt3(tmp_path: Path) -> None:
481481

482482
def test_save_dxt5(tmp_path: Path) -> None:
483483
# RGB
484-
out = str(tmp_path / "temp.dds")
484+
out = tmp_path / "temp.dds"
485485
with Image.open(TEST_FILE_DXT1) as im:
486486
im.convert("RGB").save(out, pixel_format="DXT5")
487487
assert_image_similar_tofile(im, out, 1.84)
@@ -503,7 +503,7 @@ def test_save_dxt5(tmp_path: Path) -> None:
503503

504504

505505
def test_save_dx10_bc5(tmp_path: Path) -> None:
506-
out = str(tmp_path / "temp.dds")
506+
out = tmp_path / "temp.dds"
507507
with Image.open(TEST_FILE_DX10_BC5_TYPELESS) as im:
508508
im.save(out, pixel_format="BC5")
509509
assert_image_similar_tofile(im, out, 9.56)

Tests/test_file_eps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def test_transparency() -> None:
239239
def test_file_object(tmp_path: Path) -> None:
240240
# issue 479
241241
with Image.open(FILE1) as image1:
242-
with open(str(tmp_path / "temp.eps"), "wb") as fh:
242+
with open(tmp_path / "temp.eps", "wb") as fh:
243243
image1.save(fh, "EPS")
244244

245245

@@ -274,7 +274,7 @@ def test_1(filename: str) -> None:
274274

275275
def test_image_mode_not_supported(tmp_path: Path) -> None:
276276
im = hopper("RGBA")
277-
tmpfile = str(tmp_path / "temp.eps")
277+
tmpfile = tmp_path / "temp.eps"
278278
with pytest.raises(ValueError):
279279
im.save(tmpfile)
280280

0 commit comments

Comments
 (0)