This commit is contained in:
Emmanuel Garette 2019-02-25 20:30:20 +01:00
parent a248e114de
commit da015d3af0
6 changed files with 57 additions and 13 deletions

View file

@ -264,6 +264,23 @@ def test_config_multi():
assert config.option('test3').value.get() == [2, 1] assert config.option('test3').value.get() == [2, 1]
def test_prefix_error():
i1 = IntOption('test1', '')
od = OptionDescription('test', '', [i1])
config = Config(od)
config.property.read_write()
config.option('test1').value.set(1)
try:
config.option('test1').value.set('yes')
except Exception as err:
assert str(err) == '"yes" is an invalid integer for "test1"'
try:
config.option('test1').value.set('yes')
except Exception as err:
err.prefix = ''
assert str(err) == 'invalid value'
def test_no_validation(): def test_no_validation():
i1 = IntOption('test1', '') i1 = IntOption('test1', '')
od = OptionDescription('test', '', [i1]) od = OptionDescription('test', '', [i1])

View file

@ -417,6 +417,7 @@ def test_mandatory_leader():
api = Config(descr) api = Config(descr)
api.property.read_only() api.property.read_only()
raises(PropertiesOptionError, "api.option('ip_admin_eth0.ip_admin_eth0').value.get()") raises(PropertiesOptionError, "api.option('ip_admin_eth0.ip_admin_eth0').value.get()")
raises(PropertiesOptionError, "api.value.dict()")
def test_mandatory_warnings_leader(): def test_mandatory_warnings_leader():

View file

@ -63,6 +63,12 @@ def test_unknown_config():
raises(ConfigError, "meta.config('unknown')") raises(ConfigError, "meta.config('unknown')")
def test_error_metaconfig():
od2 = make_description()
conf1 = Config(od2, session_id='conf1')
raises(TypeError, "MetaConfig([GroupConfig([conf1])], session_id='meta')")
def test_path(): def test_path():
meta = make_metaconfig() meta = make_metaconfig()
assert meta.config.path() == 'meta' assert meta.config.path() == 'meta'

View file

@ -88,6 +88,24 @@ def test_consistency_warnings_only_more_option():
assert len(w) == 1 assert len(w) == 1
def test_consistency_error_prefix():
a = IntOption('a', '')
b = IntOption('b', '')
od = OptionDescription('od', '', [a, b])
a.impl_add_consistency('not_equal', b)
api = Config(od)
api.option('a').value.set(1)
try:
api.option('b').value.set(1)
except Exception as err:
assert str(err) == '"1" is an invalid integer for "b", must be different from the value of "a"'
try:
api.option('b').value.set(1)
except Exception as err:
err.prefix = ''
assert str(err) == 'must be different from the value of "a"'
def test_consistency_warnings_only_option(): def test_consistency_warnings_only_option():
a = IntOption('a', '') a = IntOption('a', '')
b = IntOption('b', '', warnings_only=True) b = IntOption('b', '', warnings_only=True)

View file

@ -1198,12 +1198,13 @@ class KernelMetaConfig(KernelMixConfig):
if not isinstance(child, _CommonConfig): if not isinstance(child, _CommonConfig):
try: try:
child = child._config child = child._config
except: except Exception:
raise TypeError(_("{}config's children " raise TypeError(_("{}config's children "
"should be config, not {}" "should be config, not {}"
).format(self.impl_type, ).format(self.impl_type,
type(child))) type(child)))
if not isinstance(child, (KernelConfig, KernelMetaConfig)): if __debug__ and not isinstance(child, (KernelConfig,
KernelMetaConfig)):
raise TypeError(_("child must be a Config or MetaConfig")) raise TypeError(_("child must be a Config or MetaConfig"))
if descr is None: if descr is None:
descr = child.cfgimpl_get_description() descr = child.cfgimpl_get_description()

View file

@ -19,6 +19,7 @@
# the whole pypy projet is under MIT licence # the whole pypy projet is under MIT licence
# ____________________________________________________________ # ____________________________________________________________
from ipaddress import ip_interface, ip_network from ipaddress import ip_interface, ip_network
from typing import List
from ..error import ConfigError from ..error import ConfigError
from ..setting import undefined, OptionBag, Undefined from ..setting import undefined, OptionBag, Undefined
@ -49,11 +50,11 @@ class NetmaskOption(StrOption):
raise ValueError() raise ValueError()
def _cons_network_netmask(self, def _cons_network_netmask(self,
current_opt, current_opt: Option,
opts, opts: List[Option],
vals, vals: List[str],
warnings_only, warnings_only: bool,
context): context: 'Config'):
if context is undefined and len(vals) != 2: if context is undefined and len(vals) != 2:
raise ConfigError(_('network_netmask needs a network and a netmask')) raise ConfigError(_('network_netmask needs a network and a netmask'))
if None in vals or len(vals) != 2: if None in vals or len(vals) != 2:
@ -71,12 +72,12 @@ class NetmaskOption(StrOption):
opt_network.impl_get_display_name())) opt_network.impl_get_display_name()))
def _cons_ip_netmask(self, def _cons_ip_netmask(self,
current_opt, current_opt: Option,
opts, opts: List[Option],
vals, vals: List[str],
warnings_only, warnings_only: bool,
context, context: 'config',
_cidr=False): _cidr: bool=False):
if context is undefined and len(vals) != 2: if context is undefined and len(vals) != 2:
raise ConfigError(_('ip_netmask needs an IP and a netmask')) raise ConfigError(_('ip_netmask needs an IP and a netmask'))
if None in vals or len(vals) != 2: if None in vals or len(vals) != 2: