Skip to content

Conversation

@AhmarZaidi
Copy link
Contributor

@AhmarZaidi AhmarZaidi commented Jun 23, 2024

Summary

Fixes #1256

This PR addresses an issue where running Composer test commands directly shows error related to missing constants but doesn't show that the user should run the npm command to run tests.

Relevant technical choices

Added missing environment variables check and file exists check for php unit test functions.php in the test bootstrap.php.

Now, running composer test:plugins command directly:

> phpunit '--verbose' '--testsuite' 'auto-sizes'
Please use 'npm run test-php' instead of running the composer test command directly.
If you are intending to run PHPUnit directly, the following environment variables are missing: WORDPRESS_DB_PASSWORD, WORDPRESS_DB_HOST, WORDPRESS_DB_USER, WORDPRESS_DB_NAME, PHP_VERSION, WP_TESTS_DIR
Script phpunit handling the test event returned with error code 1
Script @test --verbose --testsuite auto-sizes was called via test:auto-sizes
Script @test:auto-sizes was called via test:plugins

@AhmarZaidi
Copy link
Contributor Author

AhmarZaidi commented Jun 23, 2024

Please add relevant [Type] label and milestone to this PR.

@westonruter westonruter added the [Type] Enhancement A suggestion for improvement of an existing feature label Jun 24, 2024
@westonruter westonruter added this to the performance-lab n.e.x.t milestone Jun 24, 2024
@westonruter westonruter added the skip changelog PRs that should not be mentioned in changelogs label Jun 24, 2024
@AhmarZaidi AhmarZaidi force-pushed the fix-plugin-test-error branch from 92fb6ac to 87d8d96 Compare June 24, 2024 12:48
@AhmarZaidi AhmarZaidi marked this pull request as ready for review June 25, 2024 15:32
@github-actions
Copy link

github-actions bot commented Jun 25, 2024

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: AhmarZaidi <[email protected]>
Co-authored-by: westonruter <[email protected]>
Co-authored-by: thelovekesh <[email protected]>
Co-authored-by: mukeshpanchal27 <[email protected]>

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

@thelovekesh
Copy link
Member

I suggest we handle the exit code from PHPUnit to detect if it exits due to the absence of the WP tests setup. This way, we can wrap the test command in a bash or PHP script, avoiding the need to check global variables that might be initialized at multiple points during the WP test suite lifecycle.

@AhmarZaidi
Copy link
Contributor Author

I suggest we handle the exit code from PHPUnit to detect if it exits due to the absence of the WP tests setup. This way, we can wrap the test command in a bash or PHP script, avoiding the need to check global variables that might be initialized at multiple points during the WP test suite lifecycle.

Makes sense but I just have one doubt.

Currently this is the error we are getting on running the composer test commands directly:

➜  composer test:plugins
> phpunit '--verbose' '--testsuite' 'auto-sizes'
Error: The following required constants are not defined: WP_TESTS_DOMAIN, WP_TESTS_EMAIL, WP_TESTS_TITLE, WP_PHP_BINARY.
Please check out `wp-tests-config-sample.php` for an example.
Script phpunit handling the test event returned with error code 1
Script @test --verbose --testsuite auto-sizes was called via test:auto-sizes
Script @test:auto-sizes was called via test:plugins

This error code 1 is from vendor/wp-phpunit/wp-phpunit/includes/bootstrap.php. A lot of other errors produce error code 1 so how would we determine that this is from incorrect WP test setup without checking for missing environment variables?

@thelovekesh
Copy link
Member

Humm yes, this is what I was concerned about. A better exit code between 0 to 255 can be used apart from the reserved ones to communicate the error type. But I am not sure if it can be added to the core tests bootstrap file.

Also, I think adding an exit with the proper message above this line should work in most of cases.

require_once $_test_root . '/includes/functions.php';

It can check if $_test_root exists or not and then show the message.

@thelovekesh
Copy link
Member

thelovekesh commented Jun 25, 2024

@westonruter Any thoughts on if we can propose to add a meaningful error code if the test process is exiting prematurely in case dependencies not met?

@westonruter
Copy link
Member

I guess any error code other than what phpunit returns in the case of a failure. I think it returns 1, so if you want a different exit code to be able to determine it failed for some other reason, then 2 seems fine.

@AhmarZaidi
Copy link
Contributor Author

AhmarZaidi commented Jun 25, 2024

Also, I think adding an exit with the proper message above this line should work in most of cases.

require_once $_test_root . '/includes/functions.php';

