summaryrefslogtreecommitdiff
path: root/coverage/collector.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2016-01-30 15:51:08 -0500
committerNed Batchelder <ned@nedbatchelder.com>2016-01-30 15:51:08 -0500
commit58d4c073a8ab0c7b5d2ca1d700e77729a07e3aa7 (patch)
treed81a55facc686fdacde985590a5ee6c72b83b614 /coverage/collector.py
parent2002903f2257a361ad42ab4f2e338e5212f8eaf6 (diff)
parent60297f4fda470bc7d44de20384efd96fbbaea7de (diff)
downloadpython-coveragepy-git-58d4c073a8ab0c7b5d2ca1d700e77729a07e3aa7.tar.gz
Merged who-tests-what-170
Diffstat (limited to 'coverage/collector.py')
-rw-r--r--coverage/collector.py31
1 files changed, 30 insertions, 1 deletions
diff --git a/coverage/collector.py b/coverage/collector.py
index 0a43d87c..aabf10b7 100644
--- a/coverage/collector.py
+++ b/coverage/collector.py
@@ -37,6 +37,11 @@ class FileDisposition(object):
pass
+def should_start_context(frame):
+ fn_name = frame.f_code.co_name
+ if fn_name.startswith("test"):
+ return fn_name
+
class Collector(object):
"""Collects trace data.
@@ -116,6 +121,10 @@ class Collector(object):
"Couldn't trace with concurrency=%s, the module isn't installed." % concurrency
)
+ # Who-Tests-What is just a hack at the moment, so turn it on with an
+ # environment variable.
+ self.wtw = int(os.getenv('COVERAGE_WTW', 0))
+
self.reset()
if timid:
@@ -147,6 +156,10 @@ class Collector(object):
# pairs as keys (if branch coverage).
self.data = {}
+ # A dict mapping contexts to data dictionaries.
+ self.contexts = {}
+ self.contexts[None] = self.data
+
# A dictionary mapping file names to file tracer plugin names that will
# handle them.
self.file_tracers = {}
@@ -206,6 +219,11 @@ class Collector(object):
tracer.threading = self.threading
if hasattr(tracer, 'check_include'):
tracer.check_include = self.check_include
+ if self.wtw:
+ if hasattr(tracer, 'should_start_context'):
+ tracer.should_start_context = should_start_context
+ if hasattr(tracer, 'switch_context'):
+ tracer.switch_context = self.switch_context
fn = tracer.start()
self.tracers.append(tracer)
@@ -294,7 +312,7 @@ class Collector(object):
if stats:
print("\nCoverage.py tracer stats:")
for k in sorted(stats.keys()):
- print("%16s: %s" % (k, stats[k]))
+ print("%20s: %s" % (k, stats[k]))
if self.threading:
self.threading.settrace(None)
@@ -307,6 +325,11 @@ class Collector(object):
else:
self._start_tracer()
+ def switch_context(self, new_context):
+ data = self.contexts.setdefault(new_context, {})
+ for tracer in self.tracers:
+ tracer.data = data
+
def save_data(self, covdata):
"""Save the collected data to a `CoverageData`.
@@ -323,4 +346,10 @@ class Collector(object):
covdata.add_lines(abs_file_dict(self.data))
covdata.add_file_tracers(abs_file_dict(self.file_tracers))
+ if self.wtw:
+ # Just a hack, so just hack it.
+ import pprint
+ with open("coverage_wtw.py", "w") as wtw_out:
+ pprint.pprint(self.contexts, wtw_out)
+
self.reset()