Skip to content
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

TST: Correctly close temporary files #2396

Merged
merged 3 commits into from
Jan 6, 2024
Merged
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
38 changes: 15 additions & 23 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@
GHOSTSCRIPT_BINARY = shutil.which("gs")


def _get_write_target(convert) -> Any:
target = convert
if callable(convert):
with NamedTemporaryFile(suffix=".pdf", delete=False) as temporary:
target = temporary.name
return target


def test_writer_exception_non_binary(tmp_path, caplog):
src = RESOURCE_ROOT / "pdflatex-outline.pdf"

Expand Down Expand Up @@ -223,14 +231,8 @@ def writer_operate(writer: PdfWriter) -> None:
],
)
def test_writer_operations_by_traditional_usage(convert, needs_cleanup):
if callable(convert):
with NamedTemporaryFile(suffix=".pdf", delete=False) as fo:
write_data_here = convert(fo.name)
else:
write_data_here = convert

write_data_here = _get_write_target(convert)
writer = PdfWriter()

writer_operate(writer)

# finally, write "output" to pypdf-output.pdf
Expand All @@ -254,11 +256,7 @@ def test_writer_operations_by_traditional_usage(convert, needs_cleanup):
],
)
def test_writer_operations_by_semi_traditional_usage(convert, needs_cleanup):
if callable(convert):
with NamedTemporaryFile(suffix=".pdf", delete=False) as fo:
write_data_here = convert(fo.name)
else:
write_data_here = convert
write_data_here = _get_write_target(convert)

with PdfWriter() as writer:
writer_operate(writer)
Expand All @@ -283,12 +281,10 @@ def test_writer_operations_by_semi_traditional_usage(convert, needs_cleanup):
(BytesIO(), False),
],
)
def test_writer_operations_by_semi_new_traditional_usage(convert, needs_cleanup):
if callable(convert):
with NamedTemporaryFile(suffix=".pdf", delete=False) as fo:
write_data_here = convert(fo.name)
else:
write_data_here = convert
def test_writer_operations_by_semi_new_traditional_usage(
convert, needs_cleanup
):
write_data_here = _get_write_target(convert)

with PdfWriter() as writer:
writer_operate(writer)
Expand All @@ -309,11 +305,7 @@ def test_writer_operations_by_semi_new_traditional_usage(convert, needs_cleanup)
],
)
def test_writer_operation_by_new_usage(convert, needs_cleanup):
if callable(convert):
with NamedTemporaryFile(suffix=".pdf", delete=False) as fo:
write_data_here = convert(fo.name)
else:
write_data_here = convert
write_data_here = _get_write_target(convert)

# This includes write "output" to pypdf-output.pdf
with PdfWriter(write_data_here) as writer:
Expand Down
Loading