diff options
author | jrg@chromium.org <jrg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-26 18:50:39 +0000 |
---|---|---|
committer | jrg@chromium.org <jrg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-26 18:50:39 +0000 |
commit | e2027b09db15ed5ecf0a481a006655ecd5fd097a (patch) | |
tree | 7bce88ade113400a78bd9cfcfce0475f45cfb883 /tools/code_coverage | |
parent | 8400e0328996ef46e390903b6c378549f7b13aa3 (diff) | |
download | chromium_src-e2027b09db15ed5ecf0a481a006655ecd5fd097a.zip chromium_src-e2027b09db15ed5ecf0a481a006655ecd5fd097a.tar.gz chromium_src-e2027b09db15ed5ecf0a481a006655ecd5fd097a.tar.bz2 |
Add resilience in croc to a failed build (e.g. 0 stats generated).
Makes a failure case suck less.
Review URL: http://codereview.chromium.org/660142
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@40138 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/code_coverage')
-rwxr-xr-x | tools/code_coverage/croc.py | 21 | ||||
-rw-r--r-- | tools/code_coverage/croc_test.py | 16 |
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().""" |