From 350687dcb725ee3b9b28dcec9cf392ecb50a3cc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20W=C3=B3jcik?= Date: Fri, 24 Mar 2023 10:48:03 +0100 Subject: [PATCH 1/2] Handle different line endings from baseline. --- src/Psalm/ErrorBaseline.php | 2 +- .../LanguageServer/LanguageServer.php | 2 +- src/Psalm/IssueBuffer.php | 2 +- tests/IssueBufferTest.php | 20 +++++++++++++++++++ 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/Psalm/ErrorBaseline.php b/src/Psalm/ErrorBaseline.php index 9a22ec8295b..9a83b0a2899 100644 --- a/src/Psalm/ErrorBaseline.php +++ b/src/Psalm/ErrorBaseline.php @@ -118,7 +118,7 @@ public static function read(FileProvider $fileProvider, string $baselineFile): a foreach ($codeSamples as $codeSample) { $files[$fileName][$issueType]['o'] += 1; - $files[$fileName][$issueType]['s'][] = trim($codeSample->textContent); + $files[$fileName][$issueType]['s'][] = str_replace("\r\n", "\n", trim($codeSample->textContent)); } // TODO: Remove in Psalm 6 diff --git a/src/Psalm/Internal/LanguageServer/LanguageServer.php b/src/Psalm/Internal/LanguageServer/LanguageServer.php index 2f99b784cb6..daec34e8d22 100644 --- a/src/Psalm/Internal/LanguageServer/LanguageServer.php +++ b/src/Psalm/Internal/LanguageServer/LanguageServer.php @@ -782,7 +782,7 @@ function (IssueData $issue_data): Diagnostic { if ($issue_baseline[$file][$type]['o'] === count($issue_baseline[$file][$type]['s'])) { /** @psalm-suppress MixedArrayAccess, MixedAssignment */ $position = array_search( - trim($issue_data->selected_text), + str_replace("\r\n", "\n", trim($issue_data->selected_text)), $issue_baseline[$file][$type]['s'], true, ); diff --git a/src/Psalm/IssueBuffer.php b/src/Psalm/IssueBuffer.php index 50f347b9815..0c40288a067 100644 --- a/src/Psalm/IssueBuffer.php +++ b/src/Psalm/IssueBuffer.php @@ -592,7 +592,7 @@ public static function finish( if (isset($issue_baseline[$file][$type]) && $issue_baseline[$file][$type]['o'] > 0) { if ($issue_baseline[$file][$type]['o'] === count($issue_baseline[$file][$type]['s'])) { $position = array_search( - trim($issue_data->selected_text), + str_replace("\r\n", "\n", trim($issue_data->selected_text)), $issue_baseline[$file][$type]['s'], true, ); diff --git a/tests/IssueBufferTest.php b/tests/IssueBufferTest.php index cf876901cf4..bbf2add82ad 100644 --- a/tests/IssueBufferTest.php +++ b/tests/IssueBufferTest.php @@ -79,11 +79,31 @@ public function testFinishDoesNotCorruptInternalState(): void 0, ), ], + '/path/four.php' => [ + new IssueData( + "error", + 0, + 0, + "MissingPropertyType", + 'Message', + "four.php", + "/path/four.php", + "snippet-4-multiline\r\nwith-carriage-return\r", + "snippet-4-multiline\r\nwith-carriage-return\r", + 0, + 0, + 0, + 0, + 0, + 0, + ), + ], ]); $baseline = [ 'one.php' => ['MissingPropertyType' => ['o' => 1, 's' => ['snippet-1']] ], 'two.php' => ['MissingPropertyType' => ['o' => 1, 's' => ['snippet-2']] ], 'three.php' => ['MissingPropertyType' => ['o' => 1, 's' => ['snippet-3-has-carriage-return']] ], + 'four.php' => ['MissingPropertyType' => ['o' => 1, 's' => ["snippet-4-multiline\nwith-carriage-return"]] ], ]; $analyzer = $this->createMock(Analyzer::class); From 793b8d34d83721edc03815c0e07798b85baf0562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20W=C3=B3jcik?= Date: Tue, 28 Mar 2023 14:40:11 +0200 Subject: [PATCH 2/2] Add baseline ignoring carriage return test. --- tests/ErrorBaselineTest.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/ErrorBaselineTest.php b/tests/ErrorBaselineTest.php index ca0fb333254..ac0e2cad43f 100644 --- a/tests/ErrorBaselineTest.php +++ b/tests/ErrorBaselineTest.php @@ -98,6 +98,37 @@ public function testLoadShouldIgnoreLineEndingsInIssueSnippet(): void ); } + public function testShouldIgnoreCarriageReturnInMultilineSnippets(): void + { + $baselineFilePath = 'baseline.xml'; + + $this->fileProvider->allows()->fileExists($baselineFilePath)->andReturns(true); + $this->fileProvider->allows()->getContents($baselineFilePath)->andReturns( + " + + + + +foo +bar + + + + ", + ); + + $expectedParsedBaseline = [ + 'sample/sample-file.php' => [ + 'MixedAssignment' => ['o' => 1, 's' => ["foo\nbar"]], + ], + ]; + + $this->assertSame( + $expectedParsedBaseline, + ErrorBaseline::read($this->fileProvider, $baselineFilePath), + ); + } + public function testLoadShouldThrowExceptionWhenFilesAreNotDefinedInBaselineFile(): void { $this->expectException(ConfigException::class);