-
Notifications
You must be signed in to change notification settings - Fork 40
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
Allow for checking test code coverage #560
Conversation
bin/unit-tests-coverage.txt
Outdated
# ./bin/unit-test-coverage --coverage-html ./build/logs/ | ||
# | ||
|
||
"$(pwd)/vendor/bin/phpunit" --filter WordPressVIPMinimum "$(pwd)/vendor/squizlabs/php_codesniffer/tests/AllTests.php" %* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does the %*
do here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will take any additional arguments you pass on the command line and append them.
Without it, the second example of how to run the command (line 13) wouldn't (shouldn't) work as --coverage-html ./build/logs/
would be thrown away instead of added to the command.
Hmm.. trying to remember now whether this is a Windows syntax or cross-platform. Would you mind testing both the commands ? IIRC you are on Mac or not ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved, with one question.
I probably should have mentioned in the issue above that generating code coverage only works with Xdebug turned on. If it's turned off, the tests will still run, but no coverage reports are generated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Running:
$ composer coverage
> bin/unit-test-coverage
sh: bin/unit-test-coverage: No such file or directory
Script bin/unit-test-coverage handling the coverage event returned with error code 127
I:
- updated
composer.json
line to add the missing plural (bin/unit-test-coverage
tobin/unit-tests-coverage
) - renamed the file from
unit-tests-coverage.txt
tounit-tests-coverage
(no extension) - Ran
chmod +x unit-tests-coverage
(to make the file executable).
Then tried the composer coverage
command again:
> bin/unit-tests-coverage
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.
Cannot open file "%*".
Trying the ./bin/unit-tests-coverage
directly naturally gave me the same result.
Removing the %*
from bin/unit-tests-coverage
, and it ran successfully:
> bin/unit-tests-coverage
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.
.......................................... 42 / 42 (100%)
43 sniff test files generated 117 unique error codes; 0 were fixable (0%)
Time: 6.16 seconds, Memory: 18.00 MB
OK (42 tests, 0 assertions)
Generating code coverage report in Clover XML format ... done
...and I now have a populated build/logs/clover.xml
file.
f43d947
to
f640e6a
Compare
@GaryJones And you've just demonstrated why I find it important that people actually test my PRs ;-) Thanks a lot for this!
Remaining:
|
f640e6a
to
45ea7cd
Compare
Done. Squashed into the commit that added the file in the first place (first commit).
As noted previously, the
Since generation is slow, then this is likely to be done as a manual (not CI) task, so if a |
@GaryJones Thank you very much for making those changes.
For now, the clover file would then not be needed and the config to generate the HTML output could be added to the PHPUnit config file instead (and removed from the script). Would you like me to change that ? Other than that - the ability to pass extra command line arguments to the script can still be useful, both for the test as well as the coverage script. As previously said, it's not relevant for me as I run these via my own bat scripts anyway, but as an example of typical other command line args I use:
I imagine it can be added to CI at some point, but yes, that's not high priority at the moment. For reference - both PHPCompatibility and PHPCSUtils check code coverage during CI and as part of the required status checks for each PR. The way this is set up:
As the high/low PHPCS versions for VIPCS are so close, I can even imagine that for VIPCS a coverage stage with only two builds, PHP 7.4 + PHPCS |
Yes please.
In which case, please go ahead and restore |
45ea7cd
to
29c834c
Compare
Done - I've made the base config + script change in the first commit and added a third commit adding the ability to pass additional arguments to both scripts. |
Re: Contributing.md - I've not made any changes there (yet) as it looks like that whole section should probably get a rewrite. Currently looks to be taken largely from WPCS and doesn't account for the scripts used in this repo yet, Might be more efficient to do that rewrite when PHPCSUtils is added, as that section would need to change then anyway. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I run unit-tests
, it works fine.
When I run unit-tests-coverage
(having just deleted my local branch and checked it out again), I now get:
./bin/unit-tests-coverage
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.
PHP Fatal error: Cannot declare class WordPressVIPMinimum\Sniffs\Compatibility\ZoninatorSniff, because the name is already in use in /Users/gary/code/vipcs/WordPressVIPMinimum/Sniffs/Compatibility/ZoninatorSniff.php on line 17
PHP Stack trace:
PHP 1. {main}() /Users/gary/code/vipcs/vendor/phpunit/phpunit/phpunit:0
PHP 2. PHPUnit\TextUI\Command::main() /Users/gary/code/vipcs/vendor/phpunit/phpunit/phpunit:61
PHP 3. PHPUnit\TextUI\Command->run() /Users/gary/code/vipcs/vendor/phpunit/phpunit/src/TextUI/Command.php:162
PHP 4. PHPUnit\TextUI\TestRunner->doRun() /Users/gary/code/vipcs/vendor/phpunit/phpunit/src/TextUI/Command.php:206
PHP 5. PHP_CodeSniffer\Tests\TestSuite->run() /Users/gary/code/vipcs/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:652
PHP 6. PHP_CodeSniffer\Tests\TestSuite->run() /Users/gary/code/vipcs/vendor/squizlabs/php_codesniffer/tests/TestSuite7.php:28
PHP 7. PHPUnit\Framework\TestSuite->run() /Users/gary/code/vipcs/vendor/phpunit/phpunit/src/Framework/TestSuite.php:746
PHP 8. PHPUnit\Framework\TestSuite->run() /Users/gary/code/vipcs/vendor/phpunit/phpunit/src/Framework/TestSuite.php:746
PHP 9. WordPressVIPMinimum\Tests\Compatibility\ZoninatorUnitTest->run() /Users/gary/code/vipcs/vendor/phpunit/phpunit/src/Framework/TestSuite.php:746
PHP 10. PHPUnit\Framework\TestResult->run() /Users/gary/code/vipcs/vendor/phpunit/phpunit/src/Framework/TestCase.php:796
PHP 11. WordPressVIPMinimum\Tests\Compatibility\ZoninatorUnitTest->runBare() /Users/gary/code/vipcs/vendor/phpunit/phpunit/src/Framework/TestResult.php:693
PHP 12. WordPressVIPMinimum\Tests\Compatibility\ZoninatorUnitTest->runTest() /Users/gary/code/vipcs/vendor/phpunit/phpunit/src/Framework/TestCase.php:842
PHP 13. WordPressVIPMinimum\Tests\Compatibility\ZoninatorUnitTest->testSniff() /Users/gary/code/vipcs/vendor/phpunit/phpunit/src/Framework/TestCase.php:1154
PHP 14. PHP_CodeSniffer\Ruleset->__construct() /Users/gary/code/vipcs/vendor/squizlabs/php_codesniffer/tests/Standards/AbstractSniffUnitTest.php:149
PHP 15. PHP_CodeSniffer\Ruleset->registerSniffs() /Users/gary/code/vipcs/vendor/squizlabs/php_codesniffer/src/Ruleset.php:217
PHP 16. PHP_CodeSniffer\Autoload::loadFile() /Users/gary/code/vipcs/vendor/squizlabs/php_codesniffer/src/Ruleset.php:1159
PHP 17. include() /Users/gary/code/vipcs/vendor/squizlabs/php_codesniffer/autoload.php:167
Fatal error: Cannot declare class WordPressVIPMinimum\Sniffs\Compatibility\ZoninatorSniff, because the name is already in use in /Users/gary/code/vipcs/WordPressVIPMinimum/Sniffs/Compatibility/ZoninatorSniff.php on line 17
Call Stack:
0.0004 401816 1. {main}() /Users/gary/code/vipcs/vendor/phpunit/phpunit/phpunit:0
0.0034 784680 2. PHPUnit\TextUI\Command::main() /Users/gary/code/vipcs/vendor/phpunit/phpunit/phpunit:61
0.0034 784792 3. PHPUnit\TextUI\Command->run() /Users/gary/code/vipcs/vendor/phpunit/phpunit/src/TextUI/Command.php:162
0.0918 7188136 4. PHPUnit\TextUI\TestRunner->doRun() /Users/gary/code/vipcs/vendor/phpunit/phpunit/src/TextUI/Command.php:206
0.1093 7655264 5. PHP_CodeSniffer\Tests\TestSuite->run() /Users/gary/code/vipcs/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:652
0.1093 7655264 6. PHP_CodeSniffer\Tests\TestSuite->run() /Users/gary/code/vipcs/vendor/squizlabs/php_codesniffer/tests/TestSuite7.php:28
0.1266 7656144 7. PHPUnit\Framework\TestSuite->run() /Users/gary/code/vipcs/vendor/phpunit/phpunit/src/Framework/TestSuite.php:746
0.1287 7657024 8. PHPUnit\Framework\TestSuite->run() /Users/gary/code/vipcs/vendor/phpunit/phpunit/src/Framework/TestSuite.php:746
0.1297 7659144 9. WordPressVIPMinimum\Tests\Compatibility\ZoninatorUnitTest->run() /Users/gary/code/vipcs/vendor/phpunit/phpunit/src/Framework/TestSuite.php:746
0.1297 7659144 10. PHPUnit\Framework\TestResult->run() /Users/gary/code/vipcs/vendor/phpunit/phpunit/src/Framework/TestCase.php:796
0.4381 11920376 11. WordPressVIPMinimum\Tests\Compatibility\ZoninatorUnitTest->runBare() /Users/gary/code/vipcs/vendor/phpunit/phpunit/src/Framework/TestResult.php:693
0.4386 11936920 12. WordPressVIPMinimum\Tests\Compatibility\ZoninatorUnitTest->runTest() /Users/gary/code/vipcs/vendor/phpunit/phpunit/src/Framework/TestCase.php:842
0.4386 11936920 13. WordPressVIPMinimum\Tests\Compatibility\ZoninatorUnitTest->testSniff() /Users/gary/code/vipcs/vendor/phpunit/phpunit/src/Framework/TestCase.php:1154
0.4514 12115752 14. PHP_CodeSniffer\Ruleset->__construct() /Users/gary/code/vipcs/vendor/squizlabs/php_codesniffer/tests/Standards/AbstractSniffUnitTest.php:149
0.4526 12117928 15. PHP_CodeSniffer\Ruleset->registerSniffs() /Users/gary/code/vipcs/vendor/squizlabs/php_codesniffer/src/Ruleset.php:217
0.4526 12117928 16. PHP_CodeSniffer\Autoload::loadFile() /Users/gary/code/vipcs/vendor/squizlabs/php_codesniffer/src/Ruleset.php:1159
0.4528 12167568 17. include('/Users/gary/code/vipcs/WordPressVIPMinimum/Sniffs/Compatibility/ZoninatorSniff.php') /Users/gary/code/vipcs/vendor/squizlabs/php_codesniffer/autoload.php:167
29c834c
to
010cc39
Compare
@GaryJones Argh... ok, did some digging and the I've removed that now from the first commit and 🤞 all should work fine again now, including generating the code coverage. |
✅ ./bin/unit-tests-coverage --filter AlwaysReturnInFilter
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
1 sniff test files generated 2 unique error codes; 0 were fixable (0%)
Time: 945 ms, Memory: 14.00 MB ❌ composer coverage
> bin/unit-tests-coverage
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.
........................................... 43 / 43 (100%)
44 sniff test files generated 120 unique error codes; 0 were fixable (0%)
Time: 6.48 seconds, Memory: 18.00 MB
OK (43 tests, 0 assertions)
Generating code coverage report in HTML format ... failed
Directory "/build/logs/" does not exist. Not sure what's changed here, as I didn't have the |
PHPUnit creates that fine for me. Could it be that there is still a lock on the name on your system as it previously existed ? |
I get the same error as well even with manually running the command with the empty directory in place:
|
Hmm... just to see if it makes a difference, could you try changing the Oh, and another thing to try could be |
👏 |
010cc39
to
2e141c1
Compare
* PHPUnit config: add code coverage configuration. By default, when there is a code coverage configuration and Xdebug is enabled, code coverage will be generated. For now, code coverage is set up for local use with an HTML code coverage report being generated in a `build/logs/` directory. Note: generating code coverage is slow. * Git ignore the `build` directory as created by PHPUnit to store the log files. * Adjust the "normal" test script to not generate code coverage information. * Add a `bin/unit-tests-coverage` to run the unit tests with code coverage enabled. * Add a Composer script to call the `unit-tests-coverage` script.
... to all unit test files, as well as enable strict coverage recording.
@rebeccahum @GaryJones Thank you both for testing. Seems that even PHPUnit has some cross-platform quirks. I've added the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
As part of the sniff reviews, I'll be checking that the sniffs are properly covered by unit tests.
Adding a code coverage configuration and
@covers
tags makes that simpler.Note: this doesn't yet add code coverage checking to the Travis build/PR build checks. If so desired, that can be added separately.
The current code coverage results as is:
Commit details
Tests: add code coverage configuration
By default, when there is a code coverage configuration and Xdebug is enabled, code coverage will be generated.
Note: generating code coverage is slow.
build
directory as created by PHPUnit to store the log files.bin/unit-tests-coverage
to run the unit tests with code coverage enabled.unit-tests-coverage
script.Code coverage recording: add @Covers tags
... to all unit test files, as well as enable strict coverage recording.