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

Skip to content

Commit 317f6f3

Browse files
changes to the test_backend_pgf to work according to the flag
1 parent 2d7e0fb commit 317f6f3

1 file changed

Lines changed: 39 additions & 13 deletions

File tree

lib/matplotlib/tests/test_backend_pgf.py

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from matplotlib.testing.compare import compare_images, ImageComparisonFailure
1515
from matplotlib.testing.decorators import image_comparison, _image_directories
1616
from matplotlib.backends.backend_pgf import PdfPages, common_texification
17+
import contextlib
1718

1819
baseline_dir, result_dir = _image_directories(lambda: 'dummy func')
1920

@@ -51,16 +52,38 @@ def _has_sfmath():
5152
and subprocess.run(["kpsewhich", "sfmath.sty"],
5253
stdout=subprocess.PIPE).returncode == 0)
5354

54-
55-
def compare_figure(fname, savefig_kwargs={}, tol=0):
55+
def create_baseline(fname):
56+
src_path = result_dir / fname
57+
dest_file = Path(baseline_dir / src_path.name)
58+
try:
59+
if dest_file.exists():
60+
return dest_file
61+
Path(dest_file).parent.mkdir(parents=True, exist_ok=True)
62+
try:
63+
os.symlink(src_path, dest_file)
64+
except OSError: # On Windows, symlink *may* be unavailable.
65+
shutil.copyfile(src_path, dest_file)
66+
except OSError as err:
67+
raise ImageComparisonFailure(
68+
f"Missing baseline image {dest_file} because the "
69+
f"following file cannot be accessed: "
70+
f"{src_path}") from err
71+
return dest_file
72+
73+
74+
# Cannot use Image Comparison decorator because rc_context calls save_figure before releasing memory whereas image
75+
# decorator is returning it afterwards.
76+
def compare_figure(fname, savefig_kwargs={}, tol=0, generating=False):
5677
actual = os.path.join(result_dir, fname)
5778
plt.savefig(actual, **savefig_kwargs)
58-
59-
expected = os.path.join(result_dir, "expected_%s" % fname)
60-
shutil.copyfile(os.path.join(baseline_dir, fname), expected)
61-
err = compare_images(expected, actual, tol=tol)
62-
if err:
63-
raise ImageComparisonFailure(err)
79+
if generating:
80+
expected_creation_path = create_baseline(fname)
81+
else:
82+
expected = os.path.join(result_dir, "expected_%s" % fname)
83+
shutil.copyfile(os.path.join(baseline_dir, fname), expected)
84+
err = compare_images(expected, actual, tol=tol)
85+
if err:
86+
raise ImageComparisonFailure(err)
6487

6588

6689
def create_figure():
@@ -135,7 +158,8 @@ def test_pdflatex():
135158
@pytest.mark.skipif(not _has_sfmath(), reason='needs sfmath.sty')
136159
@pytest.mark.style('default')
137160
@pytest.mark.backend('pgf')
138-
def test_rcupdate():
161+
@pytest.mark.generate_images
162+
def test_rcupdate(request):
139163
rc_sets = [{'font.family': 'sans-serif',
140164
'font.size': 30,
141165
'figure.subplot.left': .2,
@@ -152,10 +176,11 @@ def test_rcupdate():
152176
'\\usepackage[T1]{fontenc}'
153177
'\\usepackage{sfmath}')}]
154178
tol = [6, 0]
179+
generate_images = request.config.getoption("--generate_images")
155180
for i, rc_set in enumerate(rc_sets):
156181
with mpl.rc_context(rc_set):
157182
create_figure()
158-
compare_figure('pgf_rcupdate%d.pdf' % (i + 1), tol=tol[i])
183+
compare_figure('pgf_rcupdate%d.pdf' % (i + 1), tol=tol[i], generating=generate_images)
159184

160185

161186
# test backend-side clipping, since large numbers are not supported by TeX
@@ -193,7 +218,8 @@ def test_mixedmode():
193218
@needs_xelatex
194219
@pytest.mark.style('default')
195220
@pytest.mark.backend('pgf')
196-
def test_bbox_inches():
221+
@pytest.mark.generate_images
222+
def test_bbox_inches(request):
197223
rc_xelatex = {'font.family': 'serif',
198224
'pgf.rcfonts': False}
199225
mpl.rcParams.update(rc_xelatex)
@@ -205,10 +231,10 @@ def test_bbox_inches():
205231
ax2 = fig.add_subplot(122)
206232
ax2.plot(range(5))
207233
plt.tight_layout()
208-
234+
generate_images = request.config.getoption("--generate_images")
209235
bbox = ax1.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
210236
compare_figure('pgf_bbox_inches.pdf', savefig_kwargs={'bbox_inches': bbox},
211-
tol=0)
237+
tol=0, generating=generate_images)
212238

213239

214240
@pytest.mark.style('default')

0 commit comments

Comments
 (0)