From f1129460d871d4bca46036085f8d869ff913ad49 Mon Sep 17 00:00:00 2001 From: Lamar Pavel Date: Tue, 26 May 2015 13:54:27 +0200 Subject: [PATCH] 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. --- qutebrowser/config/configtypes.py | 34 ++++++++++++++++--------------- tests/config/test_configtypes.py | 3 +-- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index d0d822a6f..4b0b5b70f 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -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): @@ -1178,23 +1182,21 @@ class UserStyleSheet(File): value = os.path.expandvars(value) value = os.path.expanduser(value) try: - if not os.path.isabs(value): - abspath = os.path.join(standarddir.config(), value) - if os.path.isfile(abspath): + super().validate(value) + except configexc.ValidationError: + 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 - # 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 - elif not os.path.isfile(value): - raise configexc.ValidationError(value, "must be a valid file!") - except UnicodeEncodeError as e: - raise configexc.ValidationError(value, e) + except UnicodeEncodeError as e: + raise configexc.ValidationError(value, e) class AutoSearch(BaseType): diff --git a/tests/config/test_configtypes.py b/tests/config/test_configtypes.py index 871b6cf4d..c515c6cef 100644 --- a/tests/config/test_configtypes.py +++ b/tests/config/test_configtypes.py @@ -1378,8 +1378,7 @@ 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') + self.t.validate('foobar') def test_validate_expanduser(self, os_path): """Test if validate expands the user correctly."""