From 8060d89b731c1d0c7069737ec154c0ae4746d854 Mon Sep 17 00:00:00 2001 From: Yushion Yang <2685310462@qq.com> Date: Fri, 23 Oct 2026 22:21:28 +0800 Subject: [PATCH] Fix matched custom reserved names in validation errors --- pathvalidate/_base.py | 4 ++-- test/test_filename.py | 56 +++++++++++++++++++++++++++++++++++++++++++ test/test_filepath.py | 19 +++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) diff --git a/pathvalidate/_base.py b/pathvalidate/_base.py index c74e842..296ff6a 100644 --- a/pathvalidate/_base.py +++ b/pathvalidate/_base.py @@ -224,9 +224,9 @@ def _validate_reserved_keywords(self, name: str) -> None: for name in (root_name, base_name): if self._is_reserved_keyword(name): raise ReservedNameError( - f"'{root_name}' is a reserved name", + f"'{name}' is a reserved name", reusable_name=False, - reserved_name=root_name, + reserved_name=name, platform=self.platform, ) diff --git a/test/test_filename.py b/test/test_filename.py index 285dc2b..090806b 100644 --- a/test/test_filename.py +++ b/test/test_filename.py @@ -412,6 +412,23 @@ def test_reserved_name(self, value, platform, expected): def test_normal_additional_reserved_names(self, value, arn, expected): assert is_valid_filename(value, additional_reserved_names=arn) == expected + @pytest.mark.parametrize("platform", ["windows", "universal", "linux"]) + @pytest.mark.parametrize( + ["value", "reserved", "expected"], + [ + [".env", ".env", ".env"], + [".GitIgnore", ".gitignore", ".GitIgnore"], + ["config.json", "config.json", "config.json"], + ["archive.tar.gz", "archive.tar.gz", "archive.tar.gz"], + ["config.json", "config", "config"], + ], + ) + def test_matched_custom_reserved_name(self, platform, value, reserved, expected): + with pytest.raises(ValidationError) as exc: + validate_filename(value, platform=platform, additional_reserved_names=[reserved]) + assert exc.value.reason == ErrorReason.RESERVED_NAME + assert exc.value.reserved_name == expected + @pytest.mark.parametrize( ["platform", "value", "expected", "reason"], [ @@ -709,6 +726,45 @@ def test_normal_additional_reserved_names(self, value, arn, expected): == expected ), platform + @pytest.mark.parametrize("platform", ["windows", "universal", "linux"]) + @pytest.mark.parametrize("value", [".env", ".gitignore", "config.json"]) + @pytest.mark.parametrize( + ["handler", "prefix", "suffix"], + [ + [ReservedNameHandler.add_trailing_underscore, "", "_"], + [ReservedNameHandler.add_leading_underscore, "_", ""], + [ReservedNameHandler.as_is, "", ""], + ], + ) + def test_matched_custom_reserved_name(self, platform, value, handler, prefix, suffix): + assert ( + sanitize_filename( + value, + platform=platform, + additional_reserved_names=[value], + reserved_name_handler=handler, + ) + == prefix + value + suffix + ) + + def test_custom_handler_receives_complete_reserved_name(self): + names = [] + + def replace_name(error: ValidationError) -> str: + names.append(error.reserved_name) + return "settings" + + assert ( + sanitize_filename( + ".env", + additional_reserved_names=[".env"], + reserved_name_handler=replace_name, + validate_after_sanitize=True, + ) + == "settings" + ) + assert names == [".env"] + @pytest.mark.parametrize( ["value", "check_reserved", "expected"], [ diff --git a/test/test_filepath.py b/test/test_filepath.py index 23b559a..9f9ec75 100644 --- a/test/test_filepath.py +++ b/test/test_filepath.py @@ -579,6 +579,25 @@ def test_exception(self, value, platform, expected): class Test_sanitize_filepath: + @pytest.mark.parametrize( + ["platform", "value", "expected"], + [ + ["linux", "secrets/.env", "secrets/.env_"], + ["universal", "secrets/.env", "secrets/.env_"], + ["windows", "secrets\\.env", "secrets\\.env_"], + ], + ) + def test_matched_custom_reserved_name(self, platform, value, expected): + assert ( + sanitize_filepath( + value, + platform=platform, + additional_reserved_names=[".env"], + validate_after_sanitize=True, + ) + == expected + ) + SANITIZE_CHARS = INVALID_WIN_PATH_CHARS + unprintable_ascii_chars NOT_SANITIZE_CHARS = VALID_PATH_CHARS REPLACE_TEXTS = ["", "_"]