Skip to content

Commit be01a6c

Browse files
authored
Merge branch 'main' into feat/line/profiler/webApp
2 parents 6168a52 + 95d1f6e commit be01a6c

2 files changed

Lines changed: 91 additions & 6 deletions

File tree

codeflash/verification/comparator.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,25 @@
3030
# These paths vary between test runs but are logically equivalent
3131
PYTEST_TEMP_PATH_PATTERN = re.compile(r"/tmp/pytest-of-[^/]+/pytest-\d+/") # noqa: S108
3232

33+
# Pattern to match Python tempfile directories: /tmp/tmp<random>/
34+
# Created by tempfile.mkdtemp() or tempfile.TemporaryDirectory()
35+
PYTHON_TEMPFILE_PATTERN = re.compile(r"/tmp/tmp[a-zA-Z0-9_]+/") # noqa: S108
36+
3337

3438
def _normalize_temp_path(path: str) -> str:
3539
"""Normalize temporary file paths by replacing session-specific components.
3640
37-
Pytest creates temp directories like /tmp/pytest-of-<user>/pytest-<N>/
38-
where N is a session number that increments. When comparing return values
39-
from different test runs, these paths should be considered equivalent.
41+
Handles two types of temp paths:
42+
- Pytest: /tmp/pytest-of-<user>/pytest-<N>/ -> /tmp/pytest-temp/
43+
- Python tempfile: /tmp/tmp<random>/ -> /tmp/python-temp/
4044
"""
41-
return PYTEST_TEMP_PATH_PATTERN.sub("/tmp/pytest-temp/", path) # noqa: S108
45+
path = PYTEST_TEMP_PATH_PATTERN.sub("/tmp/pytest-temp/", path) # noqa: S108
46+
return PYTHON_TEMPFILE_PATTERN.sub("/tmp/python-temp/", path) # noqa: S108
4247

4348

4449
def _is_temp_path(s: str) -> bool:
45-
"""Check if a string looks like a pytest temp path."""
46-
return PYTEST_TEMP_PATH_PATTERN.search(s) is not None
50+
"""Check if a string looks like a temp path (pytest or Python tempfile)."""
51+
return PYTEST_TEMP_PATH_PATTERN.search(s) is not None or PYTHON_TEMPFILE_PATTERN.search(s) is not None
4752

4853

4954
def _extract_exception_from_message(msg: str) -> Optional[BaseException]: # noqa: FA100

