Skip to content

Commit

Permalink
Verify a specific plugin version (#108)
Browse files Browse the repository at this point in the history
* 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 <wojsmol@wp.pl>

* tests: check successful validation also

* fix --version description

Co-authored-by: Daniel Bachhuber <daniel@bachhuber.co>

* README.md: fix --version description

Co-authored-by: Daniel Bachhuber <daniel@bachhuber.co>

* rename local variable

---------

Co-authored-by: Wojciech Smoliński <wojsmol@wp.pl>
Co-authored-by: Daniel Bachhuber <daniel@bachhuber.co>
  • Loading branch information
3 people committed May 23, 2023
1 parent ac6b835 commit 058df1e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -86,6 +86,9 @@ wp plugin verify-checksums [<plugin>...] [--all] [--strict] [--format=<format>]
If set, even "soft changes" like readme.txt changes will trigger
checksum errors.

[--version=<version>]
Verify checksums against a specific plugin version.

[--format=<format>]
Render output in a specific format.
---
Expand Down
18 changes: 18 additions & 0 deletions features/checksum-plugin.feature
Expand Up @@ -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

Expand Down
16 changes: 10 additions & 6 deletions src/Checksum_Plugin_Command.php
Expand Up @@ -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=<version>]
* : Verify checksums against a specific plugin version.
*
* [--format=<format>]
* : Render output in a specific format.
* ---
Expand Down Expand Up @@ -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.' );
Expand All @@ -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." );
Expand Down

0 comments on commit 058df1e

Please sign in to comment.