Skip to content

Commit

Permalink
Merge pull request #3 from zopefoundation/test-coverage
Browse files Browse the repository at this point in the history
Improve test coverage
  • Loading branch information
mgedmin committed Nov 15, 2017
2 parents 08955a7 + 5c5d19b commit 8a79171
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 48 deletions.
4 changes: 3 additions & 1 deletion src/zope/app/server/main.py
Expand Up @@ -34,6 +34,8 @@ class ZopeOptions(zdoptions.ZDOptions):
logsectionname = None

def default_configfile(self):
# XXX: this probably assumes a monolithic zope 3 source tree
# layout and isn't likely to work
dir = os.path.normpath(
os.path.join(os.path.dirname(__file__),
os.pardir, os.pardir, os.pardir, os.pardir))
Expand Down Expand Up @@ -92,7 +94,7 @@ def load_options(args=None):
options.realize(args)
options = options.configroot

if options.path:
if options and options.path:
sys.path[:0] = [os.path.abspath(p) for p in options.path]
return options

Expand Down
22 changes: 21 additions & 1 deletion src/zope/app/server/tests/__init__.py
@@ -1 +1,21 @@
# Make this a package.
import sys
from contextlib import contextmanager
from io import StringIO, BytesIO


# By using io.BytesIO() instead of cStringIO.StringIO() on Python 2 we make
# sure we're not trying to accidentally print unicode to stdout/stderr.
NativeStringIO = BytesIO if str is bytes else StringIO


@contextmanager
def capture_output(stdout=None, stderr=None):
old_stdout = sys.stdout
old_stderr = sys.stderr
sys.stdout = stdout = stdout or NativeStringIO()
sys.stderr = stderr = stderr or NativeStringIO()
try:
yield stdout, stderr
finally:
sys.stdout = old_stdout
sys.stderr = old_stderr
20 changes: 20 additions & 0 deletions src/zope/app/server/tests/test_main.py
@@ -0,0 +1,20 @@
import unittest

from zope.app.server.main import main, load_options
from zope.app.server.tests import capture_output


class TestMain(unittest.TestCase):

def test_main(self):
with self.assertRaises(SystemExit) as cm:
with capture_output() as (stdout, stderr):
main(['invalid'])
self.assertEqual(cm.exception.code, 2)
self.assertNotEqual(stderr.getvalue(), "")

def test_load_options_no_config_file_specified(self):
options = load_options([])
self.assertIs(options, None)

# TODO: somebody finish this please
56 changes: 12 additions & 44 deletions src/zope/app/server/tests/test_mkzopeinstance.py
Expand Up @@ -16,31 +16,11 @@

import os
import shutil
import sys
import tempfile
import unittest
from contextlib import contextmanager
from io import StringIO, BytesIO

from zope.app.server import mkzopeinstance


# By using io.BytesIO() instead of cStringIO.StringIO() on Python 2 we make
# sure we're not trying to accidentally print unicode to stdout/stderr.
NativeStringIO = BytesIO if str is bytes else StringIO


@contextmanager
def capture_output(stdout=None, stderr=None):
old_stdout = sys.stdout
old_stderr = sys.stderr
sys.stdout = stdout = stdout or NativeStringIO()
sys.stderr = stderr = stderr or NativeStringIO()
try:
yield stdout, stderr
finally:
sys.stdout = old_stdout
sys.stderr = old_stderr
from zope.app.server.tests import capture_output


class ArgumentParsingTestCase(unittest.TestCase):
Expand Down Expand Up @@ -69,15 +49,12 @@ def test_help_short(self):
self.check_stdout_content(["-h"])

def check_stdout_content(self, args):
try:
with self.assertRaises(SystemExit) as cm:
with capture_output() as (stdout, stderr):
self.parse_args(args, stdout, stderr)
except SystemExit as e:
self.assertEqual(e.code, 0)
self.assert_(stdout.getvalue())
self.failIf(stderr.getvalue())
else:
self.fail("expected SystemExit")
self.assertEqual(cm.exception.code, 0)
self.assertNotEqual(stdout.getvalue(), "")
self.assertEqual(stderr.getvalue(), "")

def test_without_destination(self):
options = self.parse_args([])
Expand Down Expand Up @@ -145,12 +122,9 @@ def test_password_manager_long(self):
self.assertEqual(options.password_manager, "Manager")

