Skip to content

Commit

Permalink
Fix filename validations: #18
Browse files Browse the repository at this point in the history
that include '\' (backslash) on other than Windows.
  • Loading branch information
thombashi committed Apr 3, 2021
1 parent 709084a commit 82fc86a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
18 changes: 12 additions & 6 deletions pathvalidate/_filename.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,18 @@ def validate(self, value: PathType) -> None:
self.__validate_unix_filename(unicode_filename)

def validate_abspath(self, value: str) -> None:
if any([ntpath.isabs(value), posixpath.isabs(value)]):
raise ValidationError(
description="found an absolute path ({}), expected a filename".format(value),
platform=self.platform,
reason=ErrorReason.FOUND_ABS_PATH,
)
err = ValidationError(
description="found an absolute path ({}), expected a filename".format(value),
platform=self.platform,
reason=ErrorReason.FOUND_ABS_PATH,
)

if self._is_universal() or self._is_windows():
if ntpath.isabs(value):
raise err

if posixpath.isabs(value):
raise err

def __validate_unix_filename(self, unicode_filename: str) -> None:
match = _RE_INVALID_FILENAME.findall(unicode_filename)
Expand Down
24 changes: 24 additions & 0 deletions test/test_filename.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,30 @@ def test_reserved_name(self, value, platform, expected):

assert not is_valid_filename(value, platform=platform)

@pytest.mark.parametrize(
["platform", "value", "expected"],
[
[win_abspath, platform, None]
for win_abspath, platform in product(
["linux", "macos", "posix"],
["\\", "\\\\", "\\ ", "C:\\", "c:\\", "\\xyz", "\\xyz "],
)
]
+ [
[win_abspath, platform, ValidationError]
for win_abspath, platform in product(
["windows", "universal"], ["\\", "\\\\", "\\ ", "C:\\", "c:\\", "\\xyz", "\\xyz "]
)
],
)
def test_win_abs_path(self, platform, value, expected):
if expected is None:
validate_filename(value, platform=platform)
else:
with pytest.raises(expected) as e:
validate_filename(value, platform=platform)
assert e.value.reason == ErrorReason.FOUND_ABS_PATH

@pytest.mark.parametrize(
["value", "platform"],
[
Expand Down

0 comments on commit 82fc86a

Please sign in to comment.