8000 Merge pull request #27417 from timhoffm/tmp_path · matplotlib/matplotlib@ac74b74 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac74b74

Browse files
authored
Merge pull request #27417 from timhoffm/tmp_path
Switch pytest fixture from tmpdir to tmp_path
2 parents ee241ad + 1ccb661 commit ac74b74

8 files changed

+37
-42
lines changed

lib/matplotlib/tests/test_backend_ps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ def test_patheffects():
124124

125125
@needs_usetex
126126
@needs_ghostscript
127-
def test_tilde_in_tempfilename(tmpdir):
127+
def test_tilde_in_tempfilename(tmp_path):
128128
# Tilde ~ in the tempdir path (e.g. TMPDIR, TMP or TEMP on windows
129129
# when the username is very long and windows uses a short name) breaks
130130
# latex before https://github.com/matplotlib/matplotlib/pull/5928
131-
base_tempdir = Path(tmpdir, "short-1")
131+
base_tempdir = tmp_path / "short-1"
132132
base_tempdir.mkdir()
133133
# Change the path for new tempdirs, which is used internally by the ps
134134
# backend to write a file.

lib/matplotlib/tests/test_figure.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import copy
22
from datetime import datetime
33
import io
4-
from pathlib import Path
54
import pickle
65
import platform
76
from threading import Timer
@@ -739,8 +738,8 @@ def test_add_artist(fig_test, fig_ref):
739738

740739

741740
@pytest.mark.parametrize("fmt", ["png", "pdf", "ps", "eps", "svg"])
742-
def test_fspath(fmt, tmpdir):
743-
out = Path(tmpdir, f"test.{fmt}")
741+
def test_fspath(fmt, tmp_path):
742+
out = tmp_path / f"test.{fmt}"
744743
plt.savefig(out)
745744
with out.open("rb") as file:
746745
# All the supported formats include the format name (case-insensitive)

lib/matplotlib/tests/test_font_manager.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,11 @@ def test_score_weight():
4646
fontManager.score_weight(400, 400))
4747

4848

49-
def test_json_serialization(tmpdir):
49+
def test_json_serialization(tmp_path):
5050
# Can't open a NamedTemporaryFile twice on Windows, so use a temporary
5151
# directory instead.
52-
path = Path(tmpdir, "fontlist.json")
53-
json_dump(fontManager, path)
54-
copy = json_load(path)
52+
json_dump(fontManager, tmp_path / "fontlist.json")
53+
copy = json_load(tmp_path / "fontlist.json")
5554
with warnings.catch_warnings():
5655
warnings.filterwarnings('ignore', 'findfont: Font family.*not found')
5756
for prop in ({'family': 'STIXGeneral'},
@@ -133,8 +132,7 @@ def test_find_noto():
133132
fig.savefig(BytesIO(), format=fmt)
134133

135134

136-
def test_find_invalid(tmpdir):
137-
tmp_path = Path(tmpdir)
135+
def test_find_invalid(tmp_path):
138136

139137
with pytest.raises(FileNotFoundError):
140138
get_font(tmp_path / 'non-existent-font-name.ttf')

lib/matplotlib/tests/test_mathtext.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,8 @@ def test_mathtext_fallback(fallback, fontlist):
459459
mpl.font_manager.fontManager.ttflist.pop()
460460

461461

462-
def test_math_to_image(tmpdir):
463-
mathtext.math_to_image('$x^2$', str(tmpdir.join('example.png')))
462+
def test_math_to_image(tmp_path):
463+
mathtext.math_to_image('$x^2$', tmp_path / 'example.png')
464464
mathtext.math_to_image('$x^2$', io.BytesIO())
465465
mathtext.math_to_image('$x^2$', io.BytesIO(), color='Maroon')
466466

lib/matplotlib/tests/test_matplotlib.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,26 @@ def test_parse_to_version_info(version_str, version_tuple):
2121
reason="chmod() doesn't work as is on Windows")
2222
@pytest.mark.skipif(sys.platform != "win32" and os.geteuid() == 0,
2323
reason="chmod() doesn't work as root")
24-
def test_tmpconfigdir_warning(tmpdir):
24+
def test_tmpconfigdir_warning(tmp_path):
2525
"""Test that a warning is emitted if a temporary configdir must be used."""
26-
mode = os.stat(tmpdir).st_mode
26+
mode = os.stat(tmp_path).st_mode
2727
try:
28-
os.chmod(tmpdir, 0)
28+
os.chmod(tmp_path, 0)
2929
proc = subprocess.run(
3030
[sys.executable, "-c", "import matplotlib"],
31-
env={**os.environ, "MPLCONFIGDIR": str(tmpdir)},
31+
env={**os.environ, "MPLCONFIGDIR": str(tmp_path)},
3232
stderr=subprocess.PIPE, text=True, check=True)
3333
assert "set the MPLCONFIGDIR" in proc.stderr
3434
finally:
35-
os.chmod(tmpdir, mode)
35+
os.chmod(tmp_path, mode)
3636

