From 058df1ead2202f8113d773e9494390b65918ca92 Mon Sep 17 00:00:00 2001 From: Sandor Semsey Date: Tue, 23 May 2023 18:53:04 +0200 Subject: [PATCH] Verify a specific plugin version (#108) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add functional tests * add --version option to plugin command * plugin command: use version if supplied * update README.md with --version * tests: add check for return code=1 * tests: use `When I try` when error is expected Co-authored-by: Wojciech Smoliński * tests: check successful validation also * fix --version description Co-authored-by: Daniel Bachhuber * README.md: fix --version description Co-authored-by: Daniel Bachhuber * rename local variable --------- Co-authored-by: Wojciech Smoliński Co-authored-by: Daniel Bachhuber --- README.md | 3 +++ features/checksum-plugin.feature | 18 ++++++++++++++++++ src/Checksum_Plugin_Command.php | 16 ++++++++++------ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d7e79677..a06aabdd 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,9 @@ wp plugin verify-checksums [...] [--all] [--strict] [--format=] If set, even "soft changes" like readme.txt changes will trigger checksum errors. + [--version=] + Verify checksums against a specific plugin version. + [--format=] Render output in a specific format. --- diff --git a/features/checksum-plugin.feature b/features/checksum-plugin.feature index 8b1bb477..e70af037 100644 --- a/features/checksum-plugin.feature +++ b/features/checksum-plugin.feature @@ -13,6 +13,24 @@ Feature: Validate checksums for WordPress plugins Success: Verified 1 of 1 plugins. """ + When I run `wp plugin verify-checksums duplicate-post --format=json --version=3.2.1` + Then STDOUT should be: + """ + Success: Verified 1 of 1 plugins. + """ + And STDERR should be empty + + When I try `wp plugin verify-checksums duplicate-post --format=json --version=3.2.2` + Then the return code should be 1 + And STDOUT should contain: + """ + "plugin_name":"duplicate-post","file":"duplicate-post-jetpack.php","message":"File is missing" + """ + And STDERR should be: + """ + Error: No plugins verified (1 failed). + """ + Scenario: Modified plugin doesn't verify Given a WP install diff --git a/src/Checksum_Plugin_Command.php b/src/Checksum_Plugin_Command.php index e66710cc..e60b097d 100644 --- a/src/Checksum_Plugin_Command.php +++ b/src/Checksum_Plugin_Command.php @@ -48,6 +48,9 @@ class Checksum_Plugin_Command extends Checksum_Base_Command { * : If set, even "soft changes" like readme.txt changes will trigger * checksum errors. * + * [--version=] + * : Verify checksums against a specific plugin version. + * * [--format=] * : Render output in a specific format. * --- @@ -75,11 +78,12 @@ class Checksum_Plugin_Command extends Checksum_Base_Command { */ public function __invoke( $args, $assoc_args ) { - $fetcher = new Fetchers\UnfilteredPlugin(); - $all = (bool) Utils\get_flag_value( $assoc_args, 'all', false ); - $strict = (bool) Utils\get_flag_value( $assoc_args, 'strict', false ); - $insecure = (bool) Utils\get_flag_value( $assoc_args, 'insecure', false ); - $plugins = $fetcher->get_many( $all ? $this->get_all_plugin_names() : $args ); + $fetcher = new Fetchers\UnfilteredPlugin(); + $all = (bool) Utils\get_flag_value( $assoc_args, 'all', false ); + $strict = (bool) Utils\get_flag_value( $assoc_args, 'strict', false ); + $insecure = (bool) Utils\get_flag_value( $assoc_args, 'insecure', false ); + $plugins = $fetcher->get_many( $all ? $this->get_all_plugin_names() : $args ); + $version_arg = isset( $assoc_args['version'] ) ? $assoc_args['version'] : ''; if ( empty( $plugins ) && ! $all ) { WP_CLI::error( 'You need to specify either one or more plugin slugs to check or use the --all flag to check all plugins.' ); @@ -88,7 +92,7 @@ public function __invoke( $args, $assoc_args ) { $skips = 0; foreach ( $plugins as $plugin ) { - $version = $this->get_plugin_version( $plugin->file ); + $version = empty( $version_arg ) ? $this->get_plugin_version( $plugin->file ) : $version_arg; if ( false === $version ) { WP_CLI::warning( "Could not retrieve the version for plugin {$plugin->name}, skipping." );