Commit 41d955a
committed
Ruleset: remove test specific code
The PHPCS 2.7.1 release introduced a change to how sniff tests are run.
Originally, sniff tests would be run in the context of their ruleset. This could lead to parts of the sniff being untestable, like if the ruleset would exclude a error code or contained custom property settings.
As of PHPCS 2.7.1, sniff tests are run in isolation and no longer take the ruleset of the standard they belong to into account, which solves the above problem.
This change was introduced via commit 3df85dc.
Unfortunately, the way this change was made was via a change to the `Ruleset` class, not by changing the test framework.
It basically meant that the `Ruleset` class would now behave differently when the following two conditions were met:
1. It was being used in a test context (`PHP_CODESNIFFER_IN_TESTS` constant defined).
2. Sniff restrictions were applied via the Config, either by using the CLI `--sniffs` argument or by setting the `Config::$sniffs` property directly.
In other words, this now created a new problem when testing parts of the `Ruleset` class which would need the `Config::$sniffs` property to be set.
It also makes writing various other tests for the framework more difficult as there are plenty of times when tests would benefit from the `Config::$sniffs` property being respected. In other words, this led to work-arounds being needed in various other places in the tests, either by emulating the sniff selection or by bypassing the `Ruleset` conditions and doing the sniff selection in a test specific ruleset XML fixture file.
Aside from the above, it also makes the barrier to entry for contributors to write tests for PHPCS that much higher as the behaviour is non-intuitive.
All in all, time to get rid of the condition in the `Ruleset` class.
Now, of course, we do still want to test sniffs in isolation, so to do that the `AbstractSniffUnitTest` class has been updated to on-the-fly create ruleset XML files which only include a single sniff without any customizations.
I considered using `php://memory` or `php://temp` instead of writing the file to disk, however, neither of those work with `file_get_contents()`, which is used to read the XML ruleset files in the `Ruleset` class, so unfortunately, those temporary file writes cannot be avoided without making bigger changes, which is outside the scope of this PR.
Now, I wondered about the performance impact of writing a file to disk for every sniff being tested, and while there is a small, but noticeable, difference when running on Windows (20 vs 27 seconds for `--filter Standards`), IMO this difference is acceptable when offset against the hours and hours of dev time lost trying to debug why perfectly valid tests weren't working due to `--sniffs` not being respected in the tests.
And to be sure, I've also tested the new test isolation mechanism with an external standard which is using the PHPCS native test framework and it looks to be working fine.
Note: even though the `AbstractSniffUnitTest` will clean up the temporary file after each test, if the test run would crash, it could be possible for the file to remain on disk. With this in mind, the file name for the temporary file has been added to the `.gitignore` file.
Related to 9661 parent 7bc2ff3 commit 41d955a
File tree
3 files changed
+65
-45
lines changed- src
- tests/Standards
3 files changed
+65
-45
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
201 | 201 | | |
202 | 202 | | |
203 | 203 | | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | 204 | | |
221 | 205 | | |
222 | 206 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | | - | |
| 29 | + | |
30 | 30 | | |
31 | | - | |
| 31 | + | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
34 | 59 | | |
35 | 60 | | |
36 | | - | |
| 61 | + | |
37 | 62 | | |
38 | | - | |
| 63 | + | |
39 | 64 | | |
40 | | - | |
| 65 | + | |
41 | 66 | | |
42 | 67 | | |
43 | 68 | | |
| |||
52 | 77 | | |
53 | 78 | | |
54 | 79 | | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
55 | 92 | | |
56 | 93 | | |
57 | 94 | | |
| |||
103 | 140 | | |
104 | 141 | | |
105 | 142 | | |
106 | | - | |
| 143 | + | |
| 144 | + | |
107 | 145 | | |
108 | 146 | | |
109 | 147 | | |
| |||
122 | 160 | | |
123 | 161 | | |
124 | 162 | | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
125 | 180 | | |
126 | 181 | | |
127 | 182 | | |
| |||
130 | 185 | | |
131 | 186 | | |
132 | 187 | | |
133 | | - | |
| 188 | + | |
134 | 189 | | |
135 | 190 | | |
136 | 191 | | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
| 192 | + | |
158 | 193 | | |
159 | 194 | | |
160 | 195 | | |
| |||
0 commit comments