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