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 apt-get install pip | 19 sudo apt-get install pip |
20 sudo pip install --upgrade coverage | 20 sudo pip install --upgrade coverage |
21 %s""") % (version, msg) | 21 %s""") % (version, msg) |
22 sys.exit(1) | 22 sys.exit(1) |
23 | 23 |
24 def covered_main(includes, require_native=None): | 24 def covered_main(includes, require_native=None, required_percentage=100.0): |
25 """Equivalent of unittest.main(), except that it gathers coverage data, and | 25 """Equivalent of unittest.main(), except that it gathers coverage data, and |
26 asserts if the test is not at 100% coverage. | 26 asserts if the test is not at 100% coverage. |
27 | 27 |
28 Args: | 28 Args: |
29 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. |
30 May also be a single path instead of a list. | 30 May also be a single path instead of a list. |
31 require_native (str) - If non-None, will require that | 31 require_native (str) - If non-None, will require that |
32 at least |require_native| version of coverage is installed on the | 32 at least |require_native| version of coverage is installed on the |
33 system with CTracer. | 33 system with CTracer. |
34 """ | 34 """ |
(...skipping 21 matching lines...) Expand all Loading... |
56 COVERAGE = coverage.coverage(include=includes) | 56 COVERAGE = coverage.coverage(include=includes) |
57 COVERAGE.start() | 57 COVERAGE.start() |
58 | 58 |
59 retcode = 0 | 59 retcode = 0 |
60 try: | 60 try: |
61 unittest.main() | 61 unittest.main() |
62 except SystemExit as e: | 62 except SystemExit as e: |
63 retcode = e.code or retcode | 63 retcode = e.code or retcode |
64 | 64 |
65 COVERAGE.stop() | 65 COVERAGE.stop() |
66 if COVERAGE.report() != 100.0: | 66 if COVERAGE.report() < required_percentage: |
67 print 'FATAL: not at 100% coverage.' | 67 print 'FATAL: not at required %f% coverage.' % required_percentage |
68 retcode = 2 | 68 retcode = 2 |
69 | 69 |
70 return retcode | 70 return retcode |
OLD | NEW |