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