Skip to content
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

Merged
merged 20 commits into from
Jan 28, 2025

Conversation

ShyamGadde
Copy link
Contributor

Summary

Fixes #1433

Relevant technical choices

  • tests/bootstrap.php has been relocated to /tools/phpunit for consistency with other configuration helpers.
  • Test images have been moved to the tests/data/images directories of the specific plugins that use them.
  • Optimization Detective test helpers have been moved to /plugins/optimization-detective/tests.

@ShyamGadde ShyamGadde added [Type] Enhancement A suggestion for improvement of an existing feature Infrastructure Issues for the overall performance plugin infrastructure no milestone PRs that do not have a defined milestone for release skip changelog PRs that should not be mentioned in changelogs labels Jan 16, 2025
Copy link

codecov bot commented Jan 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 65.89%. Comparing base (3ce0f45) to head (a985976).
Report is 40 commits behind head on trunk.

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     
Flag Coverage Δ
multisite 65.89% <ø> (+0.03%) ⬆️
single 38.58% <ø> (+0.11%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@westonruter
Copy link
Member

This is still marked as a draft. Is there still work to do on it?

@ShyamGadde
Copy link
Contributor Author

There's no further work needed on this PR—it's ready for review. Apologies for leaving it as a draft for so long.

Copy link

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: ShyamGadde <[email protected]>
Co-authored-by: westonruter <[email protected]>
Co-authored-by: felixarntz <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Member

@felixarntz felixarntz left a 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' ),
Copy link
Member

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?

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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:

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.

Copy link
Member

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:

// 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" );

Copy link
Contributor Author

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.

Copy link
Member

@felixarntz felixarntz Jan 27, 2025

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.

westonruter and others added 4 commits January 27, 2025 11:08
TESTS_PLUGIN_DIR is now dynamic and points to each plugins tests
directory.

Signed-off-by: Shyamsundar Gadde <[email protected]>
@ShyamGadde ShyamGadde requested a review from felixarntz January 27, 2025 21:32
@@ -58,13 +58,15 @@
$plugin_name = 'performance-lab';
}

define( 'TESTS_PLUGIN_DIR', REPO_ROOT_DIR . "/plugins/$plugin_name/tests" );
Copy link
Member

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.

Suggested change
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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in 70de425.

Copy link
Member

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.)

'image_path' => __DIR__ . '/images/animated.gif',
'image_path' => TESTS_PLUGIN_DIR . '/tests/data/images/animated.gif',
Copy link
Contributor Author

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?

Copy link
Member

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' ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

Is this if necessary?

Copy link
Contributor Author

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.

Copy link
Member

@westonruter westonruter Jan 28, 2025

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';

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in b002bff.

@@ -5,18 +5,18 @@
* @package performance-lab
*/

define( 'TESTS_PLUGIN_DIR', dirname( __DIR__ ) );
define( 'REPO_ROOT_DIR', dirname( __DIR__, 2 ) );
Copy link
Member

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.

Copy link
Contributor Author

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.

@@ -7,3 +7,6 @@

// Require the suggested plugin.
require_once __DIR__ . '/../../optimization-detective/load.php';
Copy link
Member

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:

Suggested change
require_once __DIR__ . '/../../optimization-detective/load.php';
require_once TESTS_REPO_ROOT_DIR . '/plugins/optimization-detective/load.php';

Copy link
Member

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.

Copy link
Member

@westonruter westonruter Jan 28, 2025

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';

Copy link
Member

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

Copy link
Member

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

Removed in 6421043

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

Looks great!

Copy link
Member

@felixarntz felixarntz left a 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/optimization-detective/tests/test-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;
Copy link
Member

Choose a reason for hiding this comment

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

See above:

Suggested change
$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.

Copy link
Member

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.

Copy link
Member

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?

Copy link
Member

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.

Copy link
Member

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).

Copy link
Member

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?

Copy link
Member

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.

Comment on lines +107 to +109
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';
Copy link
Member

Choose a reason for hiding this comment

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

See above:

Suggested change
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';

Copy link
Member

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)

Copy link
Member

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.

Copy link
Member

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)

Copy link
Member

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.

Copy link
Member

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

Copy link
Member

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 ) {

@westonruter westonruter merged commit d94977f into WordPress:trunk Jan 28, 2025
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Infrastructure Issues for the overall performance plugin infrastructure no milestone PRs that do not have a defined milestone for release skip changelog PRs that should not be mentioned in changelogs [Type] Enhancement A suggestion for improvement of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Clear out and remove /tests directory
3 participants