Exemplo n.º 1
0
def test_parse_string():
    from pycobertura import Cobertura

    xml_path = 'tests/cobertura.xml'
    with open(xml_path) as f:
        xml_string = f.read()
    assert ET.tostring(Cobertura(xml_path).xml) == ET.tostring(
        Cobertura(xml_string).xml)
Exemplo n.º 2
0
def test_parse_path():
    from pycobertura import Cobertura

    xml_path = 'tests/cobertura.xml'
    with mock.patch('pycobertura.cobertura.ET.parse') as mock_parse:
        cobertura = Cobertura(xml_path)

    assert cobertura.xml is mock_parse.return_value.getroot.return_value
Exemplo n.º 3
0
def test_parse_path():
    from pycobertura import Cobertura

    xml_path = 'foo.xml'

    with mock.patch('pycobertura.cobertura.os.path.exists', return_value=True):
        with mock.patch('pycobertura.cobertura.ET.parse') as mock_parse:
            cobertura = Cobertura(xml_path)

    assert cobertura.xml is mock_parse.return_value.getroot.return_value
Exemplo n.º 4
0
def make_cobertura(xml=SOURCE_FILE, *args, **kwargs):
    from pycobertura import Cobertura
    cobertura = Cobertura(xml, *args, **kwargs)
    return cobertura
Exemplo n.º 5
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)
Exemplo n.º 6
0
def make_cobertura(xml=SOURCE_FILE, **kwargs):
    from pycobertura import Cobertura
    from pycobertura.filesystem import filesystem_factory
    source_filesystem = filesystem_factory(xml, **kwargs)
    cobertura = Cobertura(xml, filesystem=source_filesystem)
    return cobertura
Exemplo n.º 7
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,
        }
Exemplo n.º 8
0
def make_cobertura(xml='tests/cobertura.xml', source=None, **kwargs):
    if not source:
        source = get_dir_from_file_path(xml)
    source_filesystem = filesystem_factory(source, **kwargs)
    cobertura = Cobertura(xml, filesystem=source_filesystem)
    return cobertura