def test_junk_positional_arg(self):
try:
with self.assertRaises(SystemExit) as cm:
self.parse_args(["junk"])
except SystemExit as e:
self.assert_(e.code)
else:
self.fail("expected SystemExit")
self.assertNotEqual(cm.exception.code, 0)


class InputCollectionTestCase(unittest.TestCase):
Expand Down Expand Up @@ -286,13 +260,10 @@ def test_get_wrong_password_manager(self):
options = self.createOptions()
options.password_manager = "Unknown"
app = ControlledInputApplication(options, [])
try:
with self.assertRaises(SystemExit) as cm:
with capture_output() as (stdout, stderr):
app.get_password_manager()
except SystemExit as e:
self.assertEqual(e.code, 1)
else:
self.fail("expected SystemExit")
self.assertEqual(cm.exception.code, 1)
self.failUnless(stderr.getvalue())
self.failIf(stdout.getvalue())
self.failUnless(app.all_input_consumed())
Expand All @@ -310,13 +281,10 @@ def test_get_password(self):
def test_get_password_not_verified(self):
options = self.createOptions()
app = ControlledInputApplication(options, ["foo", "bar"])
try:
with self.assertRaises(SystemExit) as cm:
with capture_output() as (stdout, stderr):
app.get_password()
except SystemExit as e:
self.assertEqual(e.code, 1)
else:
self.fail("expected SystemExit")
self.assertEqual(cm.exception.code, 1)
self.failUnless(stderr.getvalue())
self.failUnless(stdout.getvalue())
self.failUnless(app.all_input_consumed())
Expand Down Expand Up @@ -404,7 +372,7 @@ def test_zope_namespace_package_doesnt_affect_software_home(self):
# cleanup the fake 'zope' module
if old_path is None:
del zope.__file__
else:
else: # pragma: no cover
zope.__file__ = old_path


Expand Down
23 changes: 23 additions & 0 deletions src/zope/app/server/tests/test_servercontrol.py
@@ -0,0 +1,23 @@
import unittest

import zope.app.server.main
from zope.interface.verify import verifyObject
from zope.app.server.servercontrol import serverControl
from zope.applicationcontrol.interfaces import IServerControl


class TestServerControl(unittest.TestCase):

def tearDown(self):
zope.app.server.main.exit_status = None

def test(self):
verifyObject(IServerControl, serverControl)

def test_shutdown(self):
serverControl.shutdown()
self.assertEqual(zope.app.server.main.exit_status, 0)

def test_restart(self):
serverControl.restart()
self.assertEqual(zope.app.server.main.exit_status, 1)
7 changes: 7 additions & 0 deletions src/zope/app/server/tests/test_wsgi.py
Expand Up @@ -3,10 +3,17 @@
from zope.interface.verify import verifyObject
from zope.app.server.servertype import IServerType
from zope.app.server.wsgi import http, pmhttp
from zope.server.http.wsgihttpserver import WSGIHTTPServer


class TestWSGIServerType(unittest.TestCase):

def test(self):
verifyObject(IServerType, http)
verifyObject(IServerType, pmhttp)

def test_create(self):
dispatcher = 'my task dispatcher'
db = 'my database'
server = http.create("HTTP", dispatcher, db)
self.assertIsInstance(server, WSGIHTTPServer)
7 changes: 7 additions & 0 deletions src/zope/app/server/tests/test_zpasswd.py
@@ -0,0 +1,7 @@
import unittest


class TestZpasswdDeprecation(unittest.TestCase):

def test(self):
from zope.app.server.zpasswd import Principal # noqa
1 change: 0 additions & 1 deletion src/zope/app/server/wsgi.py
Expand Up @@ -13,7 +13,6 @@
##############################################################################
"""WSGI-compliant HTTP server setup.
"""
__docformat__ = "reStructuredText"

import zope.interface
from zope.server.http.commonaccesslogger import CommonAccessLogger
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Expand Up @@ -14,4 +14,4 @@ deps =
coverage
commands =
coverage run -m zope.testrunner --test-path=src {posargs:-pvc}
coverage report -m
coverage report -m --fail-under=90

0 comments on commit 8a79171

Please sign in to comment.