It can check if $_test_root exists or not and then show the message.

The $_test_root is also present on local without proper test env setup because it is being set by vendor/wp-phpunit/wp-phpunit/__loaded.php on composer install.

putenv( sprintf( 'WP_PHPUNIT__DIR=%s', dirname( __FILE__ ) ) );
elseif ( false !== getenv( 'WP_PHPUNIT__DIR' ) ) {
	$_test_root = getenv( 'WP_PHPUNIT__DIR' );
}

So, I don't think we can use that to determine if we should show error.

Also, the original issue was that if the user runs composer test:plugins command then they get a non descriptive error but if I'm not wrong, we would need to run composer test:plugins command from external script (bash or php) in order to get the exit code so that wouldn't solve the issue.

Please feel free correct me if I have misunderstood something here.

@thelovekesh
Copy link
Member

The $_test_root is also present on local without proper test env setup because it is being set by vendor/wp-phpunit/wp-phpunit/__loaded.php on composer install.

Well, that's another case, I am not aware of. Also, I am not sure if we use wp-phpunit apart from symbol identification for IDEs. Can we remove it? I am not fully sure why it was added.

Also, the #1256 was that if the user runs composer test:plugins command then they get a non descriptive error but if I'm not wrong, we would need to run composer test:plugins command from external script (bash or php) in order to get the exit code so that wouldn't solve the issue.Also, the #1256 was that if the user runs composer test:plugins command then they get a non descriptive error but if I'm not wrong, we would need to run composer test:plugins command from external script (bash or php) in order to get the exit code so that wouldn't solve the issue.

That doesn't mean we can't add a bash script to run the tests inside this command... right? But it's not doable right now because of the existing return code.

@thelovekesh
Copy link
Member

The issue with modifying the bootstrap file is that it can be loaded by PHPUnit in various environments, whereas this change only considers wp-env adjustments. Currently, if I run composer test:plugin in Lando, I encounter the following error:

➜  performance git:(trunk) ✗ lando composer test:plugins
> phpunit '--verbose' '--testsuite' 'auto-sizes'
Please use 'npm run test-php' instead of running the composer test command directly.
If you are intending to run PHPUnit directly, the following environment variables are missing: WORDPRESS_DB_PASSWORD, WORDPRESS_DB_HOST, WORDPRESS_DB_USER, WORDPRESS_DB_NAME
Script phpunit handling the test event returned with error code 1
Script @test --verbose --testsuite auto-sizes was called via test:auto-sizes
Script @test:auto-sizes was called via test:plugins

Without this change, my tests run as expected in the Lando environment.

The correct behavior should be: We need to detect when tests are failing due to a missing WP test suite setup and suggest using wp-env to run their tests.

@thelovekesh
Copy link
Member

Also, I think adding an exit with the proper message above this line should work in most of cases.

require_once $_test_root . '/includes/functions.php';

It can check if $_test_root exists or not and then show the message.

We can still achieve this. If $_test_root contains wp-phpunit/wp-phpunit, then check if the WP_PHPUNIT__TESTS_CONFIG environment variable or WP_TESTS_CONFIG_FILE_PATH exists. If it doesn't, we can exit with an error message like this:

echo 'Error: wp-tests-config.php is missing! Please use wp-tests-config-sample.php to create a config file.' . PHP_EOL;
echo 'Info: Alternatively, you can run `npm run wp-env start && npm run test-php` to execute tests in the wp-env environment.' . PHP_EOL;
exit(1);

In the rest of the cases, WP tests bootstrap will handle on its own:

https://github.com/WordPress/wordpress-develop/blob/df256e414c183ff001cdf26ebf37b19b960180de/tests/phpunit/includes/bootstrap.php#L25-L28

It will only jump to other cases if the user is using wp-env or some custom setup, so they know what they are doing :)

@thelovekesh
Copy link
Member

Given what @westonruter noted in #1314 (comment), I doubt if these lines are relevant anymore:

} elseif ( file_exists( TESTS_PLUGIN_DIR . '/../../../../tests/phpunit/includes/functions.php' ) ) {
$_test_root = TESTS_PLUGIN_DIR . '/../../../../tests/phpunit';
} else { // Fallback.
$_test_root = '/tmp/wordpress-tests-lib';
}

because wp-phpunit always defines the WP_PHPUNIT__DIR env variable.

@westonruter westonruter merged commit b71d0a0 into WordPress:trunk Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

composer test:plugins failed with error

3 participants