Skip to content

Commit c29573a

Browse files
committed
Clean up API entrypoint & split tests
1 parent 0963982 commit c29573a

6 files changed

Lines changed: 76 additions & 40 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ Options:
4848

4949
## Library Usage
5050

51-
Call the CLI from Python:
5251
```python
53-
from diffpdf import main
54-
main(["-vv","foo.pdf", "bar.pdf"])
52+
from diffpdf import diffpdf
53+
54+
diffpdf("reference.pdf", "actual.pdf")
55+
diffpdf("reference.pdf", "actual.pdf", output_dir="./output", threshold=0.2, dpi=150, verbosity=2)
5556
```
5657

5758
## Development

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ dev = [
4040
]
4141

4242
[project.scripts]
43-
diffpdf = "diffpdf:main"
43+
diffpdf = "diffpdf.cli:cli"
4444

4545
[tool.hatch.version]
4646
source = "vcs"

src/diffpdf/__init__.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
from importlib.metadata import version
2+
from pathlib import Path
23

3-
from .cli import cli
4+
from .comparators import compare_pdfs
5+
from .logger import setup_logging
46

57
__version__ = version("diffpdf")
68

79

8-
def main(args=None): # pragma: no cover
9-
if args is None:
10-
cli()
11-
else:
12-
cli(args, standalone_mode=False)
10+
def diffpdf(
11+
reference: str | Path,
12+
actual: str | Path,
13+
threshold: float = 0.1,
14+
dpi: int = 96,
15+
output_dir: str | Path = "./",
16+
verbosity: int = 0,
17+
save_log: bool = False,
18+
) -> None:
19+
ref_path = Path(reference) if isinstance(reference, str) else reference
20+
actual_path = Path(actual) if isinstance(actual, str) else actual
21+
out_path = Path(output_dir) if isinstance(output_dir, str) else output_dir
1322

23+
logger = setup_logging(verbosity, save_log)
24+
compare_pdfs(ref_path, actual_path, threshold, dpi, out_path, logger)
1425

15-
__all__ = ["main", "__version__"]
26+
27+
__all__ = ["diffpdf", "__version__"]

tests/test_api.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
5+
from diffpdf import diffpdf
6+
7+
TEST_ASSETS_DIR = Path(__file__).parent / "assets"
8+
9+
10+
def test_diffpdf_function():
11+
with pytest.raises(SystemExit) as exc_info:
12+
diffpdf(
13+
TEST_ASSETS_DIR / "pass/identical-A.pdf",
14+
TEST_ASSETS_DIR / "pass/identical-B.pdf",
15+
output_dir=Path("./"),
16+
)
17+
assert exc_info.value.code == 0

tests/test_cli.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from pathlib import Path
22

3-
import pytest
43
from click.testing import CliRunner
54

65
from diffpdf.cli import cli
@@ -35,31 +34,3 @@ def test_double_verbose_flag():
3534
)
3635
assert result.exit_code == 0
3736
assert "DEBUG" in result.output
38-
39-
40-
@pytest.mark.parametrize(
41-
"ref_pdf_rel,actual_pdf_rel,expected_exit_code",
42-
[
43-
# Pass cases (exit code 0)
44-
("pass/identical-A.pdf", "pass/identical-B.pdf", 0),
45-
("pass/hash-diff-A.pdf", "pass/hash-diff-B.pdf", 0),
46-
("pass/minor-color-diff-A.pdf", "pass/minor-color-diff-B.pdf", 0),
47-
("pass/multiplatform-diff-A.pdf", "pass/multiplatform-diff-B.pdf", 0),
48-
# Fail cases (exit code 1)
49-
("fail/1-letter-diff-A.pdf", "fail/1-letter-diff-B.pdf", 1),
50-
("fail/major-color-diff-A.pdf", "fail/major-color-diff-B.pdf", 1),
51-
("fail/page-count-diff-A.pdf", "fail/page-count-diff-B.pdf", 1),
52-
# Critical error cases (exit code 2)
53-
("nonexistent.pdf", "another.pdf", 2),
54-
],
55-
)
56-
def test_comparators(ref_pdf_rel, actual_pdf_rel, expected_exit_code):
57-
"""Parametric integration test: CLI should exit with correct code for various PDF pairs."""
58-
runner = CliRunner()
59-
60-
ref_pdf = str(TEST_ASSETS_DIR / ref_pdf_rel)
61-
actual_pdf = str(TEST_ASSETS_DIR / actual_pdf_rel)
62-
63-
result = runner.invoke(cli, [ref_pdf, actual_pdf])
64-
65-
assert result.exit_code == expected_exit_code

tests/test_comparators.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
from click.testing import CliRunner
5+
6+
from diffpdf.cli import cli
7+
8+
TEST_ASSETS_DIR = Path(__file__).parent / "assets"
9+
10+
11+
@pytest.mark.parametrize(
12+
"ref_pdf_rel,actual_pdf_rel,expected_exit_code",
13+
[
14+
# Pass cases (exit code 0)
15+
("pass/identical-A.pdf", "pass/identical-B.pdf", 0),
16+
("pass/hash-diff-A.pdf", "pass/hash-diff-B.pdf", 0),
17+
("pass/minor-color-diff-A.pdf", "pass/minor-color-diff-B.pdf", 0),
18+
("pass/multiplatform-diff-A.pdf", "pass/multiplatform-diff-B.pdf", 0),
19+
# Fail cases (exit code 1)
20+
("fail/1-letter-diff-A.pdf", "fail/1-letter-diff-B.pdf", 1),
21+
("fail/major-color-diff-A.pdf", "fail/major-color-diff-B.pdf", 1),
22+
("fail/page-count-diff-A.pdf", "fail/page-count-diff-B.pdf", 1),
23+
# Critical error cases (exit code 2)
24+
("nonexistent.pdf", "another.pdf", 2),
25+
],
26+
)
27+
def test_comparators(ref_pdf_rel, actual_pdf_rel, expected_exit_code):
28+
runner = CliRunner()
29+
30+
ref_pdf = str(TEST_ASSETS_DIR / ref_pdf_rel)
31+
actual_pdf = str(TEST_ASSETS_DIR / actual_pdf_rel)
32+
33+
result = runner.invoke(cli, [ref_pdf, actual_pdf])
34+
35+
assert result.exit_code == expected_exit_code

0 commit comments

Comments
 (0)