Skip to content

Feature LCOV code coverage #869

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

Closed
wants to merge 3 commits into from
Closed

Feature LCOV code coverage #869

wants to merge 3 commits into from

Conversation

Bwen
Copy link

@Bwen Bwen commented Sep 29, 2021

Feature request issue #735

Generate an lcov code coverage, which can be tested with genhtml which is part of most Debian derived distribution package lcov.
Ref: http://ltp.sourceforge.net/coverage/lcov/geninfo.1.php

Once you have generated the lcov file you can generate the html coverage with the following command:

vendor/bin/phpunit --path-coverage --coverage-lcov lcov.info

genhtml lcov.info -o html-coverage-directory --branch-coverage

@Bwen Bwen changed the title Feature request issue #735 Feature LCOV code coverage Sep 29, 2021
@sebastianbergmann
Copy link
Owner

Please rebase your branch so that it has #870. Thanks!

@codecov
Copy link

codecov bot commented Sep 30, 2021

Codecov Report

Merging #869 (5b12628) into master (2e93305) will increase coverage by 0.07%.
The diff coverage is 90.00%.

❗ Current head 5b12628 differs from pull request most recent head 9a17e03. Consider uploading reports for the commit 9a17e03 to get more accurate results
Impacted file tree graph

@@             Coverage Diff              @@
##             master     #869      +/-   ##
============================================
+ Coverage     84.75%   84.82%   +0.07%     
- Complexity     1086     1104      +18     
============================================
  Files            60       61       +1     
  Lines          3575     3625      +50     
============================================
+ Hits           3030     3075      +45     
- Misses          545      550       +5     
Impacted Files Coverage Δ
src/Report/Lcov.php 90.00% <90.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2e93305...9a17e03. Read the comment docs.

@sebastianbergmann
Copy link
Owner

Out of curiosity: what are you missing from the HTML report generated by this library that you get from the one generated by LCOV's genhtml?

@Bwen
Copy link
Author

Bwen commented Oct 3, 2021

Out of curiosity: what are you missing from the HTML report generated by this library that you get from the one generated by LCOV's genhtml?

To be honest, I prefer the built-in HTML report. I am only using the genhtml to test out the lcov.info file. The reason for me doing this is purely for convenience for external tools as mentioned in:
#735

And for that specific case, line coverage is more than enough imo.

@Bwen
Copy link
Author

Bwen commented Oct 12, 2021

@sebastianbergmann @dvdoug
Fixed all the lil count problems. It now matches exactly the count of --coverage-text, Cheers!

return $testName[0];
}

private function getCoverageByTestCase(File $file): array
Copy link
Contributor

Choose a reason for hiding this comment

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

I get the below when generating LCOV for php-code-coverage's own test suite (using version of PHPUnit from your PR in that repo). I think you need a guard clause around line 35/36

Message: SebastianBergmann\CodeCoverage\Report\Lcov::getCoverageByTestCase(): Argument #1 ($file) must be of type SebastianBergmann\CodeCoverage\Node\File, SebastianBergmann\CodeCoverage\Node\Directory given

foreach ($report as $file) {
$tests = $this->getCoverageByTestCase($file);

foreach ($tests as $name => $test) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This overrides the $name given in the function signature (which is unused?)


$testSpecificData[$test]['functionLineNumbers'][$methodName] = $method['startLine'];

if (0 < $method['executedLines'] && $method['coverage'] == 100) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I recommend strict equality (===) here and on line 118

}
}

$buffer = implode("\n", $lcovLines) . "\n";
Copy link
Contributor

Choose a reason for hiding this comment

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

I understand this is how the other reports do it (generate in memory and then write to disk), but I'm concerned these might turn out to be very, very large files.

What's the largest project you've tested this with?

Copy link
Author

@Bwen Bwen Oct 13, 2021

Choose a reason for hiding this comment

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

I've been trying to find a good phpunit project that I could wire up to test, has been challenging... Any suggestions? 😅

Since lcov is pretty much a per line file, not like a closed structure like xml or json, I was tempted to just file_put_contents($filename, $line, FILE_APPEND); but seeing how the other coverage did it, I didnt want to "rock the boat"... I could switch it tho?

EDIT: Only concerned I had about the file append, is the buffer that is returned? file_get_content() ?

Copy link
Contributor

Choose a reason for hiding this comment

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

fwrite is probably a better choice? file_put_contents would open and close the file on every call.

I've had good luck with Symfony in the past when seeking out a giant codebase, it's one repo that has all of the individual components.

Copy link
Author

@Bwen Bwen Oct 14, 2021

Choose a reason for hiding this comment

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

fwrite is probably a better choice? file_put_contents would open and close the file on every call.

What do I do about returning the "buffer" at the end? Just open up the file and return the whole thing as string? 🤔

EDIT: what about Moderate phpunit projects I could rewire up easily for further testing?

Copy link
Contributor

Choose a reason for hiding this comment

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

What do I do about returning the "buffer" at the end? Just open up the file and return the whole thing as string?

It's only the text report where the contents are reused by the calling application 🤔. @sebastianbergmann would you be OK if nothing was returned?

Copy link
Author

Choose a reason for hiding this comment

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

its used for the tests as well, returning an empty string would make the lcov tests fail... I would have to refactor to test it another way... 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, 💩

);
}

public function testCloverForFileWithIgnoredLines(): void

Choose a reason for hiding this comment

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

Suggested change
public function testCloverForFileWithIgnoredLines(): void
public function testLcovForFileWithIgnoredLines(): void

);
}

public function testCloverForClassWithAnonymousFunction(): void

Choose a reason for hiding this comment

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

Suggested change
public function testCloverForClassWithAnonymousFunction(): void
public function testLcovForClassWithAnonymousFunction(): void

@Bwen
Copy link
Author

Bwen commented Oct 18, 2021

I think we've been going about this the wrong way. I dont think we should have the "per test case basis" as a default output, this will create huge files ex: number of source files * number of test cases.

I believe we should have a sane default output which should be on a source file basis line coverage, and if the user really want the coverage on a per test basis he can add the extra option --coverage-lcov-per-test or maybe based on an already existing option. The genhtml does not give you the per test case coverage details unless you request it, I think we should follow that logic as well.

@sebastianbergmann @dvdoug What do you think? 🤔 💭

@dvdoug
Copy link
Contributor

dvdoug commented Oct 19, 2021

I will defer to @sebastianbergmann on adding options, I'm just a humble contributor.

@sebastianbergmann
Copy link
Owner

Thank you for your contribution. I appreciate the time you invested in preparing this pull request. However, I have decided not to merge it.

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

Successfully merging this pull request may close these issues.

4 participants