-
Notifications
You must be signed in to change notification settings - Fork 107
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
Clear out and remove /tests
directory
#1804
Clear out and remove /tests
directory
#1804
Conversation
Signed-off-by: Shyamsundar Gadde <[email protected]>
Signed-off-by: Shyamsundar Gadde <[email protected]>
Signed-off-by: Shyamsundar Gadde <[email protected]>
…tory Signed-off-by: Shyamsundar Gadde <[email protected]>
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## trunk #1804 +/- ##
==========================================
+ Coverage 65.85% 65.89% +0.03%
==========================================
Files 88 88
Lines 6851 6878 +27
==========================================
+ Hits 4512 4532 +20
- Misses 2339 2346 +7
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Signed-off-by: Shyamsundar Gadde <[email protected]>
This is still marked as a draft. Is there still work to do on it? |
There's no further work needed on this PR—it's ready for review. Apologies for leaving it as a draft for so long. |
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
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.
@ShyamGadde Thanks, it's great to see our project structure finally cleaned up properly. I have just one question on the changes.
self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/car.jpeg' ), | ||
self::factory()->attachment->create_upload_object( __DIR__ . '/data/images/car.jpeg' ), |
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.
Curious why this was changed throughout all these files? Does TESTS_PLUGIN_DIR
no longer work? Shouldn't it still work for each plugin?
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.
TESTS_PLUGIN_DIR
is obsolete since it refers to the /tests
as opposed to /plugins/*/tests
, right? Now that the images have been moved into the respective plugins' tests
directories, then there's no need for TESTS_PLUGIN_DIR
.
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.
I think it's a useful constant to have, that's why I don't think it should be obsolete. It would make sense for this to always point to the root directory of the plugin that is being tested - that is how it was working initially and would continue to be helpful.
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.
I don't think that's how it was working initially. It is defined here:
performance/tests/bootstrap.php
Line 8 in 93257ee
define( 'TESTS_PLUGIN_DIR', dirname( __DIR__ ) ); |
So it was referring to the tests
directory in the root of the performance
repo, which now no longer exists.
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.
So re-introduce that constant but have it point to the plugin currently being tested? Actually, I see in this PR it still exists:
define( 'TESTS_PLUGIN_DIR', dirname( __DIR__, 2 ) ); |
But it is pointing to the repo root it seems. That seems like it should be changed to be something like REPO_ROOT_DIR
. And then TESTS_PLUGIN_DIR
could be re-introduced to be dynamic after here:
performance/tools/phpunit/bootstrap.php
Lines 44 to 59 in f829057
// Check if we use the plugin's test suite. If so, disable the PL plugin and only load the requested plugin. | |
$plugin_name = ''; | |
foreach ( $_SERVER['argv'] as $index => $arg ) { | |
if ( | |
'--testsuite' === $arg && | |
isset( $_SERVER['argv'][ $index + 1 ] ) && | |
file_exists( TESTS_PLUGIN_DIR . '/plugins/' . $_SERVER['argv'][ $index + 1 ] ) | |
) { | |
$plugin_name = $_SERVER['argv'][ $index + 1 ]; | |
} | |
} | |
// Set default plugin to load if not specified. | |
if ( '' === $plugin_name ) { | |
$plugin_name = 'performance-lab'; | |
} |
Like:
define( 'TESTS_PLUGIN_DIR', REPO_ROOT_DIR . "/plugins/$plugin_name/tests" );
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.
Curious why this was changed throughout all these files? Does
TESTS_PLUGIN_DIR
no longer work? Shouldn't it still work for each plugin?
TESTS_PLUGIN_DIR
has always pointed to /var/www/html/wp-content/plugins/performance
rather than individual plugin directories. In the dominant-color-images
plugin, the existing pattern used __DIR__ . <image>
to reference image paths within its own directory. To maintain consistency, I followed the same approach across all relevant instances.
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.
@westonruter I think what you're saying would be a good solution.
@ShyamGadde The original reason for TESTS_PLUGIN_DIR
was to point to the plugin's directory - that was back when the whole repository was for only one plugin, the Performance Lab plugin. This is also reflected in the name TESTS_PLUGIN_DIR
, which with its current usage doesn't make sense anymore. But it does have value as a concept of pointing to the plugin's directory. With the dominant-color-images
plugin tests, they were probably using __DIR__
because their images had already been moved before. But having a dedicated constant pointing to the plugin directory and using that would be a better solution IMO.
Signed-off-by: Shyamsundar Gadde <[email protected]>
TESTS_PLUGIN_DIR is now dynamic and points to each plugins tests directory. Signed-off-by: Shyamsundar Gadde <[email protected]>
tools/phpunit/bootstrap.php
Outdated
@@ -58,13 +58,15 @@ | |||
$plugin_name = 'performance-lab'; | |||
} | |||
|
|||
define( 'TESTS_PLUGIN_DIR', REPO_ROOT_DIR . "/plugins/$plugin_name/tests" ); |
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.
This constant should point to the root directory of the plugin, not the tests
directory. That's the original purpose of the constant, and it IMO still makes sense as it simplifies to refer to anything within the plugin's directory, not just test-specific things. The only reason it's prefixed with TESTS_
is that the constant is test specific.
define( 'TESTS_PLUGIN_DIR', REPO_ROOT_DIR . "/plugins/$plugin_name/tests" ); | |
define( 'TESTS_PLUGIN_DIR', REPO_ROOT_DIR . "/plugins/$plugin_name" ); |
Of course after this the references will need to be adjusted again. For many occurrences though, this effectively means leaving them like they used to be and making the PR diff much smaller. A huge part of the PR diff now is only because of this change to the TESTS_PLUGIN_DIR
constant pointing to the plugin's tests
dir.
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.
Done in 70de425.
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.
The only reason it's prefixed with
TESTS_
is that the constant is test specific.
Ah, right. It's just the prefix. I was interpreting it as PLUGIN_TESTS_DIR
. So if we wanted a tests/
dir constant then we could have TESTS_PLUGIN_TESTS_DIR
😅 (Not needed.)
Signed-off-by: Shyamsundar Gadde <[email protected]>
'image_path' => __DIR__ . '/images/animated.gif', | ||
'image_path' => TESTS_PLUGIN_DIR . '/tests/data/images/animated.gif', |
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.
Regarding this specific change, should this also be reverted to the original __DIR__
pattern?
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.
I think it makes sense to consistently use TESTS_PLUGIN_DIR
in those cases. This way all path strings after the constant are relative to the same root.
* @noinspection PhpUnhandledExceptionInspection | ||
*/ | ||
|
||
if ( ! trait_exists( 'Optimization_Detective_Test_Helpers' ) ) { |
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.
Is this if
necessary?
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.
I added this as I kept encountering the following error:
PHP Fatal error: Cannot declare trait Optimization_Detective_Test_Helpers, because the name is already in use in /var/www/html/wp-content/plugins/performance/plugins/optimization-detective/tests/class-optimization-detective-test-helpers.php on line 18
Adding this if
condition resolved the issue. If there's a better way to address this, I'd be happy to update it.
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.
Humm. Even with the require_once
. I guess it has to do with Docker and how it mounts the directories in a way that PHP doesn't realize that the paths are the same?
What if lines like:
require_once __DIR__ . '/../../optimization-detective/tests/class-optimization-detective-test-helpers.php';
Are replaced with:
require_once TESTS_REPO_ROOT_DIR . '/plugins/optimization-detective/tests/class-optimization-detective-test-helpers.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.
Replacing it with TESTS_REPO_ROOT_DIR
works perfectly and resolves the issue.
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.
Done in b002bff.
tools/phpunit/bootstrap.php
Outdated
@@ -5,18 +5,18 @@ | |||
* @package performance-lab | |||
*/ | |||
|
|||
define( 'TESTS_PLUGIN_DIR', dirname( __DIR__ ) ); | |||
define( 'REPO_ROOT_DIR', dirname( __DIR__, 2 ) ); |
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.
Maybe we should prefix REPO_ROOT_DIR
with TESTS_
just to be sure we are clear it is only for tests.
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.
That makes sense. Updated it in 6df3ca2.
Signed-off-by: Shyamsundar Gadde <[email protected]>
Signed-off-by: Shyamsundar Gadde <[email protected]>
Signed-off-by: Shyamsundar Gadde <[email protected]>
@@ -7,3 +7,6 @@ | |||
|
|||
// Require the suggested plugin. | |||
require_once __DIR__ . '/../../optimization-detective/load.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.
Other places where __DIR__ . '../../'
is used we can also replace with:
require_once __DIR__ . '/../../optimization-detective/load.php'; | |
require_once TESTS_REPO_ROOT_DIR . '/plugins/optimization-detective/load.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.
Note that this manual loading of Optimization Detective is done for Embed Optimizer but not for Image Prioritizer because the latter has Optimization Detective as an explicit dependency, whereas the former it is a recommended dependency. The test bootstrap logic automatically loads any explicit dependencies.
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.
Here are the relevant instances (updated):
plugins/optimization-detective/tests/test-uninstall.php:27: require __DIR__ . '/../../../plugins/optimization-detective/uninstall.php';
plugins/embed-optimizer/tests/bootstrap.php:9:require_once __DIR__ . '/../../optimization-detective/load.php';
plugins/speculation-rules/tests/test-speculation-rules-uninstall.php:27: require __DIR__ . '/../../../plugins/speculation-rules/uninstall.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.
Oh, the lines in auto-sizes
are obsolete. They should have been removed with #1476
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.
Signed-off-by: Shyamsundar Gadde <[email protected]>
Signed-off-by: Shyamsundar Gadde <[email protected]>
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.
Looks great!
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.
@ShyamGadde Only a few non-blocking points where we could use a more specific constant, but I'm pre-approving. Thanks for the iterations, this looks good overall!
plugins/speculation-rules/tests/test-speculation-rules-uninstall.php
Outdated
Show resolved
Hide resolved
/** | ||
* Load plugin bootstrap and any dependencies. | ||
* | ||
* @param string $plugin_name Plugin slug to load. | ||
*/ | ||
$load_plugin = static function ( string $plugin_name ) use ( &$load_plugin ): void { | ||
$plugin_test_path = TESTS_PLUGIN_DIR . '/plugins/' . $plugin_name; | ||
$plugin_test_path = TESTS_REPO_ROOT_DIR . '/plugins/' . $plugin_name; |
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.
See above:
$plugin_test_path = TESTS_REPO_ROOT_DIR . '/plugins/' . $plugin_name; | |
$plugin_test_path = TESTS_PLUGIN_DIR; |
Alternatively, we could get rid of this variable entirely, since it's only referencing the constant as-is.
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.
Actually, we can't do this. This function also needs to load plugin dependencies. I thought this at first myself.
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.
Which function do you mean?
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.
See this $load_plugin
function here is used recursively. It loads any Requires Plugins
.
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.
I'm not sure when the $load_plugin
approach was added, just seeing this now.
It doesn't make sense to me to use this approach, especially given the complexity you're describing. There is a special global to control which plugins should be active. For example:
$GLOBALS['wp_tests_options'] = array(
'active_plugins' => array( basename( TESTS_PLUGIN_DIR ) . '/load.php' ),
);
Can we not use that approach? If the problem is that we're loading plugins from a custom subdirectory, I wonder whether we could maybe override WP_PLUGIN_DIR
to be TESTS_REPO_ROOT_DIR . '/plugins'
.
Overall, it certainly makes more sense to me to tie into WordPress's built-in plugin loading mechanism instead of trying to manually require files here (which BTW plugins_loaded
is, strictly speaking, also too late for).
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.
The current approach seems to have been working well, but what you propose seems good. Maybe do it another PR?
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.
Makes sense, that would be a separate enhancement.
require_once TESTS_REPO_ROOT_DIR . '/plugins/' . $plugin_name . '/includes/admin/load.php'; | ||
require_once TESTS_REPO_ROOT_DIR . '/plugins/' . $plugin_name . '/includes/admin/server-timing.php'; | ||
require_once TESTS_REPO_ROOT_DIR . '/plugins/' . $plugin_name . '/includes/admin/plugins.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.
See above:
require_once TESTS_REPO_ROOT_DIR . '/plugins/' . $plugin_name . '/includes/admin/load.php'; | |
require_once TESTS_REPO_ROOT_DIR . '/plugins/' . $plugin_name . '/includes/admin/server-timing.php'; | |
require_once TESTS_REPO_ROOT_DIR . '/plugins/' . $plugin_name . '/includes/admin/plugins.php'; | |
require_once TESTS_PLUGIN_DIR . '/includes/admin/load.php'; | |
require_once TESTS_PLUGIN_DIR . '/includes/admin/server-timing.php'; | |
require_once TESTS_PLUGIN_DIR . '/includes/admin/plugins.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.
Actually we can't do this. See #1804 (comment)
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 do you mean? These files don't require any dependencies.
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.
Plugin dependencies (i.e. Image Prioritizer depending on Optimization Detective)
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.
I don't understand. Specific files are being loaded here that only matter for performance-lab
.
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.
Oh right. At least these should be wrapped in a condition so they only are loaded if the $plugin_name
is performance-lab
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.
Oh, but that already it the case. This logic is wrapped in:
if ( 'performance-lab' === $plugin_name ) {
Co-authored-by: Felix Arntz <[email protected]>
Summary
Fixes #1433
Relevant technical choices
tests/bootstrap.php
has been relocated to/tools/phpunit
for consistency with other configuration helpers.tests/data/images
directories of the specific plugins that use them./plugins/optimization-detective/tests
.