Skip to content

Commit

Permalink
Improve test coverage for zdoptions.py
Browse files Browse the repository at this point in the history
  • Loading branch information
mgedmin committed Apr 15, 2015
1 parent 12e32a7 commit fcc7f87
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .coveragerc
@@ -1,3 +1,9 @@
[run]
source = zdaemon
omit = */tests/*, */__main__.py

[report]
exclude_lines =
pragma: nocover
except ImportError:
if __name__ == .__main__.:
30 changes: 27 additions & 3 deletions src/zdaemon/tests/testzdoptions.py
Expand Up @@ -23,7 +23,7 @@
import ZConfig
import zdaemon
from zdaemon.zdoptions import (
ZDOptions, RunnerOptions,
ZDOptions, RunnerOptions, list_of_ints,
existing_parent_directory, existing_parent_dirpath)

try:
Expand Down Expand Up @@ -147,8 +147,8 @@ class NoHelp(self.OptionsClass):
def test_help(self):
# test what happens when the subclass has None for __doc__
class HasHelp(self.OptionsClass):
__doc__ = 'Some help'
self.help_test_helper(HasHelp, {}, 'Some help')
__doc__ = 'Some help for %s'
self.help_test_helper(HasHelp, {'progname': 'me'}, 'Some help for me')

def test_has_help_with_doc_kw(self):
# test what happens when the subclass has something for __doc__,
Expand Down Expand Up @@ -205,6 +205,13 @@ def test_conflicting_flags(self):
options.add("setting", None, "b", flag=2)
self.check_exit_code(options, ["-a", "-b"])

def test_duplicate_flags(self):
# Check that we don't get an error for flags which reinforce the
# same option setting.
options = self.OptionsClass()
options.add("setting", None, "a", flag=1)
options.realize(["-a", "-a"])

def test_handler_simple(self):
# Test that a handler is called; use one that doesn't return None.
options = self.OptionsClass()
Expand All @@ -227,6 +234,17 @@ def test_handler_with_bad_value(self):
options.add("setting", None, "a:", handler=int)
self.check_exit_code(options, ["-afoo"])

def test_required_options(self):
# Check that we get an error if a required option is not specified
options = self.OptionsClass()
options.add("setting", None, "a:", handler=int, required=True)
self.check_exit_code(options, [])

def test_overrides_without_config_file(self):
# Check that we get an error if we use -X without -C
options = self.OptionsClass()
self.check_exit_code(options, ["-Xfoo"])

def test_raise_getopt_errors(self):
options = self.OptionsClass()
# note that we do not add "a" to the list of options;
Expand All @@ -235,6 +253,12 @@ def test_raise_getopt_errors(self):
# check_exit_code realizes the options with raise_getopt_errs=True
self.check_exit_code(options, ['-afoo'])

def test_list_of_ints(self):
self.assertEqual(list_of_ints(''), [])
self.assertEqual(list_of_ints('42'), [42])
self.assertEqual(list_of_ints('42,43'), [42, 43])
self.assertEqual(list_of_ints('42, 43'), [42, 43])


class EnvironmentOptions(ZDOptionsTestBase):

Expand Down
2 changes: 1 addition & 1 deletion src/zdaemon/zdoptions.py
Expand Up @@ -424,7 +424,7 @@ def existing_parent_dirpath(arg):
'does not exist.' % arg)


def _test():
def _test(): # pragma: nocover
# Stupid test program
z = ZDOptions()
z.add("program", "zdctl.program", "p:", "program=")
Expand Down

0 comments on commit fcc7f87

Please sign in to comment.