Skip to content

Commit 9993b5b

Browse files
committed
Set environment and rc params as needed for reproducible EPS and SVG output
1 parent 95d4b9d commit 9993b5b

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

pytest_mpl/plugin.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,8 @@ def save_figure(self, item, fig, filename):
557557
savefig_kwargs = compare.kwargs.get('savefig_kwargs', {})
558558
deterministic = compare.kwargs.get('deterministic', False)
559559

560+
original_source_date_epoch = os.environ.get('SOURCE_DATE_EPOCH', None)
561+
560562
if deterministic:
561563

562564
# Make sure we don't modify the original dictionary in case is a common
@@ -568,18 +570,28 @@ def save_figure(self, item, fig, filename):
568570

569571
ext = self._file_extension(item)
570572

573+
extra_rcparams = {}
574+
571575
if ext == 'png':
572576
extra_metadata = {"Software": None}
573577
elif ext == 'pdf':
574578
extra_metadata = {"Creator": None, "Producer": None, "CreationDate": None}
575579
elif ext == 'eps':
576580
extra_metadata = {"Creator": "test"}
581+
os.environ['SOURCE_DATE_EPOCH'] = '1234567890'
577582
elif ext == 'svg':
578583
extra_metadata = {"Date": None}
584+
extra_rcparams["svg.hashsalt"] = "test"
579585

580586
savefig_kwargs['metadata'].update(extra_metadata)
581587

582-
fig.savefig(filename, **savefig_kwargs)
588+
import matplotlib.pyplot as plt
589+
590+
with plt.rc_context(**extra_rcparams):
591+
fig.savefig(filename, **savefig_kwargs)
592+
593+
if original_source_date_epoch is not None:
594+
os.environ['SOURCE_DATE_EPOCH'] = original_source_date_epoch
583595

584596
def compare_image_to_hash_library(self, item, fig, result_dir, summary=None):
585597
hash_comparison_pass = False

0 commit comments

Comments
 (0)