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!") raise configexc.ValidationError(value, "may not be empty!")
value = os.path.expanduser(value) value = os.path.expanduser(value)
try: 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): if not os.path.isfile(value):
raise configexc.ValidationError(value, "must be a valid file!") raise configexc.ValidationError(value, "must be a valid file!")
if not os.path.isabs(value): if not os.path.isabs(value):
@ -1178,23 +1182,21 @@ class UserStyleSheet(File):
value = os.path.expandvars(value) value = os.path.expandvars(value)
value = os.path.expanduser(value) value = os.path.expanduser(value)
try: try:
if not os.path.isabs(value): super().validate(value)
abspath = os.path.join(standarddir.config(), value) except configexc.ValidationError:
if os.path.isfile(abspath): try:
if not os.path.isabs(value):
# probably a CSS, so we don't handle it as filename.
# FIXME We just try if it is encodable, maybe we should
# validate CSS?
# https://github.com/The-Compiler/qutebrowser/issues/115
try:
value.encode('utf-8')
except UnicodeEncodeError as e:
raise configexc.ValidationError(value, str(e))
return return
# probably a CSS, so we don't handle it as filename. except UnicodeEncodeError as e:
# FIXME We just try if it is encodable, maybe we should raise configexc.ValidationError(value, e)
# validate CSS?
# https://github.com/The-Compiler/qutebrowser/issues/115
try:
value.encode('utf-8')
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)
class AutoSearch(BaseType): class AutoSearch(BaseType):

View File

@ -1378,8 +1378,7 @@ class TestFile:
os_path.expanduser.side_effect = lambda x: x os_path.expanduser.side_effect = lambda x: x
os_path.isfile.return_value = True os_path.isfile.return_value = True
os_path.isabs.return_value = False os_path.isabs.return_value = False
with pytest.raises(configexc.ValidationError): self.t.validate('foobar')
self.t.validate('foobar')
def test_validate_expanduser(self, os_path): def test_validate_expanduser(self, os_path):
"""Test if validate expands the user correctly.""" """Test if validate expands the user correctly."""