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

Unified 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: fix coverage race Created 6 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: scripts/slave/unittests/expect_tests/cover.py
diff --git a/scripts/slave/unittests/expect_tests/cover.py b/scripts/slave/unittests/expect_tests/cover.py
new file mode 100644
index 0000000000000000000000000000000000000000..21f717e170e20e2668f24e5d9480f63868bb9fce
--- /dev/null
+++ b/scripts/slave/unittests/expect_tests/cover.py
@@ -0,0 +1,89 @@
+# Copyright 2014 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+from cStringIO import StringIO
+
+try:
+ import coverage
+ coverage_version = coverage.__version__
+except ImportError:
+ coverage = None
+ coverage_version = None
+
+from distutils.version import StrictVersion
+
+if (not coverage or
+ not hasattr(coverage.collector, 'CTracer') or
+ not coverage.collector.CTracer or
+ StrictVersion(coverage_version) < StrictVersion('3.7')):
+ raise ImportError(
+ 'This test requires natively-installed python-coverage (>=3.7). '
+ 'Current is %s.' % coverage_version)
+
+
+# This is instead of a contextmanager because it causes old pylints to crash :(
+class _Cover(object):
+ def __init__(self, enabled, maybe_kwargs):
+ self.enabled = enabled
+ self.kwargs = maybe_kwargs or {}
+ self.c = None
+
+ def __enter__(self):
+ if self.enabled:
+ self.c = coverage.coverage(**self.kwargs)
+ self.c._warn_no_data = False
+ self.c.start()
+
+ def __exit__(self, *_):
+ if self.enabled:
+ self.c.stop()
+ self.c.save()
+
+
+class CoverageContext(object):
+ def __init__(self, name, includes, omits, enabled=True):
+ self.opts = None
+ self.cov = None
+ self.enabled = enabled
+
+ if enabled:
+ self.opts = {
+ 'include': includes,
+ 'omit': omits,
+ 'data_file': '.%s_coverage' % name,
+ 'data_suffix': True
+ }
+ self.cov = coverage.coverage(**self.opts)
+ self.cov.erase()
+
+ def cleanup(self):
+ if self.enabled:
+ self.cov.combine()
+
+ def report(self, verbose):
+ fail = False
+
+ if self.enabled:
+ outf = StringIO()
+ fail = self.cov.report(file=outf) != 100.0
+ summary = outf.getvalue().replace('%- 15s' % 'Name', 'Coverage Report', 1)
+ if verbose:
+ print
+ print summary
+ elif fail:
+ print
+ lines = summary.splitlines()
+ lines[2:-2] = [l for l in lines[2:-2]
+ if not l.strip().endswith('100%')]
+ print '\n'.join(lines)
+ print
+ print 'FATAL: Test coverage is not at 100%.'
+
+ return not fail
+
+ def create_subprocess_context(self):
+ # Can't have this method be the contextmanager because otherwise
+ # self (and self.cov) will get pickled to the subprocess, and we don't want
+ # that :(
+ return _Cover(self.enabled, self.opts)
« no previous file with comments | « scripts/slave/unittests/expect_tests/__init__.py ('k') | scripts/slave/unittests/expect_tests/handle_debug.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698