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

RPP / Add 100% unit test coverage requirement for src #7510

Merged
merged 78 commits into from
Oct 27, 2023

Conversation

RadoslavGeorgiev
Copy link
Contributor

@RadoslavGeorgiev RadoslavGeorgiev commented Oct 18, 2023

Changes proposed in this Pull Request

Allow the src directory's unit coverage to be checked separately from includes.

  1. There is a new test:php-coverage-src npm command, which only checks src, and is significantly faster than the full npm run test:php-coverage command.
  2. A new "Code coverage (src) / Code coverage" workflow in GitHub which fails if coverage is less than 100%.

How this was achieved:

  • Introduced a custom phpunit-src.xml.dist to allow the selection of only the src directory. Otherwise it is the same as phpunit.xml.dist.
  • bin/phpunit.sh does not run by with -c phpunit.xml.dist by default anymore, instead allowing other commands to define it.
  • Introduced 3 new files:
    1. bin/check-src-test-coverage.sh for the local command. Identical to bin/check-test-coverage.sh, but with the new XML config.
    2. bin/run-ci-tests-check-src-coverage.bash for the CI job, almost identical to the non-src one as well.
    3. .github/workflows/src-coverage.yml is the definition of the GitHub workflow.

Testing instructions

  1. Run both npm run test:php-coverage and npm run test:php-coverage-src locally to make sure they work.
  2. All CI checks should pass in GitHub.

  • Run npm run changelog to add a changelog file, choose patch to leave it empty if the change is not significant. You can add multiple changelog files in one PR by running this command a few times.
  • Covered with tests (or have a good reason not to test in description ☝️)

Base automatically changed from rpp/7414-authentication-required-state to develop October 23, 2023 15:44
Copy link
Member

@htdat htdat left a comment

Choose a reason for hiding this comment

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

Left some comments for re-using current bash and workflow files (i.e. DRY principle), rather than introducing new files with very similar structures of existing files - which can be a burden for Harmony to update them in the future.

bin/check-src-test-coverage.sh Outdated Show resolved Hide resolved
bin/run-ci-tests-check-coverage.bash Outdated Show resolved Hide resolved
.github/workflows/src-coverage.yml Outdated Show resolved Hide resolved
@RadoslavGeorgiev
Copy link
Contributor Author

@htdat all good points. I've removed all nearly duplicate files, except for the XMLs. Could you please take another look?

Copy link
Member

@htdat htdat left a comment

Choose a reason for hiding this comment

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

@RadoslavGeorgiev - Looks excellent now.

I've removed all nearly duplicate files, except for the XMLs.

Agreed, we need to have a new XML config file to have a separate code coverage report for src except that if there is a way that I don't know. However, even if there was a way, it seemed it's more complicated than just changing a few config lines.


My last comment is that we might also need to use the phpunit.xml.dist config for only includes, and leave src for phpunit-src.xml.dist.

This is my suggestion:
Index: phpunit.xml.dist
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
--- a/phpunit.xml.dist	(revision e5dc5a913730827dfdf40671d0e2788cd528ba02)
+++ b/phpunit.xml.dist	(date 1698307623999)
@@ -11,6 +11,7 @@
 		<testsuite name="WCPay">
 			<directory suffix=".php">./tests/unit</directory>
 			<exclude>./tests/unit/multi-currency</exclude>
+			<exclude>./tests/unit/src</exclude>
 		</testsuite>
 		<testsuite name="WCPayMultiCurrency">
 			<directory suffix=".php">./tests/unit/helpers</directory>
@@ -22,12 +23,6 @@
 	<filter>
 		<whitelist>
 			<directory suffix=".php">includes</directory>
-			<directory suffix=".php">src</directory>
-			<exclude>
-				<!-- Service providers are simple, and ideally should not be used within tests. -->
-				<directory suffix=".php">src/Internal/DependencyManagement/ServiceProvider</directory>
-			</exclude>
 		</whitelist>
 	</filter>
-
 </phpunit>

@RadoslavGeorgiev
Copy link
Contributor Author

@htdat I'd prefer to intentionally keep src covered by the standard commands. Most developers are/will not be aware of the difference in src requirements at first, and it might be confusing for them. I wanted a separate set of rules and commands:

  • To allow quick src checks locally, especially for coverage.
  • Enforce a 100% coverage in GitHub
  • Not to run tests separately.

@htdat
Copy link
Member

htdat commented Oct 26, 2023

@RadoslavGeorgiev - that also makes sense. If so, at least, we would still get rid of directory src from the code coverage in phpunit.xml.dist to maintain really 60% for includes. Otherwise, includes can go lower 60% while src keeps 100%, but the overall coverage can be still 60% - which is definitely NOT what we'd like to have at the end.

@RadoslavGeorgiev
Copy link
Contributor Author

@htdat done with 3 total files:

  • npm run test:php through phpunit.xml.dist is the standard unit tests for all of the plugin, as devs are used to.
  • npm run test:php-coverage through phpunit-includes.xml.dist, also used for CI checks.
  • npm run test:php-coverage-src through phpunit-src.xml.dist, also used for CI checks.

The includes check is now down about 1% because of excluding src.

Copy link
Member

@htdat htdat left a comment

Choose a reason for hiding this comment

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

I think that makes sense now. Thanks for addressing my review.

@RadoslavGeorgiev
Copy link
Contributor Author

Merging without Harmony's approval for now. If needed, we can change things.

@RadoslavGeorgiev RadoslavGeorgiev merged commit a08754d into develop Oct 27, 2023
28 checks passed
@RadoslavGeorgiev RadoslavGeorgiev deleted the rpp/100-coverage-command branch October 27, 2023 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants