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 7 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 68.31%. Comparing base (d21b74d) to head (66b31f2).
Report is 10 commits behind head on trunk.

Additional details and impacted files
@@            Coverage Diff             @@
##            trunk    #1845      +/-   ##
==========================================
+ Coverage   66.64%   68.31%   +1.67%     
==========================================
  Files          88       87       -1     
  Lines        7015     6982      -33     
==========================================
+ Hits         4675     4770      +95     
+ Misses       2340     2212     -128     
Flag Coverage Δ
multisite 68.31% <ø> (+1.67%) ⬆️
single 39.18% <ø> (+1.90%) ⬆️

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  • Ideally it should be added in data_plsr_sanitize_setting data provider but Code coverage is unable to track these 3 lines that are being tested if passed via data provider.
    image

  • However when adding these test in another function and calling plsr_sanitize_setting() then it seems to recognise the line being covered.
    image

I renamed the function as per @felixarntz suggestion.

cc : @westonruter

@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
}

@@ -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!

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

LGTM, although the issue with @covers is still mystifying.

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