Skip to content

Commit cc845e7

Browse files
committed
TST: use our subprocess helper
1 parent 78657d4 commit cc845e7

File tree

8 files changed

+42
-28
lines changed

8 files changed

+42
-28
lines changed

lib/matplotlib/tests/test_backend_nbagg.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import os
22
from pathlib import Path
3-
import subprocess
3+
44
from tempfile import TemporaryDirectory
55

66
import pytest
77

8+
from matplotlib.testing import subprocess_run_for_testing
9+
810
nbformat = pytest.importorskip('nbformat')
911
pytest.importorskip('nbconvert')
1012
pytest.importorskip('ipykernel')
@@ -17,11 +19,12 @@ def test_ipynb():
1719

1820
with TemporaryDirectory() as tmpdir:
1921
out_path = Path(tmpdir, "out.ipynb")
20-
subprocess.check_call(
22+
subprocess_run_for_testing(
2123
["jupyter", "nbconvert", "--to", "notebook",
2224
"--execute", "--ExecutePreprocessor.timeout=500",
2325
"--output", str(out_path), str(nb_path)],
24-
env={**os.environ, "IPYTHONDIR": tmpdir})
26+
env={**os.environ, "IPYTHONDIR": tmpdir},
27+
check=True)
2528
with out_path.open() as out:
2629
nb = nbformat.read(out, nbformat.current_nbformat)
2730

lib/matplotlib/tests/test_backend_webagg.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import subprocess
21
import os
32
import sys
43
import pytest
54
import matplotlib.backends.backend_webagg_core
65

6+
from matplotlib.testing import subprocess_run_for_testing
7+
78

89
@pytest.mark.parametrize("backend", ["webagg", "nbagg"])
910
def test_webagg_fallback(backend):
@@ -23,9 +24,9 @@ def test_webagg_fallback(backend):
2324
+ "print(plt.get_backend());"
2425
f"assert '{backend}' == plt.get_backend().lower();"
2526
)
26-
ret = subprocess.call([sys.executable, "-c", test_code], env=env)
27-
28-
assert ret == 0
27+
subprocess_run_for_testing(
28+
[sys.executable, "-c", test_code], env=env, check=True
29+
)
2930

3031

3132
def test_webagg_core_no_toolbar():

lib/matplotlib/tests/test_basic.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import builtins
22
import os
3-
import subprocess
43
import sys
54
import textwrap
65

6+
from matplotlib.testing import subprocess_run_for_testing
7+
78

