OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import distutils.version |
| 6 import os |
| 7 import sys |
| 8 import textwrap |
| 9 import unittest |
| 10 |
| 11 ROOT_PATH = os.path.abspath(os.path.join( |
| 12 os.path.dirname(os.path.dirname(__file__)))) |
| 13 |
| 14 |
| 15 def native_error(msg, version): |
| 16 print textwrap.dedent("""\ |
| 17 ERROR: Native python-coverage (version: %s) is required to be |
| 18 installed on your PYTHONPATH to run this test. Recommendation: |
| 19 sudo pip install python-coverage |
| 20 %s""") % (version, msg) |
| 21 sys.exit(1) |
| 22 |
| 23 def covered_main(includes, require_native=None): |
| 24 """Equivalent of unittest.main(), except that it gathers coverage data, and |
| 25 asserts if the test is not at 100% coverage. |
| 26 |
| 27 Args: |
| 28 includes (list(str) or str) - List of paths to include in coverage report. |
| 29 May also be a single path instead of a list. |
| 30 require_native (str) - If non-None, will require that |
| 31 at least |require_native| version of coverage is installed on the |
| 32 system with CTracer. |
| 33 """ |
| 34 try: |
| 35 import coverage |
| 36 if require_native is not None: |
| 37 got_ver = coverage.__version__ |
| 38 if not coverage.collector.CTracer: |
| 39 native_error(( |
| 40 "Native python-coverage module required.\n" |
| 41 "Pure-python implementation (version: %s) found: %s" |
| 42 ) % (got_ver, coverage), require_native) |
| 43 if got_ver < distutils.version.LooseVersion(require_native): |
| 44 native_error("Wrong version (%s) found: %s" % (got_ver, coverage), |
| 45 require_native) |
| 46 except ImportError: |
| 47 if require_native is None: |
| 48 sys.path.insert(0, os.path.join(ROOT_PATH, 'third_party')) |
| 49 import coverage |
| 50 else: |
| 51 print ("ERROR: python-coverage (%s) is required to be installed on your " |
| 52 "PYTHONPATH to run this test." % require_native) |
| 53 sys.exit(1) |
| 54 |
| 55 COVERAGE = coverage.coverage(include=includes) |
| 56 COVERAGE.start() |
| 57 |
| 58 retcode = 0 |
| 59 try: |
| 60 unittest.main() |
| 61 except SystemExit as e: |
| 62 retcode = e.code or retcode |
| 63 |
| 64 COVERAGE.stop() |
| 65 if COVERAGE.report() != 100.0: |
| 66 print 'FATAL: not at 100% coverage.' |
| 67 retcode = 2 |
| 68 |
| 69 return retcode |
OLD | NEW |