Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions diff_cover/violationsreporters/violations_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,20 @@ def get_src_path_line_nodes_clover(xml_document, src_path):
If file is not present in `xml_document`, return None
"""

files = [
file_tree
for file_tree in xml_document.findall(".//file")
if GitPathTool.relative_path(file_tree.get("path")) == src_path
]
files = []
normalized_src_path = util.to_unix_path(src_path)
for file_tree in xml_document.findall(".//file"):
file_path = file_tree.get("path") or file_tree.get("name")
if not file_path:
continue

normalized_file_path = util.to_unix_path(file_path)
relative_file_path = util.to_unix_path(GitPathTool.relative_path(file_path))
if (
relative_file_path == normalized_src_path
or normalized_file_path.endswith(f"/{normalized_src_path}")
):
files.append(file_tree)
if not files:
return None
lines = []
Expand All @@ -142,6 +151,13 @@ def get_src_path_line_nodes_clover(xml_document, src_path):
lines.append(file_tree.findall('./line[@type="cond"]'))
Comment on lines 154 to 161

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be clear, I know this was not in your changes, but I think it will prove important for ... honeslty both php clover and other clover reports

return list(itertools.chain(*lines))

@staticmethod
def _is_clover_report(xml_document):
return (
bool(xml_document.findall(".[@clover]"))
or xml_document.find(".//file/line[@num][@count]") is not None
)

def _measured_source_path_matches(self, package_name, file_name, src_path):
# find src_path in any of the source roots
if not src_path.endswith(util.to_unix_path(file_name)):
Expand Down Expand Up @@ -204,7 +220,7 @@ def _cache_file(self, src_path):

# Loop through the files that contain the xml roots
for i, xml_document in enumerate(self._xml_roots):
if xml_document.findall(".[@clover]"):
if self._is_clover_report(xml_document):

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not have to recompute this for every file.

Perhaps we just compute a mapping in init so we can just turn this check into a dict lookup rather then re-searchign though the xml file?

# see etc/schema/clover.xsd at https://bitbucket.org/atlassian/clover/src
line_nodes = self.get_src_path_line_nodes_clover(
xml_document, src_path
Expand Down
15 changes: 15 additions & 0 deletions tests/test_violations_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,21 @@ def test_violations(self):
result = coverage.violations("file1.java")
assert result == violations

def test_phpunit_clover_without_clover_attribute(self):
xml = self._coverage_xml(
["/workspace/project/subdir/file.java"],
self.FEW_VIOLATIONS,
self.FEW_MEASURED,
)
del xml.attrib["clover"]
file_node = xml.find(".//file")
file_node.set("name", file_node.attrib.pop("path"))

coverage = XmlCoverageReporter([xml])

assert coverage.violations("subdir/file.java") == self.FEW_VIOLATIONS
assert coverage.measured_lines("subdir/file.java") == self.FEW_MEASURED

def test_two_inputs_first_violate(self):
# Construct the XML report
file_paths = ["file1.java"]
Expand Down