89
def test_simple():
910
assert 1 + 1 == 2
@@ -41,6 +42,7 @@ def test_lazy_imports():
4142
assert 'urllib.request' not in sys.modules
4243
""")
4344

44-
subprocess.check_call(
45+
subprocess_run_for_testing(
4546
[sys.executable, '-c', source],
46-
env={**os.environ, "MPLBACKEND": "", "MATPLOTLIBRC": os.devnull})
47+
env={**os.environ, "MPLBACKEND": "", "MATPLOTLIBRC": os.devnull},
48+
check=True)

lib/matplotlib/tests/test_determinism.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import matplotlib.testing.compare
1313
from matplotlib import pyplot as plt
1414
from matplotlib.testing._markers import needs_ghostscript, needs_usetex
15+
from matplotlib.testing import subprocess_run_for_testing
1516

1617

1718
def _save_figure(objects='mhi', fmt="pdf", usetex=False):
@@ -89,12 +90,13 @@ def test_determinism_check(objects, fmt, usetex):
8990
Output format.
9091
"""
9192
plots = [
92-
subprocess.check_output(
93+
subprocess_run_for_testing(
9394
[sys.executable, "-R", "-c",
9495
f"from matplotlib.tests.test_determinism import _save_figure;"
9596
f"_save_figure({objects!r}, {fmt!r}, {usetex})"],
9697
env={**os.environ, "SOURCE_DATE_EPOCH": "946684800",
97-
"MPLBACKEND": "Agg"})
98+
"MPLBACKEND": "Agg"},
99+
text=False, capture_output=True, check=True).stdout
98100
for _ in range(3)
99101
]
100102
for p in plots[1:]:
@@ -129,10 +131,10 @@ def test_determinism_source_date_epoch(fmt, string):
129131
string : bytes
130132
Timestamp string for 2000-01-01 00:00 UTC.
131133
"""
132-
buf = subprocess.check_output(
134+
buf = subprocess_run_for_testing(
133135
[sys.executable, "-R", "-c",
134136
f"from matplotlib.tests.test_determinism import _save_figure; "
135137
f"_save_figure('', {fmt!r})"],
136138
env={**os.environ, "SOURCE_DATE_EPOCH": "946684800",
137-
"MPLBACKEND": "Agg"})
139+
"MPLBACKEND": "Agg"}, capture_output=True, text=False, check=True).stdout
138140
assert string in buf

lib/matplotlib/tests/test_font_manager.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from pathlib import Path
66
from PIL import Image
77
import shutil
8-
import subprocess
98
import sys
109
import warnings
1110

@@ -17,6 +16,8 @@
1716
json_dump, json_load, get_font, is_opentype_cff_font,
1817
MSUserFontDirectories, _get_fontconfig_fonts, ttfFontProperty)
1918
from matplotlib import cbook, ft2font, pyplot as plt, rc_context, figure as mfigure
19+
from matplotlib.testing import subprocess_run_helper
20+
2021

2122
has_fclist = shutil.which('fc-list') is not None
2223

@@ -275,11 +276,10 @@ def bad_idea(n):
275276

276277
def test_fontcache_thread_safe():
277278
pytest.importorskip('threading')
278-
import inspect
279279

280-
proc = subprocess.run(
281-
[sys.executable, "-c",
282-
inspect.getsource(_test_threading) + '\n_test_threading()']
280+
proc = subprocess_run_helper(
281+
_test_threading,
282+
timeout=10
283283
)
284284
if proc.returncode:
285285
pytest.fail("The subprocess returned with non-zero exit status "

lib/matplotlib/tests/test_matplotlib.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66

77
import matplotlib
8+
from matplotlib.testing import subprocess_run_for_testing
89

910

1011
@pytest.mark.parametrize('version_str, version_tuple', [
@@ -26,7 +27,7 @@ def test_tmpconfigdir_warning(tmp_path):
2627
mode = os.stat(tmp_path).st_mode
2728
try:
2829
os.chmod(tmp_path, 0)
29-
proc = subprocess.run(
30+
proc = subprocess_run_for_testing(
3031
[sys.executable, "-c", "import matplotlib"],
3132
env={**os.environ, "MPLCONFIGDIR": str(tmp_path)},
3233
stderr=subprocess.PIPE, text=True, check=True)
@@ -36,7 +37,7 @@ def test_tmpconfigdir_warning(tmp_path):
3637

3738

3839
def test_importable_with_no_home(tmp_path):
39-
subprocess.run(
40+
subprocess_run_for_testing(
4041
[sys.executable, "-c",
4142
"import pathlib; pathlib.Path.home = lambda *args: 1/0; "
4243
"import matplotlib.pyplot"],
@@ -74,4 +75,6 @@ def test_importable_with__OO():
7475
"import matplotlib.patches as mpatches"
7576
)
7677
cmd = [sys.executable, "-OO", "-c", program]
77-
assert subprocess.call(cmd, env={**os.environ, "MPLBACKEND": ""}) == 0
78+
subprocess_run_for_testing(
79+
cmd, env={**os.environ, "MPLBACKEND": ""}, check=True
80+
)

lib/matplotlib/tests/test_rcparams.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
validate_sketch,
3030
_validate_linestyle,
3131
_listify_validator)
32+
from matplotlib.testing import subprocess_run_for_testing
3233

3334

3435
def test_rcparams(tmp_path):
@@ -524,7 +525,7 @@ def test_backend_fallback_headless(tmp_path):
524525
"DISPLAY": "", "WAYLAND_DISPLAY": "",
525526
"MPLBACKEND": "", "MPLCONFIGDIR": str(tmp_path)}
526527
with pytest.raises(subprocess.CalledProcessError):
527-
subprocess.run(
528+
subprocess_run_for_testing(
528529
[sys.executable, "-c",
529530
"import matplotlib;"
530531
"matplotlib.use('tkagg');"
@@ -540,7 +541,7 @@ def test_backend_fallback_headless(tmp_path):
540541
def test_backend_fallback_headful(tmp_path):
541542
pytest.importorskip("tkinter")
542543
env = {**os.environ, "MPLBACKEND": "", "MPLCONFIGDIR": str(tmp_path)}
543-
backend = subprocess.check_output(
544+
backend = subprocess_run_for_testing(
544545
[sys.executable, "-c",
545546
"import matplotlib as mpl; "
546547
"sentinel = mpl.rcsetup._auto_backend_sentinel; "
@@ -549,7 +550,7 @@ def test_backend_fallback_headful(tmp_path):
549550
"assert mpl.rcParams._get('backend') == sentinel; "
550551
"import matplotlib.pyplot; "
551552
"print(matplotlib.get_backend())"],
552-
env=env, text=True)
553+
env=env, text=True, check=True, capture_output=True).stdout
553554
# The actual backend will depend on what's installed, but at least tkagg is
554555
# present.
555556
assert backend.strip().lower() != "agg"

lib/matplotlib/tests/test_texmanager.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import os
22
from pathlib import Path
33
import re
4-
import subprocess
4+
55
import sys
66

77
import matplotlib.pyplot as plt
88
from matplotlib.texmanager import TexManager
99
from matplotlib.testing._markers import needs_usetex
1010
import pytest
1111

12+
from matplotlib.testing import subprocess_run_for_testing
13+
1214

1315
def test_fontconfig_preamble():
1416
"""Test that the preamble is included in the source."""
@@ -64,11 +66,11 @@ def test_unicode_characters():
6466

6567
@needs_usetex
6668
def test_openin_any_paranoid():
67-
completed = subprocess.run(
69+
completed = subprocess_run_for_testing(
6870
[sys.executable, "-c",
6971
'import matplotlib.pyplot as plt;'
7072
'plt.rcParams.update({"text.usetex": True});'
7173
'plt.title("paranoid");'
7274
'plt.show(block=False);'],
7375
env={**os.environ, 'openin_any': 'p'}, check=True, capture_output=True)
74-
assert completed.stderr == b""
76+
assert completed.stderr == ""

0 commit comments

Comments
 (0)