Skip to content

Commit 90dde3f

Browse files
committed
Convert files to PNG before computing hash
1 parent da8364a commit 90dde3f

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

pytest_mpl/plugin.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,21 @@ def generate_image_hash(self, item, fig):
453453
compare = get_compare(item)
454454
savefig_kwargs = compare.kwargs.get('savefig_kwargs', {})
455455

456-
imgdata = io.BytesIO()
457-
458-
fig.savefig(imgdata, **savefig_kwargs)
456+
ext = self._file_extension(item)
459457

460-
out = _hash_file(imgdata)
461-
imgdata.close()
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'))
462471

463472
close_mpl_figure(fig)
464473
return out

tests/test_pytest_mpl.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -691,9 +691,6 @@ def test_formats(pytester, use_hash_library, passes, file_format):
691691
else:
692692
pytest.skip('Comparing EPS and PDF files requires ghostscript to be installed')
693693

694-
if use_hash_library:
695-
pytest.skip('Using the hash library does not currently work because the hashes are not deterministic')
696-
697694
pytester.makepyfile(
698695
f"""
699696
import pytest

0 commit comments

Comments
 (0)