Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
[r"C:\Users:a", "windows", ErrorReason.INVALID_CHARACTER],
],
)
def test_exception(self, value, platform, expected):
with pytest.raises(ValidationError) as e:
validate_filepath(value, platform=platform)
assert e.value.reason == expected
assert not is_valid_filepath(value, platform=platform)
["a" * 5000, "windows", 10000, ErrorReason.INVALID_LENGTH],
["a" * 260, "windows", None, None],
["a" * 300, "windows", 1024, ErrorReason.INVALID_LENGTH],
["a" * 261, Platform.WINDOWS, None, ErrorReason.INVALID_LENGTH],
["a" * 261, "windows", None, ErrorReason.INVALID_LENGTH],
["a" * 260, "universal", None, None],
["a" * 261, "universal", None, ErrorReason.INVALID_LENGTH],
["a" * 300, "universal", 1024, ErrorReason.INVALID_LENGTH],
["a" * 261, Platform.UNIVERSAL, None, ErrorReason.INVALID_LENGTH],
],
)
def test_normal_max_len(self, value, platform, max_len, expected):
if expected is None:
validate_filepath(value, platform=platform, max_len=max_len)
assert is_valid_filepath(value, platform=platform, max_len=max_len)
return
["invalid_length", 200, ErrorReason.INVALID_LENGTH],
],
)
def test_normal_min_len(self, value, min_len, expected):
if expected is None:
validate_filepath(value, min_len=min_len)
assert is_valid_filepath(value, min_len=min_len)
return
with pytest.raises(ValidationError) as e:
validate_filepath(value, min_len=min_len)
assert e.value.reason == expected
["C:\\Users\\" + "a" * 1024, "windows", ErrorReason.INVALID_LENGTH],
[r"C:\Users:a", "windows", ErrorReason.INVALID_CHARACTER],
],
)
def test_exception(self, value, platform, expected):
with pytest.raises(ValidationError) as e:
validate_filepath(value, platform=platform)
assert e.value.reason == expected
assert not is_valid_filepath(value, platform=platform)
["invalid_length", 200, ErrorReason.INVALID_LENGTH],
],
)
def test_min_len(self, value, min_len, expected):
if expected is None:
validate_filename(value, min_len=min_len)
assert is_valid_filename(value, min_len=min_len)
else:
with pytest.raises(ValidationError) as e:
validate_filename(value, min_len=min_len)
assert e.value.reason == expected
def test_exception_escape_err_msg(self, value, platform, expected):
with pytest.raises(ValidationError) as e:
print(platform, repr(value))
validate_filename(value, platform=platform)
assert e.value.reason == ErrorReason.INVALID_CHARACTER
assert str(e.value) == (
r"invalid char found: invalids=('\r'), value='asdf\rsdf', "
"reason=INVALID_CHARACTER, target-platform=Windows"
[r"C:\Users:a", "universal", ErrorReason.MALFORMED_ABS_PATH],
["C:\\Users\\" + "a" * 1024, "windows", ErrorReason.INVALID_LENGTH],
[r"C:\Users:a", "windows", ErrorReason.INVALID_CHARACTER],
],
)
def test_exception(self, value, platform, expected):
with pytest.raises(ValidationError) as e:
validate_filepath(value, platform=platform)
assert e.value.reason == expected
assert not is_valid_filepath(value, platform=platform)
def table_name(self, value):
from ..sanitizer import ElasticsearchIndexNameSanitizer
from pathvalidate import ValidationError, ErrorReason
try:
self._table_name = ElasticsearchIndexNameSanitizer(value).sanitize(replacement_text="_")
except ValidationError as e:
if e.reason is ErrorReason.NULL_NAME:
self._table_name = None
else:
raise