From 1ccb66176636de3e290b6f07e434056b21eee206 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Fri, 1 Dec 2023 14:02:43 +0100 Subject: [PATCH] Switch pytest fixture from tmpdir to tmp_path tmp_path is the more modern version providing standard pathlib.Path objects and preferred nowadays https://docs.pytest.org/en/7 .4.x/reference/reference.html#tmpdir. --- lib/matplotlib/tests/test_backend_ps.py | 4 ++-- lib/matplotlib/tests/test_figure.py | 5 ++-- lib/matplotlib/tests/test_font_manager.py | 10 ++++---- lib/matplotlib/tests/test_mathtext.py | 4 ++-- lib/matplotlib/tests/test_matplotlib.py | 14 +++++------ lib/matplotlib/tests/test_pyplot.py | 4 ++-- lib/matplotlib/tests/test_rcparams.py | 29 +++++++++++------------ lib/matplotlib/tests/test_style.py | 9 ++++--- 8 files changed, 37 insertions(+), 42 deletions(-) diff --git a/lib/matplotlib/tests/test_backend_ps.py b/lib/matplotlib/tests/test_backend_ps.py index cbf33ccc5a1b..c587a00c0af9 100644 --- a/lib/matplotlib/tests/test_backend_ps.py +++ b/lib/matplotlib/tests/test_backend_ps.py @@ -124,11 +124,11 @@ def test_patheffects(): @needs_usetex @needs_ghostscript -def test_tilde_in_tempfilename(tmpdir): +def test_tilde_in_tempfilename(tmp_path): # Tilde ~ in the tempdir path (e.g. TMPDIR, TMP or TEMP on windows # when the username is very long and windows uses a short name) breaks # latex before https://github.com/matplotlib/matplotlib/pull/5928 - base_tempdir = Path(tmpdir, "short-1") + base_tempdir = tmp_path / "short-1" base_tempdir.mkdir() # Change the path for new tempdirs, which is used internally by the ps # backend to write a file. diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 24d4f1c0f059..99b2602bc4a7 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -1,7 +1,6 @@ import copy from datetime import datetime import io -from pathlib import Path import pickle import platform from threading import Timer @@ -739,8 +738,8 @@ def test_add_artist(fig_test, fig_ref): @pytest.mark.parametrize("fmt", ["png", "pdf", "ps", "eps", "svg"]) -def test_fspath(fmt, tmpdir): - out = Path(tmpdir, f"test.{fmt}") +def test_fspath(fmt, tmp_path): + out = tmp_path / f"test.{fmt}" plt.savefig(out) with out.open("rb") as file: # All the supported formats include the format name (case-insensitive) diff --git a/lib/matplotlib/tests/test_font_manager.py b/lib/matplotlib/tests/test_font_manager.py index ec901452ee20..79121a794d12 100644 --- a/lib/matplotlib/tests/test_font_manager.py +++ b/lib/matplotlib/tests/test_font_manager.py @@ -46,12 +46,11 @@ def test_score_weight(): fontManager.score_weight(400, 400)) -def test_json_serialization(tmpdir): +def test_json_serialization(tmp_path): # Can't open a NamedTemporaryFile twice on Windows, so use a temporary # directory instead. - path = Path(tmpdir, "fontlist.json") - json_dump(fontManager, path) - copy = json_load(path) + json_dump(fontManager, tmp_path / "fontlist.json") + copy = json_load(tmp_path / "fontlist.json") with warnings.catch_warnings(): warnings.filterwarnings('ignore', 'findfont: Font family.*not found') for prop in ({'family': 'STIXGeneral'}, @@ -133,8 +132,7 @@ def test_find_noto(): fig.savefig(BytesIO(), format=fmt) -def test_find_invalid(tmpdir): - tmp_path = Path(tmpdir) +def test_find_invalid(tmp_path): with pytest.raises(FileNotFoundError): get_font(tmp_path / 'non-existent-font-name.ttf') diff --git a/lib/matplotlib/tests/test_mathtext.py b/lib/matplotlib/tests/test_mathtext.py index 7ebc76dabc1d..e3659245d0e7 100644 --- a/lib/matplotlib/tests/test_mathtext.py +++ b/lib/matplotlib/tests/test_mathtext.py @@ -459,8 +459,8 @@ def test_mathtext_fallback(fallback, fontlist): mpl.font_manager.fontManager.ttflist.pop() -def test_math_to_image(tmpdir): - mathtext.math_to_image('$x^2$', str(tmpdir.join('example.png'))) +def test_math_to_image(tmp_path): + mathtext.math_to_image('$x^2$', tmp_path / 'example.png') mathtext.math_to_image('$x^2$', io.BytesIO()) mathtext.math_to_image('$x^2$', io.BytesIO(), color='Maroon') diff --git a/lib/matplotlib/tests/test_matplotlib.py b/lib/matplotlib/tests/test_matplotlib.py index ac1c3455c3d9..9ed8c46615bf 100644 --- a/lib/matplotlib/tests/test_matplotlib.py +++ b/lib/matplotlib/tests/test_matplotlib.py @@ -21,26 +21,26 @@ def test_parse_to_version_info(version_str, version_tuple): reason="chmod() doesn't work as is on Windows") @pytest.mark.skipif(sys.platform != "win32" and os.geteuid() == 0, reason="chmod() doesn't work as root") -def test_tmpconfigdir_warning(tmpdir): +def test_tmpconfigdir_warning(tmp_path): """Test that a warning is emitted if a temporary configdir must be used.""" - mode = os.stat(tmpdir).st_mode + mode = os.stat(tmp_path).st_mode try: - os.chmod(tmpdir, 0) + os.chmod(tmp_path, 0) proc = subprocess.run( [sys.executable, "-c", "import matplotlib"], - env={**os.environ, "MPLCONFIGDIR": str(tmpdir)}, + env={**os.environ, "MPLCONFIGDIR": str(tmp_path)}, stderr=subprocess.PIPE, text=True, check=True) assert "set the MPLCONFIGDIR" in proc.stderr finally: - os.chmod(tmpdir, mode) + os.chmod(tmp_path, mode) -def test_importable_with_no_home(tmpdir): +def test_importable_with_no_home(tmp_path): subprocess.run( [sys.executable, "-c", "import pathlib; pathlib.Path.home = lambda *args: 1/0; " "import matplotlib.pyplot"], - env={**os.environ, "MPLCONFIGDIR": str(tmpdir)}, check=True) + env={**os.environ, "MPLCONFIGDIR": str(tmp_path)}, check=True) def test_use_doc_standard_backends(): diff --git a/lib/matplotlib/tests/test_pyplot.py b/lib/matplotlib/tests/test_pyplot.py index 68a1de24a561..6ebc24c46e29 100644 --- a/lib/matplotlib/tests/test_pyplot.py +++ b/lib/matplotlib/tests/test_pyplot.py @@ -11,14 +11,14 @@ from matplotlib import pyplot as plt -def test_pyplot_up_to_date(tmpdir): +def test_pyplot_up_to_date(tmp_path): pytest.importorskip("black") gen_script = Path(mpl.__file__).parents[2] / "tools/boilerplate.py" if not gen_script.exists(): pytest.skip("boilerplate.py not found") orig_contents = Path(plt.__file__).read_text() - plt_file = tmpdir.join('pyplot.py') + plt_file = tmp_path / 'pyplot.py' plt_file.write_text(orig_contents, 'utf-8') subprocess_run_for_testing( diff --git a/lib/matplotlib/tests/test_rcparams.py b/lib/matplotlib/tests/test_rcparams.py index 65cd823f13a9..e3e10145533d 100644 --- a/lib/matplotlib/tests/test_rcparams.py +++ b/lib/matplotlib/tests/test_rcparams.py @@ -1,6 +1,5 @@ import copy import os -from pathlib import Path import subprocess import sys from unittest import mock @@ -32,14 +31,14 @@ _listify_validator) -def test_rcparams(tmpdir): +def test_rcparams(tmp_path): mpl.rc('text', usetex=False) mpl.rc('lines', linewidth=22) usetex = mpl.rcParams['text.usetex'] linewidth = mpl.rcParams['lines.linewidth'] - rcpath = Path(tmpdir) / 'test_rcparams.rc' + rcpath = tmp_path / 'test_rcparams.rc' rcpath.write_text('lines.linewidth: 33', encoding='utf-8') # test context given dictionary @@ -197,8 +196,8 @@ def test_axes_titlecolor_rcparams(): assert title.get_color() == 'r' -def test_Issue_1713(tmpdir): - rcpath = Path(tmpdir) / 'test_rcparams.rc' +def test_Issue_1713(tmp_path): + rcpath = tmp_path / 'test_rcparams.rc' rcpath.write_text('timezone: UTC', encoding='utf-8') with mock.patch('locale.getpreferredencoding', return_value='UTF-32-BE'): rc = mpl.rc_params_from_file(rcpath, True, False) @@ -522,10 +521,10 @@ def test_rcparams_reset_after_fail(): @pytest.mark.skipif(sys.platform != "linux", reason="Linux only") -def test_backend_fallback_headless(tmpdir): +def test_backend_fallback_headless(tmp_path): env = {**os.environ, "DISPLAY": "", "WAYLAND_DISPLAY": "", - "MPLBACKEND": "", "MPLCONFIGDIR": str(tmpdir)} + "MPLBACKEND": "", "MPLCONFIGDIR": str(tmp_path)} with pytest.raises(subprocess.CalledProcessError): subprocess.run( [sys.executable, "-c", @@ -540,9 +539,9 @@ def test_backend_fallback_headless(tmpdir): @pytest.mark.skipif( sys.platform == "linux" and not _c_internal_utils.display_is_valid(), reason="headless") -def test_backend_fallback_headful(tmpdir): +def test_backend_fallback_headful(tmp_path): pytest.importorskip("tkinter") - env = {**os.environ, "MPLBACKEND": "", "MPLCONFIGDIR": str(tmpdir)} + env = {**os.environ, "MPLBACKEND": "", "MPLCONFIGDIR": str(tmp_path)} backend = subprocess.check_output( [sys.executable, "-c", "import matplotlib as mpl; " @@ -620,12 +619,12 @@ def test_rcparams_legend_loc(value): (0.9, .7), (-0.9, .7), ]) -def test_rcparams_legend_loc_from_file(tmpdir, value): +def test_rcparams_legend_loc_from_file(tmp_path, value): # rcParams['legend.loc'] should be settable from matplotlibrc. # if any of these are not allowed, an exception will be raised. # test for gh issue #22338 - rc_path = tmpdir.join("matplotlibrc") - rc_path.write(f"legend.loc: {value}") + rc_path = tmp_path / "matplotlibrc" + rc_path.write_text(f"legend.loc: {value}") with mpl.rc_context(fname=rc_path): assert mpl.rcParams["legend.loc"] == value @@ -647,8 +646,8 @@ def test_validate_sketch_error(value): @pytest.mark.parametrize("value", ['1, 2, 3', '(1,2,3)']) -def test_rcparams_path_sketch_from_file(tmpdir, value): - rc_path = tmpdir.join("matplotlibrc") - rc_path.write(f"path.sketch: {value}") +def test_rcparams_path_sketch_from_file(tmp_path, value): + rc_path = tmp_path / "matplotlibrc" + rc_path.write_text(f"path.sketch: {value}") with mpl.rc_context(fname=rc_path): assert mpl.rcParams["path.sketch"] == (1, 2, 3) diff --git a/lib/matplotlib/tests/test_style.py b/lib/matplotlib/tests/test_style.py index 96715ac0492b..be038965e33d 100644 --- a/lib/matplotlib/tests/test_style.py +++ b/lib/matplotlib/tests/test_style.py @@ -58,8 +58,8 @@ def test_use(): assert mpl.rcParams[PARAM] == VALUE -def test_use_url(tmpdir): - path = Path(tmpdir, 'file') +def test_use_url(tmp_path): + path = tmp_path / 'file' path.write_text('axes.facecolor: adeade', encoding='utf-8') with temp_style('test', DUMMY_SETTINGS): url = ('file:' @@ -69,10 +69,9 @@ def test_use_url(tmpdir): assert mpl.rcParams['axes.facecolor'] == "#adeade" -def test_single_path(tmpdir): +def test_single_path(tmp_path): mpl.rcParams[PARAM] = 'gray' - temp_file = f'text.{STYLE_EXTENSION}' - path = Path(tmpdir, temp_file) + path = tmp_path / f'text.{STYLE_EXTENSION}' path.write_text(f'{PARAM} : {VALUE}', encoding='utf-8') with style.context(path): assert mpl.rcParams[PARAM] == VALUE