Skip to content

Commit

Permalink
fix jacoco (#248)
Browse files Browse the repository at this point in the history
* fix jacoco xml parsing

* fix tests

* update version
  • Loading branch information
qododavid authored Dec 17, 2024
1 parent 5c4b887 commit e045a0d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 19 deletions.
21 changes: 11 additions & 10 deletions cover_agent/CoverageProcessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,10 @@ def parse_coverage_report_jacoco(self) -> Tuple[list, list, float]:

missed, covered = 0, 0
if file_extension == 'xml':
missed, covered = self.parse_missed_covered_lines_jacoco_xml(
lines_missed, lines_covered = self.parse_missed_covered_lines_jacoco_xml(
class_name
)
missed, covered = len(lines_missed), len(lines_covered)
elif file_extension == 'csv':
missed, covered = self.parse_missed_covered_lines_jacoco_csv(
package_name, class_name
Expand All @@ -244,7 +245,7 @@ def parse_coverage_report_jacoco(self) -> Tuple[list, list, float]:

def parse_missed_covered_lines_jacoco_xml(
self, class_name: str
) -> tuple[int, int]:
) -> tuple[list, list]:
"""Parses a JaCoCo XML code coverage report to extract covered and missed line numbers for a specific file."""
tree = ET.parse(self.file_path)
root = tree.getroot()
Expand All @@ -254,14 +255,14 @@ def parse_missed_covered_lines_jacoco_xml(
)

if sourcefile is None:
return 0, 0

missed, covered = 0, 0
for counter in sourcefile.findall('counter'):
if counter.attrib.get('type') == 'LINE':
missed += int(counter.attrib.get('missed', 0))
covered += int(counter.attrib.get('covered', 0))
break
return [], []

missed, covered = [], []
for line in sourcefile.findall('line'):
if line.attrib.get('mi') == '0':
covered += [int(line.attrib.get('nr', 0))]
else :
missed += [int(line.attrib.get('nr', 0))]

return missed, covered

Expand Down
2 changes: 1 addition & 1 deletion cover_agent/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.8
0.2.9
35 changes: 28 additions & 7 deletions tests/test_CoverageProcessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def test_returns_empty_lists_and_float(self, mocker):
)
mocker.patch(
"cover_agent.CoverageProcessor.CoverageProcessor.parse_missed_covered_lines_jacoco_xml",
return_value=(0, 0),
return_value=([], []),
)

# Initialize the CoverageProcessor object
Expand Down Expand Up @@ -301,6 +301,13 @@ def test_parse_missed_covered_lines_jacoco_xml_no_source_file(self, mocker):
xml_str = """<report>
<package name="path/to">
<sourcefile name="MyClass.java">
<line nr="35" mi="0" ci="9" mb="0" cb="0"/>
<line nr="36" mi="0" ci="1" mb="0" cb="0"/>
<line nr="37" mi="0" ci="3" mb="0" cb="0"/>
<line nr="38" mi="0" ci="9" mb="0" cb="0"/>
<line nr="39" mi="1" ci="0" mb="0" cb="0"/>
<line nr="40" mi="5" ci="0" mb="0" cb="0"/>
<line nr="41" mi="9" ci="0" mb="0" cb="0"/>
<counter type="INSTRUCTION" missed="53" covered="387"/>
<counter type="BRANCH" missed="2" covered="6"/>
<counter type="LINE" missed="9" covered="94"/>
Expand All @@ -326,8 +333,8 @@ def test_parse_missed_covered_lines_jacoco_xml_no_source_file(self, mocker):
)

# Assert
assert missed == 0
assert covered == 0
assert missed == []
assert covered == []

def test_parse_missed_covered_lines_jacoco_xml(self, mocker):
#, mock_xml_tree
Expand All @@ -339,6 +346,13 @@ def test_parse_missed_covered_lines_jacoco_xml(self, mocker):
xml_str = """<report>
<package name="path/to">
<sourcefile name="MyClass.java">
<line nr="35" mi="0" ci="9" mb="0" cb="0"/>
<line nr="36" mi="0" ci="1" mb="0" cb="0"/>
<line nr="37" mi="0" ci="3" mb="0" cb="0"/>
<line nr="38" mi="0" ci="9" mb="0" cb="0"/>
<line nr="39" mi="1" ci="0" mb="0" cb="0"/>
<line nr="40" mi="5" ci="0" mb="0" cb="0"/>
<line nr="41" mi="9" ci="0" mb="0" cb="0"/>
<counter type="INSTRUCTION" missed="53" covered="387"/>
<counter type="BRANCH" missed="2" covered="6"/>
<counter type="LINE" missed="9" covered="94"/>
Expand All @@ -364,8 +378,8 @@ def test_parse_missed_covered_lines_jacoco_xml(self, mocker):
)

# Assert
assert missed == 9
assert covered == 94
assert missed == [39, 40, 41]
assert covered == [35, 36, 37, 38]

def test_parse_missed_covered_lines_kotlin_jacoco_xml(self, mocker):
#, mock_xml_tree
Expand All @@ -377,6 +391,13 @@ def test_parse_missed_covered_lines_kotlin_jacoco_xml(self, mocker):
xml_str = """<report>
<package name="path/to">
<sourcefile name="MyClass.kt">
<line nr="35" mi="0" ci="9" mb="0" cb="0"/>
<line nr="36" mi="0" ci="1" mb="0" cb="0"/>
<line nr="37" mi="0" ci="3" mb="0" cb="0"/>
<line nr="38" mi="0" ci="9" mb="0" cb="0"/>
<line nr="39" mi="1" ci="0" mb="0" cb="0"/>
<line nr="40" mi="5" ci="0" mb="0" cb="0"/>
<line nr="41" mi="9" ci="0" mb="0" cb="0"/>
<counter type="INSTRUCTION" missed="53" covered="387"/>
<counter type="BRANCH" missed="2" covered="6"/>
<counter type="LINE" missed="9" covered="94"/>
Expand All @@ -402,8 +423,8 @@ def test_parse_missed_covered_lines_kotlin_jacoco_xml(self, mocker):
)

# Assert
assert missed == 9
assert covered == 94
assert missed == [39, 40, 41]
assert covered == [35, 36, 37, 38]

def test_get_file_extension_with_valid_file_extension(self):
processor = CoverageProcessor(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_UnitTestValidator.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_validate_test_pass_no_coverage_increase_with_prompt(self):
result = generator.validate_test(test_to_validate)

assert result["status"] == "FAIL"
assert result["reason"] == "Coverage did not increase"
assert result["reason"] == "Coverage did not increase. Maybe the test did run but did not increase coverage, or maybe the test execution was skipped due to some problem"
assert result["exit_code"] == 0

def test_initial_test_suite_analysis_with_prompt_builder(self):
Expand Down

0 comments on commit e045a0d

Please sign in to comment.