|
25 | 25 | HAS_XARRAY = find_spec("xarray") is not None |
26 | 26 | HAS_TENSORFLOW = find_spec("tensorflow") is not None |
27 | 27 |
|
| 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 | + |
28 | 47 |
|
29 | 48 | def _extract_exception_from_message(msg: str) -> Optional[BaseException]: # noqa: FA100 |
30 | 49 | """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 |
109 | 128 | return False |
110 | 129 | return all(comparator(elem1, elem2, superset_obj) for elem1, elem2 in zip(orig, new)) |
111 | 130 |
|
| 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 | + |
112 | 140 | if isinstance( |
113 | 141 | orig, |
114 | 142 | ( |
115 | | - str, |
116 | 143 | int, |
117 | 144 | bool, |
118 | 145 | complex, |
|
0 commit comments