Skip to content

Commit

Permalink
TST: Remove files after tests ran
Browse files Browse the repository at this point in the history
Use the tmp_path fixture in cases where we're not interested to manually
check the resulting PDF file
  • Loading branch information
MartinThoma committed Aug 27, 2022
1 parent 84460f5 commit bce300e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 62 deletions.
11 changes: 4 additions & 7 deletions tests/bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_page_operations(benchmark):
benchmark(page_ops, "libreoffice-writer-password.pdf", "openpassword")


def merge():
def merge(tmp_path):
pdf_path = RESOURCE_ROOT / "crazyones.pdf"
outline = RESOURCE_ROOT / "pdflatex-outline.pdf"
pdf_forms = RESOURCE_ROOT / "pdflatex-forms.pdf"
Expand Down Expand Up @@ -83,12 +83,12 @@ def merge():
merger.set_page_layout("/SinglePage")
merger.set_page_mode("/UseThumbs")

tmp_path = "dont_commit_merged.pdf"
merger.write(tmp_path)
write_path = tmp_path / "dont_commit_merged.pdf"
merger.write(write_path)
merger.close()

# Check if outline is correct
reader = PyPDF2.PdfReader(tmp_path)
reader = PyPDF2.PdfReader(write_path)
assert [
el.title for el in reader._get_outline() if isinstance(el, Destination)
] == [
Expand All @@ -105,9 +105,6 @@ def merge():
"True",
]

# Clean up
os.remove(tmp_path)


def test_merge(benchmark):
"""
Expand Down
54 changes: 0 additions & 54 deletions tests/test_basic_features.py

This file was deleted.

43 changes: 43 additions & 0 deletions tests/test_workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,49 @@
sys.path.append(str(PROJECT_ROOT))


def test_basic_features(tmp_path):
pdf_path = RESOURCE_ROOT / "crazyones.pdf"
reader = PdfReader(pdf_path)
writer = PdfWriter()

assert len(reader.pages) == 1

# add page 1 from input1 to output document, unchanged
writer.add_page(reader.pages[0])

# add page 2 from input1, but rotated clockwise 90 degrees
writer.add_page(reader.pages[0].rotate(90))

# add page 3 from input1, but first add a watermark from another PDF:
page3 = reader.pages[0]
watermark_pdf = pdf_path
watermark = PdfReader(watermark_pdf)
page3.merge_page(watermark.pages[0])
writer.add_page(page3)

# add page 4 from input1, but crop it to half size:
page4 = reader.pages[0]
page4.mediabox.upper_right = (
page4.mediabox.right / 2,
page4.mediabox.top / 2,
)
writer.add_page(page4)

# add some Javascript to launch the print window on opening this PDF.
# the password dialog may prevent the print dialog from being shown,
# comment the the encription lines, if that's the case, to try this out
writer.add_js("this.print({bUI:true,bSilent:false,bShrinkToFit:true});")

# encrypt your new PDF and add a password
password = "secret"
writer.encrypt(password)

# finally, write "output" to PyPDF2-output.pdf
write_path = tmp_path / "PyPDF2-output.pdf"
with open(write_path, "wb") as output_stream:
writer.write(output_stream)


def test_dropdown_items():
inputfile = RESOURCE_ROOT / "libreoffice-form.pdf"
reader = PdfReader(inputfile)
Expand Down
8 changes: 7 additions & 1 deletion tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ def test_fill_form():
with open(tmp_filename, "wb") as output_stream:
writer.write(output_stream)

os.remove(tmp_filename) # cleanup


@pytest.mark.parametrize(
("use_128bit", "user_pwd", "owner_pwd"),
Expand Down Expand Up @@ -595,14 +597,18 @@ def test_io_streams():


def test_regression_issue670():
tmp_file = "dont_commit_issue670.pdf"
filepath = RESOURCE_ROOT / "crazyones.pdf"
reader = PdfReader(filepath, strict=False)
for _ in range(2):
writer = PdfWriter()
writer.add_page(reader.pages[0])
with open("dont_commit_issue670.pdf", "wb") as f_pdf:
with open(tmp_file, "wb") as f_pdf:
writer.write(f_pdf)

# cleanup
os.remove(tmp_file)


def test_issue301():
"""
Expand Down

0 comments on commit bce300e

Please sign in to comment.