tests: test for regressions when converting PDFs when running the tests

This stores a reference version of the converted PDFs and diffs them when
the newly converted document during the tests.
This commit is contained in:
Alexis Métaireau 2025-03-05 15:14:24 +01:00
parent 6d269572ae
commit 628f8cb3f2
No known key found for this signature in database
GPG key ID: C65C7A89A8FFC56E
29 changed files with 21 additions and 3 deletions

View file

@ -122,7 +122,7 @@ test_docs_compressed_dir = Path(__file__).parent.joinpath(SAMPLE_COMPRESSED_DIRE
test_docs = [
p
for p in test_docs_dir.rglob("*")
for p in test_docs_dir.glob("*")
if p.is_file()
and not (p.name.endswith(SAFE_EXTENSION) or p.name.startswith("sample_bad"))
]

View file

@ -11,6 +11,7 @@ import traceback
from pathlib import Path
from typing import Optional, Sequence
import fitz
import pytest
from click.testing import CliRunner, Result
from pytest_mock import MockerFixture
@ -191,9 +192,26 @@ class TestCliConversion(TestCliBasic):
result.assert_failure()
@for_each_doc
def test_formats(self, doc: Path) -> None:
result = self.run_cli(str(doc))
def test_formats(self, doc: Path, tmp_path_factory: pytest.TempPathFactory) -> None:
reference = (doc.parent / "reference" / doc.stem).with_suffix(".pdf")
destination = tmp_path_factory.mktemp(doc.stem).with_suffix(".pdf")
result = self.run_cli([str(doc), "--output-filename", str(destination)])
result.assert_success()
# When needed, regenerate the reference PDFs by uncommenting the following line:
# reference.parent.mkdir(parents=True, exist_ok=True)
# shutil.copy(destination, reference)
converted = fitz.open(destination)
ref = fitz.open(reference)
assert len(converted) == len(ref), "different number of pages"
for page, ref_page in zip(converted, ref):
page.get_pixmap(dpi=150)
ref_page.get_pixmap(dpi=150)
assert page.get_pixmap().tobytes() == ref_page.get_pixmap().tobytes(), (
f"different page content for page {page.number}"
)
def test_output_filename(self, sample_pdf: str) -> None:
temp_dir = tempfile.mkdtemp(prefix="dangerzone-")

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.