Skip to content
Merged
Show file tree
Hide file tree
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
30 changes: 28 additions & 2 deletions src/diffpdf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from importlib.metadata import version
from pathlib import Path

from .comparators import compare_pdfs
from .hash_check import check_hash
from .logger import setup_logging
from .page_check import check_page_counts
from .text_check import check_text_content
from .visual_check import check_visual_content

__version__ = version("diffpdf")

Expand All @@ -21,7 +24,30 @@ def diffpdf(
out_path = Path(output_dir) if isinstance(output_dir, str) else output_dir

logger = setup_logging(verbosity, save_log)
return compare_pdfs(ref_path, actual_path, threshold, dpi, out_path, logger)
logger.debug("Debug logging enabled")

logger.info("[1/4] Checking file hashes...")
if check_hash(ref_path, actual_path):
logger.info("Files are identical (hash match)")
return True
logger.info("Hashes differ, continuing checks")

logger.info("[2/4] Checking page counts...")
if not check_page_counts(ref_path, actual_path, logger):
return False

logger.info("[3/4] Checking text content...")
if not check_text_content(ref_path, actual_path, logger):
return False

logger.info("[4/4] Checking visual content...")
if not check_visual_content(
ref_path, actual_path, threshold, dpi, out_path, logger
):
return False

logger.info("PDFs are equivalent")
return True


__all__ = ["diffpdf", "__version__"]
8 changes: 3 additions & 5 deletions src/diffpdf/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import click

from .comparators import compare_pdfs
from . import diffpdf
from .logger import setup_logging


Expand Down Expand Up @@ -41,14 +41,12 @@ def cli(
save_log: bool,
) -> None:
"""Compare two PDF files for structural, textual, and visual differences."""
logger = setup_logging(verbosity, save_log)
logger.debug("Debug logging enabled")

try:
if compare_pdfs(reference, actual, threshold, dpi, output_dir, logger):
if diffpdf(reference, actual, threshold, dpi, output_dir, verbosity, save_log):
sys.exit(0)
else:
sys.exit(1)
except Exception as e: # pragma: no cover
logger = setup_logging(verbosity, save_log)
logger.critical(f"Error: {e}", exc_info=True)
sys.exit(2)
37 changes: 0 additions & 37 deletions src/diffpdf/comparators.py

This file was deleted.

28 changes: 23 additions & 5 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
# type: ignore
from pathlib import Path

import pytest

from diffpdf import diffpdf

TEST_ASSETS_DIR = Path(__file__).parent / "assets"


def test_diffpdf():
assert diffpdf(
TEST_ASSETS_DIR / "pass/identical-A.pdf",
TEST_ASSETS_DIR / "pass/identical-B.pdf",
)
@pytest.mark.parametrize(
"ref_pdf_rel,actual_pdf_rel,should_pass",
[
# Pass cases
("pass/identical-A.pdf", "pass/identical-B.pdf", True),
("pass/hash-diff-A.pdf", "pass/hash-diff-B.pdf", True),
("pass/minor-color-diff-A.pdf", "pass/minor-color-diff-B.pdf", True),
("pass/multiplatform-diff-A.pdf", "pass/multiplatform-diff-B.pdf", True),
# Fail cases
("fail/1-letter-diff-A.pdf", "fail/1-letter-diff-B.pdf", False),
("fail/major-color-diff-A.pdf", "fail/major-color-diff-B.pdf", False),
("fail/page-count-diff-A.pdf", "fail/page-count-diff-B.pdf", False),
],
)
def test_api(ref_pdf_rel, actual_pdf_rel, should_pass):
ref_pdf = TEST_ASSETS_DIR / ref_pdf_rel
actual_pdf = TEST_ASSETS_DIR / actual_pdf_rel

result = diffpdf(ref_pdf, actual_pdf)

assert result == should_pass
13 changes: 13 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@
TEST_ASSETS_DIR = Path(__file__).parent / "assets"


def test_cli_with_output_dir():
runner = CliRunner()

with runner.isolated_filesystem():
ref_pdf = str(TEST_ASSETS_DIR / "fail/major-color-diff-A.pdf")
actual_pdf = str(TEST_ASSETS_DIR / "fail/major-color-diff-B.pdf")

result = runner.invoke(cli, [ref_pdf, actual_pdf, "--output-dir", "./diff"])

assert result.exit_code == 1
assert Path("./diff").exists()


def test_verbose_flag():
runner = CliRunner()
result = runner.invoke(
Expand Down
49 changes: 0 additions & 49 deletions tests/test_comparators.py

This file was deleted.