Skip to content

Commit 70a069b

Browse files
authored
Merge pull request #1112 from codeflash-ai/comparator-temp-paths
extend comparator to pytest temp paths
2 parents 3418b61 + 9a00b0b commit 70a069b

2 files changed

Lines changed: 656 additions & 1 deletion

File tree

codeflash/verification/comparator.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@
2525
HAS_XARRAY = find_spec("xarray") is not None
2626
HAS_TENSORFLOW = find_spec("tensorflow") is not None
2727

28+
# Pattern to match pytest temp directories: /tmp/pytest-of-<user>/pytest-<N>/
29+
# These paths vary between test runs but are logically equivalent
30+
PYTEST_TEMP_PATH_PATTERN = re.compile(r"/tmp/pytest-of-[^/]+/pytest-\d+/") # noqa: S108
31+
32+
33+
def _normalize_temp_path(path: str) -> str:
34+
"""Normalize temporary file paths by replacing session-specific components.
35+
36+
Pytest creates temp directories like /tmp/pytest-of-<user>/pytest-<N>/
37+
where N is a session number that increments. When comparing return values
38+
from different test runs, these paths should be considered equivalent.
39+
"""
40+
return PYTEST_TEMP_PATH_PATTERN.sub("/tmp/pytest-temp/", path) # noqa: S108
41+
42+
43+
def _is_temp_path(s: str) -> bool:
44+
"""Check if a string looks like a pytest temp path."""
45+
return PYTEST_TEMP_PATH_PATTERN.search(s) is not None
46+
2847

2948
def _extract_exception_from_message(msg: str) -> Optional[BaseException]: # noqa: FA100
3049
"""Try to extract a wrapped exception type from an error message.
@@ -109,10 +128,18 @@ def comparator(orig: Any, new: Any, superset_obj=False) -> bool: # noqa: ANN001
109128
return False
110129
return all(comparator(elem1, elem2, superset_obj) for elem1, elem2 in zip(orig, new))
111130

131+
# Handle strings separately to normalize temp paths
132+
if isinstance(orig, str):
133+
if orig == new:
134+
return True
135+
# If strings differ, check if they're temp paths that differ only in session number
136+
if _is_temp_path(orig) and _is_temp_path(new):
137+
return _normalize_temp_path(orig) == _normalize_temp_path(new)
138+
return False
139+
112140
if isinstance(
113141
orig,
114142
(
115-
str,
116143
int,
117144
bool,
118145
complex,

0 commit comments

Comments
 (0)