3737

38-
def test_importable_with_no_home(tmpdir):
38+
def test_importable_with_no_home(tmp_path):
3939
subprocess.run(
4040
[sys.executable, "-c",
4141
"import pathlib; pathlib.Path.home = lambda *args: 1/0; "
4242
"import matplotlib.pyplot"],
43-
env={**os.environ, "MPLCONFIGDIR": str(tmpdir)}, check=True)
43+
env={**os.environ, "MPLCONFIGDIR": str(tmp_path)}, check=True)
4444

4545

4646
def test_use_doc_standard_backends():

lib/matplotlib/tests/test_pyplot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
from matplotlib import pyplot as plt
1212

1313

14-
def test_pyplot_up_to_date(tmpdir):
14+
def test_pyplot_up_to_date(tmp_path):
1515
pytest.importorskip("black")
1616

1717
gen_script = Path(mpl.__file__).parents[2] / "tools/boilerplate.py"
1818
if not gen_script.exists():
1919
pytest.skip("boilerplate.py not found")
2020
orig_contents = Path(plt.__file__).read_text()
21-
plt_file = tmpdir.join('pyplot.py')
21+
plt_file = tmp_path / 'pyplot.py'
2222
plt_file.write_text(orig_contents, 'utf-8')
2323

2424
subprocess_run_for_testing(

lib/matplotlib/tests/test_rcparams.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import copy
22
import os
3-
from pathlib import Path
43
import subprocess
54
import sys
65
from unittest import mock
@@ -32,14 +31,14 @@
3231
_listify_validator)
3332

3433

35-
def test_rcparams(tmpdir):
34+
def test_rcparams(tmp_path):
3635
mpl.rc('text', usetex=False)
3736
mpl.rc('lines', linewidth=22)
3837

3938
usetex = mpl.rcParams['text.usetex']
4039
linewidth = mpl.rcParams['lines.linewidth']
4140

42-
rcpath = Path(tmpdir) / 'test_rcparams.rc'
41+
rcpath = tmp_path / 'test_rcparams.rc'
4342
rcpath.write_text('lines.linewidth: 33', encoding='utf-8')
4443

4544
# test context given dictionary
@@ -197,8 +196,8 @@ def test_axes_titlecolor_rcparams():
197196
assert title.get_color() == 'r'
198197

199198

200-
def test_Issue_1713(tmpdir):
201-
rcpath = Path(tmpdir) / 'test_rcparams.rc'
199+
def test_Issue_1713(tmp_path):
200+
rcpath = tmp_path / 'test_rcparams.rc'
202201
rcpath.write_text('timezone: UTC', encoding='utf-8')
203202
with mock.patch('locale.getpreferredencoding', return_value='UTF-32-BE'):
204203
rc = mpl.rc_params_from_file(rcpath, True, False)
@@ -522,10 +521,10 @@ def test_rcparams_reset_after_fail():
522521

523522

