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

Tests: stabelize tests using the Config class #275

Merged
merged 2 commits into from
Jan 15, 2024

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Jan 15, 2024

Context

Nearly every time I work on expanding the Core test suite or adding tests to the Core test suite for a new feature , I run into issues related to the static properties from Config bleeding through between tests and causing problems.

While it can be considered a known issue, it is still annoying and having to work around it each time (and remembering to do so), has lost me enough time by now.

Additionally, there are performance issues with the Config class, due to the use of shell commands, like stty/which/where and file system access. This is not necessarily a problem during a "normal" PHPCS run which only instantiates the Config class once, but can have an impact on the test suite where the Config class gets instantiated numerous times.
These performance issues can be circumvented, but again, doing so is something which needs to be remembered for each test instantiating the Config class.

The test double I'm now introducing will hopefully prevent/circumvent the majority of these issues for future tests.

Note: there is still an issue with --sniffs and --exclude CLI arguments not being respected by the Ruleset class when the referenced sniffs are test fixtures, but that's something to be solved another time.

Description

Tests: new ConfigDouble test helper class

The PHP_CodeSniffer native Config class contains a number of static properties.
As the value of these static properties will be retained between instantiations of the class, config values set in one test can influence the results for another test, which makes tests unstable.

This commit introduces a test "double" of the Config class which prevents this from happening.
In most cases, tests should be using this class instead of the "normal" Config, with the exception of select tests for the Config class itself.

Tests: implement use of the new ConfigDouble class

Note: for the AbstractSniffUnitTest class, this change has little to no effect, other than protecting the sniff tests from changes made to the static properties in the Config class by the Core tests.
This is due to the Config being cached to a global variable. Fixing that is outside the scope of this PR.

Related issues: squizlabs/PHP_CodeSniffer#2899 and #25.

Suggested changelog entry

N/A

Related issues/external references

Loosely related to #146

Previous related issues around the performance issues with Config in the tests: squizlabs/PHP_CodeSniffer#3831 / #61

PHPCSUtils PR for the same: PHPCSStandards/PHPCSUtils#550

The PHP_CodeSniffer native `Config` class contains a number of static properties.
As the value of these static properties will be retained between instantiations of the class, config values set in one test can influence the results for another test, which makes tests unstable.

This commit introduces a test "double" of the `Config` class which prevents this from happening.
In _most_ cases, tests should be using this class instead of the "normal" Config, with the exception of select tests for the Config class itself.
Note: for the `AbstractSniffUnitTest` class, this change has little to no effect, other than protecting the sniff tests from changes made to the static properties in the `Config` class by the `Core` tests.
This is due to the `Config` being cached to a global variable. Fixing that is outside the scope of this PR.

Related issues: squizlabs/PHP_CodeSniffer 2899 and 25.
@jrfnl jrfnl added this to the 3.8.x Next milestone Jan 15, 2024
@jrfnl jrfnl merged commit ff2765d into master Jan 15, 2024
46 checks passed
@jrfnl jrfnl deleted the feature/tests-stablelize-config-use branch January 15, 2024 09:13
@jrfnl jrfnl modified the milestones: 3.8.x Next, 3.9.0 Jan 19, 2024
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.

1 participant