/usr/share/pyshared/nose/plugins/cover.py is in python-nose 1.1.2-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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | """If you have Ned Batchelder's coverage_ module installed, you may activate a
coverage report with the ``--with-coverage`` switch or NOSE_WITH_COVERAGE
environment variable. The coverage report will cover any python source module
imported after the start of the test run, excluding modules that match
testMatch. If you want to include those modules too, use the ``--cover-tests``
switch, or set the NOSE_COVER_TESTS environment variable to a true value. To
restrict the coverage report to modules from a particular package or packages,
use the ``--cover-packages`` switch or the NOSE_COVER_PACKAGES environment
variable.
.. _coverage: http://www.nedbatchelder.com/code/modules/coverage.html
"""
import logging
import os
import re
import sys
from nose.plugins.base import Plugin
from nose.util import src, tolist
log = logging.getLogger(__name__)
COVERAGE_TEMPLATE = '''<html>
<head>
%(title)s
</head>
<body>
%(header)s
<style>
.coverage pre {float: left; margin: 0px 1em; border: none;
padding: 0px; }
.num pre { margin: 0px }
.nocov, .nocov pre {background-color: #faa}
.cov, .cov pre {background-color: #cfc}
div.coverage div { clear: both; height: 1.1em}
</style>
<div class="stats">
%(stats)s
</div>
<div class="coverage">
%(body)s
</div>
</body>
</html>
'''
COVERAGE_STATS_TEMPLATE = '''Covered: %(covered)s lines<br/>
Missed: %(missed)s lines<br/>
Skipped %(skipped)s lines<br/>
Percent: %(percent)s %%<br/>
'''
class Coverage(Plugin):
"""
Activate a coverage report using Ned Batchelder's coverage module.
"""
coverTests = False
coverPackages = None
_coverInstance = None
score = 200
status = {}
def coverInstance(self):
if not self._coverInstance:
import coverage
try:
self._coverInstance = coverage.coverage()
except coverage.CoverageException:
self._coverInstance = coverage
return self._coverInstance
coverInstance = property(coverInstance)
def options(self, parser, env):
"""
Add options to command line.
"""
Plugin.options(self, parser, env)
parser.add_option("--cover-package", action="append",
default=env.get('NOSE_COVER_PACKAGE'),
metavar="PACKAGE",
dest="cover_packages",
help="Restrict coverage output to selected packages "
"[NOSE_COVER_PACKAGE]")
parser.add_option("--cover-erase", action="store_true",
default=env.get('NOSE_COVER_ERASE'),
dest="cover_erase",
help="Erase previously collected coverage "
"statistics before run")
parser.add_option("--cover-tests", action="store_true",
dest="cover_tests",
default=env.get('NOSE_COVER_TESTS'),
help="Include test modules in coverage report "
"[NOSE_COVER_TESTS]")
parser.add_option("--cover-inclusive", action="store_true",
dest="cover_inclusive",
default=env.get('NOSE_COVER_INCLUSIVE'),
help="Include all python files under working "
"directory in coverage report. Useful for "
"discovering holes in test coverage if not all "
"files are imported by the test suite. "
"[NOSE_COVER_INCLUSIVE]")
parser.add_option("--cover-html", action="store_true",
default=env.get('NOSE_COVER_HTML'),
dest='cover_html',
help="Produce HTML coverage information")
parser.add_option('--cover-html-dir', action='store',
default=env.get('NOSE_COVER_HTML_DIR', 'cover'),
dest='cover_html_dir',
metavar='DIR',
help='Produce HTML coverage information in dir')
def configure(self, options, config):
"""
Configure plugin.
"""
try:
self.status.pop('active')
except KeyError:
pass
Plugin.configure(self, options, config)
if config.worker:
return
if self.enabled:
try:
import coverage
except ImportError:
log.error("Coverage not available: "
"unable to import coverage module")
self.enabled = False
return
self.conf = config
self.coverErase = options.cover_erase
self.coverTests = options.cover_tests
self.coverPackages = []
if options.cover_packages:
for pkgs in [tolist(x) for x in options.cover_packages]:
self.coverPackages.extend(pkgs)
self.coverInclusive = options.cover_inclusive
if self.coverPackages:
log.info("Coverage report will include only packages: %s",
self.coverPackages)
self.coverHtmlDir = None
if options.cover_html:
self.coverHtmlDir = options.cover_html_dir
log.debug('Will put HTML coverage report in %s', self.coverHtmlDir)
if self.enabled:
self.status['active'] = True
def begin(self):
"""
Begin recording coverage information.
"""
log.debug("Coverage begin")
self.skipModules = sys.modules.keys()[:]
if self.coverErase:
log.debug("Clearing previously collected coverage statistics")
self.coverInstance.erase()
self.coverInstance.exclude('#pragma[: ]+[nN][oO] [cC][oO][vV][eE][rR]')
self.coverInstance.start()
def report(self, stream):
"""
Output code coverage report.
"""
log.debug("Coverage report")
self.coverInstance.stop()
self.coverInstance.save()
modules = [ module
for name, module in sys.modules.items()
if self.wantModuleCoverage(name, module) ]
log.debug("Coverage report will cover modules: %s", modules)
self.coverInstance.report(modules, file=stream)
if self.coverHtmlDir:
log.debug("Generating HTML coverage report")
if hasattr(self.coverInstance, 'html_report'):
self.coverInstance.html_report(modules, self.coverHtmlDir)
else:
self.report_html(modules)
def report_html(self, modules):
if not os.path.exists(self.coverHtmlDir):
os.makedirs(self.coverHtmlDir)
files = {}
for m in modules:
if hasattr(m, '__name__') and hasattr(m, '__file__'):
files[m.__name__] = m.__file__
self.coverInstance.annotate(files.values())
global_stats = {'covered': 0, 'missed': 0, 'skipped': 0}
file_list = []
for m, f in files.iteritems():
if f.endswith('pyc'):
f = f[:-1]
coverfile = f+',cover'
outfile, stats = self.htmlAnnotate(m, f, coverfile,
self.coverHtmlDir)
for field in ('covered', 'missed', 'skipped'):
global_stats[field] += stats[field]
file_list.append((stats['percent'], m, outfile, stats))
os.unlink(coverfile)
file_list.sort()
global_stats['percent'] = self.computePercent(
global_stats['covered'], global_stats['missed'])
# Now write out an index file for the coverage HTML
index = open(os.path.join(self.coverHtmlDir, 'index.html'), 'w')
index.write('<html><head><title>Coverage Index</title></head>'
'<body><p>')
index.write(COVERAGE_STATS_TEMPLATE % global_stats)
index.write('<table><tr><td>File</td><td>Covered</td><td>Missed'
'</td><td>Skipped</td><td>Percent</td></tr>')
for junk, name, outfile, stats in file_list:
stats['a'] = '<a href="%s">%s</a>' % (outfile, name)
index.write('<tr><td>%(a)s</td><td>%(covered)s</td><td>'
'%(missed)s</td><td>%(skipped)s</td><td>'
'%(percent)s %%</td></tr>' % stats)
index.write('</table></p></html')
index.close()
def htmlAnnotate(self, name, file, coverfile, outputDir):
log.debug('Name: %s file: %s' % (name, file, ))
rows = []
data = open(coverfile, 'r').read().split('\n')
padding = len(str(len(data)))
stats = {'covered': 0, 'missed': 0, 'skipped': 0}
for lineno, line in enumerate(data):
lineno += 1
if line:
status = line[0]
line = line[2:]
else:
status = ''
line = ''
lineno = (' ' * (padding - len(str(lineno)))) + str(lineno)
for old, new in (('&', '&'), ('<', '<'), ('>', '>'),
('"', '"'), ):
line = line.replace(old, new)
if status == '!':
rows.append('<div class="nocov"><span class="num"><pre>'
'%s</pre></span><pre>%s</pre></div>' % (lineno,
line))
stats['missed'] += 1
elif status == '>':
rows.append('<div class="cov"><span class="num"><pre>%s</pre>'
'</span><pre>%s</pre></div>' % (lineno, line))
stats['covered'] += 1
else:
rows.append('<div class="skip"><span class="num"><pre>%s</pre>'
'</span><pre>%s</pre></div>' % (lineno, line))
stats['skipped'] += 1
stats['percent'] = self.computePercent(stats['covered'],
stats['missed'])
html = COVERAGE_TEMPLATE % {'title': '<title>%s</title>' % name,
'header': name,
'body': '\n'.join(rows),
'stats': COVERAGE_STATS_TEMPLATE % stats,
}
outfilename = name + '.html'
outfile = open(os.path.join(outputDir, outfilename), 'w')
outfile.write(html)
outfile.close()
return outfilename, stats
def computePercent(self, covered, missed):
if covered + missed == 0:
percent = 1
else:
percent = covered/(covered+missed+0.0)
return int(percent * 100)
def wantModuleCoverage(self, name, module):
if not hasattr(module, '__file__'):
log.debug("no coverage of %s: no __file__", name)
return False
module_file = src(module.__file__)
if not module_file or not module_file.endswith('.py'):
log.debug("no coverage of %s: not a python file", name)
return False
if self.coverPackages:
for package in self.coverPackages:
if (re.findall(r'^%s\b' % re.escape(package), name)
and (self.coverTests
or not self.conf.testMatch.search(name))):
log.debug("coverage for %s", name)
return True
if name in self.skipModules:
log.debug("no coverage for %s: loaded before coverage start",
name)
return False
if self.conf.testMatch.search(name) and not self.coverTests:
log.debug("no coverage for %s: is a test", name)
return False
# accept any package that passed the previous tests, unless
# coverPackages is on -- in that case, if we wanted this
# module, we would have already returned True
return not self.coverPackages
def wantFile(self, file, package=None):
"""If inclusive coverage enabled, return true for all source files
in wanted packages.
"""
if self.coverInclusive:
if file.endswith(".py"):
if package and self.coverPackages:
for want in self.coverPackages:
if package.startswith(want):
return True
else:
return True
return None
|