Clear out and remove /tests directory - #1804
Conversation
Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
…tory Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
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 <shyamsundar.gadde@rtcamp.com>
|
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. |
felixarntz
left a comment
There was a problem hiding this comment.
@ShyamGadde Thanks, it's great to see our project structure finally cleaned up properly. I have just one question on the changes.
| add_filter( 'wp_image_editors', '__return_empty_array' ); | ||
| yield 'when no editor is present' => array( | ||
| 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.
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.
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.
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.
I don't think that's how it was working initially. It is defined here:
performance/tests/bootstrap.php
Line 8 in 93257ee
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.
So re-introduce that constant but have it point to the plugin currently being tested? Actually, I see in this PR it still exists:
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
Like:
define( 'TESTS_PLUGIN_DIR', REPO_ROOT_DIR . "/plugins/$plugin_name/tests" );There was a problem hiding this comment.
Curious why this was changed throughout all these files? Does
TESTS_PLUGIN_DIRno 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.
@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 <shyamsundar.gadde@rtcamp.com>
TESTS_PLUGIN_DIR is now dynamic and points to each plugins tests directory. Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
| $plugin_name = 'performance-lab'; | ||
| } | ||
|
|
||
| define( 'TESTS_PLUGIN_DIR', REPO_ROOT_DIR . "/plugins/$plugin_name/tests" ); |
There was a problem hiding this comment.
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.
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 <shyamsundar.gadde@rtcamp.com>
| return array( | ||
| 'animated_gif' => array( | ||
| 'image_path' => __DIR__ . '/images/animated.gif', | ||
| 'image_path' => TESTS_PLUGIN_DIR . '/tests/data/images/animated.gif', |
There was a problem hiding this comment.
Regarding this specific change, should this also be reverted to the original __DIR__ pattern?
There was a problem hiding this comment.
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.
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.
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.
Replacing it with TESTS_REPO_ROOT_DIR works perfectly and resolves the issue.
| */ | ||
|
|
||
| define( 'TESTS_PLUGIN_DIR', dirname( __DIR__ ) ); | ||
| define( 'REPO_ROOT_DIR', dirname( __DIR__, 2 ) ); |
There was a problem hiding this comment.
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.
That makes sense. Updated it in 6df3ca2.
Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
| @@ -7,3 +7,6 @@ | |||
|
|
|||
| // Require the suggested plugin. | |||
| require_once __DIR__ . '/../../optimization-detective/load.php'; | |||
There was a problem hiding this comment.
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.
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.
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.
Oh, the lines in auto-sizes are obsolete. They should have been removed with #1476
Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
felixarntz
left a comment
There was a problem hiding this comment.
@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!
| */ | ||
| $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.
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.
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.
See this $load_plugin function here is used recursively. It loads any Requires Plugins.
There was a problem hiding this comment.
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.
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.
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.
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.
What do you mean? These files don't require any dependencies.
There was a problem hiding this comment.
Plugin dependencies (i.e. Image Prioritizer depending on Optimization Detective)
There was a problem hiding this comment.
I don't understand. Specific files are being loaded here that only matter for performance-lab.
There was a problem hiding this comment.
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.
Oh, but that already it the case. This logic is wrapped in:
if ( 'performance-lab' === $plugin_name ) {Co-authored-by: Felix Arntz <flixos90@gmail.com>
Summary
Fixes #1433
Relevant technical choices
tests/bootstrap.phphas been relocated to/tools/phpunitfor consistency with other configuration helpers.tests/data/imagesdirectories of the specific plugins that use them./plugins/optimization-detective/tests.