Skip to content

Commit 86f36f5

Browse files
VasekPurchartondrejmirtes
authored andcommitted
Keep same amount of newlines as before
1 parent 0263134 commit 86f36f5

File tree

3 files changed

+84
-8
lines changed

3 files changed

+84
-8
lines changed

src/Command/AnalyseCommand.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\Command\Symfony\SymfonyOutput;
1212
use PHPStan\Command\Symfony\SymfonyStyle;
1313
use PHPStan\File\CouldNotWriteFileException;
14+
use PHPStan\File\FileReader;
1415
use PHPStan\File\FileWriter;
1516
use PHPStan\File\ParentDirectoryRelativePathHelper;
1617
use PHPStan\File\PathNotFoundException;
@@ -32,6 +33,7 @@
3233
use function is_array;
3334
use function is_bool;
3435
use function is_dir;
36+
use function is_file;
3537
use function is_string;
3638
use function mkdir;
3739
use function pathinfo;
@@ -279,10 +281,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
279281
$baselineFileDirectory = dirname($generateBaselineFile);
280282
$baselineErrorFormatter = new BaselineNeonErrorFormatter(new ParentDirectoryRelativePathHelper($baselineFileDirectory));
281283

284+
$existingBaselineContent = is_file($generateBaselineFile) ? FileReader::read($generateBaselineFile) : '';
285+
282286
$streamOutput = $this->createStreamOutput();
283287
$errorConsoleStyle = new ErrorsConsoleStyle(new StringInput(''), $streamOutput);
284288
$baselineOutput = new SymfonyOutput($streamOutput, new SymfonyStyle($errorConsoleStyle));
285-
$baselineErrorFormatter->formatErrors($analysisResult, $baselineOutput);
289+
$baselineErrorFormatter->formatErrors($analysisResult, $baselineOutput, $existingBaselineContent);
286290

287291
$stream = $streamOutput->getStream();
288292
rewind($stream);

src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Nette\DI\Helpers;
66
use Nette\Neon\Neon;
7+
use Nette\Utils\Strings;
78
use PHPStan\Command\AnalysisResult;
89
use PHPStan\Command\Output;
910
use PHPStan\File\RelativePathHelper;
@@ -23,10 +24,11 @@ public function __construct(private RelativePathHelper $relativePathHelper)
2324
public function formatErrors(
2425
AnalysisResult $analysisResult,
2526
Output $output,
27+
string $existingBaselineContent,
2628
): int
2729
{
2830
if (!$analysisResult->hasErrors()) {
29-
$output->writeRaw($this->getNeon([]));
31+
$output->writeRaw($this->getNeon([], $existingBaselineContent));
3032
return 0;
3133
}
3234

@@ -61,15 +63,15 @@ public function formatErrors(
6163
}
6264
}
6365

64-
$output->writeRaw($this->getNeon($errorsToOutput));
66+
$output->writeRaw($this->getNeon($errorsToOutput, $existingBaselineContent));
6567

6668
return 1;
6769
}
6870

