From b22bb88fb63455d7dce4129e18f91911779b8533 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Thu, 28 Mar 2024 07:17:45 +0100 Subject: [PATCH] Backport PR #27985: TST: Remove superfluous chdir from tests --- lib/matplotlib/tests/test_backend_pdf.py | 32 +++++++++++++----------- lib/matplotlib/tests/test_backend_pgf.py | 32 +++++++++++++----------- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/lib/matplotlib/tests/test_backend_pdf.py b/lib/matplotlib/tests/test_backend_pdf.py index 68ce02cc366c..c697821d7273 100644 --- a/lib/matplotlib/tests/test_backend_pdf.py +++ b/lib/matplotlib/tests/test_backend_pdf.py @@ -81,43 +81,47 @@ def test_multipage_properfinalize(): def test_multipage_keep_empty(tmp_path): - os.chdir(tmp_path) - # test empty pdf files # an empty pdf is left behind with keep_empty unset - with pytest.warns(mpl.MatplotlibDeprecationWarning), PdfPages("a.pdf") as pdf: + fn = tmp_path / "a.pdf" + with pytest.warns(mpl.MatplotlibDeprecationWarning), PdfPages(fn) as pdf: pass - assert os.path.exists("a.pdf") + assert fn.exists() # an empty pdf is left behind with keep_empty=True + fn = tmp_path / "b.pdf" with pytest.warns(mpl.MatplotlibDeprecationWarning), \ - PdfPages("b.pdf", keep_empty=True) as pdf: + PdfPages(fn, keep_empty=True) as pdf: pass - assert os.path.exists("b.pdf") + assert fn.exists() # an empty pdf deletes itself afterwards with keep_empty=False - with PdfPages("c.pdf", keep_empty=False) as pdf: + fn = tmp_path / "c.pdf" + with PdfPages(fn, keep_empty=False) as pdf: pass - assert not os.path.exists("c.pdf") + assert not fn.exists() # test pdf files with content, they should never be deleted # a non-empty pdf is left behind with keep_empty unset - with PdfPages("d.pdf") as pdf: + fn = tmp_path / "d.pdf" + with PdfPages(fn) as pdf: pdf.savefig(plt.figure()) - assert os.path.exists("d.pdf") + assert fn.exists() # a non-empty pdf is left behind with keep_empty=True + fn = tmp_path / "e.pdf" with pytest.warns(mpl.MatplotlibDeprecationWarning), \ - PdfPages("e.pdf", keep_empty=True) as pdf: + PdfPages(fn, keep_empty=True) as pdf: pdf.savefig(plt.figure()) - assert os.path.exists("e.pdf") + assert fn.exists() # a non-empty pdf is left behind with keep_empty=False - with PdfPages("f.pdf", keep_empty=False) as pdf: + fn = tmp_path / "f.pdf" + with PdfPages(fn, keep_empty=False) as pdf: pdf.savefig(plt.figure()) - assert os.path.exists("f.pdf") + assert fn.exists() def test_composite_image(): diff --git a/lib/matplotlib/tests/test_backend_pgf.py b/lib/matplotlib/tests/test_backend_pgf.py index a866916c58c6..8a83515f161c 100644 --- a/lib/matplotlib/tests/test_backend_pgf.py +++ b/lib/matplotlib/tests/test_backend_pgf.py @@ -288,43 +288,47 @@ def test_pdf_pages_metadata_check(monkeypatch, system): @needs_pgf_xelatex def test_multipage_keep_empty(tmp_path): - os.chdir(tmp_path) - # test empty pdf files # an empty pdf is left behind with keep_empty unset - with pytest.warns(mpl.MatplotlibDeprecationWarning), PdfPages("a.pdf") as pdf: + fn = tmp_path / "a.pdf" + with pytest.warns(mpl.MatplotlibDeprecationWarning), PdfPages(fn) as pdf: pass - assert os.path.exists("a.pdf") + assert fn.exists() # an empty pdf is left behind with keep_empty=True + fn = tmp_path / "b.pdf" with pytest.warns(mpl.MatplotlibDeprecationWarning), \ - PdfPages("b.pdf", keep_empty=True) as pdf: + PdfPages(fn, keep_empty=True) as pdf: pass - assert os.path.exists("b.pdf") + assert fn.exists() # an empty pdf deletes itself afterwards with keep_empty=False - with PdfPages("c.pdf", keep_empty=False) as pdf: + fn = tmp_path / "c.pdf" + with PdfPages(fn, keep_empty=False) as pdf: pass - assert not os.path.exists("c.pdf") + assert not fn.exists() # test pdf files with content, they should never be deleted # a non-empty pdf is left behind with keep_empty unset - with PdfPages("d.pdf") as pdf: + fn = tmp_path / "d.pdf" + with PdfPages(fn) as pdf: pdf.savefig(plt.figure()) - assert os.path.exists("d.pdf") + assert fn.exists() # a non-empty pdf is left behind with keep_empty=True + fn = tmp_path / "e.pdf" with pytest.warns(mpl.MatplotlibDeprecationWarning), \ - PdfPages("e.pdf", keep_empty=True) as pdf: + PdfPages(fn, keep_empty=True) as pdf: pdf.savefig(plt.figure()) - assert os.path.exists("e.pdf") + assert fn.exists() # a non-empty pdf is left behind with keep_empty=False - with PdfPages("f.pdf", keep_empty=False) as pdf: + fn = tmp_path / "f.pdf" + with PdfPages(fn, keep_empty=False) as pdf: pdf.savefig(plt.figure()) - assert os.path.exists("f.pdf") + assert fn.exists() @needs_pgf_xelatex