Skip to content

Commit b06aa0f

Browse files
committed
Tests: improve performance of the sniff tests
The `Fixer::generateDiff()` method uses `shell_exec()` with the `diff` command to generate a file diff. Using this command is slow, in particular on Windows. This commit introduces a preliminary check in the test logic to see if a diff is even needed and skips generating the diff if no differences are expected. I don't know whether and if so, how much this will make a difference for *nix users, but on Windows, it makes a significant difference when running the sniff tests. A run of just the sniff tests without this fix takes > 1 minute (~01.03.701 last time I ran it). With this fix, the run time of the sniff tests is brought down to ~8 seconds. So let's call this a quality of life improvement for all devs which regularly need to run sniff tests for either PHPCS itself or for external standards which base their test suite on the PHPCS native test framework.
1 parent 795136c commit b06aa0f

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

tests/Standards/AbstractSniffUnitTest.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,13 @@ final public function testSniff()
193193
$fixedFile = $testFile.'.fixed';
194194
$filename = basename($testFile);
195195
if (file_exists($fixedFile) === true) {
196-
$diff = $phpcsFile->fixer->generateDiff($fixedFile);
197-
if (trim($diff) !== '') {
198-
$fixedFilename = basename($fixedFile);
199-
$failureMessages[] = "Fixed version of $filename does not match expected version in $fixedFilename; the diff is\n$diff";
196+
if ($phpcsFile->fixer->getContents() !== file_get_contents($fixedFile)) {
197+
// Only generate the (expensive) diff if a difference is expected.
198+
$diff = $phpcsFile->fixer->generateDiff($fixedFile);
199+
if (trim($diff) !== '') {
200+
$fixedFilename = basename($fixedFile);
201+
$failureMessages[] = "Fixed version of $filename does not match expected version in $fixedFilename; the diff is\n$diff";
202+
}
200203
}
201204
} else if (is_callable([$this, 'addWarning']) === true) {
202205
$this->addWarning("Missing fixed version of $filename to verify the accuracy of fixes, while the sniff is making fixes against the test case file");

0 commit comments

Comments
 (0)