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 import contextlib | |
6 | |
7 from cStringIO import StringIO | |
8 | |
9 import test_env # pylint: disable=unused-import | |
10 | |
11 import coverage | |
12 | |
13 @contextlib.contextmanager | |
14 def _cover(**kwargs): | |
15 c = coverage.coverage(**kwargs) | |
16 c._warn_no_data = False # pylint: disable=protected-access | |
17 c.start() | |
18 try: | |
19 yield | |
20 finally: | |
21 c.stop() | |
22 c.save() | |
23 | |
24 | |
25 @contextlib.contextmanager | |
26 def _nop_cover(): | |
27 yield | |
28 | |
29 class CoverageContext(object): | |
30 def __init__(self, includes, omits, enabled=True): | |
31 self.opts = None | |
32 self.cov = None | |
33 self.enabled = enabled | |
34 | |
35 if enabled: | |
36 self.opts = { | |
37 'include': includes, | |
38 'omit': omits, | |
39 'data_suffix': True | |
40 } | |
41 self.cov = coverage.coverage(**self.opts) | |
42 self.cov.erase() | |
43 | |
44 def cleanup(self): | |
45 if self.enabled: | |
46 self.cov.combine() | |
47 | |
48 def report(self, verbose): | |
49 fail = False | |
50 | |
51 if self.enabled: | |
52 outf = StringIO() | |
53 fail = self.cov.report(file=outf) != 100.0 | |
54 summary = outf.getvalue().replace('%- 15s' % 'Name', 'Coverage Report', 1) | |
55 if verbose: | |
56 print | |
57 print summary | |
58 elif fail: | |
59 print | |
60 lines = summary.splitlines() | |
61 lines[2:-2] = [l for l in lines[2:-2] | |
62 if not l.strip().endswith('100%')] | |
63 print '\n'.join(lines) | |
64 print | |
65 print 'FATAL: Test coverage is not at 100%.' | |
66 | |
67 return not fail | |
68 | |
69 def create_subprocess_context(self): | |
Vadim Sh.
2014/04/02 22:37:59
@contextlib.contextmanager
def create_subprocess_c
iannucci
2014/04/03 00:03:49
added comment and merged _cover and _nop_cover
| |
70 if self.enabled: | |
71 return _cover(**self.opts) | |
72 else: | |
73 return _nop_cover() | |
OLD | NEW |