OLD | NEW |
1 #!/usr/bin/python2.4 | 1 #!/usr/bin/python2.4 |
2 # | 2 # |
3 # Copyright 2009, Google Inc. | 3 # Copyright 2009, Google Inc. |
4 # All rights reserved. | 4 # All rights reserved. |
5 # | 5 # |
6 # Redistribution and use in source and binary forms, with or without | 6 # Redistribution and use in source and binary forms, with or without |
7 # modification, are permitted provided that the following conditions are | 7 # modification, are permitted provided that the following conditions are |
8 # met: | 8 # met: |
9 # | 9 # |
10 # * Redistributions of source code must retain the above copyright | 10 # * Redistributions of source code must retain the above copyright |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 | 45 |
46 class CrocStatError(CrocError): | 46 class CrocStatError(CrocError): |
47 """Error evaluating coverage stat.""" | 47 """Error evaluating coverage stat.""" |
48 | 48 |
49 #------------------------------------------------------------------------------ | 49 #------------------------------------------------------------------------------ |
50 | 50 |
51 | 51 |
52 class CoverageStats(dict): | 52 class CoverageStats(dict): |
53 """Coverage statistics.""" | 53 """Coverage statistics.""" |
54 | 54 |
| 55 # Default dictionary values for this stat. |
| 56 DEFAULTS = { 'files_covered': 0, |
| 57 'files_instrumented': 0, |
| 58 'files_executable': 0, |
| 59 'lines_covered': 0, |
| 60 'lines_instrumented': 0, |
| 61 'lines_executable': 0 } |
| 62 |
55 def Add(self, coverage_stats): | 63 def Add(self, coverage_stats): |
56 """Adds a contribution from another coverage stats dict. | 64 """Adds a contribution from another coverage stats dict. |
57 | 65 |
58 Args: | 66 Args: |
59 coverage_stats: Statistics to add to this one. | 67 coverage_stats: Statistics to add to this one. |
60 """ | 68 """ |
61 for k, v in coverage_stats.iteritems(): | 69 for k, v in coverage_stats.iteritems(): |
62 if k in self: | 70 if k in self: |
63 self[k] += v | 71 self[k] += v |
64 else: | 72 else: |
65 self[k] = v | 73 self[k] = v |
66 | 74 |
| 75 def AddDefaults(self): |
| 76 """Add some default stats which might be assumed present. |
| 77 |
| 78 Do not clobber if already present. Adds resilience when evaling a |
| 79 croc file which expects certain stats to exist.""" |
| 80 for k, v in self.DEFAULTS.iteritems(): |
| 81 if not k in self: |
| 82 self[k] = v |
| 83 |
67 #------------------------------------------------------------------------------ | 84 #------------------------------------------------------------------------------ |
68 | 85 |
69 | 86 |
70 class CoveredFile(object): | 87 class CoveredFile(object): |
71 """Information about a single covered file.""" | 88 """Information about a single covered file.""" |
72 | 89 |
73 def __init__(self, filename, **kwargs): | 90 def __init__(self, filename, **kwargs): |
74 """Constructor. | 91 """Constructor. |
75 | 92 |
76 Args: | 93 Args: |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 # print the stats from just that subdir | 409 # print the stats from just that subdir |
393 | 410 |
394 # Make sure the group exists | 411 # Make sure the group exists |
395 if group not in self.tree.stats_by_group: | 412 if group not in self.tree.stats_by_group: |
396 if default is None: | 413 if default is None: |
397 raise CrocStatError('Group %r not found.' % group) | 414 raise CrocStatError('Group %r not found.' % group) |
398 else: | 415 else: |
399 return default | 416 return default |
400 | 417 |
401 stats = self.tree.stats_by_group[group] | 418 stats = self.tree.stats_by_group[group] |
| 419 # Unit tests use real dicts, not CoverageStats objects, |
| 420 # so we can't AddDefaults() on them. |
| 421 if group == 'all' and hasattr(stats, 'AddDefaults'): |
| 422 stats.AddDefaults() |
402 try: | 423 try: |
403 return eval(stat, {'__builtins__': {'S': self.GetStat}}, stats) | 424 return eval(stat, {'__builtins__': {'S': self.GetStat}}, stats) |
404 except Exception, e: | 425 except Exception, e: |
405 if default is None: | 426 if default is None: |
406 raise CrocStatError('Error evaluating stat %r: %s' % (stat, e)) | 427 raise CrocStatError('Error evaluating stat %r: %s' % (stat, e)) |
407 else: | 428 else: |
408 return default | 429 return default |
409 | 430 |
410 def PrintStat(self, stat, format=None, outfile=sys.stdout, **kwargs): | 431 def PrintStat(self, stat, format=None, outfile=sys.stdout, **kwargs): |
411 """Prints a statistic from the coverage object. | 432 """Prints a statistic from the coverage object. |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
709 html.Write() | 730 html.Write() |
710 | 731 |
711 # Normal exit | 732 # Normal exit |
712 return 0 | 733 return 0 |
713 | 734 |
714 | 735 |
715 #------------------------------------------------------------------------------ | 736 #------------------------------------------------------------------------------ |
716 | 737 |
717 if __name__ == '__main__': | 738 if __name__ == '__main__': |
718 sys.exit(Main(sys.argv)) | 739 sys.exit(Main(sys.argv)) |
OLD | NEW |