Skip to content

Commit

Permalink
100% coverage for cmdline.py
Browse files Browse the repository at this point in the history
Had to use one 'no cover' sadly.
  • Loading branch information
jamadden committed Feb 11, 2017
1 parent 55a4523 commit b794868
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
17 changes: 10 additions & 7 deletions ZConfig/cmdline.py
Expand Up @@ -24,10 +24,13 @@ class from the :mod:`ZConfig.loader` module. This provides support for
:meth:`ExtendedConfigLoader.addOption`.
"""

import sys

import ZConfig
import ZConfig.loader
import ZConfig.matcher

from ZConfig._compat import reraise

class ExtendedConfigLoader(ZConfig.loader.ConfigLoader):
"""A :class:`~.ConfigLoader` subclass that adds support for
Expand Down Expand Up @@ -94,8 +97,6 @@ def createSchemaMatcher(self):
def cook(self):
if self.clopts:
return OptionBag(self.schema, self.schema, self.clopts)
else:
return None


class OptionBag(object):
Expand All @@ -116,9 +117,10 @@ def __init__(self, schema, sectiontype, options):
def basic_key(self, s, pos):
try:
return self._basic_key(s)
except ValueError:
raise ZConfig.ConfigurationSyntaxError(
"could not convert basic-key value", *pos)
except ValueError as e:
e = ZConfig.ConfigurationSyntaxError(
"could not convert basic-key value: " + str(e), *pos)
reraise(type(e), e, sys.exc_info()[2])

def add_value(self, name, val, pos):
if name in self.keypairs:
Expand Down Expand Up @@ -155,7 +157,7 @@ def get_section_info(self, type, name):
bk = self.basic_key(s, pos)
if name and self._normalize_case(s) == name:
L.append((optpath[1:], val, pos))
elif bk == type:
elif bk == type: # pragma: no cover
L.append((optpath[1:], val, pos))
else:
R.append(item)
Expand Down Expand Up @@ -183,7 +185,8 @@ def addValue(self, key, value, position):
try:
realkey = self.type.keytype(key)
except ValueError as e:
raise ZConfig.DataConversionError(e, key, position)
dce = ZConfig.DataConversionError(e, key, position)
reraise(type(dce), dce, sys.exc_info()[2])
if realkey in self.optionbag:
return
ZConfig.matcher.BaseMatcher.addValue(self, key, value, position)
Expand Down
41 changes: 40 additions & 1 deletion ZConfig/tests/test_cmdline.py
Expand Up @@ -24,6 +24,8 @@

class CommandLineTest(ZConfig.tests.support.TestHelper, unittest.TestCase):

clopts = ()

def create_config_loader(self, schema):
loader = ExtendedConfigLoader(schema)
for item in self.clopts:
Expand Down Expand Up @@ -79,14 +81,32 @@ def test_named_sections(self):
</schema>
""")
self.clopts = [("foo/k1=v1", None), ("bar/k2=v2", ("someurl", 2, 3))]
bag = self.create_config_loader(schema).cook()
loader = self.create_config_loader(schema)
bag = loader.cook()
foo = bag.get_section_info("st2", "foo")
bar = bag.get_section_info("st2", "bar")
bag.finish()
self.assertEqual(bar.get_key("k2"), [("v2", ("someurl", 2, 3))])
bar.finish()
# Ignore foo for now; it's not really important *when* it fails.

# ValueErrors are converted into ConfigurationSyntaxErrors
self.assertRaisesRegexp(ZConfig.ConfigurationSyntaxError,
"could not convert",
foo.basic_key,
'invalid name', ('<place>', 1,))

# missing keys return empty lists
self.assertEqual(foo.get_key('no such key'), [])

# VE for matchers do the same conversion
matcher = loader.createSchemaMatcher()
self.assertRaisesRegexp(ZConfig.DataConversionError,
"value did not match",
matcher.addValue,
'invalid name', 'value', (1, 1, '<place>'))


simple_schema = None

def get_simple_schema(self):
Expand Down Expand Up @@ -172,6 +192,25 @@ def test_section_contents(self):
self.assertEqual(conf.s2.k2, 3)
self.assertEqual(conf.s2.k3, ["value1", "value2", "value3", "value4"])

self.clopts = [("path/that/dne=foo",)]
self.assertRaisesRegexp(ZConfig.ConfigurationError,
"not all command line options were consumed",
self.load_config_text,
schema, "<st s1/>")

def test_bad_overrides(self):
schema = self.get_simple_schema()
self.clopts = [('',)]
self.assertRaisesRegexp(ZConfig.ConfigurationSyntaxError,
"invalid configuration specifier",
self.create_config_loader,
schema)

self.clopts = [('double//slashes=value',)]
self.assertRaisesRegexp(ZConfig.ConfigurationSyntaxError,
"not allowed in an option path",
self.create_config_loader,
schema)

def test_suite():
return unittest.makeSuite(CommandLineTest)
Expand Down

0 comments on commit b794868

Please sign in to comment.