/usr/share/pyshared/zope/testrunner/coverage.py is in python-zope.testrunner 4.0.3-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | ##############################################################################
#
# Copyright (c) 2004-2008 Zope Foundation 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
"""
import trace
import sys
import os.path
import threading
import zope.testrunner.feature
from zope.testrunner.find import test_dirs
# 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))
class Coverage(zope.testrunner.feature.Feature):
tracer = None
directory = None
def __init__(self, runner):
super(Coverage, self).__init__(runner)
self.active = bool(runner.options.coverage)
def global_setup(self):
"""Executed once when the test runner is being set up."""
self.directory = os.path.join(os.getcwd(), self.runner.options.coverage)
# FIXME: This shouldn't rely on the find feature directly.
self.tracer = TestTrace(test_dirs(self.runner.options, {}),
trace=False, count=True)
self.tracer.start()
def early_teardown(self):
"""Executed once directly after all tests."""
self.tracer.stop()
def report(self):
"""Executed once after all tests have been run and all setup was
torn down."""
r = self.tracer.results()
r.write_results(summary=True, coverdir=self.directory)
|