diff --git a/pathvalidate/_filename.py b/pathvalidate/_filename.py index f2651ed..2214262 100644 --- a/pathvalidate/_filename.py +++ b/pathvalidate/_filename.py @@ -86,7 +86,7 @@ def sanitize(self, value: PathType, replacement_text: str = "") -> PathType: replacement_word = self._reserved_name_handler(e) if e.reserved_name != replacement_word: sanitized_filename = re.sub( - re.escape(e.reserved_name), replacement_word, sanitized_filename + re.escape(e.reserved_name), replacement_word, sanitized_filename, count=1 ) elif e.reason == ErrorReason.INVALID_CHARACTER and self._is_windows( include_universal=True diff --git a/test/test_reserved_name_extensions.py b/test/test_reserved_name_extensions.py new file mode 100644 index 0000000..e0da538 --- /dev/null +++ b/test/test_reserved_name_extensions.py @@ -0,0 +1,34 @@ +import pytest + +from pathvalidate import is_valid_filename, sanitize_filename, sanitize_filepath +from pathvalidate.handler import ReservedNameHandler + + +@pytest.mark.parametrize("platform", ["windows", "universal"]) +@pytest.mark.parametrize("filename", ["con.icon", "CON.CON.txt", "aux.aux", "COM1.COM1"]) +@pytest.mark.parametrize( + ["handler", "prefix", "suffix"], + [ + [ReservedNameHandler.add_leading_underscore, "_", ""], + [ReservedNameHandler.add_trailing_underscore, "", "_"], + ], +) +def test_reserved_name_handler_preserves_extension(filename, platform, handler, prefix, suffix): + stem, extension = filename.split(".", 1) + expected = f"{prefix}{stem}{suffix}.{extension}" + + result = sanitize_filename(filename, platform=platform, reserved_name_handler=handler) + + assert result == expected + assert is_valid_filename(result, platform=platform) + + +def test_additional_reserved_name_preserves_extension(): + assert ( + sanitize_filename("foo.foo", platform="linux", additional_reserved_names=["foo"]) + == "foo_.foo" + ) + + +def test_filepath_preserves_extension(): + assert sanitize_filepath("folder/con.icon", platform="windows") == r"folder\con_.icon"