Skip to content

Commit 3692ded

Browse files
committed
MNT: hoist latex helpers from pgf tests to matplotlib.testing
Move the marks to skip if various latex installs are missing to be visible on other modules.
1 parent 7211646 commit 3692ded

File tree

4 files changed

+47
-27
lines changed

4 files changed

+47
-27
lines changed

lib/matplotlib/backends/backend_ps.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,7 @@ def draw(self, *args, **kwargs):
11301130
_no_output_draw(self.figure)
11311131
return super().draw()
11321132

1133+
11331134
def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
11341135
paper_width, paper_height, orientation):
11351136
"""

lib/matplotlib/testing/__init__.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
import locale
66
import logging
7+
import subprocess
8+
from pathlib import Path
9+
from tempfile import TemporaryDirectory
710

811
import matplotlib as mpl
912
from matplotlib import _api
@@ -44,3 +47,31 @@ def setup():
4447
# are not necessarily the default values as specified in rcsetup.py.
4548
set_font_settings_for_testing()
4649
set_reproducibility_for_testing()
50+
51+
52+
def check_for_pgf(texsystem):
53+
"""
54+
Check if a given TeX system + pgf is available
55+
56+
Parameters
57+
----------
58+
texsystem : str
59+
The executable name to check
60+
"""
61+
with TemporaryDirectory() as tmpdir:
62+
tex_path = Path(tmpdir, "test.tex")
63+
tex_path.write_text(r"""
64+
\documentclass{minimal}
65+
\usepackage{pgf}
66+
\begin{document}
67+
\typeout{pgfversion=\pgfversion}
68+
\makeatletter
69+
\@@end
70+
""")
71+
try:
72+
subprocess.check_call(
73+
[texsystem, "-halt-on-error", str(tex_path)], cwd=tmpdir,
74+
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
75+
except (OSError, subprocess.CalledProcessError):
76+
return False
77+
return True

lib/matplotlib/tests/test_backend_bases.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import re
22

3+
from matplotlib.testing import check_for_pgf
4+
35
from matplotlib.backend_bases import (
46
FigureCanvasBase, LocationEvent, MouseButton, MouseEvent,
57
NavigationToolbar2, RendererBase)
@@ -11,6 +13,9 @@
1113
import numpy as np
1214
import pytest
1315

16+
needs_xelatex = pytest.mark.skipif(not check_for_pgf('xelatex'),
17+
reason='xelatex + pgf is required')
18+
1419

1520
def test_uses_per_path():
1621
id = transforms.Affine2D()
@@ -181,7 +186,9 @@ def test_toolbar_zoompan():
181186
assert ax.get_navigate_mode() == "PAN"
182187

183188

184-
@pytest.mark.parametrize("backend", ['svg', 'pgf', 'ps', 'pdf'])
189+
@pytest.mark.parametrize(
190+
"backend", ['svg', 'ps', 'pdf', pytest.param('pgf', marks=needs_xelatex)]
191+
)
185192
def test_draw(backend):
186193
from matplotlib.figure import Figure
187194
from matplotlib.backends.backend_agg import FigureCanvas

lib/matplotlib/tests/test_backend_pgf.py

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import datetime
22
from io import BytesIO
33
import os
4-
from pathlib import Path
54
import shutil
65
import subprocess
7-
from tempfile import TemporaryDirectory
6+
87

98
import numpy as np
109
import pytest
1110

1211
import matplotlib as mpl
1312
import matplotlib.pyplot as plt
13+
14+
from matplotlib.testing import check_for_pgf
1415
from matplotlib.testing.compare import compare_images, ImageComparisonFailure
1516
from matplotlib.backends.backend_pgf import PdfPages, common_texification
1617
from matplotlib.testing.decorators import (_image_directories,
@@ -20,31 +21,11 @@
2021
baseline_dir, result_dir = _image_directories(lambda: 'dummy func')
2122

2223

23-
def check_for(texsystem):
24-
with TemporaryDirectory() as tmpdir:
25-
tex_path = Path(tmpdir, "test.tex")
26-
tex_path.write_text(r"""
27-
\documentclass{minimal}
28-
\usepackage{pgf}
29-
\begin{document}
30-
\typeout{pgfversion=\pgfversion}
31-
\makeatletter
32-
\@@end
33-
""")
34-
try:
35-
subprocess.check_call(
36-
[texsystem, "-halt-on-error", str(tex_path)], cwd=tmpdir,
37-
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
38-
except (OSError, subprocess.CalledProcessError):
39-
return False
40-
return True
41-
42-
43-
needs_xelatex = pytest.mark.skipif(not check_for('xelatex'),
24+
needs_xelatex = pytest.mark.skipif(not check_for_pgf('xelatex'),
4425
reason='xelatex + pgf is required')
45-
needs_pdflatex = pytest.mark.skipif(not check_for('pdflatex'),
26+
needs_pdflatex = pytest.mark.skipif(not check_for_pgf('pdflatex'),
4627
reason='pdflatex + pgf is required')
47-
needs_lualatex = pytest.mark.skipif(not check_for('lualatex'),
28+
needs_lualatex = pytest.mark.skipif(not check_for_pgf('lualatex'),
4829
reason='lualatex + pgf is required')
4930
needs_ghostscript = pytest.mark.skipif(
5031
"eps" not in mpl.testing.compare.converter,
@@ -341,7 +322,7 @@ def test_unknown_font(caplog):
341322
@pytest.mark.parametrize("texsystem", ("pdflatex", "xelatex", "lualatex"))
342323
@pytest.mark.backend("pgf")
343324
def test_minus_signs_with_tex(fig_test, fig_ref, texsystem):
344-
if not check_for(texsystem):
325+
if not check_for_pgf(texsystem):
345326
pytest.skip(texsystem + ' + pgf is required')
346327
mpl.rcParams["pgf.texsystem"] = texsystem
347328
fig_test.text(.5, .5, "$-1$")

0 commit comments

Comments
 (0)