Skip to content

Commit 5eefbaf

Browse files
DEV: Write to temporary files instead of the working directory (#2379)
The tests tended to be unstable due to parallel access to the same file inside the working directory. This PR attempts to fix this by using temporary files instead.
1 parent 2cdc0d5 commit 5eefbaf

File tree

2 files changed

+65
-54
lines changed

2 files changed

+65
-54
lines changed

tests/bench.py

+23-21
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77
from io import BytesIO
88
from pathlib import Path
9+
from tempfile import NamedTemporaryFile
910

1011
import pytest
1112

@@ -98,27 +99,28 @@ def merge():
9899
writer.set_page_layout("/SinglePage")
99100
writer.page_mode = "/UseThumbs"
100101

101-
write_path = "dont_commit_merged.pdf"
102-
writer.write(write_path)
103-
writer.close()
104-
105-
# Check if outline is correct
106-
reader = PdfReader(write_path)
107-
assert [
108-
el.title for el in reader._get_outline() if isinstance(el, Destination)
109-
] == [
110-
"Foo",
111-
"Bar",
112-
"Baz",
113-
"Foo",
114-
"Bar",
115-
"Baz",
116-
"Foo",
117-
"Bar",
118-
"Baz",
119-
"True",
120-
"An outline item",
121-
]
102+
with NamedTemporaryFile(suffix=".pdf") as target_file:
103+
write_path = target_file.name
104+
writer.write(write_path)
105+
writer.close()
106+
107+
# Check if outline is correct
108+
reader = PdfReader(write_path)
109+
assert [
110+
el.title for el in reader._get_outline() if isinstance(el, Destination)
111+
] == [
112+
"Foo",
113+
"Bar",
114+
"Baz",
115+
"Foo",
116+
"Bar",
117+
"Baz",
118+
"Foo",
119+
"Bar",
120+
"Baz",
121+
"True",
122+
"An outline item",
123+
]
122124

123125

124126
def test_merge(benchmark):

tests/test_writer.py

+42-33
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import subprocess
55
from io import BytesIO
66
from pathlib import Path
7+
from tempfile import NamedTemporaryFile
78
from typing import Any
89

910
import pytest
@@ -213,18 +214,20 @@ def writer_operate(writer: PdfWriter) -> None:
213214
assert k in objects_hash, f"Missing {v}"
214215

215216

216-
tmp_path = "dont_commit_writer.pdf"
217-
218-
219217
@pytest.mark.parametrize(
220-
("write_data_here", "needs_cleanup"),
218+
("convert", "needs_cleanup"),
221219
[
222-
("dont_commit_writer.pdf", True),
223-
(Path("dont_commit_writer.pdf"), True),
220+
(str, True),
221+
(Path, True),
224222
(BytesIO(), False),
225223
],
226224
)
227-
def test_writer_operations_by_traditional_usage(write_data_here, needs_cleanup):
225+
def test_writer_operations_by_traditional_usage(convert, needs_cleanup):
226+
if callable(convert):
227+
write_data_here = convert(NamedTemporaryFile(suffix=".pdf", delete=False).name)
228+
else:
229+
write_data_here = convert
230+
228231
writer = PdfWriter()
229232

230233
writer_operate(writer)
@@ -242,14 +245,19 @@ def test_writer_operations_by_traditional_usage(write_data_here, needs_cleanup):
242245

243246

244247
@pytest.mark.parametrize(
245-
("write_data_here", "needs_cleanup"),
248+
("convert", "needs_cleanup"),
246249
[
247-
("dont_commit_writer.pdf", True),
248-
(Path("dont_commit_writer.pdf"), True),
250+
(str, True),
251+
(Path, True),
249252
(BytesIO(), False),
250253
],
251254
)
252-
def test_writer_operations_by_semi_traditional_usage(write_data_here, needs_cleanup):
255+
def test_writer_operations_by_semi_traditional_usage(convert, needs_cleanup):
256+
if callable(convert):
257+
write_data_here = convert(NamedTemporaryFile(suffix=".pdf", delete=False).name)
258+
else:
259+
write_data_here = convert
260+
253261
with PdfWriter() as writer:
254262
writer_operate(writer)
255263

@@ -266,16 +274,21 @@ def test_writer_operations_by_semi_traditional_usage(write_data_here, needs_clea
266274

267275

268276
@pytest.mark.parametrize(
269-
("write_data_here", "needs_cleanup"),
277+
("convert", "needs_cleanup"),
270278
[
271-
("dont_commit_writer.pdf", True),
272-
(Path("dont_commit_writer.pdf"), True),
279+
(str, True),
280+
(Path, True),
273281
(BytesIO(), False),
274282
],
275283
)
276284
def test_writer_operations_by_semi_new_traditional_usage(
277-
write_data_here, needs_cleanup
285+
convert, needs_cleanup
278286
):
287+
if callable(convert):
288+
write_data_here = convert(NamedTemporaryFile(suffix=".pdf", delete=False).name)
289+
else:
290+
write_data_here = convert
291+
279292
with PdfWriter() as writer:
280293
writer_operate(writer)
281294

@@ -287,14 +300,19 @@ def test_writer_operations_by_semi_new_traditional_usage(
287300

288301

289302
@pytest.mark.parametrize(
290-
("write_data_here", "needs_cleanup"),
303+
("convert", "needs_cleanup"),
291304
[
292-
("dont_commit_writer.pdf", True),
293-
(Path("dont_commit_writer.pdf"), True),
305+
(str, True),
306+
(Path, True),
294307
(BytesIO(), False),
295308
],
296309
)
297-
def test_writer_operation_by_new_usage(write_data_here, needs_cleanup):
310+
def test_writer_operation_by_new_usage(convert, needs_cleanup):
311+
if callable(convert):
312+
write_data_here = convert(NamedTemporaryFile(suffix=".pdf", delete=False).name)
313+
else:
314+
write_data_here = convert
315+
298316
# This includes write "output" to pypdf-output.pdf
299317
with PdfWriter(write_data_here) as writer:
300318
writer_operate(writer)
@@ -1439,16 +1457,8 @@ def test_named_dest_page_number():
14391457
assert len(writer._root_object["/Names"]["/Dests"]["/Names"]) == 6
14401458

14411459

1442-
@pytest.mark.parametrize(
1443-
("write_data_here", "needs_cleanup"),
1444-
[
1445-
(
1446-
"dont_commit_writer.pdf",
1447-
True,
1448-
)
1449-
],
1450-
)
1451-
def test_update_form_fields(write_data_here, needs_cleanup):
1460+
def test_update_form_fields(tmp_path):
1461+
write_data_here = tmp_path / "out.pdf"
14521462
writer = PdfWriter(clone_from=RESOURCE_ROOT / "FormTestFromOo.pdf")
14531463
writer.update_page_form_field_values(
14541464
writer.pages[0],
@@ -1472,8 +1482,8 @@ def test_update_form_fields(write_data_here, needs_cleanup):
14721482
auto_regenerate=False,
14731483
)
14741484

1475-
writer.write("dont_commit_writer.pdf")
1476-
reader = PdfReader("dont_commit_writer.pdf")
1485+
writer.write(write_data_here)
1486+
reader = PdfReader(write_data_here)
14771487
flds = reader.get_fields()
14781488
assert flds["CheckBox1"]["/V"] == "/Yes"
14791489
assert flds["CheckBox1"].indirect_reference.get_object()["/AS"] == "/Yes"
@@ -1495,8 +1505,7 @@ def test_update_form_fields(write_data_here, needs_cleanup):
14951505
assert all(x in flds["RadioGroup1"]["/_States_"] for x in ["/1", "/2", "/3"])
14961506
assert all(x in flds["Liste1"]["/_States_"] for x in ["Liste1", "Liste2", "Liste3"])
14971507

1498-
if needs_cleanup:
1499-
Path(write_data_here).unlink()
1508+
Path(write_data_here).unlink()
15001509

15011510

15021511
@pytest.mark.enable_socket()

0 commit comments

Comments
 (0)