Skip to content

Commit 28b171e

Browse files
committed
Don't convert to PNG for hash calculation
1 parent adcce3c commit 28b171e

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

pytest_mpl/plugin.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -455,19 +455,10 @@ def generate_image_hash(self, item, fig):
455455

456456
ext = self._file_extension(item)
457457

458-
if ext == 'png':
459-
imgdata = io.BytesIO()
460-
fig.savefig(imgdata, **savefig_kwargs)
461-
out = _hash_file(imgdata)
462-
imgdata.close()
463-
else:
464-
# Always convert to PNG to compute hash as some vector graphics
465-
# outputs cannot be made deterministic
466-
from matplotlib.testing.compare import convert as convert_to_png
467-
img_filename = tempfile.mktemp() + f'.{ext}'
468-
fig.savefig(img_filename, **savefig_kwargs)
469-
png_filename = convert_to_png(img_filename, cache=True)
470-
out = _hash_file(open(png_filename, 'rb'))
458+
imgdata = io.BytesIO()
459+
fig.savefig(imgdata, **savefig_kwargs)
460+
out = _hash_file(imgdata)
461+
imgdata.close()
471462

472463
close_mpl_figure(fig)
473464
return out

tests/test_pytest_mpl.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,11 +699,16 @@ def test_formats(pytester, use_hash_library, passes, file_format):
699699

700700
if file_format == 'png':
701701
metadata = '{"Software": None}'
702-
else:
703-
metadata = '{}'
702+
elif file_format == 'pdf':
703+
metadata = '{"Creator": None, "Producer": None, "CreationDate": None}'
704+
elif file_format == 'eps':
705+
metadata = '{"Creator": None}'
706+
elif file_format == 'svg':
707+
metadata = '{"Date": None}'
704708

705709
pytester.makepyfile(
706710
f"""
711+
import os
707712
import pytest
708713
import matplotlib.pyplot as plt
709714
@pytest.mark.mpl_image_compare(baseline_dir=r"{baseline_dir_abs}",
@@ -712,6 +717,13 @@ def test_formats(pytester, use_hash_library, passes, file_format):
712717
savefig_kwargs={{'format': '{file_format}',
713718
'metadata': {metadata}}})
714719
def test_format_{file_format}():
720+
721+
# For reproducible EPS output
722+
os.environ['SOURCE_DATE_EPOCH'] = '1680254601'
723+
724+
# For reproducible SVG output
725+
plt.rcParams['svg.hashsalt'] = 'test'
726+
715727
fig = plt.figure()
716728
ax = fig.add_subplot(1, 1, 1)
717729
ax.plot([{1 if passes else 3}, 2, 3])

0 commit comments

Comments
 (0)