Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods to cobertura report #655

Closed
wants to merge 4 commits into from

Conversation

Spacetown
Copy link
Member

As suggested in #654 this PR adds methods to the cobertura report.

The evaluation of the line coverage for functions a dummy FileCoverage is created to which the lines of the function are added.

Closes #654.

@codecov
Copy link

codecov bot commented Aug 9, 2022

Codecov Report

Merging #655 (dc0e5c0) into master (933731a) will increase coverage by 0.05%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #655      +/-   ##
==========================================
+ Coverage   95.23%   95.29%   +0.05%     
==========================================
  Files          24       24              
  Lines        3402     3443      +41     
  Branches      639      647       +8     
==========================================
+ Hits         3240     3281      +41     
  Misses         91       91              
  Partials       71       71              
Flag Coverage Δ
ubuntu-18.04 94.01% <100.00%> (+0.07%) ⬆️
windows-2019 94.94% <100.00%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
gcovr/writer/cobertura.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Member

@latk latk left a comment

Choose a reason for hiding this comment

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

I have two general concerns: method-level line coverage, and the concept of dummy FileCoverage objects.

Attributing lines to functions is impossible for us (right now)

I am worried that the line-to-function attribution might not work in GNU-C or C++ since functions can be nested.

In GNU-C, we can have simple nested functions:

int outer() {
  int inner() {
    return 40;
  }

  return inner() + 2;
}

In C++11, we'll frequently see lambdas, or other inner classes:

int outer() {
  auto inner = []() { return 40; };
  return inner() + 2;
}

If I read your code correctly, the outer return would be misattributed to the inner function. There is no way for us to fix this without parsing the source code, and we just can't do that. I'd rather provide no data than wrong data (at least by default).

The only way to do this correctly would be to use compiler-level data. Gcov does actually provide the necessary data in its JSON output, which we still aren't parsing 😅

Dummy FileCoverage

This seems like a bit of a code smell. This suggests the need for some kind of RegionCoverage that contains branch/line coverage but does not have a filename. The FileCoverage might inherit from such a RegionCoverage.

Comment on lines +97 to +106
for lineno in function_linenos:
line_cov = data.lines[lineno]
dummy_file_coverage.lines[lineno] = line_cov
if not (line_cov.is_covered or line_cov.is_uncovered):
continue

b = line_cov.branch_coverage()
if b.total:
function_branch += b
lines.append(_line_element(line_cov))
Copy link
Member

Choose a reason for hiding this comment

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

This looks like duplicate code to the line element generation below – could probably extracted into a function.

Comment on lines +89 to +94
function_linenos = []
for lineno in iter_reversed_sorted_lines:
if lineno >= function_cov.lineno:
function_linenos.insert(0, lineno)
else:
break
Copy link
Member

Choose a reason for hiding this comment

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

This seems to assume that every line belongs to a function, and that there's no code between functions. Perhaps it would be better to avoid making such guesses, and just generate an empty <lines/> element in each method section.

Copy link
Member Author

Choose a reason for hiding this comment

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

But then the method element has no information about coverage.

@Spacetown
Copy link
Member Author

@latk You're complete right. Since the methods only make sense with the lines (needed to get the coverage), I think we should close this without merge.

@latk
Copy link
Member

latk commented Aug 11, 2022

Since the methods only make sense with the lines (needed to get the coverage)

Oh right. Hmm. This is more difficult than expected. So I think this is blocked on adapting our internal data model to include additional necessary information and implementing a gcov-json reader (#282).

That reader should also enable significant performance improvements, and might be able to circumvent the need for the heuristics for finding the working directory. But handling exclude markers + noncode lines would be much more difficult. I think the necessary logic would have to be extracted first from the parser, probably as a function apply_exclude_markers(cov: FileCoverage, lines: list[str], options) -> FileCoverage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Method level coverage in the xml generated
2 participants