Skip to content

Commit

Permalink
fix(coverage): fix coverage count for branch coverage
Browse files Browse the repository at this point in the history
If branch coverage is collected, show partial lines as not covered (#121)
  • Loading branch information
jvarho authored and TheKevJames committed Feb 14, 2017
1 parent 97ad9b0 commit b9ab703
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
8 changes: 6 additions & 2 deletions coveralls/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,13 @@ def get_hits(self, line_num, analysis):
"""
if line_num in analysis.missing:
return 0
if line_num in analysis.statements:
if line_num not in analysis.statements:
return None
if not analysis.has_arcs():
return 1
return None
if line_num in analysis.missing_branch_arcs():
return 0
return 1

def parse_file(self, cu, analysis):
""" Generate data for single file """
Expand Down
8 changes: 7 additions & 1 deletion example/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ class Foo(object):


def baz():
print('this is not tested')
print('this is not tested')

def branch(cond1, cond2):
if cond1:
print('condition tested both ways')
if cond2:
print('condition not tested both ways')
6 changes: 4 additions & 2 deletions example/runtests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# coding: utf-8
from project import hello
from project import hello, branch

if __name__ == '__main__':
hello()
hello()
branch(False, True)
branch(True, True)
20 changes: 16 additions & 4 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,24 @@ def test_reporter(self):
results = self.cover.get_coverage()
assert len(results) == 2
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\')',
'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]}, results[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\n\nif __name__ == '__main__':\n hello()",
'name': 'runtests.py', 'coverage': [None, 1, None, 1, 1]}, results[1])
'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, 1, 1, 1, 1]}, results[1])

def test_reporter_with_branches(self):
sh.coverage('run', '--branch', 'runtests.py')
results = self.cover.get_coverage()
assert len(results) == 2
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])
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])

def test_missing_file(self):
sh.echo('print("Python rocks!")', _out="extra.py")
Expand Down

0 comments on commit b9ab703

Please sign in to comment.