Class File now validates relative paths

The code from function validate in class UserStyleSheet has been migrated to
class File. One test had to be modified due to different expected behaviour.
This commit is contained in:
Lamar Pavel 2015-05-26 13:54:27 +02:00
parent c54c637ccc
commit f1129460d8
2 changed files with 19 additions and 18 deletions

View File

@ -818,6 +818,10 @@ class File(BaseType):
raise configexc.ValidationError(value, "may not be empty!")
value = os.path.expanduser(value)
try:
if not os.path.isabs(value):
abspath = os.path.join(standarddir.config(), value)
if os.path.isfile(abspath):
return
if not os.path.isfile(value):
raise configexc.ValidationError(value, "must be a valid file!")
if not os.path.isabs(value):
@ -1177,11 +1181,11 @@ class UserStyleSheet(File):
raise configexc.ValidationError(value, "may not be empty!")
value = os.path.expandvars(value)
value = os.path.expanduser(value)
try:
super().validate(value)
except configexc.ValidationError:
try:
if not os.path.isabs(value):
abspath = os.path.join(standarddir.config(), value)
if os.path.isfile(abspath):
return
# probably a CSS, so we don't handle it as filename.
# FIXME We just try if it is encodable, maybe we should
# validate CSS?
@ -1191,8 +1195,6 @@ class UserStyleSheet(File):
except UnicodeEncodeError as e:
raise configexc.ValidationError(value, str(e))
return
elif not os.path.isfile(value):
raise configexc.ValidationError(value, "must be a valid file!")
except UnicodeEncodeError as e:
raise configexc.ValidationError(value, e)

View File

@ -1378,7 +1378,6 @@ class TestFile:
os_path.expanduser.side_effect = lambda x: x
os_path.isfile.return_value = True
os_path.isabs.return_value = False
with pytest.raises(configexc.ValidationError):
self.t.validate('foobar')
def test_validate_expanduser(self, os_path):