Skip to content

Commit b4b9eb2

Browse files
committed
Fixes showing files changed in JSON formatter
1 parent 38d1caf commit b4b9eb2

File tree

3 files changed

+96
-6
lines changed

3 files changed

+96
-6
lines changed

src/ChangesReporting/Output/JsonOutputFormatter.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,22 @@ public function report(ProcessResult $processResult, Configuration $configuratio
2828
],
2929
];
3030

31-
$fileDiffs = $processResult->getFileDiffs();
31+
// We need onlyWithChanges: false to include all file diffs
32+
$fileDiffs = $processResult->getFileDiffs(onlyWithChanges: false);
3233
ksort($fileDiffs);
3334
foreach ($fileDiffs as $fileDiff) {
3435
$filePath = $configuration->isReportingWithRealPath()
3536
? ($fileDiff->getAbsoluteFilePath() ?? '')
3637
: $fileDiff->getRelativeFilePath()
3738
;
3839

39-
$errorsJson[Bridge::FILE_DIFFS][] = [
40-
'file' => $filePath,
41-
'diff' => $fileDiff->getDiff(),
42-
'applied_rectors' => $fileDiff->getRectorClasses(),
43-
];
40+
if ($configuration->shouldShowDiffs() && $fileDiff->getDiff() !== '') {
41+
$errorsJson[Bridge::FILE_DIFFS][] = [
42+
'file' => $filePath,
43+
'diff' => $fileDiff->getDiff(),
44+
'applied_rectors' => $fileDiff->getRectorClasses(),
45+
];
46+
}
4447

4548
// for Rector CI
4649
$errorsJson['changed_files'][] = $filePath;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"totals": {
3+
"changed_files": 2,
4+
"errors": 1
5+
},
6+
"changed_files": [
7+
"some/file.php",
8+
"some/file_foo.php"
9+
],
10+
"errors": [
11+
{
12+
"message": "Some error message",
13+
"file": "some/file.php",
14+
"line": 1
15+
}
16+
]
17+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ChangesReporting\Output;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Rector\ChangesReporting\Output\JsonOutputFormatter;
9+
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
10+
use Rector\Php80\Rector\Identical\StrStartsWithRector;
11+
use Rector\ValueObject\Configuration;
12+
use Rector\ValueObject\Error\SystemError;
13+
use Rector\ValueObject\ProcessResult;
14+
use Rector\ValueObject\Reporting\FileDiff;
15+
16+
final class JsonOutputFormatterTest extends TestCase
17+
{
18+
private readonly JsonOutputFormatter $jsonOutputFormatter;
19+
20+
protected function setUp(): void
21+
{
22+
$this->jsonOutputFormatter = new JsonOutputFormatter();
23+
24+
parent::setUp();
25+
}
26+
27+
public function testGetName(): void
28+
{
29+
$this->assertSame('json', $this->jsonOutputFormatter->getName());
30+
}
31+
32+
public function testReportShouldShowNumberOfChangesWithNoDiffs(): void
33+
{
34+
$this->expectOsOutputString((string) file_get_contents(__DIR__ . '/Fixtures/without_diffs.json'));
35+
36+
$this->jsonOutputFormatter->report(
37+
new ProcessResult(
38+
[new SystemError('Some error message', 'some/file.php', 1)],
39+
[
40+
new FileDiff(
41+
'some/file.php',
42+
'--- Original' . PHP_EOL . '+++ New' . PHP_EOL .
43+
'@@ -38,5 +39,6 @@' . PHP_EOL .
44+
'return true;' . PHP_EOL . '}' . PHP_EOL,
45+
'diff console formatted',
46+
[new RectorWithLineChange(StrStartsWithRector::class, 38)]
47+
),
48+
new FileDiff(
49+
'some/file_foo.php',
50+
'',
51+
'',
52+
[new RectorWithLineChange(StrStartsWithRector::class, 38)]
53+
),
54+
],
55+
2
56+
),
57+
new Configuration(showDiffs: false)
58+
);
59+
}
60+
61+
protected function expectOsOutputString(string $expectedOutput): void
62+
{
63+
$isWindows = strncasecmp(PHP_OS, 'WIN', 3) === 0;
64+
if ($isWindows) {
65+
$expectedOutput = str_replace('%0A', '%0D%0A', $expectedOutput);
66+
}
67+
68+
parent::expectOutputString($expectedOutput);
69+
}
70+
}

0 commit comments

Comments
 (0)