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

Improve behavior for single-file plugins #319

Merged
merged 11 commits into from
Nov 22, 2023

Conversation

mukeshpanchal27
Copy link
Member

Fixes #307

This PR improve the behaviour for the single file plugins. You can test single file plugin such as Hello Dolly.

CLI command: npm run wp-env run cli -- wp plugin check hello.php

@mukeshpanchal27
Copy link
Member Author

In an attempt to test the Plugin_Request_Utility::is_single_file_plugin() method, I tried to add a unit test in the plugins folder (/tests/phpunit/testdata/plugins/foo-bar.php). However, due to the condition at this line in Plugin_Context, it appears that this approach will not work for unit tests.

The issue might be related to the behaviour of Plugin_Context::location(). Whether this is a bug that needs to be addressed or an accepted behaviour would require further investigation.

public function test_single_file_plugin_without_error() {
	$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'foo-bar.php' );
	$checks        = new Checks();
	$checks_to_run = array(
		new Plugin_Readme_Check(),
		new Trademarks_Check(),
	);

	add_filter(
		'wp_plugin_check_checks',
		function ( $checks ) {
			return array(
				'plugin_readme' => new Plugin_Readme_Check(),
				'trademarks'    => new Trademarks_Check(),
			);
		}
	);

	$results = $checks->run_checks( $check_context, $checks_to_run );

	$this->assertInstanceOf( Check_Result::class, $results );

	$this->assertEmpty( $results->get_warnings() );
	$this->assertEmpty( $results->get_errors() );
}

@mukeshpanchal27 mukeshpanchal27 marked this pull request as ready for review November 7, 2023 05:39
Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

@mukeshpanchal27 Good catch, and I think you're on the right track with the approach. The solution could be simplified though, some of the changes aren't needed.

includes/Checker/Checks/Abstract_File_Check.php Outdated Show resolved Hide resolved
includes/Checker/Checks/Abstract_File_Check.php Outdated Show resolved Hide resolved
includes/Utilities/Plugin_Request_Utility.php Outdated Show resolved Hide resolved
Copy link
Member

@swissspidy swissspidy left a comment

Choose a reason for hiding this comment

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

Nice and concise!

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

Thanks @mukeshpanchal27, looks great!

includes/Plugin_Context.php Outdated Show resolved Hide resolved
Copy link

codecov bot commented Nov 8, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (db28ffe) 39.49% compared to head (5da0075) 36.57%.

Additional details and impacted files
@@            Coverage Diff             @@
##            trunk     #319      +/-   ##
==========================================
- Coverage   39.49%   36.57%   -2.92%     
==========================================
  Files          43       43              
  Lines        2028     2034       +6     
==========================================
- Hits          801      744      -57     
- Misses       1227     1290      +63     
Flag Coverage Δ
feature 36.57% <100.00%> (-2.92%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@felixarntz
Copy link
Member

@swissspidy I believe the codecov report failing here was added as part of the Behat tests PR? Can you advise how to best proceed with this? How would we fix this, and is it even relevant here?

@swissspidy
Copy link
Member

@felixarntz The failed Behat check was just a flake, can be simply restarted. Or do you mean the codecov/project status check? That's purely informative. If we want to make it blocking, we can though.

@felixarntz
Copy link
Member

@swissspidy What's the threshold for codecov to fail? Making it blocking is probably a good idea to make sure new tests are added for new code.

On that note @mukeshpanchal27, could you maybe add 2 unit tests for the extra condition in Plugin_Readme_Check and Trademarks_Check, using single file plugins?

@swissspidy
Copy link
Member

Not sure what the default threshold is, I suppose any negative change causes a failed check.

Note that right now coverage right now is only collected through Behat tests, not the unit tests. That needs to be added separately. Happy to whip up a quick PR.

@felixarntz
Copy link
Member

@swissspidy Oh okay. In that case it would be great if you could open an extra PR for this, or feel free to use this one.

@swissspidy
Copy link
Member

Working on a new one over here: #321

@mukeshpanchal27
Copy link
Member Author

On that note @mukeshpanchal27, could you maybe add 2 unit tests for the extra condition in Plugin_Readme_Check and Trademarks_Check, using single file plugins?

@felixarntz I have added two tests, but Codecov is indicating an error. Could we address the coverage issue once this pull request is merged?

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

Thank you @mukeshpanchal27 for adding the two tests, those looks great.

Regarding the continuing workflow error, I think we'll just have to wait for #321 to be merged, then we can refresh this PR with trunk and then it should hopefully pass.

@swissspidy
Copy link
Member

Why not just merge this now? 🤔

@felixarntz
Copy link
Member

@swissspidy I think we should avoid failing tests. There's no rush on getting this particular PR shipped, so I don't think we have to merge this before addressing the failures.

@swissspidy
Copy link
Member

To be fair it's not a failed test, but a failed status check because there is more code being introduced and we don't have code coverage set up.

@swissspidy swissspidy merged commit e37bc88 into trunk Nov 22, 2023
24 checks passed
@swissspidy swissspidy deleted the fix/307-improve-behavior-for-single-file-plugins branch November 22, 2023 14:22
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.

Improve behavior for single-file plugins such as hello.php
3 participants