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 Test Coverage for Speculation Rules Plugin #1845

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from

Conversation

sarthak-19
Copy link
Contributor

@sarthak-19 sarthak-19 commented Feb 4, 2025

Summary

This is part of #1789:

  • Ignore Coverage for Non-Critical Code Blocks
  • Add Missing @covers Annotations
  • Add Missing Tests
Before : 50.00% ⚠️ After: 92.00% ✅
image  image

cc : @westonruter

@sarthak-19 sarthak-19 changed the title Add missing test case for improving code coverage Improve Test Coverage for Speculation Rules Plugin Feb 4, 2025
Copy link

codecov bot commented Feb 4, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 67.66%. Comparing base (68c6deb) to head (66eddb7).
Report is 16 commits behind head on trunk.

Additional details and impacted files
@@            Coverage Diff             @@
##            trunk    #1845      +/-   ##
==========================================
+ Coverage   65.97%   67.66%   +1.69%     
==========================================
  Files          88       87       -1     
  Lines        6895     6863      -32     
==========================================
+ Hits         4549     4644      +95     
+ Misses       2346     2219     -127     
Flag Coverage Δ
multisite 67.66% <ø> (+1.69%) ⬆️
single 40.11% <ø> (+1.94%) ⬆️

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.

@westonruter westonruter added this to the speculation-rules n.e.x.t milestone Feb 5, 2025
@westonruter westonruter added [Type] Enhancement A suggestion for improvement of an existing feature [Plugin] Speculative Loading Issues for the Speculative Loading plugin (formerly Speculation Rules) skip changelog PRs that should not be mentioned in changelogs labels Feb 5, 2025
@sarthak-19 sarthak-19 marked this pull request as ready for review February 5, 2025 13:44
Copy link

github-actions bot commented Feb 5, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @[email protected].

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: [email protected].

Co-authored-by: sarthak-19 <[email protected]>
Co-authored-by: westonruter <[email protected]>
Co-authored-by: felixarntz <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

}

/**
* Function to test sanitize_setting() with various inputs.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Function to test sanitize_setting() with various inputs.
* Function to test sanitize_setting() with various inputs.
*
* @covers ::plsr_sanitize_setting

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Initially I kept the @covers annotation just like suggested changes however that's resulting in reduced code coverage.
If covers annotation is not there it automatically finds the function that is being tested.
However if we are specifying like this then coverage reduced. Also tested with ClassName::FunctionName annotation.

  • Without coverage annotation :
image image
  • With coverage annotation :
image image

cc : @westonruter

Copy link
Member

Choose a reason for hiding this comment

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

Strange. I don't know why that would be.

Nevertheless, I see that the test_plsr_sanitize_setting() test is already covering plsr_sanitize_setting. Therefore, do we even need this test_sanitize_setting test? Should its test cases not be put into the data_plsr_sanitize_setting data provider?

@westonruter
Copy link
Member

In uninstall.php:

// If uninstall.php is not called by WordPress, bail.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}

Can be updated to follow Optimization Detective to add the // @codeCoverageIgnore comment:

// If uninstall.php is not called by WordPress, bail.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit; // @codeCoverageIgnore
}

* @covers ::plsr_get_setting_default
* @covers ::plsr_register_setting
*/
public function test_register_settings(): void {
Copy link
Member

Choose a reason for hiding this comment

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

I think better name this test_plsr_register_setting in accordance with the primary method it tests.

Suggested change
public function test_register_settings(): void {
public function test_plsr_register_setting(): void {

public function test_register_settings(): void {
plsr_register_setting();
$settings = plsr_get_setting_default();
$this->assertArrayHasKey( 'mode', $settings );
Copy link
Member

Choose a reason for hiding this comment

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

If we check this, we should probably also check for the eagerness key.

Suggested change
$this->assertArrayHasKey( 'mode', $settings );
$this->assertArrayHasKey( 'mode', $settings );
$this->assertArrayHasKey( 'eagerness', $settings );

/**
* Function to test sanitize_setting() with various inputs.
*/
public function test_sanitize_setting(): void {
Copy link
Member

Choose a reason for hiding this comment

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

See above:

Suggested change
public function test_sanitize_setting(): void {
public function test_plsr_sanitize_setting(): void {

/**
* @covers ::plsr_add_setting_ui
*/
public function test_add_setting_ui(): void {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
public function test_add_setting_ui(): void {
public function test_plsr_add_setting_ui(): void {

@@ -29,8 +29,6 @@ private function require_uninstall(): void {

/**
* Test option deletion.
*
* @covers ::plsr_delete_plugin_option
Copy link
Member

Choose a reason for hiding this comment

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

Why remove this? The function is located in uninstall.php so when that file is being required, then that function is indeed being loaded and executed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As mentioned here #1845 (comment)
for some functions adding @Covers annotation is reducing code coverage and exactly why it's happening is still not known. I thought it might be a case where some functions are not accessible (private or protected) but tested that as well, it's happening for public functions as well.

Without adding @covers ::plsr_delete_plugin_option

image image

After adding @covers ::plsr_delete_plugin_option

image image

cc : @westonruter

Copy link
Member

Choose a reason for hiding this comment

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

Strange!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Plugin] Speculative Loading Issues for the Speculative Loading plugin (formerly Speculation Rules) skip changelog PRs that should not be mentioned in changelogs [Type] Enhancement A suggestion for improvement of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants