From f3eac53f45961a30c02f0a987572e58f901d721b Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 20 Feb 2024 09:56:04 +0100 Subject: [PATCH] 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. --- tests/Standards/AbstractSniffUnitTest.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/Standards/AbstractSniffUnitTest.php b/tests/Standards/AbstractSniffUnitTest.php index 7c6b500e95..ccd90c513c 100644 --- a/tests/Standards/AbstractSniffUnitTest.php +++ b/tests/Standards/AbstractSniffUnitTest.php @@ -193,10 +193,13 @@ final public function testSniff() $fixedFile = $testFile.'.fixed'; $filename = basename($testFile); if (file_exists($fixedFile) === true) { - $diff = $phpcsFile->fixer->generateDiff($fixedFile); - if (trim($diff) !== '') { - $fixedFilename = basename($fixedFile); - $failureMessages[] = "Fixed version of $filename does not match expected version in $fixedFilename; the diff is\n$diff"; + if ($phpcsFile->fixer->getContents() !== file_get_contents($fixedFile)) { + // Only generate the (expensive) diff if a difference is expected. + $diff = $phpcsFile->fixer->generateDiff($fixedFile); + if (trim($diff) !== '') { + $fixedFilename = basename($fixedFile); + $failureMessages[] = "Fixed version of $filename does not match expected version in $fixedFilename; the diff is\n$diff"; + } } } else if (is_callable([$this, 'addWarning']) === true) { $this->addWarning("Missing fixed version of $filename to verify the accuracy of fixes, while the sniff is making fixes against the test case file");