From b4b9eb275dbca41d6430c6e768b2322f4cff4c9c Mon Sep 17 00:00:00 2001 From: Peter Fox Date: Mon, 5 Jan 2026 14:27:03 +0000 Subject: [PATCH 1/3] Fixes showing files changed in JSON formatter --- .../Output/JsonOutputFormatter.php | 15 ++-- .../Output/Fixtures/without_diffs.json | 17 +++++ .../Output/JsonOutputFormatterTest.php | 70 +++++++++++++++++++ 3 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 tests/ChangesReporting/Output/Fixtures/without_diffs.json create mode 100644 tests/ChangesReporting/Output/JsonOutputFormatterTest.php diff --git a/src/ChangesReporting/Output/JsonOutputFormatter.php b/src/ChangesReporting/Output/JsonOutputFormatter.php index 521208a7ba2..5ec742933c3 100644 --- a/src/ChangesReporting/Output/JsonOutputFormatter.php +++ b/src/ChangesReporting/Output/JsonOutputFormatter.php @@ -28,7 +28,8 @@ public function report(ProcessResult $processResult, Configuration $configuratio ], ]; - $fileDiffs = $processResult->getFileDiffs(); + // We need onlyWithChanges: false to include all file diffs + $fileDiffs = $processResult->getFileDiffs(onlyWithChanges: false); ksort($fileDiffs); foreach ($fileDiffs as $fileDiff) { $filePath = $configuration->isReportingWithRealPath() @@ -36,11 +37,13 @@ public function report(ProcessResult $processResult, Configuration $configuratio : $fileDiff->getRelativeFilePath() ; - $errorsJson[Bridge::FILE_DIFFS][] = [ - 'file' => $filePath, - 'diff' => $fileDiff->getDiff(), - 'applied_rectors' => $fileDiff->getRectorClasses(), - ]; + if ($configuration->shouldShowDiffs() && $fileDiff->getDiff() !== '') { + $errorsJson[Bridge::FILE_DIFFS][] = [ + 'file' => $filePath, + 'diff' => $fileDiff->getDiff(), + 'applied_rectors' => $fileDiff->getRectorClasses(), + ]; + } // for Rector CI $errorsJson['changed_files'][] = $filePath; diff --git a/tests/ChangesReporting/Output/Fixtures/without_diffs.json b/tests/ChangesReporting/Output/Fixtures/without_diffs.json new file mode 100644 index 00000000000..19629ae13dc --- /dev/null +++ b/tests/ChangesReporting/Output/Fixtures/without_diffs.json @@ -0,0 +1,17 @@ +{ + "totals": { + "changed_files": 2, + "errors": 1 + }, + "changed_files": [ + "some/file.php", + "some/file_foo.php" + ], + "errors": [ + { + "message": "Some error message", + "file": "some/file.php", + "line": 1 + } + ] +} diff --git a/tests/ChangesReporting/Output/JsonOutputFormatterTest.php b/tests/ChangesReporting/Output/JsonOutputFormatterTest.php new file mode 100644 index 00000000000..8fe6f194346 --- /dev/null +++ b/tests/ChangesReporting/Output/JsonOutputFormatterTest.php @@ -0,0 +1,70 @@ +jsonOutputFormatter = new JsonOutputFormatter(); + + parent::setUp(); + } + + public function testGetName(): void + { + $this->assertSame('json', $this->jsonOutputFormatter->getName()); + } + + public function testReportShouldShowNumberOfChangesWithNoDiffs(): void + { + $this->expectOsOutputString((string) file_get_contents(__DIR__ . '/Fixtures/without_diffs.json')); + + $this->jsonOutputFormatter->report( + new ProcessResult( + [new SystemError('Some error message', 'some/file.php', 1)], + [ + new FileDiff( + 'some/file.php', + '--- Original' . PHP_EOL . '+++ New' . PHP_EOL . + '@@ -38,5 +39,6 @@' . PHP_EOL . + 'return true;' . PHP_EOL . '}' . PHP_EOL, + 'diff console formatted', + [new RectorWithLineChange(StrStartsWithRector::class, 38)] + ), + new FileDiff( + 'some/file_foo.php', + '', + '', + [new RectorWithLineChange(StrStartsWithRector::class, 38)] + ), + ], + 2 + ), + new Configuration(showDiffs: false) + ); + } + + protected function expectOsOutputString(string $expectedOutput): void + { + $isWindows = strncasecmp(PHP_OS, 'WIN', 3) === 0; + if ($isWindows) { + $expectedOutput = str_replace('%0A', '%0D%0A', $expectedOutput); + } + + parent::expectOutputString($expectedOutput); + } +} From 541d21ac38fd76aeec7f3885353ffc50a89c948d Mon Sep 17 00:00:00 2001 From: Peter Fox Date: Mon, 5 Jan 2026 16:29:12 +0000 Subject: [PATCH 2/3] test change --- .../Output/JsonOutputFormatterTest.php | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/tests/ChangesReporting/Output/JsonOutputFormatterTest.php b/tests/ChangesReporting/Output/JsonOutputFormatterTest.php index 8fe6f194346..b5c2fc2dbbc 100644 --- a/tests/ChangesReporting/Output/JsonOutputFormatterTest.php +++ b/tests/ChangesReporting/Output/JsonOutputFormatterTest.php @@ -31,7 +31,7 @@ public function testGetName(): void public function testReportShouldShowNumberOfChangesWithNoDiffs(): void { - $this->expectOsOutputString((string) file_get_contents(__DIR__ . '/Fixtures/without_diffs.json')); + $this->expectOutputString((string) file_get_contents(__DIR__ . '/Fixtures/without_diffs.json')); $this->jsonOutputFormatter->report( new ProcessResult( @@ -57,14 +57,4 @@ public function testReportShouldShowNumberOfChangesWithNoDiffs(): void new Configuration(showDiffs: false) ); } - - protected function expectOsOutputString(string $expectedOutput): void - { - $isWindows = strncasecmp(PHP_OS, 'WIN', 3) === 0; - if ($isWindows) { - $expectedOutput = str_replace('%0A', '%0D%0A', $expectedOutput); - } - - parent::expectOutputString($expectedOutput); - } } From 66a267b74f7cc801d9a2004b1b62e9d5642b4172 Mon Sep 17 00:00:00 2001 From: Peter Fox Date: Mon, 5 Jan 2026 16:34:49 +0000 Subject: [PATCH 3/3] Revert "test change" This reverts commit e9b5a0f13addb2c4d9c5faf8b72cc51b48ec3088. --- .../Output/JsonOutputFormatterTest.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/ChangesReporting/Output/JsonOutputFormatterTest.php b/tests/ChangesReporting/Output/JsonOutputFormatterTest.php index b5c2fc2dbbc..8fe6f194346 100644 --- a/tests/ChangesReporting/Output/JsonOutputFormatterTest.php +++ b/tests/ChangesReporting/Output/JsonOutputFormatterTest.php @@ -31,7 +31,7 @@ public function testGetName(): void public function testReportShouldShowNumberOfChangesWithNoDiffs(): void { - $this->expectOutputString((string) file_get_contents(__DIR__ . '/Fixtures/without_diffs.json')); + $this->expectOsOutputString((string) file_get_contents(__DIR__ . '/Fixtures/without_diffs.json')); $this->jsonOutputFormatter->report( new ProcessResult( @@ -57,4 +57,14 @@ public function testReportShouldShowNumberOfChangesWithNoDiffs(): void new Configuration(showDiffs: false) ); } + + protected function expectOsOutputString(string $expectedOutput): void + { + $isWindows = strncasecmp(PHP_OS, 'WIN', 3) === 0; + if ($isWindows) { + $expectedOutput = str_replace('%0A', '%0D%0A', $expectedOutput); + } + + parent::expectOutputString($expectedOutput); + } }