Skip to content

Commit

Permalink
feat(reporting): use coveralls' new branch reporting format (#145)
Browse files Browse the repository at this point in the history
* Use coveralls' new branch reporting format

* Improve comments for branch coverage and remove redundant tests

* Add --branch to coverage run in tox.ini
  • Loading branch information
jvarho authored and TheKevJames committed Mar 9, 2017
1 parent ce0cf20 commit e2413e3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
39 changes: 32 additions & 7 deletions coveralls/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,31 @@ def get_hits(self, line_num, analysis):
return 0
if line_num not in analysis.statements:
return None
if not analysis.has_arcs():
return 1
if line_num in analysis.missing_branch_arcs():
return 0
return 1

def get_arcs(self, analysis):
""" Hit stats for each branch.
Returns a flat list where every four values represent a branch:
1. line-number
2. block-number (not used)
3. branch-number
4. hits (we only get 1/0 from coverage.py)
"""
if not analysis.has_arcs():
return None
branch_lines = analysis.branch_lines()
executed = analysis.arcs_executed()
missing = analysis.arcs_missing()
branches = []
for l1, l2 in executed:
if l1 in branch_lines:
branches.extend((l1, 0, abs(l2), 1))
for l1, l2 in missing:
if l1 in branch_lines:
branches.extend((l1, 0, abs(l2), 0))
return branches

def parse_file(self, cu, analysis):
""" Generate data for single file """
if hasattr(analysis, 'parser'):
Expand All @@ -88,8 +107,14 @@ def parse_file(self, cu, analysis):
source_lines = list(enumerate(analysis.file_reporter.source_token_lines()))
source = analysis.file_reporter.source()
coverage_lines = [self.get_hits(i, analysis) for i in range(1, len(source_lines) + 1)]
self.source_files.append({

results = {
'name': filename,
'source': source,
'coverage': coverage_lines
})
'coverage': coverage_lines,
}
branches = self.get_arcs(analysis)
if branches:
results['branches'] = branches

self.source_files.append(results)
13 changes: 11 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def assert_coverage(actual, expected):
assert actual['source'].strip() == expected['source'].strip()
assert actual['name'] == expected['name']
assert actual['coverage'] == expected['coverage']
assert actual.get('branches') == expected.get('branches')


class ReporterTest(unittest.TestCase):
Expand All @@ -194,13 +195,21 @@ def test_reporter_with_branches(self):
sh.coverage('run', '--branch', 'runtests.py')
results = self.cover.get_coverage()
assert len(results) == 2

# Branches are expressed as four values each in a flat list
assert not len(results[0]['branches']) % 4
assert not len(results[1]['branches']) % 4

assert_coverage({
'source': '# coding: utf-8\n\n\ndef hello():\n print(\'world\')\n\n\nclass Foo(object):\n """ Bar """\n\n\ndef baz():\n print(\'this is not tested\')\n\ndef branch(cond1, cond2):\n if cond1:\n print(\'condition tested both ways\')\n if cond2:\n print(\'condition not tested both ways\')',
'name': 'project.py',
'coverage': [None, None, None, 1, 1, None, None, 1, None, None, None, 1, 0, None, 1, 1, 1, 0, 1]}, results[0])
'branches': [16, 0, 17, 1, 16, 0, 18, 1, 18, 0, 19, 1, 18, 0, 15, 0],
'coverage': [None, None, None, 1, 1, None, None, 1, None, None, None, 1, 0, None, 1, 1, 1, 1, 1]}, results[0])
assert_coverage({
'source': "# coding: utf-8\nfrom project import hello, branch\n\nif __name__ == '__main__':\n hello()\n branch(False, True)\n branch(True, True)",
'name': 'runtests.py', 'coverage': [None, 1, None, 0, 1, 1, 1]}, results[1])
'name': 'runtests.py',
'branches': [4, 0, 5, 1, 4, 0, 2, 0],
'coverage': [None, 1, None, 1, 1, 1, 1]}, results[1])

def test_missing_file(self):
sh.echo('print("Python rocks!")', _out="extra.py")
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ deps =
cov4: coverage>=4.0,<4.1
cov41: coverage>=4.1
commands =
coverage run --source=coveralls setup.py test
coverage run --branch --source=coveralls setup.py test
coverage report -m

[testenv:coveralls3]
Expand Down

0 comments on commit e2413e3

Please sign in to comment.