Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(133)

Side by Side Diff: scripts/slave/unittests/expect_tests/cover.py

Issue 220353003: Replace recipes_test.py with a new parallel expectation-based test runner. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/build
Patch Set: address comments Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 try:
10 import coverage
11 coverage_version = coverage.__version__
12 except ImportError:
13 coverage = None
14 coverage_version = None
15
16 from distutils.version import StrictVersion
17
18 if (not coverage or
19 not hasattr(coverage.collector, 'CTracer') or
20 not coverage.collector.CTracer or
21 StrictVersion(coverage_version) < StrictVersion('3.7.1')):
22 raise ImportError(
23 'This test requires natively-installed python-coverage (>=3.7.1). '
24 'Current is %s.' % coverage_version)
25
26
27 @contextlib.contextmanager
28 def _cover(enabled, maybe_kwargs):
29 if enabled:
30 c = coverage.coverage(**maybe_kwargs)
31 c._warn_no_data = False # pylint: disable=protected-access
32 c.start()
33 try:
34 yield
35 finally:
36 c.stop()
37 c.save()
38 else:
39 yield
40
41
42 class CoverageContext(object):
43 def __init__(self, includes, omits, enabled=True):
44 self.opts = None
45 self.cov = None
46 self.enabled = enabled
47
48 if enabled:
49 self.opts = {
50 'include': includes,
51 'omit': omits,
52 'data_suffix': True
53 }
54 self.cov = coverage.coverage(**self.opts)
55 self.cov.erase()
56
57 def cleanup(self):
58 if self.enabled:
59 self.cov.combine()
60
61 def report(self, verbose):
62 fail = False
63
64 if self.enabled:
65 outf = StringIO()
66 fail = self.cov.report(file=outf) != 100.0
67 summary = outf.getvalue().replace('%- 15s' % 'Name', 'Coverage Report', 1)
68 if verbose:
69 print
70 print summary
71 elif fail:
72 print
73 lines = summary.splitlines()
74 lines[2:-2] = [l for l in lines[2:-2]
75 if not l.strip().endswith('100%')]
76 print '\n'.join(lines)
77 print
78 print 'FATAL: Test coverage is not at 100%.'
79
80 return not fail
81
82 def create_subprocess_context(self):
83 # Can't have this method be the contextmanager because otherwise
84 # self (and self.cov) will get pickled to the subprocess, and we don't want
85 # that :(
86 return _cover(self.enabled, self.opts)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698