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]
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():
i1 = IntOption('test1', '')
od = OptionDescription('test', '', [i1])

View File

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

View File

@ -63,6 +63,12 @@ def test_unknown_config():
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():
meta = make_metaconfig()
assert meta.config.path() == 'meta'

View File

@ -88,6 +88,24 @@ def test_consistency_warnings_only_more_option():
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():
a = IntOption('a', '')
b = IntOption('b', '', warnings_only=True)

View File

@ -1198,12 +1198,13 @@ class KernelMetaConfig(KernelMixConfig):
if not isinstance(child, _CommonConfig):
try:
child = child._config
except:
except Exception:
raise TypeError(_("{}config's children "
"should be config, not {}"
).format(self.impl_type,
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"))
if descr is None:
descr = child.cfgimpl_get_description()

View File

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