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

In clover.xml, count all method definitions once #115

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
```
⠀⠀⠀⠀⠀⠀⣿
⠀⠀⠀⠀⠀⣼⣿⣧⠀⠀⠀⠀⠀ ⠀ ⣠⣶⣾⣿⡇⢀⣴⣾⣿⣷⣆ ⣿⣿⠀⣰⣿⡟⢸⣿⣿⣿⡇ ⣿⣿⣿⣷⣦⠀⠀⢠⣿⣿⣿⠀⠀⣿⣿⠁⠀⣼⣿⡇⠀⢀⣴⣾⣿⡷
⠀⠀⠀⠀⠀⠀⣿
⠀⠀⠀⠀⠀⣼⣿⣧⠀⠀⠀⠀⠀ ⠀ ⣠⣶⣾⣿⡇⢀⣴⣾⣿⣷⣆ ⣿⣿⠀⣰⣿⡟⢸⣿⣿⣿⡇ ⣿⣿⣿⣷⣦⠀⠀⢠⣿⣿⣿⠀⠀⣿⣿⠁⠀⣼⣿⡇⠀⢀⣴⣾⣿⡷
⠶⣶⣶⣶⣾⣿⣿⣿⣷⣶⣶⣶⠶ ⣸⣿⡟ ⠀⢠⣿⣿⠃⠈⣿⣿⠀⣿⣿⢠⣿⡿⠀⣿⣿⣧⣤⠀⢸⣿⡇⣠⣿⡿⠀⢠⣿⡟⣿⣿⠀⢸⣿⡿⠀⠀⣿⣿⠃⠀⢸⣿⣧⣄
⠀⠀⠙⢻⣿⣿⣿⣿⣿⡟⠋⠁⠀⠀ ⣿⣿⡇⠀ ⢸⣿⣿⠀⣸⣿⡟⠀⣿⣿⣾⡿⠁ ⣿⣿⠛⠛⠀⣿⣿⢿⣿⣏⠀⢀⣿⣿⣁⣿⣿⠀⣾⣿⡇⠀⢸⣿⡿⠀⠀⡀⠙⣿⣿⡆
⠀⠀⢠⣿⣿⣿⠿⣿⣿⣿⡄⠀⠀⠀ ⠙⢿⣿⣿⠇⠈⠿⣿⣿⡿⠋⠀⠀⢿⣿⡿⠁⠀⢸⣿⣿⣿⡇⢸⣿⣿⠀⣿⣿⣄⣾⣿⠛⠛⣿⣿⢠⣿⣿⣿ ⣼⣿⣿⣿ ⣿⣿⡿⠋⠀
Expand Down
19 changes: 19 additions & 0 deletions spec/coverage_reporter/parsers/clover_parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Spectator.describe CoverageReporter::CloverParser do
it "matches correct filenames" do
expect(subject.matches?("spec/fixtures/clover/clover-phpcsutils.xml")).to eq true
expect(subject.matches?("spec/fixtures/clover/clover-unleash.xml")).to eq true
expect(subject.matches?("spec/fixtures/clover/clover-untested-method.xml")).to eq true

expect(subject.matches?("spec/fixtures/cobertura/cobertura.xml")).to eq false
expect(subject.matches?("spec/fixtures/cobertura/cobertura-oneline.xml")).to eq false
Expand All @@ -38,6 +39,24 @@ Spectator.describe CoverageReporter::CloverParser do
end
end

context "with clover-untested-method.xml" do
let(filename) { "spec/fixtures/clover/clover-untested-method.xml" }

it "parses the data correctly" do
reports = subject.parse(filename)

the_file = reports.find! do |report|
report.name == "home/yu/projects/PHPCSUtils/PHPCSUtils/AbstractSniffs/AbstractArrayDeclarationSniff.php"
end

# <line num="1" type="method" name="process" visibility="public" complexity="4" crap="4" count="0"/>
expect(the_file.coverage[0]).to eq(1)

# <line num="2" type="stmt" count="0"/>
expect(the_file.coverage[1]).to eq(0)
end
end

context "with clover-unleash.xml" do
let(filename) { "spec/fixtures/clover/clover-unleash.xml" }

Expand Down
14 changes: 14 additions & 0 deletions spec/fixtures/clover/clover-untested-method.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<coverage generated="1693391816">
<project timestamp="1693391816">
<file name="/home/yu/projects/PHPCSUtils/PHPCSUtils/AbstractSniffs/AbstractArrayDeclarationSniff.php">
<class name="PHPCSUtils\AbstractSniffs\AbstractArrayDeclarationSniff" namespace="global">
<metrics complexity="44" methods="1" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="79" coveredstatements="79" elements="82" coveredelements="82"/>
</class>
<line num="1" type="method" name="process" visibility="public" complexity="4" crap="4" count="0"/>
<line num="2" type="stmt" count="0"/>
<metrics loc="552" ncloc="237" classes="1" methods="1" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="79" coveredstatements="79" elements="82" coveredelements="82"/>
</file>
<metrics files="1" loc="11201" ncloc="5558" classes="36" methods="1" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="2420" coveredstatements="2376" elements="2602" coveredelements="2539"/>
</project>
</coverage>
8 changes: 6 additions & 2 deletions src/coverage_reporter/parsers/clover_parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ module CoverageReporter
branches[line_number] << branch_hits
end

hits = line_node.attributes["count"].content.to_u64
coverage[line_number] = hits
if line_type == "method"
coverage[line_number] = 1
else
hits = line_node.attributes["count"].content.to_u64
coverage[line_number] = hits
end
end
Copy link
Member

Choose a reason for hiding this comment

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

@mike-burns question:
Will this solution place the correct number of hits for the method on the line declaring the method (in the Coveralls source file report)? See the example I refer to below, pls.

I see that this code will solve the problem of lines declaring functions displaying as (relevant but) uncovered. It appears this will always at least make sure they are shown as covered (green) in Coveralls reports.

However, given your discovery that the number of hits for a method is taken to be equivalent to the highest number of hits for any of its (statement) lines, won't this miss the opportunity to show the correct number of hits for the method, and always just show (1)?

I can understand using this code as a failsafe to make sure lines declaring a method appear as covered, but it seems to me that the correct number of hits should be what we get from a correct XML report, which appears to be that highest number for the method's lines.

Check out the example pertaining to the AbstractArrayDeclarationSniff.php file in my comment on the OP's public issue:

Screenshot 2024-04-02 at 4 27 34 PM

To be perfectly honest, I kind of forget the point of my comment there, but now I'm stuck on the fact that recent source file reports (like this one, as opposed to this older version from Aug 2023) seem to show the correct hit count for the method. in the case of Line 178, 52.

Isn't that what a correct clover.xml report should show as the hit count for the method? And isn't that what we want to show in our reports?

Forgive me for being rusty on this one, I may have missed something.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

won't this miss the opportunity to show the correct number of hits for the method, and always just show (1)?

This really comes down to: what number do we want to show for line 178 in that screenshot? What is "correct", in this case?

  • 52, the highest number of any line within the method (so 0 for an untested method).
  • max(52, 1), the highest number of any line within the method (or 1 for an untested method, meaning that line 178 is OK).
  • 1, simply indicating that the method was defined.

As a base case, what numbers do we want for this method definition:

public function someNoOpMethod()
{
}

Do we want to say that the public function someNoOpMethod() line was never run? No amount of test coverage can change that -- calling the method executes zero statements, so the method definition line is still considered 0.

I'm happy to go with your decision -- you might have more context than I do around what these numbers are used for.


files[name].coverage.merge!(coverage)
Expand Down