From b5eea81e2ed389487a8cf5cb15043ca6a8094efb Mon Sep 17 00:00:00 2001 From: Lamar Pavel Date: Thu, 28 May 2015 12:14:12 +0200 Subject: [PATCH] Fix File.validate and corresponding tests There were no tests regarding the return value of standarddir.config() and thus it wasn't caught that it returned None in some cases. This is now fixed by checking the return of standdarddir.config before calling it and modifying the corresponding test_validate_exists_rel as well as adding a new test_validate_rel_config_none. --- qutebrowser/config/configtypes.py | 12 +++++++----- tests/config/test_configtypes.py | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index c3815cb6d..edc96d89d 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -819,13 +819,15 @@ class File(BaseType): raise configexc.ValidationError(value, "may not be empty!") value = os.path.expanduser(value) try: - if os.path.isfile(os.path.join(standarddir.config(), value)): - return + if not os.path.isabs(value): + if standarddir.config(): + if os.path.isfile( + os.path.join(standarddir.config(), value)): + return + raise configexc.ValidationError(value, + "must be an absolute path!") if not os.path.isfile(value): raise configexc.ValidationError(value, "must be a valid file!") - if not os.path.isabs(value): - raise configexc.ValidationError( - value, "must be an absolute path!") except UnicodeEncodeError as e: raise configexc.ValidationError(value, e) diff --git a/tests/config/test_configtypes.py b/tests/config/test_configtypes.py index 345a79d3b..1d52afdc8 100644 --- a/tests/config/test_configtypes.py +++ b/tests/config/test_configtypes.py @@ -30,7 +30,7 @@ from PyQt5.QtGui import QColor, QFont from PyQt5.QtNetwork import QNetworkProxy from qutebrowser.config import configtypes, configexc -from qutebrowser.utils import debug, utils +from qutebrowser.utils import debug, utils, standarddir class Font(QFont): @@ -1373,12 +1373,24 @@ class TestFile: os_path.isabs.return_value = True self.t.validate('foobar') - def test_validate_exists_rel(self, os_path): + def test_validate_exists_rel(self, os_path, monkeypatch): """Test validate with a relative path to an existing file.""" + monkeypatch.setattr('qutebrowser.config.configtypes.standarddir.config', + lambda: '/home/foo/.config/') os_path.expanduser.side_effect = lambda x: x os_path.isfile.return_value = True os_path.isabs.return_value = False self.t.validate('foobar') + os_path.join.assert_called_once_with('/home/foo/.config/', + 'foobar') + + def test_validate_rel_config_none(self, os_path, monkeypatch): + """Test with a relative path and standarddir.config returning None.""" + monkeypatch.setattr('qutebrowser.config.configtypes.standarddir.config', + lambda: None) + os_path.isabs.return_value = False + with pytest.raises(configexc.ValidationError): + self.t.validate('foobar') def test_validate_expanduser(self, os_path): """Test if validate expands the user correctly."""