Skip to content

TST: Remove superfluous chdir from tests #27985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions lib/matplotlib/tests/test_backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
32 changes: 18 additions & 14 deletions lib/matplotlib/tests/test_backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down