| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 from cStringIO import StringIO | 5 from cStringIO import StringIO |
| 6 | 6 |
| 7 import test_env # pylint: disable=W0611 | 7 import test_env # pylint: disable=W0611 |
| 8 | 8 |
| 9 import coverage | 9 import coverage |
| 10 | 10 |
| 11 # This is instead of a contextmanager because it causes old pylints to crash :( | 11 # This is instead of a contextmanager because it causes old pylints to crash :( |
| 12 class _Cover(object): | 12 class _Cover(object): |
| 13 def __init__(self, enabled, maybe_kwargs): | 13 def __init__(self, enabled, maybe_kwargs): |
| 14 self.enabled = enabled | 14 self.enabled = enabled |
| 15 self.kwargs = maybe_kwargs or {} | 15 self.kwargs = maybe_kwargs or {} |
| 16 self.c = None | 16 self.c = None |
| 17 | 17 |
| 18 def __call__(self, **kwargs): |
| 19 new_kwargs = self.kwargs |
| 20 if self.enabled: |
| 21 new_kwargs = new_kwargs.copy() |
| 22 new_kwargs.update(kwargs) |
| 23 return _Cover(self.enabled, new_kwargs) |
| 24 |
| 18 def __enter__(self): | 25 def __enter__(self): |
| 19 if self.enabled: | 26 if self.enabled: |
| 20 self.c = coverage.coverage(**self.kwargs) | 27 if self.c is None: |
| 21 self.c._warn_no_data = False | 28 self.c = coverage.coverage(**self.kwargs) |
| 29 self.c._warn_no_data = False |
| 22 self.c.start() | 30 self.c.start() |
| 23 | 31 |
| 24 def __exit__(self, *_): | 32 def __exit__(self, *_): |
| 25 if self.enabled: | 33 if self.enabled: |
| 26 self.c.stop() | 34 self.c.stop() |
| 27 self.c.save() | 35 self.c.save() |
| 28 | 36 |
| 29 | 37 |
| 30 class CoverageContext(object): | 38 class CoverageContext(object): |
| 31 def __init__(self, name, includes, omits, cover_branches, html_report, | 39 def __init__(self, name, cover_branches, html_report, enabled=True): |
| 32 extra_coverage_data, enabled=True): | |
| 33 self.opts = None | 40 self.opts = None |
| 34 self.cov = None | 41 self.cov = None |
| 35 self.enabled = enabled | 42 self.enabled = enabled |
| 36 | 43 |
| 37 self.html_report = html_report | 44 self.html_report = html_report |
| 38 self.extra_data = extra_coverage_data or () | |
| 39 | 45 |
| 40 if enabled: | 46 if enabled: |
| 41 self.opts = { | 47 self.opts = { |
| 42 'include': includes, | |
| 43 'omit': omits, | |
| 44 'data_file': '.%s_coverage' % name, | 48 'data_file': '.%s_coverage' % name, |
| 45 'data_suffix': True, | 49 'data_suffix': True, |
| 46 'branch': cover_branches, | 50 'branch': cover_branches, |
| 47 } | 51 } |
| 48 self.cov = coverage.coverage(**self.opts) | 52 self.cov = coverage.coverage(**self.opts) |
| 49 self.cov.erase() | 53 self.cov.erase() |
| 50 | 54 |
| 51 def cleanup(self): | 55 def cleanup(self): |
| 52 if self.enabled: | 56 if self.enabled: |
| 53 self.cov.combine() | 57 self.cov.combine() |
| 54 | 58 |
| 55 def report(self, verbose): | 59 def report(self, verbose): |
| 56 fail = False | 60 fail = False |
| 57 | 61 |
| 58 if self.enabled: | 62 if self.enabled: |
| 59 include_files = set() | |
| 60 for datafile in self.extra_data: | |
| 61 # pylint: disable=W0212 | |
| 62 lines, arcs = self.cov.data._read_file(datafile) | |
| 63 self.cov.data.add_line_data(lines) | |
| 64 self.cov.data.add_arc_data(arcs) | |
| 65 include_files.update(lines) | |
| 66 include_files.update(arcs) | |
| 67 | |
| 68 self.cov.config.include = list( | |
| 69 set(self.cov.config.include) | include_files) | |
| 70 | |
| 71 if self.html_report: | 63 if self.html_report: |
| 72 self.cov.html_report(directory=self.html_report) | 64 self.cov.html_report(directory=self.html_report) |
| 73 | 65 |
| 74 outf = StringIO() | 66 outf = StringIO() |
| 75 fail = self.cov.report(file=outf) != 100.0 | 67 fail = self.cov.report(file=outf) != 100.0 |
| 76 summary = outf.getvalue().replace('%- 15s' % 'Name', 'Coverage Report', 1) | 68 summary = outf.getvalue().replace('%- 15s' % 'Name', 'Coverage Report', 1) |
| 77 if verbose: | 69 if verbose: |
| 78 print | 70 print |
| 79 print summary | 71 print summary |
| 80 elif fail: | 72 elif fail: |
| 81 print | 73 print |
| 82 lines = summary.splitlines() | 74 lines = summary.splitlines() |
| 83 lines[2:-2] = [l for l in lines[2:-2] | 75 lines[2:-2] = [l for l in lines[2:-2] |
| 84 if not l.strip().endswith('100%')] | 76 if not l.strip().endswith('100%')] |
| 85 print '\n'.join(lines) | 77 print '\n'.join(lines) |
| 86 print | 78 print |
| 87 print 'FATAL: Test coverage is not at 100%.' | 79 print 'FATAL: Test coverage is not at 100%.' |
| 88 | 80 |
| 89 return not fail | 81 return not fail |
| 90 | 82 |
| 91 def create_subprocess_context(self): | 83 def create_subprocess_context(self): |
| 92 # Can't have this method be the contextmanager because otherwise | 84 # Can't have this method be the contextmanager because otherwise |
| 93 # self (and self.cov) will get pickled to the subprocess, and we don't want | 85 # self (and self.cov) will get pickled to the subprocess, and we don't want |
| 94 # that :( | 86 # that :( |
| 95 return _Cover(self.enabled, self.opts) | 87 return _Cover(self.enabled, self.opts) |
| OLD | NEW |