Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pathvalidate/_filepath.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def sanitize(self, value: PathType, replacement_text: str = "") -> PathType:
drive, unicode_filepath = self.__split_drive(unicode_filepath)
unicode_filepath = self._sanitize_regexp.sub(replacement_text, unicode_filepath)
if self.__normalize and unicode_filepath:
unicode_filepath = os.path.normpath(unicode_filepath)
unicode_filepath = os.path.normpath(unicode_filepath.replace("\\", "/"))
sanitized_path = unicode_filepath

sanitized_entries: list[str] = []
Expand Down
47 changes: 47 additions & 0 deletions test/test_filepath_normalize_backslashes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from pathlib import Path

import pytest

from pathvalidate import sanitize_filepath


@pytest.mark.parametrize(
"platform,separator", [("windows", "\\"), ("linux", "/"), ("universal", "/")]
)
@pytest.mark.parametrize("value", [r"a\b\..\c.txt", r"a/b\..\c.txt", r"a\.\c.txt"])
def test_normalize_backslash_separators(platform, separator, value):
expected = separator.join(["a", "c.txt"])
assert sanitize_filepath(value, platform=platform) == expected
assert sanitize_filepath(Path(value), platform=platform) == Path(expected)


@pytest.mark.parametrize(
"platform,separator", [("windows", "\\"), ("linux", "/"), ("universal", "/")]
)
def test_backslash_normalization_opt_out(platform, separator):
value = r"a\b\..\c.txt"
expected = separator.join(["a", "b", "..", "c.txt"])
assert sanitize_filepath(value, platform=platform, normalize=False) == expected


@pytest.mark.parametrize(
"value,expected",
[
(r"C:\a\b\..\c.txt", r"C:\a\c.txt"),
(r"C:a\b\..\c.txt", r"C:\a\c.txt"),
(r"\\server\share\a\b\..\c.txt", r"\\server\share\a\c.txt"),
(r"\a\b\..\c.txt", r"\a\c.txt"),
(r"..\a\.\..\c.txt", r"..\c.txt"),
],
)
def test_normalize_backslash_windows_roots(value, expected):
assert sanitize_filepath(value, platform="windows") == expected


@pytest.mark.parametrize("platform", ["windows", "linux", "universal"])
@pytest.mark.parametrize("value", [r"a\b\c.txt", "a/b/c.txt", "../a/b.txt"])
def test_backslash_separator_control(platform, value):
separator = "\\" if platform == "windows" else "/"
assert sanitize_filepath(value, platform=platform) == value.replace("\\", "/").replace(
"/", separator
)