| 1 | # -*- coding: utf-8 -*- |
---|
| 2 | # |
---|
| 3 | # Copyright (C) 2005-2007 Christopher Lenz <cmlenz@gmx.de> |
---|
| 4 | # Copyright (C) 2008 Matt Good <matt@matt-good.net> |
---|
| 5 | # Copyright (C) 2008-2010 Edgewall Software |
---|
| 6 | # All rights reserved. |
---|
| 7 | # |
---|
| 8 | # This software is licensed as described in the file COPYING, which |
---|
| 9 | # you should have received as part of this distribution. The terms |
---|
| 10 | # are also available at http://bitten.edgewall.org/wiki/License. |
---|
| 11 | |
---|
0 | 12 | from distutils import log |
---|
0 | 13 | from distutils.errors import DistutilsOptionError |
---|
0 | 14 | import os |
---|
0 | 15 | import re |
---|
0 | 16 | from StringIO import StringIO |
---|
0 | 17 | import sys |
---|
0 | 18 | import time |
---|
0 | 19 | from pkg_resources import Distribution, EntryPoint, PathMetadata, \ |
---|
0 | 20 | normalize_path, require, working_set |
---|
0 | 21 | from setuptools.command.test import test |
---|
0 | 22 | from unittest import _TextTestResult, TextTestRunner |
---|
| 23 | |
---|
0 | 24 | from bitten import __version__ as VERSION |
---|
0 | 25 | from bitten.util import xmlio |
---|
| 26 | |
---|
0 | 27 | __docformat__ = 'restructuredtext en' |
---|
| 28 | |
---|
| 29 | |
---|
0 | 30 | class XMLTestResult(_TextTestResult): |
---|
| 31 | |
---|
0 | 32 | def __init__(self, stream, descriptions, verbosity): |
---|
1 | 33 | _TextTestResult.__init__(self, stream, descriptions, verbosity) |
---|
1 | 34 | self.tests = [] |
---|
| 35 | |
---|
0 | 36 | def startTest(self, test): |
---|
275 | 37 | _TextTestResult.startTest(self, test) |
---|
275 | 38 | filename = sys.modules[test.__module__].__file__ |
---|
275 | 39 | if filename.endswith('.pyc') or filename.endswith('.pyo'): |
---|
3 | 40 | filename = filename[:-1] |
---|
275 | 41 | self.tests.append([test, filename, time.time(), None, None]) |
---|
| 42 | |
---|
0 | 43 | def stopTest(self, test): |
---|
275 | 44 | self.tests[-1][2] = time.time() - self.tests[-1][2] |
---|
275 | 45 | _TextTestResult.stopTest(self, test) |
---|
| 46 | |
---|
| 47 | |
---|
0 | 48 | class XMLTestRunner(TextTestRunner): |
---|
| 49 | |
---|
0 | 50 | def __init__(self, stream=sys.stdout, xml_stream=None): |
---|
1 | 51 | TextTestRunner.__init__(self, stream, descriptions=0, verbosity=2) |
---|
1 | 52 | self.xml_stream = xml_stream |
---|
| 53 | |
---|
0 | 54 | def _makeResult(self): |
---|
1 | 55 | return XMLTestResult(self.stream, self.descriptions, self.verbosity) |
---|
| 56 | |
---|
0 | 57 | def run(self, test): |
---|
1 | 58 | result = TextTestRunner.run(self, test) |
---|
1 | 59 | if not self.xml_stream: |
---|
0 | 60 | return result |
---|
| 61 | |
---|
1 | 62 | root = xmlio.Element('unittest-results') |
---|
276 | 63 | for testcase, filename, timetaken, stdout, stderr in result.tests: |
---|
275 | 64 | status = 'success' |
---|
275 | 65 | tb = None |
---|
| 66 | |
---|
275 | 67 | if testcase in [e[0] for e in result.errors]: |
---|
0 | 68 | status = 'error' |
---|
0 | 69 | tb = [e[1] for e in result.errors if e[0] is testcase][0] |
---|
275 | 70 | elif testcase in [f[0] for f in result.failures]: |
---|
0 | 71 | status = 'failure' |
---|
0 | 72 | tb = [f[1] for f in result.failures if f[0] is testcase][0] |
---|
| 73 | |
---|
275 | 74 | name = str(testcase) |
---|
275 | 75 | fixture = None |
---|
275 | 76 | description = testcase.shortDescription() or '' |
---|
275 | 77 | if description.startswith('doctest of '): |
---|
0 | 78 | name = 'doctest' |
---|
0 | 79 | fixture = description[11:] |
---|
0 | 80 | description = None |
---|
0 | 81 | else: |
---|
275 | 82 | match = re.match('(\w+)\s+\(([\w.]+)\)', name) |
---|
275 | 83 | if match: |
---|
275 | 84 | name = match.group(1) |
---|
275 | 85 | fixture = match.group(2) |
---|
| 86 | |
---|
275 | 87 | test_elem = xmlio.Element('test', file=filename, name=name, |
---|
275 | 88 | fixture=fixture, status=status, |
---|
275 | 89 | duration=timetaken) |
---|
275 | 90 | if description: |
---|
8 | 91 | test_elem.append(xmlio.Element('description')[description]) |
---|
275 | 92 | if stdout: |
---|
0 | 93 | test_elem.append(xmlio.Element('stdout')[stdout]) |
---|
275 | 94 | if stderr: |
---|
0 | 95 | test_elem.append(xmlio.Element('stdout')[stderr]) |
---|
275 | 96 | if tb: |
---|
0 | 97 | test_elem.append(xmlio.Element('traceback')[tb]) |
---|
275 | 98 | root.append(test_elem) |
---|
| 99 | |
---|
1 | 100 | root.write(self.xml_stream, newlines=True) |
---|
1 | 101 | return result |
---|
| 102 | |
---|
| 103 | |
---|
0 | 104 | class unittest(test): |
---|
0 | 105 | description = test.description + ', and optionally record code coverage' |
---|
| 106 | |
---|
0 | 107 | user_options = test.user_options + [ |
---|
0 | 108 | ('xml-output=', None, |
---|
0 | 109 | "Path to the XML file where test results are written to"), |
---|
0 | 110 | ('coverage-dir=', None, |
---|
0 | 111 | "Directory where coverage files are to be stored"), |
---|
0 | 112 | ('coverage-summary=', None, |
---|
0 | 113 | "Path to the file where the coverage summary should be stored"), |
---|
0 | 114 | ('coverage-method=', None, |
---|
0 | 115 | "Whether to use trace.py or coverage.py to collect code coverage. " |
---|
0 | 116 | "Valid options are 'trace' (the default) or 'coverage'.") |
---|
0 | 117 | ] |
---|
| 118 | |
---|
0 | 119 | def initialize_options(self): |
---|
0 | 120 | test.initialize_options(self) |
---|
0 | 121 | self.xml_output = None |
---|
0 | 122 | self.xml_output_file = None |
---|
0 | 123 | self.coverage_summary = None |
---|
0 | 124 | self.coverage_dir = None |
---|
0 | 125 | self.coverage_method = 'trace' |
---|
| 126 | |
---|
0 | 127 | def finalize_options(self): |
---|
0 | 128 | test.finalize_options(self) |
---|
| 129 | |
---|
0 | 130 | if self.xml_output is not None: |
---|
0 | 131 | output_dir = os.path.dirname(self.xml_output) or '.' |
---|
0 | 132 | if not os.path.exists(output_dir): |
---|
0 | 133 | os.makedirs(output_dir) |
---|
0 | 134 | self.xml_output_file = open(self.xml_output, 'w') |
---|
| 135 | |
---|
0 | 136 | if self.coverage_method not in ('trace', 'coverage', 'figleaf'): |
---|
0 | 137 | raise DistutilsOptionError('Unknown coverage method %r' % |
---|
0 | 138 | self.coverage_method) |
---|
| 139 | |
---|
0 | 140 | def run_tests(self): |
---|
0 | 141 | if self.coverage_summary: |
---|
0 | 142 | if self.coverage_method == 'coverage': |
---|
0 | 143 | self._run_with_coverage() |
---|
0 | 144 | elif self.coverage_method == 'figleaf': |
---|
0 | 145 | self._run_with_figleaf() |
---|
0 | 146 | else: |
---|
0 | 147 | self._run_with_trace() |
---|
0 | 148 | else: |
---|
0 | 149 | self._run_tests() |
---|
| 150 | |
---|
0 | 151 | def _run_with_figleaf(self): |
---|
0 | 152 | import figleaf |
---|
0 | 153 | figleaf.start() |
---|
0 | 154 | try: |
---|
0 | 155 | self._run_tests() |
---|
0 | 156 | finally: |
---|
0 | 157 | figleaf.stop() |
---|
0 | 158 | figleaf.write_coverage(self.coverage_summary) |
---|
| 159 | |
---|
0 | 160 | def _run_with_coverage(self): |
---|
0 | 161 | import coverage |
---|
0 | 162 | coverage.use_cache(False) |
---|
0 | 163 | coverage.start() |
---|
0 | 164 | try: |
---|
0 | 165 | self._run_tests() |
---|
0 | 166 | finally: |
---|
0 | 167 | coverage.stop() |
---|
| 168 | |
---|
0 | 169 | modules = [m for _, m in sys.modules.items() |
---|
0 | 170 | if m is not None and hasattr(m, '__file__') |
---|
0 | 171 | and os.path.splitext(m.__file__)[-1] in ('.py', '.pyc')] |
---|
| 172 | |
---|
| 173 | # Generate summary file |
---|
0 | 174 | buf = StringIO() |
---|
0 | 175 | coverage.report(modules, file=buf) |
---|
0 | 176 | buf.seek(0) |
---|
0 | 177 | fileobj = open(self.coverage_summary, 'w') |
---|
0 | 178 | try: |
---|
0 | 179 | filter_coverage(buf, fileobj) |
---|
0 | 180 | finally: |
---|
0 | 181 | fileobj.close() |
---|
| 182 | |
---|
0 | 183 | if self.coverage_dir: |
---|
0 | 184 | if not os.path.exists(self.coverage_dir): |
---|
0 | 185 | os.makedirs(self.coverage_dir) |
---|
0 | 186 | coverage.annotate(modules, directory=self.coverage_dir, |
---|
0 | 187 | ignore_errors=True) |
---|
| 188 | |
---|
0 | 189 | def _run_with_trace(self): |
---|
0 | 190 | from trace import Trace |
---|
0 | 191 | trace = Trace(ignoredirs=[sys.prefix, sys.exec_prefix], trace=False, |
---|
0 | 192 | count=True) |
---|
0 | 193 | try: |
---|
0 | 194 | trace.runfunc(self._run_tests) |
---|
0 | 195 | finally: |
---|
0 | 196 | results = trace.results() |
---|
0 | 197 | real_stdout = sys.stdout |
---|
0 | 198 | sys.stdout = open(self.coverage_summary, 'w') |
---|
0 | 199 | try: |
---|
0 | 200 | results.write_results(show_missing=True, summary=True, |
---|
0 | 201 | coverdir=self.coverage_dir) |
---|
0 | 202 | finally: |
---|
0 | 203 | sys.stdout.close() |
---|
0 | 204 | sys.stdout = real_stdout |
---|
| 205 | |
---|
0 | 206 | def _run_tests(self): |
---|
1 | 207 | old_path = sys.path[:] |
---|
1 | 208 | ei_cmd = self.get_finalized_command("egg_info") |
---|
1 | 209 | path_item = normalize_path(ei_cmd.egg_base) |
---|
1 | 210 | metadata = PathMetadata( |
---|
1 | 211 | path_item, normalize_path(ei_cmd.egg_info) |
---|
1 | 212 | ) |
---|
1 | 213 | dist = Distribution(path_item, metadata, project_name=ei_cmd.egg_name) |
---|
1 | 214 | working_set.add(dist) |
---|
1 | 215 | require(str(dist.as_requirement())) |
---|
1 | 216 | loader_ep = EntryPoint.parse("x=" + self.test_loader) |
---|
1 | 217 | loader_class = loader_ep.load(require=False) |
---|
| 218 | |
---|
1 | 219 | try: |
---|
1 | 220 | import unittest |
---|
1 | 221 | unittest.main( |
---|
1 | 222 | None, None, [unittest.__file__] + self.test_args, |
---|
1 | 223 | testRunner=XMLTestRunner(stream=sys.stdout, |
---|
1 | 224 | xml_stream=self.xml_output_file), |
---|
1 | 225 | testLoader=loader_class() |
---|
1 | 226 | ) |
---|
1 | 227 | except SystemExit, e: |
---|
1 | 228 | return e.code |
---|
| 229 | |
---|
| 230 | |
---|
0 | 231 | def filter_coverage(infile, outfile): |
---|
0 | 232 | for idx, line in enumerate(infile): |
---|
0 | 233 | if idx < 2 or line.startswith('--'): |
---|
0 | 234 | outfile.write(line) |
---|
0 | 235 | continue |
---|
0 | 236 | parts = line.split() |
---|
0 | 237 | name = parts[0] |
---|
0 | 238 | if name == 'TOTAL': |
---|
0 | 239 | continue |
---|
0 | 240 | if name not in sys.modules: |
---|
0 | 241 | outfile.write(line) |
---|
0 | 242 | continue |
---|
0 | 243 | filename = os.path.normpath(sys.modules[name].__file__) |
---|
0 | 244 | if filename.endswith('.pyc') or filename.endswith('.pyo'): |
---|
0 | 245 | filename = filename[:-1] |
---|
0 | 246 | outfile.write(line.rstrip() + ' ' + filename + '\n') |
---|
| 247 | |
---|
| 248 | |
---|
0 | 249 | def main(): |
---|
0 | 250 | from distutils.dist import Distribution |
---|
0 | 251 | from optparse import OptionParser |
---|
| 252 | |
---|
0 | 253 | parser = OptionParser(usage='usage: %prog [options] test_suite ...', |
---|
0 | 254 | version='%%prog %s' % VERSION) |
---|
0 | 255 | parser.add_option('-o', '--xml-output', action='store', dest='xml_output', |
---|
0 | 256 | metavar='FILE', help='write XML test results to FILE') |
---|
0 | 257 | parser.add_option('-d', '--coverage-dir', action='store', |
---|
0 | 258 | dest='coverage_dir', metavar='DIR', |
---|
0 | 259 | help='store coverage results in DIR') |
---|
0 | 260 | parser.add_option('-s', '--coverage-summary', action='store', |
---|
0 | 261 | dest='coverage_summary', metavar='FILE', |
---|
0 | 262 | help='write coverage summary to FILE') |
---|
0 | 263 | options, args = parser.parse_args() |
---|
0 | 264 | if len(args) < 1: |
---|
0 | 265 | parser.error('incorrect number of arguments') |
---|
| 266 | |
---|
0 | 267 | cmd = unittest(Distribution()) |
---|
0 | 268 | cmd.initialize_options() |
---|
0 | 269 | cmd.test_suite = args[0] |
---|
0 | 270 | if hasattr(options, 'xml_output'): |
---|
0 | 271 | cmd.xml_output = options.xml_output |
---|
0 | 272 | if hasattr(options, 'coverage_summary'): |
---|
0 | 273 | cmd.coverage_summary = options.coverage_summary |
---|
0 | 274 | if hasattr(options, 'coverage_dir'): |
---|
0 | 275 | cmd.coverage_dir = options.coverage_dir |
---|
0 | 276 | cmd.finalize_options() |
---|
0 | 277 | cmd.run() |
---|
| 278 | |
---|
0 | 279 | if __name__ == '__main__': |
---|
0 | 280 | main() |
---|