Example #1
0
def test_class_source_lines__raises_when_no_filesystem():
    from pycobertura.cobertura import Cobertura
    cobertura = Cobertura('tests/cobertura.xml')
    for filename in cobertura.files():
        pytest.raises(Cobertura.MissingFileSystem, cobertura.source_lines,
                      filename)
Example #2
0
    def collect(self):
        from pycobertura import Cobertura

        # Check if file exists
        infile = Path(self.config.xmlpath.value)
        if not infile.is_file():
            raise FileNotFoundError('No such file {}'.format(infile))

        # Parse file
        cobertura = Cobertura(str(infile))

        # Filter files
        include = self.config.include.value
        exclude = self.config.exclude.value

        original = cobertura.files()
        relevant = [
            filename for filename in original
            if is_wanted(filename, include, exclude)
        ]

        ignored = sorted(set(original) - set(relevant))
        if ignored:
            log.info('{} files ignored from total coverage'.format(
                len(ignored)))

        # Get files coverage data
        total = {
            'total_statements': 0,
            'total_misses': 0,
            'total_hits': 0,
            'branch_rate': 0.0,
            'line_rate': 0.0,
        }

        files = {
            filename:
            {key: getattr(cobertura, key)(filename=filename)
             for key in total}
            for filename in relevant
        }

        # Calculate total
        def reducer(accumulator, element):
            for key in ['total_statements', 'total_misses', 'total_hits']:
                accumulator[key] = accumulator.get(key, 0) + element[key]
            return accumulator

        total = reduce(reducer, files.values(), total)

        # Re-calculate total statements
        if total['total_statements']:
            total['line_rate'] = 1.0 - (total['total_misses'] /
                                        total['total_statements'])

        # Assign branch-rate
        total['branch_rate'] = cobertura.branch_rate()
        if include != ['*'] or exclude:
            log.warning(
                'Branch rate cannot be re-calculated when filtering files as '
                'the cobertura XML doesn\'t include the raw number of '
                'branches hit and total branches.')

        return {
            'files': files,
            'total': total,
            'ignored': ignored,
        }