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

Add wp-cli integration #458

Merged

Conversation

matiasbenedetto
Copy link
Collaborator

@matiasbenedetto matiasbenedetto commented Sep 6, 2024

What?

Add wp-cli integration for theme-check plugin

Why?

To use the output of the theme-check plugin in automated environments like CI workflows.

How to use and how to test

  • In your WordPress install theme-check plugin using this branch.
  • Install wp-cli in your system.
  • Run in your console wp theme-check run.
  • Observe the output in your console

It should look like this:

Running wp theme-check run :
image

Running wp theme-check run --format=json:
image

Screencast

Screencast.from.06-09-24.11.39.55.webm

Copy link
Contributor

@jffng jffng left a comment

Choose a reason for hiding this comment

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

When the theme check fails, the error messages are not logged. All that is returned is:

$ wp theme-check run
Error: Theme check failed for twentytwentyfive.

wp-cli/class-theme-check-cli.php Outdated Show resolved Hide resolved
wp-cli/class-theme-check-cli.php Outdated Show resolved Hide resolved
@jffng
Copy link
Contributor

jffng commented Sep 6, 2024

Nice change! I can't push to your branch, but here is some suggestion changes to the readme:

Usage with wp-cli

To use with wp-cli, ensure the theme check plugin is active and wp-cli is installed. The theme-check subcommand is added to wp-cli and can be used as follows:

wp theme-check run [<theme>] [--format=<format>]

Options

Option Accepts Required Default
theme The slug of the theme to check No Current theme slug
format cli or json No cli

Examples

wp theme-check run
wp theme-check run twentytwentyfour
wp theme-check run --format=json
wp theme-check run twentytwentyfour --format=json

@matiasbenedetto
Copy link
Collaborator Author

matiasbenedetto commented Sep 6, 2024

@jffng cool, it looks good. I added you as a collaborator in the fork so you can push :)

@codestylist
Copy link

What a cool feature @matiasbenedetto . Thank you for this! Hope it will be in the master soon!

'result' => "Error: Theme check failed for {$check_theme_slug}.",
'messages' => array(),
);
WP_CLI::log( wp_json_encode( $json_output, JSON_PRETTY_PRINT ) );
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 output of log() is discarded to STDOUT when --quiet flag is passed. But here I think we should display JSON output whatever case if quiet flag is present or not.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Member

@vcanales vcanales Sep 11, 2024

Choose a reason for hiding this comment

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

But here I think we should display JSON output whatever case if quiet flag is present or not.

Not sure I agree with bypassing --quiet, I would expect no output at all. An alternative is to make sure that the program always exits with a descriptive code; ie. 0 for OK, non-0 for errors.

edit: On reading @matiasbenedetto's comment below, seems like the checks are not meant to return a boolean.

It's a little bit counterintuitive, but the theme-check plugin doesn't work with the objective of outputting a boolean ("theme OK/ theme with errors"). The plugin is not designed like that. It just outputs a list of messages regarding the theme, but it never says whether this theme is OK or not. Maybe in the future, we could implement that, but that's not the case yet.

Still, I don't think bypassing --quiet is cool.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think this was fixed on the latest commits.

@ernilambar
Copy link
Member

When I run check on a theme with errors. wp theme-check run mazebox. Output is:

Error: Theme check failed for mazebox.

But when there is no errors, output includes recommendation and info items.

Success: Theme check completed for mazebox.

RECOMMENDED: No reference to register_block_pattern was found in the theme. Theme authors are encouraged to implement custom block patterns as a transition to block themes.

RECOMMENDED: No reference to register_block_style was found in the theme. Theme authors are encouraged to implement new block styles as a transition to block themes.
...

@matiasbenedetto
Copy link
Collaborator Author

matiasbenedetto commented Sep 10, 2024

When I run check on a theme with errors. wp theme-check run mazebox. Output is:
Error: Theme check failed for mazebox.

I don't know what kind of errors you are checking, but I think this is expected if the checking couldn't run. Theme-check will return an error message if the check can't be run for some reason (errors parsing theme code, for example). The success and error messages are only used to signal whether the plugin was able to run the checks or not, not to output the results of those checks.

But when there is no errors, output includes recommendation and info items.

It's a little bit counterintuitive, but the theme-check plugin doesn't work with the objective of outputting a boolean ("theme OK/ theme with errors"). The plugin is not designed like that. It just outputs a list of messages regarding the theme, but it never says whether this theme is OK or not. Maybe in the future, we could implement that, but that's not the case yet.

@ernilambar
Copy link
Member

Example:

$ wp theme install twentysixteen --activate
Installing Twenty Sixteen (3.3)
Downloading installation package from https://downloads.wordpress.org/theme/twentysixteen.3.3.zip...
Using cached file '/Users/nilambarsharma/.wp-cli/cache/theme/twentysixteen-3.3.zip'...
Unpacking the package...
Installing the theme...
Theme installed successfully.
Activating 'twentysixteen'...
Success: Switched to 'Twenty Sixteen' theme.
Success: Installed 1 of 1 themes.

$ wp theme install twentyten --activate --quiet
$ echo $?
0

Another case:

$ wp theme list --format=json --quiet
[{"name":"twentytwelve","status":"inactive","update":"none","version":"4.3","update_version":"","auto_update":"off"}]

$ wp theme list --format=json
[{"name":"twentytwelve","status":"inactive","update":"none","version":"4.3","update_version":"","auto_update":"off"}]

