[Zope3-checkins]
SVN: zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/
Separate out code coverage support to its own module.
Christian Theune
ct at gocept.com
Sat May 3 10:36:04 EDT 2008
Log message for revision 86223:
Separate out code coverage support to its own module.
Changed:
U zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/__init__.py
A zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/coverage.py
U zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/testrunner-coverage.txt
U zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/tests.py
-=-
Modified: zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/__init__.py
===================================================================
--- zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/__init__.py 2008-05-03 14:34:00 UTC (rev 86222)
+++ zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/__init__.py 2008-05-03 14:36:04 UTC (rev 86223)
@@ -38,106 +38,11 @@
from zope.testing.testrunner.formatter import OutputFormatter, ColorfulOutputFormatter
from zope.testing.testrunner.formatter import terminal_has_colors
from zope.testing.testrunner.profiling import available_profilers
+from zope.testing.testrunner.coverage import TestTrace
-
real_pdb_set_trace = pdb.set_trace
-# For some reason, the doctest module resets the trace callable randomly, thus
-# disabling the coverage. Simply disallow the code from doing this. A real
-# trace can be set, so that debugging still works.
-osettrace = sys.settrace
-def settrace(trace):
- if trace is None:
- return
- osettrace(trace)
-class TestIgnore:
-
- def __init__(self, options):
- self._test_dirs = [self._filenameFormat(d[0]) + os.path.sep
- for d in test_dirs(options, {})]
- self._ignore = {}
- self._ignored = self._ignore.get
-
- def names(self, filename, modulename):
- # Special case: Modules generated from text files; i.e. doctests
- if modulename == '<string>':
- return True
- filename = self._filenameFormat(filename)
- ignore = self._ignored(filename)
- if ignore is None:
- ignore = True
- if filename is not None:
- for d in self._test_dirs:
- if filename.startswith(d):
- ignore = False
- break
- self._ignore[filename] = ignore
- return ignore
-
- def _filenameFormat(self, filename):
- return os.path.abspath(filename)
-
-if sys.platform == 'win32':
- #on win32 drive name can be passed with different case to `names`
- #that lets e.g. the coverage profiler skip complete files
- #_filenameFormat will make sure that all drive and filenames get lowercased
- #albeit trace coverage has still problems with lowercase drive letters
- #when determining the dotted module name
- OldTestIgnore = TestIgnore
-
- class TestIgnore(OldTestIgnore):
- def _filenameFormat(self, filename):
- return os.path.normcase(os.path.abspath(filename))
-
-class TestTrace(trace.Trace):
- """Simple tracer.
-
- >>> tracer = TestTrace(None, count=False, trace=False)
-
- Simple rules for use: you can't stop the tracer if it not started
- and you can't start the tracer if it already started:
-
- >>> tracer.stop()
- Traceback (most recent call last):
- File 'testrunner.py'
- AssertionError: can't stop if not started
-
- >>> tracer.start()
- >>> tracer.start()
- Traceback (most recent call last):
- File 'testrunner.py'
- AssertionError: can't start if already started
-
- >>> tracer.stop()
- >>> tracer.stop()
- Traceback (most recent call last):
- File 'testrunner.py'
- AssertionError: can't stop if not started
- """
-
- def __init__(self, options, **kw):
- trace.Trace.__init__(self, **kw)
- if options is not None:
- self.ignore = TestIgnore(options)
- self.started = False
-
- def start(self):
- assert not self.started, "can't start if already started"
- if not self.donothing:
- sys.settrace = settrace
- sys.settrace(self.globaltrace)
- threading.settrace(self.globaltrace)
- self.started = True
-
- def stop(self):
- assert self.started, "can't stop if not started"
- if not self.donothing:
- sys.settrace = osettrace
- sys.settrace(None)
- threading.settrace(None)
- self.started = False
-
class EndRun(Exception):
"""Indicate that the existing run call should stop
@@ -240,7 +145,7 @@
sys.exit()
if options.coverage:
- tracer = TestTrace(options, trace=False, count=True)
+ tracer = TestTrace(test_dirs(options, {}), trace=False, count=True)
tracer.start()
else:
tracer = None
Added: zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/coverage.py
===================================================================
--- zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/coverage.py (rev 0)
+++ zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/coverage.py 2008-05-03 14:36:04 UTC (rev 86223)
@@ -0,0 +1,120 @@
+##############################################################################
+#
+# Copyright (c) 2004-2008 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Code coverage analysis
+
+$Id: __init__.py 86218 2008-05-03 14:17:26Z ctheune $
+"""
+
+import trace
+import sys
+import os.path
+import threading
+
+
+# For some reason, the doctest module resets the trace callable randomly, thus
+# disabling the coverage. Simply disallow the code from doing this. A real
+# trace can be set, so that debugging still works.
+osettrace = sys.settrace
+def settrace(trace):
+ if trace is None:
+ return
+ osettrace(trace)
+
+
+class TestTrace(trace.Trace):
+ """Simple tracer.
+
+ >>> tracer = TestTrace([], count=False, trace=False)
+
+ Simple rules for use: you can't stop the tracer if it not started
+ and you can't start the tracer if it already started:
+
+ >>> tracer.stop()
+ Traceback (most recent call last):
+ File 'testrunner.py'
+ AssertionError: can't stop if not started
+
+ >>> tracer.start()
+ >>> tracer.start()
+ Traceback (most recent call last):
+ File 'testrunner.py'
+ AssertionError: can't start if already started
+
+ >>> tracer.stop()
+ >>> tracer.stop()
+ Traceback (most recent call last):
+ File 'testrunner.py'
+ AssertionError: can't stop if not started
+ """
+
+ def __init__(self, directories, **kw):
+ trace.Trace.__init__(self, **kw)
+ self.ignore = TestIgnore(directories)
+ self.started = False
+
+ def start(self):
+ assert not self.started, "can't start if already started"
+ if not self.donothing:
+ sys.settrace = settrace
+ sys.settrace(self.globaltrace)
+ threading.settrace(self.globaltrace)
+ self.started = True
+
+ def stop(self):
+ assert self.started, "can't stop if not started"
+ if not self.donothing:
+ sys.settrace = osettrace
+ sys.settrace(None)
+ threading.settrace(None)
+ self.started = False
+
+
+class TestIgnore:
+
+ def __init__(self, directories):
+ self._test_dirs = [self._filenameFormat(d[0]) + os.path.sep
+ for d in directories]
+ self._ignore = {}
+ self._ignored = self._ignore.get
+
+ def names(self, filename, modulename):
+ # Special case: Modules generated from text files; i.e. doctests
+ if modulename == '<string>':
+ return True
+ filename = self._filenameFormat(filename)
+ ignore = self._ignored(filename)
+ if ignore is None:
+ ignore = True
+ if filename is not None:
+ for d in self._test_dirs:
+ if filename.startswith(d):
+ ignore = False
+ break
+ self._ignore[filename] = ignore
+ return ignore
+
+ def _filenameFormat(self, filename):
+ return os.path.abspath(filename)
+
+if sys.platform == 'win32':
+ #on win32 drive name can be passed with different case to `names`
+ #that lets e.g. the coverage profiler skip complete files
+ #_filenameFormat will make sure that all drive and filenames get lowercased
+ #albeit trace coverage has still problems with lowercase drive letters
+ #when determining the dotted module name
+ OldTestIgnore = TestIgnore
+
+ class TestIgnore(OldTestIgnore):
+ def _filenameFormat(self, filename):
+ return os.path.normcase(os.path.abspath(filename))
Property changes on: zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/coverage.py
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/testrunner-coverage.txt
===================================================================
--- zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/testrunner-coverage.txt 2008-05-03 14:34:00 UTC (rev 86222)
+++ zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/testrunner-coverage.txt 2008-05-03 14:36:04 UTC (rev 86223)
@@ -91,9 +91,9 @@
... package = None
... test_path = [('/myproject/src/blah/foo', ''),
... ('/myproject/src/blah/bar', '')]
-
- >>> from zope.testing import testrunner
- >>> ignore = testrunner.TestIgnore(FauxOptions())
+ >>> from zope.testing.testrunner import coverage
+ >>> from zope.testing.testrunner import test_dirs
+ >>> ignore = coverage.TestIgnore(test_dirs(FauxOptions(), {}))
>>> ignore._test_dirs
['/myproject/src/blah/foo/', '/myproject/src/blah/bar/']
Modified: zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/tests.py
===================================================================
--- zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/tests.py 2008-05-03 14:34:00 UTC (rev 86222)
+++ zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/tests.py 2008-05-03 14:36:04 UTC (rev 86223)
@@ -107,7 +107,8 @@
setUp=setUp, tearDown=tearDown,
optionflags=doctest.ELLIPSIS+doctest.NORMALIZE_WHITESPACE,
checker=checker),
- doctest.DocTestSuite('zope.testing.testrunner')
+ doctest.DocTestSuite('zope.testing.testrunner'),
+ doctest.DocTestSuite('zope.testing.testrunner.coverage')
]
if sys.platform == 'win32':
More information about the Zope3-Checkins
mailing list