|
19 | 19 | from codeflash.models.models import FunctionTestInvocation, InvocationId, TestResults, TestType |
20 | 20 | from codeflash.verification.comparator import ( |
21 | 21 | PYTEST_TEMP_PATH_PATTERN, |
| 22 | + PYTHON_TEMPFILE_PATTERN, |
22 | 23 | _extract_exception_from_message, |
23 | 24 | _get_wrapped_exception, |
24 | 25 | _is_temp_path, |
@@ -3977,3 +3978,82 @@ def test_config_object_with_paths(self): |
3977 | 3978 | "permanent_dir": "/home/user/data/" |
3978 | 3979 | } |
3979 | 3980 | 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