There is no right or wrong approach. We can go for any approach we agree upon. But from my experience, general expectation in CLI is that if I pass --format=json then I would expect output in valid JSON format, whether quiet flag is passed or not.

@vcanales
Copy link
Member

There is no right or wrong approach. We can go for any approach we agree upon. But from my experience, general expectation in CLI is that if I pass --format=json then I would expect output in valid JSON format, whether quiet flag is passed or not.

I see now. Yes, I agree that if we're explicitly asking for a format, it makes sense to bypass --quiet. On the other hand, these seem like incompatible flags to me; why would someone use both? This is an unrelated discussion, though.

if ( ! $success ) {
WP_CLI::error( "Theme check failed for {$check_theme_slug}." );
}
$this->display_themechecks_in_cli( $check_theme_slug );
Copy link
Contributor

Choose a reason for hiding this comment

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

Execution stops if a WP_CLI::error is thrown, so the errors are not being displayed. Could this line be moved above the $success check?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure, i'm seeing this logic here:

// If the check process failed (if the checks were not able to run properly) raise an error. There's nothing else to do.
if ( ! $success ) {
WP_CLI::error( "Theme check failed for {$check_theme_slug}." );
}
// If the checking process run print in the console the checks. This will also print the things marked as an error in the theme.
$this->display_themechecks_in_cli( $check_theme_slug );

Could you explain why you think we should move it?

Copy link
Contributor

Choose a reason for hiding this comment

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

// If the check process failed (if the checks were not able to run properly) raise an error. There's nothing else to do.

I don't think this is correct. $success is the result of run_themechecks_against_theme which returns false if the theme fails a check.

Could you explain why you think we should move it?

Using twentytwentyfive, here is the output currently:

Error: Theme check failed for twentytwentyfive.

Moving call to display_themechecks_in_cli above the conditional, the result is:

Success: Theme check completed for twentytwentyfive.

REQUIRED: phpcs.xml.dist XML file found. This file must not be in the production version of the theme.

WARNING: assets/images/image-from-rawpixel-id-2222755.webp is 997.6 KB in size. Large file sizes have a negative impact on website performance and loading time. Compress images before using them.

WARNING: assets/images/image-from-rawpixel-id-2224378.webp is 879.6 KB in size. Large file sizes have a negative impact on website performance and loading time. Compress images before using them.

WARNING: assets/images/location.webp is 778.1 KB in size. Large file sizes have a negative impact on website performance and loading time. Compress images before using them.

WARNING: assets/images/poster-image-background.webp is 574.7 KB in size. Large file sizes have a negative impact on website performance and loading time. Compress images before using them.

INFO Themes that use the tag accessibility-ready will need to undergo an accessibility review. See https://make.wordpress.org/themes/handbook/review/accessibility/

WARNING: Your theme appears to be in the wrong directory for the theme name. The directory name must match the slug of the theme. This theme's correct slug and text-domain is twenty-twenty-five. (If this is a child theme, you can ignore this error.)

INFO: Only one text-domain is being used in this theme. Make sure it matches the theme's slug correctly so that the theme will be compatible with WordPress.org language packs. The domain found is twentytwentyfive.

RECOMMENDED: Requires PHP is recommended to have major and minor versions only (e.g. 7.4). Patch version is not needed (e.g. 7.4.1).

Error: Theme check failed for twentytwentyfive.

I think the latter is more useful because it reports what checks failed (the first one marked REQUIRED).

Also I am not sure we should throw an error here if $success isn't true, because the theme check successfully ran, but the result was it failed the checks.

Copy link
Member

Choose a reason for hiding this comment

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

This command's focus should be on pass/fail check for a theme, rather than run is successfully completed or not. And exit code should be returned based on that I believe.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think I could implement your feedback, please see: #458 (comment)

@ernilambar
Copy link
Member

Just an idea: What if we simply list check results like CLI displays. Below is one example.
Each message is splitted into type and message. This way we could support different format using WP_CLI\Formatter(), like table, json, csv, yaml.

When I need to check in my CI runner where I just need to check required items or not, I could use:

wp theme-check run theme-slug --type=error --format=json
Screenshot 2024-09-11 at 11 18 34 PM

@matiasbenedetto
Copy link
Collaborator Author

matiasbenedetto commented Sep 13, 2024

I went through the theme-check plugin code again, and I found that we can get a boolean signaling if the theme has required changes or not. Knowing that I was able to I implemented all the reamaining feedback (thanks you all!) on the latest commits:

These are the results using TT4 theme (I removed theme.json file and screenshot image to get REQUIRED changes):

wp theme-check run

image


wp theme-check run --format=json

image


wp theme-check run non-existing-theme

image

Copy link
Contributor

@jffng jffng left a comment

Choose a reason for hiding this comment

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

It works well in my testing.

One question that does not have to be a blocker, but do you know how to add the help text for the theme-check subcommand when a user runs wp help?

Screenshot 2024-09-16 at 4 35 38 PM

README.md Show resolved Hide resolved
@matiasbenedetto
Copy link
Collaborator Author

matiasbenedetto commented Sep 16, 2024

One question that does not have to be a blocker, but do you know how to add the help text for the theme-check subcommand when a user runs wp help?

@jffng Thanks for the review. I added it here: d60d1e3

Screenshot from 2024-09-16 17-57-05

@matiasbenedetto matiasbenedetto merged commit 0b2c2a3 into WordPress:master Sep 17, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants