Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 22f71a2

Browse files
author
Alexis Bienvenüe
committed
Tests for determinist PDF output:
- SOURCE_DATE_EPOCH support - reproducible output
1 parent 946ae45 commit 22f71a2

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

lib/matplotlib/tests/test_backend_pdf.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,137 @@ def test_composite_image():
109109
assert len(pdf._file._images.keys()) == 2
110110

111111

112+
@cleanup
113+
def test_source_date_epoch():
114+
# Test SOURCE_DATE_EPOCH support
115+
try:
116+
# save current value of SOURCE_DATE_EPOCH
117+
sde = os.environ.pop('SOURCE_DATE_EPOCH',None)
118+
fig = plt.figure()
119+
ax = fig.add_subplot(1, 1, 1)
120+
x = [1, 2, 3, 4, 5]
121+
ax.plot(x, x)
122+
os.environ['SOURCE_DATE_EPOCH'] = "946684800"
123+
with io.BytesIO() as pdf:
124+
fig.savefig(pdf, format="pdf")
125+
pdf.seek(0)
126+
buff = pdf.read()
127+
assert b"/CreationDate (D:20000101000000Z)" in buff
128+
os.environ.pop('SOURCE_DATE_EPOCH',None)
129+
with io.BytesIO() as pdf:
130+
fig.savefig(pdf, format="pdf")
131+
pdf.seek(0)
132+
buff = pdf.read()
133+
assert not b"/CreationDate (D:20000101000000Z)" in buff
134+
finally:
135+
# Restores SOURCE_DATE_EPOCH
136+
if sde == None:
137+
os.environ.pop('SOURCE_DATE_EPOCH',None)
138+
else:
139+
os.environ['SOURCE_DATE_EPOCH'] = sde
140+
141+
142+
def _test_determinism_save(filename, objects=''):
143+
# save current value of SOURCE_DATE_EPOCH and set it
144+
# to a constant value, so that time difference is not
145+
# taken into account
146+
sde = os.environ.pop('SOURCE_DATE_EPOCH',None)
147+
os.environ['SOURCE_DATE_EPOCH'] = "946684800"
148+
149+
fig = plt.figure()
150+
151+
if 'm' in objects:
152+
# use different markers, to be recorded in the PdfFile object
153+
ax1 = fig.add_subplot(1, 6, 1)
154+
x = range(10)
155+
ax1.plot(x, [1] * 10, marker=u'D')
156+
ax1.plot(x, [2] * 10, marker=u'x')
157+
ax1.plot(x, [3] * 10, marker=u'^')
158+
ax1.plot(x, [4] * 10, marker=u'H')
159+
ax1.plot(x, [5] * 10, marker=u'v')
160+
161+
if 'h' in objects:
162+
# also use different hatch patterns
163+
ax2 = fig.add_subplot(1, 6, 2)
164+
bars = ax2.bar(range(1, 5), range(1, 5)) + \
165+
ax2.bar(range(1, 5), [6] * 4, bottom=range(1, 5))
166+
ax2.set_xticks([1.5, 2.5, 3.5, 4.5])
167+
168+
patterns = ('-', '+', 'x', '\\', '*', 'o', 'O', '.')
169+
for bar, pattern in zip(bars, patterns):
170+
bar.set_hatch(pattern)
171+
172+
if 'i' in objects:
173+
# also use different images
174+
A = [[1, 2, 3], [2, 3, 1], [3, 1, 2]]
175+
fig.add_subplot(1, 6, 3).imshow(A, interpolation='nearest')
176+
A = [[1, 3, 2], [1, 2, 3], [3, 1, 2]]
177+
fig.add_subplot(1, 6, 4).imshow(A, interpolation='bilinear')
178+
A = [[2, 3, 1], [1, 2, 3], [2, 1, 3]]
179+
fig.add_subplot(1, 6, 5).imshow(A, interpolation='bicubic')
180+
181+
x=range(5)
182+
fig.add_subplot(1, 6, 6).plot(x,x)
183+
184+
fig.savefig(filename, format="pdf")
185+
186+
# Restores SOURCE_DATE_EPOCH
187+
if sde == None:
188+
os.environ.pop('SOURCE_DATE_EPOCH',None)
189+
else:
190+
os.environ['SOURCE_DATE_EPOCH'] = sde
191+
192+
193+
def _test_determinism(objects=''):
194+
import sys
195+
from subprocess import check_call
196+
from nose.tools import assert_equal
197+
filename = 'determinism_O%s.pdf' % objects
198+
plots = []
199+
for i in range(3):
200+
check_call([sys.executable, '-R', '-c',
201+
'import matplotlib; '
202+
'matplotlib.use("pdf"); '
203+
'from matplotlib.tests.test_backend_pdf '
204+
'import _test_determinism_save;'
205+
'_test_determinism_save(%r,%r)' % (filename,objects)])
206+
with open(filename, 'rb') as fd:
207+
plots.append(fd.read())
208+
os.unlink(filename)
209+
for p in plots[1:]:
210+
assert_equal(p,plots[0])
211+
212+
213+
@cleanup
214+
def test_determinism_plain():
215+
"""Test for reproducible PDF output: simple figure"""
216+
_test_determinism()
217+
218+
219+
@cleanup
220+
def test_determinism_images():
221+
"""Test for reproducible PDF output: figure with different images"""
222+
_test_determinism('i')
223+
224+
225+
@cleanup
226+
def test_determinism_hatches():
227+
"""Test for reproducible PDF output: figure with different hatches"""
228+
_test_determinism('h')
229+
230+
231+
@cleanup
232+
def test_determinism_markers():
233+
"""Test for reproducible PDF output: figure with different markers"""
234+
_test_determinism('m')
235+
236+
237+
@cleanup
238+
def test_determinism_all():
239+
"""Test for reproducible PDF output"""
240+
_test_determinism('mhi')
241+
242+
112243
@image_comparison(baseline_images=['hatching_legend'],
113244
extensions=['pdf'])
114245
def test_hatching_legend():

0 commit comments

Comments
 (0)