tests/test_comparator.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from codeflash.models.models import FunctionTestInvocation, InvocationId, TestResults, TestType
2020
from codeflash.verification.comparator import (
2121
PYTEST_TEMP_PATH_PATTERN,
22+
PYTHON_TEMPFILE_PATTERN,
2223
_extract_exception_from_message,
2324
_get_wrapped_exception,
2425
_is_temp_path,
@@ -3977,3 +3978,82 @@ def test_config_object_with_paths(self):
39773978
"permanent_dir": "/home/user/data/"
39783979
}
39793980
assert comparator(config1, config2)
3981+
3982+
3983+
class TestPythonTempfilePaths:
3984+
"""Tests for Python tempfile paths (from tempfile.mkdtemp() or TemporaryDirectory())."""
3985+
3986+
def test_is_temp_path_detects_python_tempfile(self):
3987+
"""Test that _is_temp_path detects Python tempfile paths."""
3988+
assert _is_temp_path("/tmp/tmpqtwy7hpf/special.txt")
3989+
assert _is_temp_path("/tmp/tmpp6wx3tz3/special.txt")
3990+
assert _is_temp_path("/tmp/tmpabcdef12/")
3991+
assert _is_temp_path("/tmp/tmp_underscore/file.txt")
3992+
3993+
def test_is_temp_path_various_tempfile_names(self):
3994+
"""Test various tempfile naming patterns."""
3995+
assert _is_temp_path("/tmp/tmpABCDEF/file.txt") # uppercase
3996+
assert _is_temp_path("/tmp/tmp123456/file.txt") # numeric
3997+
assert _is_temp_path("/tmp/tmpaBc123/file.txt") # mixed
3998+
assert _is_temp_path("/tmp/tmp_test_dir/subdir/file.txt") # with underscore
3999+
4000+
def test_is_temp_path_non_tempfile(self):
4001+
"""Test that non-tempfile paths are not detected."""
4002+
assert not _is_temp_path("/tmp/mydir/file.txt") # doesn't start with tmp
4003+
assert not _is_temp_path("/tmp/temp/file.txt") # temp, not tmp
4004+
assert not _is_temp_path("/home/user/tmp123/file.txt") # not in /tmp/
4005+
4006+
def test_normalize_temp_path_python_tempfile(self):
4007+
"""Test normalization of Python tempfile paths."""
4008+
path1 = _normalize_temp_path("/tmp/tmpqtwy7hpf/special.txt")
4009+
path2 = _normalize_temp_path("/tmp/tmpp6wx3tz3/special.txt")
4010+
assert path1 == path2 == "/tmp/python-temp/special.txt"
4011+
4012+
def test_normalize_temp_path_preserves_subdirs(self):
4013+
"""Test that subdirectories are preserved during normalization."""
4014+
result = _normalize_temp_path("/tmp/tmpabcdef12/subdir/nested/file.txt")
4015+
assert result == "/tmp/python-temp/subdir/nested/file.txt"
4016+
4017+
def test_comparator_python_tempfile_paths_equal(self):
4018+
"""Test that different tempfile paths with same content are equal."""
4019+
path1 = "/tmp/tmpqtwy7hpf/special.txt"
4020+
path2 = "/tmp/tmpp6wx3tz3/special.txt"
4021+
assert comparator(path1, path2)
4022+
4023+
def test_comparator_python_tempfile_different_filenames_not_equal(self):
4024+
"""Test that different filenames in tempfile paths are not equal."""
4025+
path1 = "/tmp/tmpqtwy7hpf/special.txt"
4026+
path2 = "/tmp/tmpp6wx3tz3/different.txt"
4027+
assert not comparator(path1, path2)
4028+
4029+
def test_comparator_python_tempfile_in_tuple(self):
4030+
"""Test tempfile paths in tuples."""
4031+
orig = ("/tmp/tmpqtwy7hpf/special.txt",)
4032+
new = ("/tmp/tmpp6wx3tz3/special.txt",)
4033+
assert comparator(orig, new)
4034+
4035+
def test_comparator_python_tempfile_in_list(self):
4036+
"""Test tempfile paths in lists."""
4037+
orig = ["/tmp/tmpabcdef12/file1.txt", "/tmp/tmpabcdef12/file2.txt"]
4038+
new = ["/tmp/tmpxyz78901/file1.txt", "/tmp/tmpxyz78901/file2.txt"]
4039+
assert comparator(orig, new)
4040+
4041+
def test_comparator_python_tempfile_in_dict(self):
4042+
"""Test tempfile paths in dictionaries."""
4043+
orig = {"output": "/tmp/tmpabcdef12/result.json"}
4044+
new = {"output": "/tmp/tmpxyz78901/result.json"}
4045+
assert comparator(orig, new)
4046+
4047+
def test_comparator_mixed_pytest_and_python_tempfile(self):
4048+
"""Test that pytest and Python tempfile paths don't match each other."""
4049+
pytest_path = "/tmp/pytest-of-user/pytest-0/file.txt"
4050+
python_path = "/tmp/tmpabcdef12/file.txt"
4051+
# These should not be equal - they're different temp path types
4052+
assert not comparator(pytest_path, python_path)
4053+
4054+
def test_python_tempfile_pattern_regex(self):
4055+
"""Test the PYTHON_TEMPFILE_PATTERN regex directly."""
4056+
assert PYTHON_TEMPFILE_PATTERN.search("/tmp/tmpabcdef/file.txt")
4057+
assert PYTHON_TEMPFILE_PATTERN.search("/tmp/tmp123456/")
4058+
assert not PYTHON_TEMPFILE_PATTERN.search("/tmp/mydir/file.txt")
4059+
assert not PYTHON_TEMPFILE_PATTERN.search("/home/tmp123/file.txt")

0 commit comments

Comments
 (0)