|
| 1 | +"""Tests for utils.path_helpers.normalize_file_path. |
| 2 | +
|
| 3 | +Covers the shared implementation that was previously duplicated in |
| 4 | +scripts/export.py (closes #46). All call-sites in both the web app and the |
| 5 | +CLI export script now use this single copy. |
| 6 | +
|
| 7 | +Edge-case matrix: |
| 8 | + - file:/// and file:// URI schemes |
| 9 | + - Percent-encoded characters: spaces (%20), colons (%3A), hashes (%23) |
| 10 | + - Windows-style drive paths (backslash and forward-slash) on all platforms |
| 11 | + - Drive-letter lowercasing on win32 |
| 12 | + - Plain POSIX paths pass through unchanged |
| 13 | + - Empty / None-like input |
| 14 | +""" |
| 15 | + |
| 16 | +import sys |
| 17 | +import unittest |
| 18 | + |
| 19 | +from utils.path_helpers import normalize_file_path |
| 20 | + |
| 21 | + |
| 22 | +class TestNormalizeFilePathUriStripping(unittest.TestCase): |
| 23 | + def test_file_triple_slash_stripped(self) -> None: |
| 24 | + out = normalize_file_path("file:///home/user/project") |
| 25 | + self.assertFalse(out.startswith("file:")) |
| 26 | + self.assertIn("home", out) |
| 27 | + |
| 28 | + def test_file_double_slash_stripped(self) -> None: |
| 29 | + out = normalize_file_path("file://server/share/file.txt") |
| 30 | + self.assertFalse(out.startswith("file:")) |
| 31 | + self.assertIn("share", out) |
| 32 | + |
| 33 | + def test_empty_string(self) -> None: |
| 34 | + self.assertEqual(normalize_file_path(""), "") |
| 35 | + |
| 36 | + |
| 37 | +class TestNormalizeFilePathPercentEncoding(unittest.TestCase): |
| 38 | + def test_space_decoded(self) -> None: |
| 39 | + out = normalize_file_path("file:///C:/My%20Documents/file.txt") |
| 40 | + self.assertNotIn("%20", out) |
| 41 | + self.assertIn("My Documents" if sys.platform != "win32" else "my documents", out) |
| 42 | + |
| 43 | + def test_hash_decoded(self) -> None: |
| 44 | + out = normalize_file_path("file:///C:/repo/src%23internal/mod.py") |
| 45 | + self.assertNotIn("%23", out) |
| 46 | + self.assertIn("#", out) |
| 47 | + |
| 48 | + def test_percent_encoded_colon_in_uri_prefix(self) -> None: |
| 49 | + """URI-style /d%3A/... path: %3A is decoded to ':'. |
| 50 | +
|
| 51 | + On win32 the backslash branch is entered (leading slash removed |
| 52 | + and path lowercased). On other platforms the leading slash prevents |
| 53 | + the Windows-drive branch, so the path is returned as decoded only. |
| 54 | + """ |
| 55 | + out = normalize_file_path("/d%3A/_Work/project") |
| 56 | + self.assertNotIn("%3A", out) |
| 57 | + if sys.platform == "win32": |
| 58 | + self.assertEqual(out, r"d:\_work\project") |
| 59 | + else: |
| 60 | + self.assertEqual(out, "/d:/_Work/project") |
| 61 | + |
| 62 | + |
| 63 | +class TestNormalizeFilePathWindowsDrives(unittest.TestCase): |
| 64 | + """Paths with Windows-style drive letters are normalised on all platforms. |
| 65 | +
|
| 66 | + On win32 the win32 branch handles them natively. On Linux/macOS the |
| 67 | + ``^[a-zA-Z]:[/\\]`` regex branch converts forward-slashes to backslashes |
| 68 | + and lowercases the path so cross-platform reads of Cursor's Windows |
| 69 | + workspaceStorage produce consistent keys. |
| 70 | + """ |
| 71 | + |
| 72 | + def test_backslash_drive_path_lowercased(self) -> None: |
| 73 | + out = normalize_file_path(r"D:\Work\Boost") |
| 74 | + self.assertEqual(out, r"d:\work\boost") |
| 75 | + |
| 76 | + def test_forward_slash_drive_path_converted(self) -> None: |
| 77 | + out = normalize_file_path("D:/Work/Boost") |
| 78 | + self.assertEqual(out, r"d:\work\boost") |
| 79 | + |
| 80 | + def test_file_uri_with_windows_drive(self) -> None: |
| 81 | + out = normalize_file_path("file:///C:/Users/Dev/project") |
| 82 | + self.assertIn("users", out) |
| 83 | + self.assertIn("dev", out) |
| 84 | + self.assertTrue(out.startswith("c:") or out.startswith("C:")) |
| 85 | + |
| 86 | + def test_mixed_case_drive_lowercased(self) -> None: |
| 87 | + out = normalize_file_path(r"E:\Mixed\Case\Path") |
| 88 | + self.assertTrue(out.startswith("e:")) |
| 89 | + self.assertEqual(out, r"e:\mixed\case\path") |
| 90 | + |
| 91 | + |
| 92 | +class TestNormalizeFilePathPosixPassthrough(unittest.TestCase): |
| 93 | + def test_plain_posix_path_unchanged_on_non_windows(self) -> None: |
| 94 | + if sys.platform == "win32": |
| 95 | + self.skipTest("POSIX path semantics differ on win32") |
| 96 | + out = normalize_file_path("/home/user/project") |
| 97 | + self.assertEqual(out, "/home/user/project") |
| 98 | + |
| 99 | + def test_path_without_scheme_unchanged(self) -> None: |
| 100 | + if sys.platform == "win32": |
| 101 | + self.skipTest("plain relative path behaviour differs on win32") |
| 102 | + out = normalize_file_path("relative/path/file.py") |
| 103 | + self.assertEqual(out, "relative/path/file.py") |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + unittest.main() |
0 commit comments