Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim heading spaces in Windows #28

Merged
merged 1 commit into from
Sep 24, 2022
Merged
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
11 changes: 11 additions & 0 deletions pathvalidate/_filename.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ def sanitize(self, value: PathType, replacement_text: str = "") -> PathType:
if self.platform in [Platform.UNIVERSAL, Platform.WINDOWS]:
# Do not end a file or directory name with a space or a period
sanitized_filename = sanitized_filename.rstrip(" .")
# Do not start a file or directory name with a space
sanitized_filename = sanitized_filename.lstrip(" ")
elif e.reason == ErrorReason.NULL_NAME:
return self._null_value_handler(e)
else:
Expand Down Expand Up @@ -209,6 +211,15 @@ def __validate_win_filename(self, unicode_filename: str) -> None:
description="Do not end a file or directory name with a space or a period",
)

if unicode_filename[0] in (" "):
raise InvalidCharError(
self._ERROR_MSG_TEMPLATE.format(
invalid=re.escape(unicode_filename[0]), value=repr(unicode_filename)
),
platform=Platform.WINDOWS,
description="Do not start a file or directory name with a space",
)


def validate_filename(
filename: PathType,
Expand Down
3 changes: 3 additions & 0 deletions test/test_filename.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,11 @@ def test_normal_check_reserved(self, value, check_reserved, expected):
[
["windows", "period.", "period"],
["windows", "space ", "space"],
["windows", " space ", "space"],
["windows", "space_and_period .", "space_and_period"],
["windows", "space_and_period. ", "space_and_period"],
["windows", " .space_and_period", ".space_and_period"],
["windows", ". space_and_period", ". space_and_period"],
["linux", "period.", "period."],
["linux", "space ", "space "],
["linux", "space_and_period. ", "space_and_period. "],
Expand Down