Skip to content

Commit

Permalink
Use manuel to run doctest files.
Browse files Browse the repository at this point in the history
We can't use the codeblock plugin for the using-logging test, instead
resorting to manually execing the captured source. This is because
stdout is not captured by a codeblock, so logging would be configured
to go to the *real* stdout, not one captured by the next doctest we
do (where we do want to use the doctest syntax) and our output
wouldn't get seen. This was maddening to figure out.

You can't use the capture plugin to capture literal includes, so we
still have the chdir hacks.

Overall it's not a whole lot of a win, at least right now.

I changed some of the text strings that we log to make them a bit more
descriptive. This was part of debuging too, but I think they're
better.

I ran into a problem doing 'python -m ZConfig.tests.test_readme' that
I added a workaround for.
  • Loading branch information
jamadden committed Mar 16, 2017
1 parent 0aab511 commit 7c634d8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 21 deletions.
23 changes: 19 additions & 4 deletions ZConfig/tests/test_readme.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#
##############################################################################
import doctest
import manuel.capture
import manuel.doctest
import manuel.testing
import os
import os.path
import unittest
Expand All @@ -32,6 +35,14 @@ def tearDown(test):
logger.handlers = old['handlers']

def docSetUp(test):
# Python 2 makes __path__ and __file__ relative in some
# cases (such as when we're executing with the 'ZConfig'
# directory on sys.path as CWD). This breaks finding
# schema components when we change directories.
import ZConfig.components.logger as logger
logger.__file__ = os.path.abspath(logger.__file__)
logger.__path__ = [os.path.abspath(x) for x in logger.__path__]

old['pwd'] = os.getcwd()
doc_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
Expand All @@ -44,17 +55,21 @@ def docSetUp(test):
def docTearDown(test):
os.chdir(old['pwd'])
tearDown(test)
old.clear()

def test_suite():
plugins = manuel.doctest.Manuel(optionflags=options)
plugins += manuel.capture.Manuel()
return unittest.TestSuite([
doctest.DocFileSuite(
manuel.testing.TestSuite(
plugins,
'../../README.rst',
optionflags=options,
setUp=setUp, tearDown=tearDown,
),
doctest.DocFileSuite(
manuel.testing.TestSuite(
plugins,
'../../doc/using-logging.rst',
optionflags=options, globs=globals(),
globs={'resetLoggers': lambda: tearDown(None)},
setUp=docSetUp, tearDown=docTearDown,
),
])
Expand Down
7 changes: 5 additions & 2 deletions ZConfig/tests/test_schemaless.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"""
__docformat__ = "reStructuredText"

import doctest
import manuel.doctest
import manuel.testing
import unittest

from ZConfig.schemaless import Section
Expand All @@ -32,7 +33,9 @@ def test_init_with_data(self):
def test_suite():
return unittest.TestSuite([
unittest.defaultTestLoader.loadTestsFromName(__name__),
doctest.DocFileSuite("schemaless.txt", package="ZConfig")
manuel.testing.TestSuite(
manuel.doctest.Manuel(),
'../schemaless.txt'),
])

if __name__ == '__main__':
Expand Down
37 changes: 22 additions & 15 deletions doc/using-logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,33 @@ framework. ZConfig provides one simple convenience function to do this:

.. autofunction:: ZConfig.configureLoggers


Suppose we have the following logging configuration in a file called ``simple-root-config.conf``:

.. literalinclude:: simple-root-config.conf
:language: xml

We can load this file and pass its contents to ``configureLoggers``::
We can load this file and pass its contents to ``configureLoggers``:

.. code-block:: python
from ZConfig import configureLoggers
with open('simple-root-config.conf') as f:
configureLoggers(f.read())
>>> from ZConfig import configureLoggers
>>> with open('simple-root-config.conf') as f:
... configureLoggers(f.read())
.. -> src
>>> import six
>>> six.exec_(src)
When this returns, the root logger is configured to output messages
logged at INFO or above to the console, as we can see in the following
example::
example:

.. code-block:: pycon
>>> from logging import getLogger
>>> getLogger().info('An info message')
INFO root An info message
>>> getLogger().debug('A debug message')
>>> getLogger().info('We see an info message')
INFO root We see an info message
>>> getLogger().debug('We do not see a debug message')
A more common configuration would see STDOUT replaced with a path to
the file into which log entries would be written.
Expand Down Expand Up @@ -73,17 +80,17 @@ If we load that configuration from ``root-and-child-config.conf``, we
can expect this behaviour:

..
>>> tearDown(None)
>>> resetLoggers()
.. code-block:: pycon
>>> with open('root-and-child-config.conf') as f:
... configureLoggers(f.read())
>>> getLogger().info('An info message')
INFO root An info message
>>> getLogger().debug('A debug message')
>>> getLogger('my.package').debug('A debug message')
DEBUG my.package A debug message
>>> getLogger().info('Here is another info message')
INFO root Here is another info message
>>> getLogger().debug('This debug message is hidden')
>>> getLogger('my.package').debug('The debug message for my.package shows')
DEBUG my.package The debug message for my.package shows
.. _logging-handlers:

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def alltests():

tests_require = [
'docutils',
'manuel',
'zope.testrunner',
]

Expand Down

0 comments on commit 7c634d8

Please sign in to comment.