|
| 1 | +"""Test compatibility utilities.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from setuptools_scm._compat import normalize_path_for_assertion |
| 8 | +from setuptools_scm._compat import strip_path_suffix |
| 9 | + |
| 10 | + |
| 11 | +def test_normalize_path_for_assertion() -> None: |
| 12 | + """Test path normalization for assertions.""" |
| 13 | + # Unix-style paths should remain unchanged |
| 14 | + assert normalize_path_for_assertion("/path/to/file") == "/path/to/file" |
| 15 | + |
| 16 | + # Windows-style paths should be normalized |
| 17 | + assert normalize_path_for_assertion(r"C:\path\to\file") == "C:/path/to/file" |
| 18 | + assert normalize_path_for_assertion(r"path\to\file") == "path/to/file" |
| 19 | + |
| 20 | + # Mixed paths should be normalized |
| 21 | + assert normalize_path_for_assertion(r"C:\path/to\file") == "C:/path/to/file" |
| 22 | + |
| 23 | + # Already normalized paths should remain unchanged |
| 24 | + assert normalize_path_for_assertion("path/to/file") == "path/to/file" |
| 25 | + |
| 26 | + |
| 27 | +def test_strip_path_suffix_success() -> None: |
| 28 | + """Test successful path suffix stripping.""" |
| 29 | + # Unix-style paths |
| 30 | + assert strip_path_suffix("/home/user/project", "project") == "/home/user/" |
| 31 | + assert ( |
| 32 | + strip_path_suffix("/home/user/project/subdir", "project/subdir") |
| 33 | + == "/home/user/" |
| 34 | + ) |
| 35 | + |
| 36 | + # Windows-style paths |
| 37 | + assert ( |
| 38 | + strip_path_suffix("C:\\Users\\user\\project", "project") == "C:\\Users\\user\\" |
| 39 | + ) |
| 40 | + assert ( |
| 41 | + strip_path_suffix("C:\\Users\\user\\project\\subdir", "project/subdir") |
| 42 | + == "C:\\Users\\user\\" |
| 43 | + ) |
| 44 | + |
| 45 | + # Mixed paths should work due to normalization |
| 46 | + assert ( |
| 47 | + strip_path_suffix("C:\\Users\\user\\project", "project") == "C:\\Users\\user\\" |
| 48 | + ) |
| 49 | + assert strip_path_suffix("/home/user/project", "project") == "/home/user/" |
| 50 | + |
| 51 | + # Edge cases |
| 52 | + assert strip_path_suffix("project", "project") == "" |
| 53 | + assert strip_path_suffix("/project", "project") == "/" |
| 54 | + |
| 55 | + |
| 56 | +def test_strip_path_suffix_failure() -> None: |
| 57 | + """Test failed path suffix stripping.""" |
| 58 | + with pytest.raises(AssertionError, match="Path assertion failed"): |
| 59 | + strip_path_suffix("/home/user/project", "other") |
| 60 | + |
| 61 | + with pytest.raises(AssertionError, match="Custom error"): |
| 62 | + strip_path_suffix("/home/user/project", "other", "Custom error") |
| 63 | + |
| 64 | + |
| 65 | +def test_integration_example() -> None: |
| 66 | + """Test the integration pattern used in the codebase.""" |
| 67 | + # Simulate the pattern used in git.py and _file_finders/git.py |
| 68 | + full_path = r"C:\\Users\\user\\project\\subdir" |
| 69 | + suffix = "subdir" |
| 70 | + |
| 71 | + # Now this is a single operation |
| 72 | + prefix = strip_path_suffix(full_path, suffix) |
| 73 | + assert prefix == r"C:\\Users\\user\\project\\" |
0 commit comments