From c8538bdbb8e0287c717ba878dd2c187ea1a82183 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 18 Jan 2016 07:29:15 +0100 Subject: [PATCH] Fix config validation for String types. Fixes #1231. --- qutebrowser/config/configtypes.py | 7 +++++++ tests/unit/config/test_configtypes.py | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index dbcb13fa5..c6279e5e3 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -258,6 +258,13 @@ class String(BaseType): self._basic_validation(value) if not value: return + + if self.valid_values is not None: + if value not in self.valid_values: + raise configexc.ValidationError( + value, "valid values: {}".format(', '.join( + self.valid_values))) + if self.forbidden is not None and any(c in value for c in self.forbidden): raise configexc.ValidationError(value, "may not contain the chars " diff --git a/tests/unit/config/test_configtypes.py b/tests/unit/config/test_configtypes.py index b746ba035..b150fc96e 100644 --- a/tests/unit/config/test_configtypes.py +++ b/tests/unit/config/test_configtypes.py @@ -305,6 +305,8 @@ class TestString: ({'minlen': 2}, 'fo'), ({'minlen': 2, 'maxlen': 3}, 'fo'), ({'minlen': 2, 'maxlen': 3}, 'foo'), + # valid_values + ({'valid_values': configtypes.ValidValues('fooo')}, 'fooo'), ]) def test_validate_valid(self, klass, kwargs, val): klass(**kwargs).validate(val) @@ -319,6 +321,8 @@ class TestString: ({'maxlen': 2}, 'fob'), ({'minlen': 2, 'maxlen': 3}, 'f'), ({'minlen': 2, 'maxlen': 3}, 'fooo'), + # valid_values + ({'valid_values': configtypes.ValidValues('blah')}, 'fooo'), ]) def test_validate_invalid(self, klass, kwargs, val): with pytest.raises(configexc.ValidationError):