6971
/**
7072
* @param array<int, array{message: string, count: int, path: string}> $ignoreErrors
7173
*/
72-
private function getNeon(array $ignoreErrors): string
74+
private function getNeon(array $ignoreErrors, string $existingBaselineContent): string
7375
{
7476
$neon = Neon::encode([
7577
'parameters' => [
@@ -81,7 +83,16 @@ private function getNeon(array $ignoreErrors): string
8183
throw new ShouldNotHappenException();
8284
}
8385

84-
return substr($neon, 0, -1);
86+
if ($existingBaselineContent === '') {
87+
return substr($neon, 0, -1);
88+
}
89+
90+
$existingBaselineContentEndOfFileNewlinesMatches = Strings::match($existingBaselineContent, "~(\n)+$~");
91+
$existingBaselineContentEndOfFileNewlines = $existingBaselineContentEndOfFileNewlinesMatches !== null
92+
? $existingBaselineContentEndOfFileNewlinesMatches[0]
93+
: '';
94+
95+
return substr($neon, 0, -2) . $existingBaselineContentEndOfFileNewlines;
8596
}
8697

8798
}

tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use function rewind;
2121
use function shuffle;
2222
use function sprintf;
23+
use function str_repeat;
2324
use function stream_get_contents;
2425
use function substr;
2526
use function trim;
@@ -128,6 +129,7 @@ public function testFormatErrors(
128129
$this->assertSame($exitCode, $formatter->formatErrors(
129130
$this->getAnalysisResult($numFileErrors, $numGenericErrors),
130131
$this->getOutput(),
132+
'',
131133
), sprintf('%s: response code do not match', $message));
132134

133135
$this->assertSame(trim(Neon::encode(['parameters' => ['ignoreErrors' => $expected]], Neon::BLOCK)), trim($this->getOutputContent()), sprintf('%s: output do not match', $message));
@@ -150,6 +152,7 @@ public function testFormatErrorMessagesRegexEscape(): void
150152
$formatter->formatErrors(
151153
$result,
152154
$this->getOutput(),
155+
'',
153156
);
154157

155158
self::assertSame(
@@ -186,6 +189,7 @@ public function testEscapeDiNeon(): void
186189
$formatter->formatErrors(
187190
$result,
188191
$this->getOutput(),
192+
'',
189193
);
190194
self::assertSame(
191195
trim(
@@ -248,6 +252,7 @@ public function testOutputOrdering(array $errors): void
248252
$formatter->formatErrors(
249253
$result,
250254
$this->getOutput(),
255+
'',
251256
);
252257
self::assertSame(
253258
trim(Neon::encode([
@@ -300,14 +305,63 @@ public function testOutputOrdering(array $errors): void
300305
*/
301306
public function endOfFileNewlinesProvider(): Generator
302307
{
308+
$existingBaselineContentWithoutEndNewlines = 'parameters:
309+
ignoreErrors:
310+
-
311+
message: "#^Existing error$#"
312+
count: 1
313+
path: TestfileA';
314+
303315
yield 'one error' => [
304316
'errors' => [
305317
new Error('Error #1', 'TestfileA', 1),
306318
],
319+
'existingBaselineContent' => $existingBaselineContentWithoutEndNewlines . "\n",
320+
'expectedNewlinesCount' => 1,
307321
];
308322

309323
yield 'no errors' => [
310324
'errors' => [],
325+
'existingBaselineContent' => $existingBaselineContentWithoutEndNewlines . "\n",
326+
'expectedNewlinesCount' => 1,
327+
];
328+
329+
yield 'one error with 2 newlines' => [
330+
'errors' => [
331+
new Error('Error #1', 'TestfileA', 1),
332+
],
333+
'existingBaselineContent' => $existingBaselineContentWithoutEndNewlines . "\n\n",
334+
'expectedNewlinesCount' => 2,
335+
];
336+
337+
yield 'no errors with 2 newlines' => [
338+
'errors' => [],
339+
'existingBaselineContent' => $existingBaselineContentWithoutEndNewlines . "\n\n",
340+
'expectedNewlinesCount' => 2,
341+
];
342+
343+
yield 'one error with 0 newlines' => [
344+
'errors' => [
345+
new Error('Error #1', 'TestfileA', 1),
346+
],
347+
'existingBaselineContent' => $existingBaselineContentWithoutEndNewlines,
348+
'expectedNewlinesCount' => 0,
349+
];
350+
351+
yield 'one error with 3 newlines' => [
352+
'errors' => [
353+
new Error('Error #1', 'TestfileA', 1),
354+
],
355+
'existingBaselineContent' => $existingBaselineContentWithoutEndNewlines . "\n\n\n",
356+
'expectedNewlinesCount' => 3,
357+
];
358+
359+
yield 'empty existing baseline' => [
360+
'errors' => [
361+
new Error('Error #1', 'TestfileA', 1),
362+
],
363+
'existingBaselineContent' => '',
364+
'expectedNewlinesCount' => 1,
311365
];
312366
}
313367

@@ -316,7 +370,11 @@ public function endOfFileNewlinesProvider(): Generator
316370
*
317371
* @param list<Error> $errors
318372
*/
319-
public function testEndOfFileNewlines(array $errors): void
373+
public function testEndOfFileNewlines(
374+
array $errors,
375+
string $existingBaselineContent,
376+
int $expectedNewlinesCount,
377+
): void
320378
{
321379
$formatter = new BaselineNeonErrorFormatter(new SimpleRelativePathHelper(self::DIRECTORY_PATH));
322380
$result = new AnalysisResult(
@@ -341,6 +399,7 @@ public function testEndOfFileNewlines(array $errors): void
341399
$formatter->formatErrors(
342400
$result,
343401
$output,
402+
$existingBaselineContent,
344403
);
345404

346405
rewind($outputStream->getStream());
@@ -350,8 +409,10 @@ public function testEndOfFileNewlines(array $errors): void
350409
throw new ShouldNotHappenException();
351410
}
352411

353-
Assert::assertSame("\n", substr($content, -1));
354-
Assert::assertNotSame("\n", substr($content, -2, 1));
412+
if ($expectedNewlinesCount > 0) {
413+
Assert::assertSame(str_repeat("\n", $expectedNewlinesCount), substr($content, -$expectedNewlinesCount));
414+
}
415+
Assert::assertNotSame("\n", substr($content, -($expectedNewlinesCount + 1), 1));
355416
}
356417

357418
}

0 commit comments

Comments
 (0)