summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xtools/code_coverage/croc.py21
-rw-r--r--tools/code_coverage/croc_test.py16
2 files changed, 37 insertions, 0 deletions
diff --git a/tools/code_coverage/croc.py b/tools/code_coverage/croc.py
index 9d2f3ac..1ea21ff 100755
--- a/tools/code_coverage/croc.py
+++ b/tools/code_coverage/croc.py
@@ -52,6 +52,14 @@ class CrocStatError(CrocError):
class CoverageStats(dict):
"""Coverage statistics."""
+ # Default dictionary values for this stat.
+ DEFAULTS = { 'files_covered': 0,
+ 'files_instrumented': 0,
+ 'files_executable': 0,
+ 'lines_covered': 0,
+ 'lines_instrumented': 0,
+ 'lines_executable': 0 }
+
def Add(self, coverage_stats):
"""Adds a contribution from another coverage stats dict.
@@ -64,6 +72,15 @@ class CoverageStats(dict):
else:
self[k] = v
+ def AddDefaults(self):
+ """Add some default stats which might be assumed present.
+
+ Do not clobber if already present. Adds resilience when evaling a
+ croc file which expects certain stats to exist."""
+ for k, v in self.DEFAULTS.iteritems():
+ if not k in self:
+ self[k] = v
+
#------------------------------------------------------------------------------
@@ -399,6 +416,10 @@ class Coverage(object):
return default
stats = self.tree.stats_by_group[group]
+ # Unit tests use real dicts, not CoverageStats objects,
+ # so we can't AddDefaults() on them.
+ if group == 'all' and hasattr(stats, 'AddDefaults'):
+ stats.AddDefaults()
try:
return eval(stat, {'__builtins__': {'S': self.GetStat}}, stats)
except Exception, e:
diff --git a/tools/code_coverage/croc_test.py b/tools/code_coverage/croc_test.py
index 035fcf9..36fc546 100644
--- a/tools/code_coverage/croc_test.py
+++ b/tools/code_coverage/croc_test.py
@@ -650,6 +650,22 @@ GetStat('nosuch') = 42
# In the real os.walk() call this prunes the walk.
self.assertEqual(self.mock_walk_return[0][1], ['subdir'])
+
+ def testEmptyTreeStats(self):
+ """Make sure we don't choke when absolutely nothing happened.
+
+ How we might hit this: bot compile error."""
+ c = self.cov_minimal
+ t = c.tree
+ t.stats_by_group['all'].AddDefaults()
+ self.assertEqual(t.stats_by_group, {
+ 'all': { 'files_covered': 0,
+ 'files_instrumented': 0,
+ 'files_executable': 0,
+ 'lines_covered': 0,
+ 'lines_instrumented': 0,
+ 'lines_executable': 0 }})
+
def testUpdateTreeStats(self):
"""Test UpdateTreeStats()."""