Skip to content

Commit

Permalink
TST: Correctly close temporary files (#2396)
Browse files Browse the repository at this point in the history
Improvement for #2379 to use a common method and appropriately close the temporary files.

Fixes #2394
  • Loading branch information
stefan6419846 committed Jan 6, 2024
1 parent e411b76 commit cfd8712
Showing 1 changed file with 15 additions and 23 deletions.
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

0 comments on commit cfd8712

Please sign in to comment.