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
24 changes: 24 additions & 0 deletions dir_content_diff/base_comparators.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import re
from abc import ABC
from abc import abstractmethod
from pathlib import Path
from xml.etree import ElementTree

import dictdiffer
Expand Down Expand Up @@ -625,4 +626,27 @@ def diff(self, ref, comp, *args, **kwargs):
num_threads (int): If set to 2 (the default), the image conversion are processed in
parallel. If set to 1 it is processed sequentially.
"""
tempdir = kwargs.pop("tempdir", None)
if tempdir is not None:
relative_parts = []
for i, j in zip(ref.parts[::-1], comp.parts[::-1]): # pragma: no branch
if i != j:
break
relative_parts.append(i)
if not relative_parts:
relative_parts.append(comp.name)
relative_parts[-1] = "diff-pdf-" + relative_parts[-1]
new_tempdir = Path(tempdir) / Path(*relative_parts[::-1])

# Deduplicate name if needed
num = 1
while True:
try:
new_tempdir.mkdir(parents=True, exist_ok=False)
break
except FileExistsError:
new_tempdir = new_tempdir.with_name(new_tempdir.name + f"_{num}")
num += 1

kwargs["tempdir"] = new_tempdir
return not pdf_similar(ref, comp, *args, **kwargs)
54 changes: 54 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import copy
import json
import re
import shutil

import dictdiffer
import pytest
Expand Down Expand Up @@ -646,6 +647,59 @@ def test_initodict(self, ref_tree):
"section2": {"attr3": [1, 2, "a", "b"], "attr4": {"a": 1, "b": [1, 2]}},
}

class TestPdfComparator:
"""Test the PDF comparator."""

def test_diff_tempfile(self, ref_tree, res_tree_equal):
"""Test the custom tempfile option."""
ref_file = ref_tree / "file.pdf"
res_file = res_tree_equal / "file.pdf"

# Copy the initial data into a nested directory
nested_ref = res_tree_equal / "nested" / "ref"
nested_res = res_tree_equal / "nested" / "res"
shutil.copytree(res_tree_equal, nested_res)
shutil.copytree(ref_tree, nested_ref)

# Compute difference on initial data
diff = dir_content_diff.compare_files(
ref_file,
res_file,
dir_content_diff.PdfComparator(),
tempdir=res_tree_equal,
)
assert not diff
assert (res_tree_equal / "diff-pdf-file.pdf" / "diff-1.png").exists()

# Compute difference on nested data
ref_file = nested_ref / "file.pdf"
res_file = nested_res / "file.pdf"
diff_nested = dir_content_diff.compare_files(
ref_file,
res_file,
dir_content_diff.PdfComparator(),
tempdir=nested_res.parent,
)
assert not diff_nested
assert (nested_res.parent / "diff-pdf-file.pdf" / "diff-1.png").exists()

# Compare files with different names and with existing tempdir
other_res_file = res_file.with_name("other_file.pdf")
shutil.copyfile(res_file, other_res_file)
(res_tree_equal / "diff-pdf-other_file.pdf").mkdir()
other_diff = dir_content_diff.compare_files(
ref_file,
other_res_file,
dir_content_diff.PdfComparator(),
tempdir=res_tree_equal,
)
assert not other_diff
assert (res_tree_equal / "diff-pdf-other_file.pdf").exists()
assert not list((res_tree_equal / "diff-pdf-other_file.pdf").iterdir())
assert (
res_tree_equal / "diff-pdf-other_file.pdf_1" / "diff-1.png"
).exists()


class TestRegistry:
"""Test the internal registry."""
Expand Down