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

Ruleset::processRule(): fix handling of rules included via path #2501

Conversation

jrfnl
Copy link
Contributor

@jrfnl jrfnl commented May 11, 2019

This fixes a bug where properties set for rules included via a path to the sniff file or to an included standards file, would be disregarded if the slashes used in the path did not match the slashes expected for the OS on which the ruleset is being run.

Includes adding initial integration tests for the Ruleset class.

Fixes #2497

@jrfnl jrfnl force-pushed the feature/2497-map-settings-to-sniff-included-via-path branch 2 times, most recently from 6a3e3ad to 287dbdc Compare May 11, 2019 14:59
@gsherwood gsherwood added this to Backlog in PHPCS v3 Development via automation May 13, 2019
jrfnl added a commit to Yoast/yoastcs that referenced this pull request May 17, 2019
Properties are supposed to be set per sniff, but (bug or feature?) it turns out that setting them for a standard will set them for all sniffs included in that standard.

Sniffs which don't use the property will just ignore it.
Sniffs which do use it, now have it available as if it were set for the individual sniff.

As this was unknown (and possibly/probably not supported) in the past with older PHPCS versions, WPCS added the ability to set the `minimum_supported_wp_version` for all sniffs in one go via a `config`directive.

The problem with a `config` directive is that it is currently impossible to overrule it from a repo ruleset. It can only be overruled on the command-line, and even then only since PHPCS 3.3.0.

For that reason, the `config` directive was previously set in each individual repo ruleset.

Properties, however, can easily be overruled from within repo rulesets, so this change will allow us to manage the "_minimum supported WP version_" centrally, while still allowing for individual repos to overrule the default value.

Ref:
* https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#minimum-wp-version-to-check-for-usage-of-deprecated-functions-classes-and-function-parameters - WPCS documentation about the property/config directive.
* squizlabs/PHP_CodeSniffer#2501 - PR through which this "feature" was discovered, included unit tests demonstrating it.
* squizlabs/PHP_CodeSniffer#1821 - Issue which made the config directive possible to be overruled from the command-line.
* squizlabs/PHP_CodeSniffer#2197 Feature request to allow for config directives to be overruled by other rulesets.
@jrfnl jrfnl force-pushed the feature/2497-map-settings-to-sniff-included-via-path branch from 287dbdc to ae271d0 Compare May 23, 2019 11:57
@jrfnl
Copy link
Contributor Author

jrfnl commented May 23, 2019

Rebased for merge conflicts.

@jrfnl jrfnl force-pushed the feature/2497-map-settings-to-sniff-included-via-path branch from ae271d0 to 9d79087 Compare August 29, 2019 03:44
@jrfnl
Copy link
Contributor Author

jrfnl commented Aug 29, 2019

And rebased again for merge conflicts.

@jrfnl jrfnl force-pushed the feature/2497-map-settings-to-sniff-included-via-path branch 2 times, most recently from 269541f to 1764798 Compare September 2, 2019 00:26
@jrfnl jrfnl force-pushed the feature/2497-map-settings-to-sniff-included-via-path branch from 1764798 to e8f2c75 Compare September 22, 2019 22:27
@jrfnl
Copy link
Contributor Author

jrfnl commented Sep 22, 2019

Rebased yet again

This fixes a bug where properties set for rules included via a path to the sniff file or to an included standards file, would be disregarded if the slashes used in the path did not match the slashes expected for the OS on which the ruleset is being run.

Fixes 2497
@gsherwood gsherwood added this to the 3.5.1 milestone Sep 23, 2019
@gsherwood gsherwood modified the milestones: 3.5.1, 3.5.2 Oct 1, 2019
@gsherwood gsherwood moved this from Idea Bank to Backlog in PHPCS v3 Development Oct 1, 2019
@gsherwood gsherwood modified the milestones: 3.5.3, 3.5.4 Nov 29, 2019
@gsherwood
Copy link
Member

Sorry, have finally got to this.

I'm a bit worried about modifying committed test files during a test run (much more so for me running them locally). Would it instead be possible to have different XML files that are included conditionally based on the OS the test is run on?

@jrfnl
Copy link
Contributor Author

jrfnl commented Jan 6, 2020

Would it instead be possible to have different XML files that are included conditionally based on the OS the test is run on?

No, it wouldn't, or rather: there are already different test files for different OSes.
Edit: well, actually not so much for different OSes, as for the different slash-style used on different OSes

The problem is that I cannot hard-code an absolute file-path in the XML file as the path will be different depending on where the tests are being run from, while an absolute file path is exactly the situation which needs testing.

As I documented in the test file:

// On-the-fly adjust the ruleset test file to be able to test sniffs included with absolute paths.

I'm a bit worried about modifying committed test files during a test run (much more so for me running them locally).

I understand the question, but I actually anticipated that.
The tearDown() method brings the XML files back to their original state after the test, so after running the tests, there should be no changes.

@gsherwood
Copy link
Member

The problem is that I cannot hard-code an absolute file-path in the XML file as the path will be different depending on where the tests are being run from

Yep, this is the bit I missed - I thought it was just slashes.

The tearDown() method brings the XML files back to their original state after the test

I saw that, but I'm still nervous about modifying committed files. Ideally, I'd refactor the class to allow the contents of the XML file to be passed instead of only read from disk, but I'm not sure I have the time for that at the moment, so may need to leave as is.

Thanks for clarifying for me.

@jrfnl
Copy link
Contributor Author

jrfnl commented Jan 6, 2020

I saw that, but I'm still nervous about modifying committed files

I understand, but there just isn't any other way to test this and not having tests for a change like this, which is fundamental in the ruleset processing, seemed irresponsible.

While working on this, I've ran the tests a significant number of times and didn't run into any issues with the logic now in place.

The only other thing I can suggest for now, would be to have a git reset for each of these files in the tearDownAfterClass() as an additional safeguard.

gsherwood added a commit that referenced this pull request Jan 7, 2020
@gsherwood gsherwood merged commit e8f2c75 into squizlabs:master Jan 7, 2020
PHPCS v3 Development automation moved this from Backlog to Ready for Release Jan 7, 2020
@gsherwood
Copy link
Member

Thanks a lot for this fix.

The only other thing I can suggest for now, would be to have a git reset for each of these files in the tearDownAfterClass() as an additional safeguard.

They run fine, so I'll just make sure I don't commit something if they ever fail in the future. Thanks a lot for writing these.

@jrfnl jrfnl deleted the feature/2497-map-settings-to-sniff-included-via-path branch January 7, 2020 21:55
@jrfnl
Copy link
Contributor Author

jrfnl commented Jan 7, 2020

@gsherwood Thanks for the merge, one more issue down 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
PHPCS v3 Development
Ready for Release
Development

Successfully merging this pull request may close these issues.

Sniff properties not set when referencing a sniff using relative paths or non-native slashes
2 participants