|
12 | 12 | from matplotlib import cm, rcParams
|
13 | 13 | from matplotlib.backends.backend_pdf import PdfPages
|
14 | 14 | from matplotlib import pyplot as plt
|
| 15 | +from matplotlib.testing.determinism import _test_source_date_epoch, _test_determinism |
15 | 16 | from matplotlib.testing.decorators import (image_comparison, knownfailureif,
|
16 | 17 | cleanup)
|
17 | 18 |
|
@@ -111,133 +112,37 @@ def test_composite_image():
|
111 | 112 |
|
112 | 113 | @cleanup
|
113 | 114 | 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 |
| - |
| 115 | + """Test SOURCE_DATE_EPOCH support for PDF output""" |
| 116 | + _test_source_date_epoch("pdf", b"/CreationDate (D:20000101000000Z)") |
212 | 117 |
|
213 | 118 | @cleanup
|
214 | 119 | def test_determinism_plain():
|
215 | 120 | """Test for reproducible PDF output: simple figure"""
|
216 |
| - _test_determinism() |
| 121 | + _test_determinism('', format="pdf") |
217 | 122 |
|
218 | 123 |
|
219 | 124 | @cleanup
|
220 | 125 | def test_determinism_images():
|
221 | 126 | """Test for reproducible PDF output: figure with different images"""
|
222 |
| - _test_determinism('i') |
| 127 | + _test_determinism('i', format="pdf") |
223 | 128 |
|
224 | 129 |
|
225 | 130 | @cleanup
|
226 | 131 | def test_determinism_hatches():
|
227 | 132 | """Test for reproducible PDF output: figure with different hatches"""
|
228 |
| - _test_determinism('h') |
| 133 | + _test_determinism('h', format="pdf") |
229 | 134 |
|
230 | 135 |
|
231 | 136 | @cleanup
|
232 | 137 | def test_determinism_markers():
|
233 | 138 | """Test for reproducible PDF output: figure with different markers"""
|
234 |
| - _test_determinism('m') |
| 139 | + _test_determinism('m', format="pdf") |
235 | 140 |
|
236 | 141 |
|
237 | 142 | @cleanup
|
238 | 143 | def test_determinism_all():
|
239 | 144 | """Test for reproducible PDF output"""
|
240 |
| - _test_determinism('mhi') |
| 145 | + _test_determinism(format="pdf") |
241 | 146 |
|
242 | 147 |
|
243 | 148 | @image_comparison(baseline_images=['hatching_legend'],
|
|
0 commit comments