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

Allow runtime initialization even when only addon checks are requested #612

Merged
merged 3 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions includes/Checker/Abstract_Check_Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
namespace WordPress\Plugin_Check\Checker;

use Exception;
use WordPress\Plugin_Check\Checker\Exception\Invalid_Check_Slug_Exception;
use WordPress\Plugin_Check\Checker\Preparations\Universal_Runtime_Preparation;
use WordPress\Plugin_Check\Utilities\Plugin_Request_Utility;
use WP_CLI;

/**
* Abstract Check Runner class.
Expand Down Expand Up @@ -294,17 +294,31 @@
final public function prepare() {
$cleanup_functions = array();

try {
if ( $this->has_runtime_check( $this->get_checks_to_run() ) ) {
$preparation = new Universal_Runtime_Preparation( $this->get_check_context() );
$cleanup_functions[] = $preparation->prepare();
}
} catch ( Exception $error ) {
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::error( $error->getMessage() );
} else {
throw $error;
if ( $this->initialized_early ) {
/*
* When initialized early, plugins are not loaded yet when this method is called.
* Therefore it could be that check slugs provided refer to addon checks that are not loaded yet.
* In that case, the only reliable option is to assume that it refers to an addon check and that the addon
* check is a runtime check. We don't know, but better to have the runtime preparations initialize
* unnecessarily rather than not having them when needed.
*
* The actual checks to run are retrieved later (once plugins are loaded), so if one of the provided slugs
* is actually invalid, the exception will still be thrown at that point.
*/
try {
$checks = $this->get_checks_to_run();
$initialize_runtime = $this->has_runtime_check( $checks );
} catch ( Invalid_Check_Slug_Exception $e ) {
$initialize_runtime = true;

Check warning on line 312 in includes/Checker/Abstract_Check_Runner.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Abstract_Check_Runner.php#L312

Added line #L312 was not covered by tests
}
} else {
// When not initialized early, all checks are loaded, so we can simply see if there are runtime checks.
$initialize_runtime = $this->has_runtime_check( $this->get_checks_to_run() );
}

if ( $initialize_runtime ) {
$preparation = new Universal_Runtime_Preparation( $this->get_check_context() );
$cleanup_functions[] = $preparation->prepare();
}

if ( $this->delete_plugin_folder ) {
Expand Down
4 changes: 2 additions & 2 deletions includes/Checker/Check_Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

use ArrayAccess;
use Countable;
use Exception;
use IteratorAggregate;
use WordPress\Plugin_Check\Checker\Exception\Invalid_Check_Slug_Exception;

/**
* Check Collection interface.
Expand Down Expand Up @@ -82,7 +82,7 @@ public function exclude( array $check_slugs ): Check_Collection;
* @param array $check_slugs List of slugs to limit to only those. If empty, the same collection is returned.
* @return Check_Collection The unchanged check collection.
*
* @throws Exception Thrown when any of the given check slugs is not present in the collection.
* @throws Invalid_Check_Slug_Exception Thrown when any of the given check slugs is not present in the collection.
*/
public function require( array $check_slugs ): Check_Collection;
}
6 changes: 3 additions & 3 deletions includes/Checker/Default_Check_Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
namespace WordPress\Plugin_Check\Checker;

use ArrayIterator;
use Exception;
use Traversable;
use WordPress\Plugin_Check\Checker\Exception\Invalid_Check_Slug_Exception;

/**
* Default Check Collection class.
Expand Down Expand Up @@ -150,12 +150,12 @@ static function ( Check $check, $slug ) use ( $check_slugs ) {
* @param array $check_slugs List of slugs to limit to only those. If empty, the same collection is returned.
* @return Check_Collection The unchanged check collection.
*
* @throws Exception Thrown when any of the given check slugs is not present in the collection.
* @throws Invalid_Check_Slug_Exception Thrown when any of the given check slugs is not present in the collection.
*/
public function require( array $check_slugs ): Check_Collection {
foreach ( $check_slugs as $slug ) {
if ( ! isset( $this->checks[ $slug ] ) ) {
throw new Exception(
throw new Invalid_Check_Slug_Exception(
sprintf(
/* translators: %s: The Check slug. */
__( 'Check with the slug "%s" does not exist.', 'plugin-check' ),
Expand Down
19 changes: 19 additions & 0 deletions includes/Checker/Exception/Invalid_Check_Slug_Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Class WordPress\Plugin_Check\Checker\Exception\Invalid_Check_Slug_Exception
*
* @package plugin-check
*/

namespace WordPress\Plugin_Check\Checker\Exception;

use InvalidArgumentException;

/**
* Class for an exception thrown when an invalid check slug is provided.
*
* @since n.e.x.t
*/
class Invalid_Check_Slug_Exception extends InvalidArgumentException {

}
10 changes: 9 additions & 1 deletion tests/behat/features/plugin-check.feature
Original file line number Diff line number Diff line change
Expand Up @@ -514,10 +514,18 @@ Feature: Test that the WP-CLI command works.
WordPress.WP.EnqueuedResourceParameters.NotInFooter,WARNING
"""

# This doesn't currently work, because we are not actually loading any other plugins, including pcp-addon.
# This doesn't currently work, because we are not actually loading any other plugins, including pcp-addon.
# And STDOUT should contain:
# """
# ExampleRuntimeCheck.ForbiddenScript,WARNING
# """

# This doesn't currently work.
# Run one runtime check from PCP and one from pcp-addon.
# When I run the WP-CLI command `plugin check foo-sample --checks=non_blocking_scripts,example_runtime --fields=code,type --format=csv --require=./wp-content/plugins/plugin-check/cli.php`
# Then STDOUT should contain:
# """
# ExampleRuntimeCheck.ForbiddenScript,WARNING
# """

# This doesn't currently work, because we are not actually loading any other plugins, including pcp-addon.
Expand Down