Skip to content

Commit 0a2bf7f

Browse files
Feat: Intercept 'tempfile' argument of the PdfComparator to improve it (#69)
1 parent afb0261 commit 0a2bf7f

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

dir_content_diff/base_comparators.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import re
1616
from abc import ABC
1717
from abc import abstractmethod
18+
from pathlib import Path
1819
from xml.etree import ElementTree
1920

2021
import dictdiffer
@@ -625,4 +626,27 @@ def diff(self, ref, comp, *args, **kwargs):
625626
num_threads (int): If set to 2 (the default), the image conversion are processed in
626627
parallel. If set to 1 it is processed sequentially.
627628
"""
629+
tempdir = kwargs.pop("tempdir", None)
630+
if tempdir is not None:
631+
relative_parts = []
632+
for i, j in zip(ref.parts[::-1], comp.parts[::-1]): # pragma: no branch
633+
if i != j:
634+
break
635+
relative_parts.append(i)
636+
if not relative_parts:
637+
relative_parts.append(comp.name)
638+
relative_parts[-1] = "diff-pdf-" + relative_parts[-1]
639+
new_tempdir = Path(tempdir) / Path(*relative_parts[::-1])
640+
641+
# Deduplicate name if needed
642+
num = 1
643+
while True:
644+
try:
645+
new_tempdir.mkdir(parents=True, exist_ok=False)
646+
break
647+
except FileExistsError:
648+
new_tempdir = new_tempdir.with_name(new_tempdir.name + f"_{num}")
649+
num += 1
650+
651+
kwargs["tempdir"] = new_tempdir
628652
return not pdf_similar(ref, comp, *args, **kwargs)

tests/test_base.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import copy
1818
import json
1919
import re
20+
import shutil
2021

2122
import dictdiffer
2223
import pytest
@@ -646,6 +647,59 @@ def test_initodict(self, ref_tree):
646647
"section2": {"attr3": [1, 2, "a", "b"], "attr4": {"a": 1, "b": [1, 2]}},
647648
}
648649

650+
class TestPdfComparator:
651+
"""Test the PDF comparator."""
652+
653+
def test_diff_tempfile(self, ref_tree, res_tree_equal):
654+
"""Test the custom tempfile option."""
655+
ref_file = ref_tree / "file.pdf"
656+
res_file = res_tree_equal / "file.pdf"
657+
658+
# Copy the initial data into a nested directory
659+
nested_ref = res_tree_equal / "nested" / "ref"
660+
nested_res = res_tree_equal / "nested" / "res"
661+
shutil.copytree(res_tree_equal, nested_res)
662+
shutil.copytree(ref_tree, nested_ref)
663+
664+
# Compute difference on initial data
665+
diff = dir_content_diff.compare_files(
666+
ref_file,
667+
res_file,
668+
dir_content_diff.PdfComparator(),
669+
tempdir=res_tree_equal,
670+
)
671+
assert not diff
672+
assert (res_tree_equal / "diff-pdf-file.pdf" / "diff-1.png").exists()
673+
674+
# Compute difference on nested data
675+
ref_file = nested_ref / "file.pdf"
676+
res_file = nested_res / "file.pdf"
677+
diff_nested = dir_content_diff.compare_files(
678+
ref_file,
679+
res_file,
680+
dir_content_diff.PdfComparator(),
681+
tempdir=nested_res.parent,
682+
)
683+
assert not diff_nested
684+
assert (nested_res.parent / "diff-pdf-file.pdf" / "diff-1.png").exists()
685+
686+
# Compare files with different names and with existing tempdir
687+
other_res_file = res_file.with_name("other_file.pdf")
688+
shutil.copyfile(res_file, other_res_file)
689+
(res_tree_equal / "diff-pdf-other_file.pdf").mkdir()
690+
other_diff = dir_content_diff.compare_files(
691+
ref_file,
692+
other_res_file,
693+
dir_content_diff.PdfComparator(),
694+
tempdir=res_tree_equal,
695+
)
696+
assert not other_diff
697+
assert (res_tree_equal / "diff-pdf-other_file.pdf").exists()
698+
assert not list((res_tree_equal / "diff-pdf-other_file.pdf").iterdir())
699+
assert (
700+
res_tree_equal / "diff-pdf-other_file.pdf_1" / "diff-1.png"
701+
).exists()
702+
649703

650704
class TestRegistry:
651705
"""Test the internal registry."""

0 commit comments

Comments
 (0)