From 3f729a14a95f1ae255aa9d5aa1ea48680e6b8716 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Wed, 27 Mar 2024 23:06:38 -0400 Subject: [PATCH] TST: Remove superfluous chdir from tests These tests don't chdir back to the original directory on exit, so anything using the current directory would be in the temporary directory of this test. Most notably, any image comparison results would be dumped in that temporary directory instead of the usual `result_images` in the current directory. --- 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 ae17c7a4a4e3..ad565ea9e81b 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