sbc | 0cec9d7 | 2014-11-24 17:25:29 | [diff] [blame] | 1 | """Results of coverage measurement.""" |
| 2 | |
| 3 | import os |
| 4 | |
| 5 | from coverage.backward import iitems, set, sorted # pylint: disable=W0622 |
| 6 | from coverage.misc import format_lines, join_regex, NoSource |
| 7 | from coverage.parser import CodeParser |
| 8 | |
| 9 | |
| 10 | class Analysis(object): |
| 11 | """The results of analyzing a code unit.""" |
| 12 | |
| 13 | def __init__(self, cov, code_unit): |
| 14 | self.coverage = cov |
| 15 | self.code_unit = code_unit |
| 16 | |
| 17 | self.filename = self.code_unit.filename |
| 18 | actual_filename, source = self.find_source(self.filename) |
| 19 | |
| 20 | self.parser = CodeParser( |
| 21 | text=source, filename=actual_filename, |
| 22 | exclude=self.coverage._exclude_regex('exclude') |
| 23 | ) |
| 24 | self.statements, self.excluded = self.parser.parse_source() |
| 25 | |
| 26 | # Identify missing statements. |
| 27 | executed = self.coverage.data.executed_lines(self.filename) |
| 28 | exec1 = self.parser.first_lines(executed) |
| 29 | self.missing = self.statements - exec1 |
| 30 | |
| 31 | if self.coverage.data.has_arcs(): |
| 32 | self.no_branch = self.parser.lines_matching( |
| 33 | join_regex(self.coverage.config.partial_list), |
| 34 | join_regex(self.coverage.config.partial_always_list) |
| 35 | ) |
| 36 | n_branches = self.total_branches() |
| 37 | mba = self.missing_branch_arcs() |
| 38 | n_partial_branches = sum( |
| 39 | [len(v) for k,v in iitems(mba) if k not in self.missing] |
| 40 | ) |
| 41 | n_missing_branches = sum([len(v) for k,v in iitems(mba)]) |
| 42 | else: |
| 43 | n_branches = n_partial_branches = n_missing_branches = 0 |
| 44 | self.no_branch = set() |
| 45 | |
| 46 | self.numbers = Numbers( |
| 47 | n_files=1, |
| 48 | n_statements=len(self.statements), |
| 49 | n_excluded=len(self.excluded), |
| 50 | n_missing=len(self.missing), |
| 51 | n_branches=n_branches, |
| 52 | n_partial_branches=n_partial_branches, |
| 53 | n_missing_branches=n_missing_branches, |
| 54 | ) |
| 55 | |
| 56 | def find_source(self, filename): |
| 57 | """Find the source for `filename`. |
| 58 | |
| 59 | Returns two values: the actual filename, and the source. |
| 60 | |
| 61 | The source returned depends on which of these cases holds: |
| 62 | |
| 63 | * The filename seems to be a non-source file: returns None |
| 64 | |
| 65 | * The filename is a source file, and actually exists: returns None. |
| 66 | |
| 67 | * The filename is a source file, and is in a zip file or egg: |
| 68 | returns the source. |
| 69 | |
| 70 | * The filename is a source file, but couldn't be found: raises |
| 71 | `NoSource`. |
| 72 | |
| 73 | """ |
| 74 | source = None |
| 75 | |
| 76 | base, ext = os.path.splitext(filename) |
| 77 | TRY_EXTS = { |
| 78 | '.py': ['.py', '.pyw'], |
| 79 | '.pyw': ['.pyw'], |
| 80 | } |
| 81 | try_exts = TRY_EXTS.get(ext) |
| 82 | if not try_exts: |
| 83 | return filename, None |
| 84 | |
| 85 | for try_ext in try_exts: |
| 86 | try_filename = base + try_ext |
| 87 | if os.path.exists(try_filename): |
| 88 | return try_filename, None |
| 89 | source = self.coverage.file_locator.get_zip_data(try_filename) |
| 90 | if source: |
| 91 | return try_filename, source |
| 92 | raise NoSource("No source for code: '%s'" % filename) |
| 93 | |
| 94 | def missing_formatted(self): |
| 95 | """The missing line numbers, formatted nicely. |
| 96 | |
| 97 | Returns a string like "1-2, 5-11, 13-14". |
| 98 | |
| 99 | """ |
| 100 | return format_lines(self.statements, self.missing) |
| 101 | |
| 102 | def has_arcs(self): |
| 103 | """Were arcs measured in this result?""" |
| 104 | return self.coverage.data.has_arcs() |
| 105 | |
| 106 | def arc_possibilities(self): |
| 107 | """Returns a sorted list of the arcs in the code.""" |
| 108 | arcs = self.parser.arcs() |
| 109 | return arcs |
| 110 | |
| 111 | def arcs_executed(self): |
| 112 | """Returns a sorted list of the arcs actually executed in the code.""" |
| 113 | executed = self.coverage.data.executed_arcs(self.filename) |
| 114 | m2fl = self.parser.first_line |
| 115 | executed = [(m2fl(l1), m2fl(l2)) for (l1,l2) in executed] |
| 116 | return sorted(executed) |
| 117 | |
| 118 | def arcs_missing(self): |
| 119 | """Returns a sorted list of the arcs in the code not executed.""" |
| 120 | possible = self.arc_possibilities() |
| 121 | executed = self.arcs_executed() |
| 122 | missing = [ |
| 123 | p for p in possible |
| 124 | if p not in executed |
| 125 | and p[0] not in self.no_branch |
| 126 | ] |
| 127 | return sorted(missing) |
| 128 | |
| 129 | def arcs_unpredicted(self): |
| 130 | """Returns a sorted list of the executed arcs missing from the code.""" |
| 131 | possible = self.arc_possibilities() |
| 132 | executed = self.arcs_executed() |
| 133 | # Exclude arcs here which connect a line to itself. They can occur |
| 134 | # in executed data in some cases. This is where they can cause |
| 135 | # trouble, and here is where it's the least burden to remove them. |
| 136 | unpredicted = [ |
| 137 | e for e in executed |
| 138 | if e not in possible |
| 139 | and e[0] != e[1] |
| 140 | ] |
| 141 | return sorted(unpredicted) |
| 142 | |
| 143 | def branch_lines(self): |
| 144 | """Returns a list of line numbers that have more than one exit.""" |
| 145 | exit_counts = self.parser.exit_counts() |
| 146 | return [l1 for l1,count in iitems(exit_counts) if count > 1] |
| 147 | |
| 148 | def total_branches(self): |
| 149 | """How many total branches are there?""" |
| 150 | exit_counts = self.parser.exit_counts() |
| 151 | return sum([count for count in exit_counts.values() if count > 1]) |
| 152 | |
| 153 | def missing_branch_arcs(self): |
| 154 | """Return arcs that weren't executed from branch lines. |
| 155 | |
| 156 | Returns {l1:[l2a,l2b,...], ...} |
| 157 | |
| 158 | """ |
| 159 | missing = self.arcs_missing() |
| 160 | branch_lines = set(self.branch_lines()) |
| 161 | mba = {} |
| 162 | for l1, l2 in missing: |
| 163 | if l1 in branch_lines: |
| 164 | if l1 not in mba: |
| 165 | mba[l1] = [] |
| 166 | mba[l1].append(l2) |
| 167 | return mba |
| 168 | |
| 169 | def branch_stats(self): |
| 170 | """Get stats about branches. |
| 171 | |
| 172 | Returns a dict mapping line numbers to a tuple: |
| 173 | (total_exits, taken_exits). |
| 174 | """ |
| 175 | |
| 176 | exit_counts = self.parser.exit_counts() |
| 177 | missing_arcs = self.missing_branch_arcs() |
| 178 | stats = {} |
| 179 | for lnum in self.branch_lines(): |
| 180 | exits = exit_counts[lnum] |
| 181 | try: |
| 182 | missing = len(missing_arcs[lnum]) |
| 183 | except KeyError: |
| 184 | missing = 0 |
| 185 | stats[lnum] = (exits, exits - missing) |
| 186 | return stats |
| 187 | |
| 188 | |
| 189 | class Numbers(object): |
| 190 | """The numerical results of measuring coverage. |
| 191 | |
| 192 | This holds the basic statistics from `Analysis`, and is used to roll |
| 193 | up statistics across files. |
| 194 | |
| 195 | """ |
| 196 | # A global to determine the precision on coverage percentages, the number |
| 197 | # of decimal places. |
| 198 | _precision = 0 |
| 199 | _near0 = 1.0 # These will change when _precision is changed. |
| 200 | _near100 = 99.0 |
| 201 | |
| 202 | def __init__(self, n_files=0, n_statements=0, n_excluded=0, n_missing=0, |
| 203 | n_branches=0, n_partial_branches=0, n_missing_branches=0 |
| 204 | ): |
| 205 | self.n_files = n_files |
| 206 | self.n_statements = n_statements |
| 207 | self.n_excluded = n_excluded |
| 208 | self.n_missing = n_missing |
| 209 | self.n_branches = n_branches |
| 210 | self.n_partial_branches = n_partial_branches |
| 211 | self.n_missing_branches = n_missing_branches |
| 212 | |
| 213 | def set_precision(cls, precision): |
| 214 | """Set the number of decimal places used to report percentages.""" |
| 215 | assert 0 <= precision < 10 |
| 216 | cls._precision = precision |
| 217 | cls._near0 = 1.0 / 10**precision |
| 218 | cls._near100 = 100.0 - cls._near0 |
| 219 | set_precision = classmethod(set_precision) |
| 220 | |
| 221 | def _get_n_executed(self): |
| 222 | """Returns the number of executed statements.""" |
| 223 | return self.n_statements - self.n_missing |
| 224 | n_executed = property(_get_n_executed) |
| 225 | |
| 226 | def _get_n_executed_branches(self): |
| 227 | """Returns the number of executed branches.""" |
| 228 | return self.n_branches - self.n_missing_branches |
| 229 | n_executed_branches = property(_get_n_executed_branches) |
| 230 | |
| 231 | def _get_pc_covered(self): |
| 232 | """Returns a single percentage value for coverage.""" |
| 233 | if self.n_statements > 0: |
| 234 | pc_cov = (100.0 * (self.n_executed + self.n_executed_branches) / |
| 235 | (self.n_statements + self.n_branches)) |
| 236 | else: |
| 237 | pc_cov = 100.0 |
| 238 | return pc_cov |
| 239 | pc_covered = property(_get_pc_covered) |
| 240 | |
| 241 | def _get_pc_covered_str(self): |
| 242 | """Returns the percent covered, as a string, without a percent sign. |
| 243 | |
| 244 | Note that "0" is only returned when the value is truly zero, and "100" |
| 245 | is only returned when the value is truly 100. Rounding can never |
| 246 | result in either "0" or "100". |
| 247 | |
| 248 | """ |
| 249 | pc = self.pc_covered |
| 250 | if 0 < pc < self._near0: |
| 251 | pc = self._near0 |
| 252 | elif self._near100 < pc < 100: |
| 253 | pc = self._near100 |
| 254 | else: |
| 255 | pc = round(pc, self._precision) |
| 256 | return "%.*f" % (self._precision, pc) |
| 257 | pc_covered_str = property(_get_pc_covered_str) |
| 258 | |
| 259 | def pc_str_width(cls): |
| 260 | """How many characters wide can pc_covered_str be?""" |
| 261 | width = 3 # "100" |
| 262 | if cls._precision > 0: |
| 263 | width += 1 + cls._precision |
| 264 | return width |
| 265 | pc_str_width = classmethod(pc_str_width) |
| 266 | |
| 267 | def __add__(self, other): |
| 268 | nums = Numbers() |
| 269 | nums.n_files = self.n_files + other.n_files |
| 270 | nums.n_statements = self.n_statements + other.n_statements |
| 271 | nums.n_excluded = self.n_excluded + other.n_excluded |
| 272 | nums.n_missing = self.n_missing + other.n_missing |
| 273 | nums.n_branches = self.n_branches + other.n_branches |
| 274 | nums.n_partial_branches = ( |
| 275 | self.n_partial_branches + other.n_partial_branches |
| 276 | ) |
| 277 | nums.n_missing_branches = ( |
| 278 | self.n_missing_branches + other.n_missing_branches |
| 279 | ) |
| 280 | return nums |
| 281 | |
| 282 | def __radd__(self, other): |
| 283 | # Implementing 0+Numbers allows us to sum() a list of Numbers. |
| 284 | if other == 0: |
| 285 | return self |
| 286 | return NotImplemented |