524523
@pytest.mark.skipif(sys.platform != "linux", reason="Linux only")
525-
def test_backend_fallback_headless(tmpdir):
524+
def test_backend_fallback_headless(tmp_path):
526525
env = {**os.environ,
527526
"DISPLAY": "", "WAYLAND_DISPLAY": "",
528-
"MPLBACKEND": "", "MPLCONFIGDIR": str(tmpdir)}
527+
"MPLBACKEND": "", "MPLCONFIGDIR": str(tmp_path)}
529528
with pytest.raises(subprocess.CalledProcessError):
530529
subprocess.run(
531530
[sys.executable, "-c",
@@ -540,9 +539,9 @@ def test_backend_fallback_headless(tmpdir):
540539
@pytest.mark.skipif(
541540
sys.platform == "linux" and not _c_internal_utils.display_is_valid(),
542541
reason="headless")
543-
def test_backend_fallback_headful(tmpdir):
542+
def test_backend_fallback_headful(tmp_path):
544543
pytest.importorskip("tkinter")
545-
env = {**os.environ, "MPLBACKEND": "", "MPLCONFIGDIR": str(tmpdir)}
544+
env = {**os.environ, "MPLBACKEND": "", "MPLCONFIGDIR": str(tmp_path)}
546545
backend = subprocess.check_output(
547546
[sys.executable, "-c",
548547
"import matplotlib as mpl; "
@@ -620,12 +619,12 @@ def test_rcparams_legend_loc(value):
620619
(0.9, .7),
621620
(-0.9, .7),
622621
])
623-
def test_rcparams_legend_loc_from_file(tmpdir, value):
622+
def test_rcparams_legend_loc_from_file(tmp_path, value):
624623
# rcParams['legend.loc'] should be settable from matplotlibrc.
625624
# if any of these are not allowed, an exception will be raised.
626625
# test for gh issue #22338
627-
rc_path = tmpdir.join("matplotlibrc")
628-
rc_path.write(f"legend.loc: {value}")
626+
rc_path = tmp_path / "matplotlibrc"
627+
rc_path.write_text(f"legend.loc: {value}")
629628

630629
with mpl.rc_context(fname=rc_path):
631630
assert mpl.rcParams["legend.loc"] == value
@@ -647,8 +646,8 @@ def test_validate_sketch_error(value):
647646

648647

649648
@pytest.mark.parametrize("value", ['1, 2, 3', '(1,2,3)'])
650-
def test_rcparams_path_sketch_from_file(tmpdir, value):
651-
rc_path = tmpdir.join("matplotlibrc")
652-
rc_path.write(f"path.sketch: {value}")
649+
def test_rcparams_path_sketch_from_file(tmp_path, value):
650+
rc_path = tmp_path / "matplotlibrc"
651+
rc_path.write_text(f"path.sketch: {value}")
653652
with mpl.rc_context(fname=rc_path):
654653
assert mpl.rcParams["path.sketch"] == (1, 2, 3)

lib/matplotlib/tests/test_style.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ def test_use():
5858
assert mpl.rcParams[PARAM] == VALUE
5959

6060

61-
def test_use_url(tmpdir):
62-
path = Path(tmpdir, 'file')
61+
def test_use_url(tmp_path):
62+
path = tmp_path / 'file'
6363
path.write_text('axes.facecolor: adeade', encoding='utf-8')
6464
with temp_style('test', DUMMY_SETTINGS):
6565
url = ('file:'
@@ -69,10 +69,9 @@ def test_use_url(tmpdir):
6969
assert mpl.rcParams['axes.facecolor'] == "#adeade"
7070

7171

72-
def test_single_path(tmpdir):
72+
def test_single_path(tmp_path):
7373
mpl.rcParams[PARAM] = 'gray'
74-
temp_file = f'text.{STYLE_EXTENSION}'
75-
path = Path(tmpdir, temp_file)
74+
path = tmp_path / f'text.{STYLE_EXTENSION}'
7675
path.write_text(f'{PARAM} : {VALUE}', encoding='utf-8')
7776
with style.context(path):
7877
assert mpl.rcParams[PARAM] == VALUE

0 commit comments

Comments
 (0)
0