Fix code coverage for main framework repo#242
Conversation
Codecov ReportAttention: Patch coverage is
📢 Thoughts on this report? Let us know! |
utils/generate-coverage.php
Outdated
| // In commands, all source code is in the "src" folder. | ||
| $filter->includeDirectory( "{$root_folder}/src" ); | ||
| // There is also a "*-command.php" file. | ||
| $filter->includeDirectory( $root_folder, '-command.php' ); |
There was a problem hiding this comment.
I did some testing and at least in the wp-cli-dev project / envorionment this line causes an infinite (or too long to wait for) hang, probably because of the complicated way that project uses symlinks and wp-cli-tests requiring wp-cli and vice versa. Adding some debugging into the eventual iterator from coverage code shows stuff like:
Scanning file: /Users/isla/source/wp-cli-dev/wp-cli-tests/vendor/wp-cli/wp-cli-tests/vendor/wp-cli/wp-cli-tests/vendor/wp-cli/wp-cli-tests/vendor/wp-cli/wp-cli-tests/vendor/wp-cli/wp-cli-tests/vendor/wp-cli/wp-cli-tests/vendor/wp-cli/wp-cli-tests/vendor/wp-cli/wp-cli-tests/vendor/wp-cli/wp-cli-tests/vendor/wp-cli/wp-cli-tests/vendor/wp-cli/wp-cli-tests/vendor/wp-cli/wp-cli-tests/vendor/wp-cli/wp-cli-tests/vendor/wp-cli/wp-cli-tests/vendor/wp-cli/media-command/vendor/wp-cli/checksum-command/checksum-command.php -> Resolved: /Users/isla/source/wp-cli-dev/checksum-command/checksum-command.php
Was able to reproduce this by just having $HOME/source/wp-cli-dev/wp-cli/bin
in my PATH and running:
wp --require=/Users/isla/source/wp-cli-dev/wp-cli-tests/utils/generate-coverage.php core is-installed
I noticed this because running the tests were hanging as well
There was a problem hiding this comment.
Hmm interesting, haven't checked it in that combo yet. Any ideas off the top of your head to fix it?
There was a problem hiding this comment.
Perhaps the scan should be rather based on the current working directory?
There was a problem hiding this comment.
I'm not very familiar with the source but when I looked in the filter code includeDirectory is listed as deprecated so maybe we shouldn't use that anyway? Off the top of my head I think we can do our own search for -command.php files that knows to avoid links/vendor etc... and then use includeFiles (which is not marked as deprecated).
There was a problem hiding this comment.
Yeah we can use RecursiveDirectoryIterator directly to make this more future proof.
There was a problem hiding this comment.
I wouldn't be totally surprised if this is the source of the random hangs / long runs on some projects too
There was a problem hiding this comment.
I just pushed a change using iterators. Did some slight testing already locally, but have to say the wp-cli-dev symlinking makes this a bit more complicated.
There was a problem hiding this comment.
Is the only goal for the -command.php part to find php files that define a wp cli command which aren't inside of $project/php and $project/src that we've already looked in? And does this need to find those in vendor folders as well? or only project level files?
I think we can come up with something but I don't understand the original goal of what we're trying to do here.
There was a problem hiding this comment.
Is the only goal for the -command.php part to find php files that define a wp cli command which aren't inside of $project/php and $project/src that we've already looked in?
Yes.
And does this need to find those in vendor folders as well? or only project level files?
Only project level files.
Let's say we're running WP_CLI_TEST_COVERAGE=1 composer behat within https://github.com/wp-cli/entity-command/
We want to get coverage for /entity-command.php and everything in src/, and ignore everything else.
|
OK Here are some changes I think might be helpful based on some testing: First I think using For example, I have wp-cli-dev checked out as I suggest adding a new variable that records the directory that private static $behat_run_dir;
// inside of prepare function ()
self::$behat_run_dir = getcwd();
// Then set internal variable
$variables = [
'FRAMEWORK_ROOT' => realpath( $framework_root ),
'SRC_DIR' => realpath( dirname( dirname( __DIR__ ) ) ),
'PROJECT_DIR' => realpath( dirname( dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) ) ),
'TEST_RUN_DIR' => self::$behat_run_dir,
];
// and put it in env too
$env = [
'PATH' => $bin_path . $path_separator . getenv( 'PATH' ),
'BEHAT_RUN' => 1,
'HOME' => sys_get_temp_dir() . '/wp-cli-home',
'TEST_RUN_DIR' => self::$behat_run_dir,
];
Since the tests can only be run from the root of the project (I think), that is what we would set $project_dir from. Then it will always be correct no matter what context its run from. In my testing, this makes It is always correct -- and files for coverage seem populated correctly as well with the new iterator code. Then we don't need any of the current project / package folder detection. Might possibly need a single exception for If you think that sounds reasonable to explore, I can push those changes to this PR if you want to test (or create another PR to test) |
|
Thanks, that all sounds clear to me and would nicely simplify the current logic. Feel free to push changes directly to this PR :) We can now also use |
|
hmm well switching to Presumably because wp itself needs some kind of custom bootstrap process ... This is getting into some composer / autoload weeds that I don't really know enough about. This doesn't happen with --require presumably because it isn't run until later on and wp has already bootstrapped itself properly. Any ideas for that? |
|
Agh, would have been too easy 🙃 Yes Then let's go with your proposed approach with the regex, since you've already written it out above. Sorry for the back and forth! |
Assume that the folder that `composer behat` is run in will be the project level folder in all situations, which avoids long chained dirname calls that don't work with the wp-cli-dev environment. Update itterator to use a call back filter as well, just to keep the logic of what files we want all in a single place.
Where this file is not vendored
|
@swissspidy ok put up some changes to try |
|
|
||
| $modify_command = function ( $part ) { | ||
| if ( preg_match( '/(^wp )|( wp )|(\/wp )/', $part ) ) { | ||
| $part = preg_replace( '/(^wp )|( wp )|(\/wp )/', '$1$2$3', $part ); |
There was a problem hiding this comment.
@mrsdizzie Just to clarify, does this now work with aliases?
There was a problem hiding this comment.
yes -- what fixes the previous alias problem is putting the --require at the end of the wp command instead of the beginning (since an alias must be the first thing after wp). All this extra code is needed because you can't just stick --require at the end of the full command because it might look like wp eval xyz | something else so you need to split on pipes (if present) and make sure its only put on the end of a wp command
There was a problem hiding this comment.
Ah I see it now. Tested with features/aliases.feature and it works as expected there.
Path resolution in wp-cli-tests itself works too, even within wp-cli-dev.
So I think this is good to go. You would need to approve it though :)
mrsdizzie
left a comment
There was a problem hiding this comment.
I'm happy to do a follow up PR with tests if you have any idea how to actually add tests for this to satisfy codecov. I wrote a unit test I thought would handle at least the coverage part but since it doesn't actually write coverage files until inside an on_shutdown function I wasn't able to figure out how to actually verify that easily...
See #241
To test:
wp-cli/wp-clirepocomposer installvendor/wp-cli/wp-cli-tests/utils/generate-coverage.phpWP_CLI_TEST_COVERAGE=1 composer behat features/aliases.feature:3