Skip to content

Commit

Permalink
Merge pull request #9566 from DnwK98/handle-different-line-endings
Browse files Browse the repository at this point in the history
Handle different line endings from baseline.
  • Loading branch information
orklah committed Mar 29, 2023
2 parents e2abc3e + 793b8d3 commit 8b9ad1e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Psalm/ErrorBaseline.php
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/LanguageServer/LanguageServer.php
Expand Up @@ -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,
);
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/IssueBuffer.php
Expand Up @@ -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,
);
Expand Down
31 changes: 31 additions & 0 deletions tests/ErrorBaselineTest.php
Expand Up @@ -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(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<files>
<file src=\"sample/sample-file.php\">
<MixedAssignment>
<code>
foo&#13;
bar&#13;
</code>
</MixedAssignment>
</file>
</files>",
);

$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);
Expand Down
20 changes: 20 additions & 0 deletions tests/IssueBufferTest.php
Expand Up @@ -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);
Expand Down

0 comments on commit 8b9ad1e

Please sign in to comment.