Skip to content

Commit

Permalink
encode newlines in GithubErrorFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Sep 6, 2020
1 parent 92b57a5 commit c99b7f8
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 38 deletions.
11 changes: 11 additions & 0 deletions src/Command/ErrorFormatter/GithubErrorFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in
});

$message = $fileSpecificError->getMessage();
// newlines need to be encoded
// see https://github.com/actions/starter-workflows/issues/68#issuecomment-581479448
$message = str_replace("\n", '%0A', $message);

$line = sprintf('::error %s::%s', implode(',', $metas), $message);

Expand All @@ -49,13 +52,21 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in
}

foreach ($analysisResult->getNotFileSpecificErrors() as $notFileSpecificError) {
// newlines need to be encoded
// see https://github.com/actions/starter-workflows/issues/68#issuecomment-581479448
$notFileSpecificError = str_replace("\n", '%0A', $notFileSpecificError);

$line = sprintf('::error ::%s', $notFileSpecificError);

$output->writeRaw($line);
$output->writeLineFormatted('');
}

foreach ($analysisResult->getWarnings() as $warning) {
// newlines need to be encoded
// see https://github.com/actions/starter-workflows/issues/68#issuecomment-581479448
$warning = str_replace("\n", '%0A', $warning);

$line = sprintf('::warning ::%s', $warning);

$output->writeRaw($line);
Expand Down
4 changes: 2 additions & 2 deletions src/Testing/ErrorFormatterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ protected function getAnalysisResult(int $numFileErrors, int $numGenericErrors):
$fileErrors = array_slice([
new Error('Foo', self::DIRECTORY_PATH . '/folder with unicode 😃/file name with "spaces" and unicode 😃.php', 4),
new Error('Foo', self::DIRECTORY_PATH . '/foo.php', 1),
new Error('Bar', self::DIRECTORY_PATH . '/foo.php', 5),
new Error('Bar', self::DIRECTORY_PATH . '/folder with unicode 😃/file name with "spaces" and unicode 😃.php', 2),
new Error("Bar\nBar2", self::DIRECTORY_PATH . '/foo.php', 5),
new Error("Bar\nBar2", self::DIRECTORY_PATH . '/folder with unicode 😃/file name with "spaces" and unicode 😃.php', 2),
], 0, $numFileErrors);

$genericErrors = array_slice([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function dataFormatterOutputProvider(): iterable
0,
[
[
'message' => '#^Bar$#',
'message' => "#^Bar\nBar2$#",
'count' => 1,
'path' => 'folder with unicode 😃/file name with "spaces" and unicode 😃.php',
],
Expand All @@ -57,7 +57,7 @@ public function dataFormatterOutputProvider(): iterable
'path' => 'foo.php',
],
[
'message' => '#^Bar$#',
'message' => "#^Bar\nBar2$#",
'count' => 1,
'path' => 'foo.php',
],
Expand All @@ -71,7 +71,7 @@ public function dataFormatterOutputProvider(): iterable
2,
[
[
'message' => '#^Bar$#',
'message' => "#^Bar\nBar2$#",
'count' => 1,
'path' => 'folder with unicode 😃/file name with "spaces" and unicode 😃.php',
],
Expand All @@ -86,7 +86,7 @@ public function dataFormatterOutputProvider(): iterable
'path' => 'foo.php',
],
[
'message' => '#^Bar$#',
'message' => "#^Bar\nBar2$#",
'count' => 1,
'path' => 'foo.php',
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public function dataFormatterOutputProvider(): iterable
'<?xml version="1.0" encoding="UTF-8"?>
<checkstyle>
<file name="folder with unicode 😃/file name with &quot;spaces&quot; and unicode 😃.php">
<error line="2" column="1" severity="error" message="Bar"/>
<error line="2" column="1" severity="error" message="Bar Bar2"/>
<error line="4" column="1" severity="error" message="Foo"/>
</file>
<file name="foo.php">
<error line="1" column="1" severity="error" message="Foo"/>
<error line="5" column="1" severity="error" message="Bar"/>
<error line="5" column="1" severity="error" message="Bar Bar2"/>
</file>
</checkstyle>
',
Expand Down Expand Up @@ -93,12 +93,12 @@ public function dataFormatterOutputProvider(): iterable
'<?xml version="1.0" encoding="UTF-8"?>
<checkstyle>
<file name="folder with unicode 😃/file name with &quot;spaces&quot; and unicode 😃.php">
<error line="2" column="1" severity="error" message="Bar"/>
<error line="2" column="1" severity="error" message="Bar Bar2"/>
<error line="4" column="1" severity="error" message="Foo"/>
</file>
<file name="foo.php">
<error line="1" column="1" severity="error" message="Foo"/>
<error line="5" column="1" severity="error" message="Bar"/>
<error line="5" column="1" severity="error" message="Bar Bar2"/>
</file>
<file>
<error message="first generic error" severity="error"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function dataFormatterOutputProvider(): iterable
Line folder with unicode 😃/file name with "spaces" and unicode 😃.php
------ -----------------------------------------------------------------
2 Bar
Bar2
4 Foo
------ -----------------------------------------------------------------
Expand All @@ -72,14 +73,15 @@ public function dataFormatterOutputProvider(): iterable
------ ---------
1 Foo
5 Bar
Bar2
------ ---------
[ERROR] Found 4 errors
::error file=folder with unicode 😃/file name with "spaces" and unicode 😃.php,line=2,col=0::Bar
::error file=folder with unicode 😃/file name with "spaces" and unicode 😃.php,line=2,col=0::Bar%0ABar2
::error file=folder with unicode 😃/file name with "spaces" and unicode 😃.php,line=4,col=0::Foo
::error file=foo.php,line=1,col=0::Foo
::error file=foo.php,line=5,col=0::Bar
::error file=foo.php,line=5,col=0::Bar%0ABar2
',
];

Expand Down Expand Up @@ -111,6 +113,7 @@ public function dataFormatterOutputProvider(): iterable
Line folder with unicode 😃/file name with "spaces" and unicode 😃.php
------ -----------------------------------------------------------------
2 Bar
Bar2
4 Foo
------ -----------------------------------------------------------------
Expand All @@ -119,6 +122,7 @@ public function dataFormatterOutputProvider(): iterable
------ ---------
1 Foo
5 Bar
Bar2
------ ---------
-- ----------------------
Expand All @@ -130,10 +134,10 @@ public function dataFormatterOutputProvider(): iterable
[ERROR] Found 6 errors
::error file=folder with unicode 😃/file name with "spaces" and unicode 😃.php,line=2,col=0::Bar
::error file=folder with unicode 😃/file name with "spaces" and unicode 😃.php,line=2,col=0::Bar%0ABar2
::error file=folder with unicode 😃/file name with "spaces" and unicode 😃.php,line=4,col=0::Foo
::error file=foo.php,line=1,col=0::Foo
::error file=foo.php,line=5,col=0::Bar
::error file=foo.php,line=5,col=0::Bar%0ABar2
::error ::first generic error
::error ::second generic error
',
Expand Down
16 changes: 8 additions & 8 deletions tests/PHPStan/Command/ErrorFormatter/GitlabFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public function dataFormatterOutputProvider(): iterable
0,
'[
{
"description": "Bar",
"fingerprint": "d112f1651daa597592156359ef28c9a4b81a8a96dbded1c0f1009f5bbc2bda97",
"description": "Bar\nBar2",
"fingerprint": "034b4afbfb347494c14e396ed8327692f58be4cd27e8aff5f19f4194934db7c9",
"location": {
"path": "with space/and unicode 😃/project/folder with unicode 😃/file name with \"spaces\" and unicode 😃.php",
"lines": {
Expand Down Expand Up @@ -93,8 +93,8 @@ public function dataFormatterOutputProvider(): iterable
}
},
{
"description": "Bar",
"fingerprint": "d83022ee5bc7c71b6a4988ec47a377c9998b929d12d86fc71b745ec2b04c81e5",
"description": "Bar\nBar2",
"fingerprint": "829f6c782152fdac840b39208c5b519d18e51bff2c601b6197812fffb8bcd9ed",
"location": {
"path": "with space/and unicode \ud83d\ude03/project/foo.php",
"lines": {
Expand Down Expand Up @@ -141,8 +141,8 @@ public function dataFormatterOutputProvider(): iterable
2,
'[
{
"description": "Bar",
"fingerprint": "d112f1651daa597592156359ef28c9a4b81a8a96dbded1c0f1009f5bbc2bda97",
"description": "Bar\nBar2",
"fingerprint": "034b4afbfb347494c14e396ed8327692f58be4cd27e8aff5f19f4194934db7c9",
"location": {
"path": "with space/and unicode \ud83d\ude03/project/folder with unicode \ud83d\ude03/file name with \"spaces\" and unicode \ud83d\ude03.php",
"lines": {
Expand Down Expand Up @@ -171,8 +171,8 @@ public function dataFormatterOutputProvider(): iterable
}
},
{
"description": "Bar",
"fingerprint": "d83022ee5bc7c71b6a4988ec47a377c9998b929d12d86fc71b745ec2b04c81e5",
"description": "Bar\nBar2",
"fingerprint": "829f6c782152fdac840b39208c5b519d18e51bff2c601b6197812fffb8bcd9ed",
"location": {
"path": "with space/and unicode \ud83d\ude03/project/foo.php",
"lines": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function dataFormatterOutputProvider(): iterable
"errors":2,
"messages":[
{
"message": "Bar",
"message": "Bar\nBar2",
"line": 2,
"ignorable": true
},
Expand All @@ -106,7 +106,7 @@ public function dataFormatterOutputProvider(): iterable
"ignorable": true
},
{
"message": "Bar",
"message": "Bar\nBar2",
"line": 5,
"ignorable": true
}
Expand Down Expand Up @@ -152,7 +152,7 @@ public function dataFormatterOutputProvider(): iterable
"errors":2,
"messages":[
{
"message": "Bar",
"message": "Bar\nBar2",
"line": 2,
"ignorable": true
},
Expand All @@ -172,7 +172,7 @@ public function dataFormatterOutputProvider(): iterable
"ignorable": true
},
{
"message": "Bar",
"message": "Bar\nBar2",
"line": 5,
"ignorable": true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function dataFormatterOutputProvider(): Generator
'<?xml version="1.0" encoding="UTF-8"?>
<testsuite failures="4" name="phpstan" tests="4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/junit-team/junit5/r5.5.1/platform-tests/src/test/resources/jenkins-junit.xsd">
<testcase name="folder with unicode &#x1F603;/file name with &quot;spaces&quot; and unicode &#x1F603;.php:2">
<failure type="ERROR" message="Bar" />
<failure type="ERROR" message="Bar Bar2" />
</testcase>
<testcase name="folder with unicode &#x1F603;/file name with &quot;spaces&quot; and unicode &#x1F603;.php:4">
<failure type="ERROR" message="Foo" />
Expand All @@ -78,7 +78,7 @@ public function dataFormatterOutputProvider(): Generator
<failure type="ERROR" message="Foo"/>
</testcase>
<testcase name="foo.php:5">
<failure type="ERROR" message="Bar"/>
<failure type="ERROR" message="Bar Bar2"/>
</testcase>
</testsuite>
',
Expand Down Expand Up @@ -107,7 +107,7 @@ public function dataFormatterOutputProvider(): Generator
'<?xml version="1.0" encoding="UTF-8"?>
<testsuite failures="6" name="phpstan" tests="6" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/junit-team/junit5/r5.5.1/platform-tests/src/test/resources/jenkins-junit.xsd">
<testcase name="folder with unicode &#x1F603;/file name with &quot;spaces&quot; and unicode &#x1F603;.php:2">
<failure type="ERROR" message="Bar" />
<failure type="ERROR" message="Bar Bar2" />
</testcase>
<testcase name="folder with unicode &#x1F603;/file name with &quot;spaces&quot; and unicode &#x1F603;.php:4">
<failure type="ERROR" message="Foo" />
Expand All @@ -116,7 +116,7 @@ public function dataFormatterOutputProvider(): Generator
<failure type="ERROR" message="Foo"/>
</testcase>
<testcase name="foo.php:5">
<failure type="ERROR" message="Bar"/>
<failure type="ERROR" message="Bar Bar2"/>
</testcase>
<testcase name="General error">
<failure type="ERROR" message="first generic error" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public function dataFormatterOutputProvider(): iterable
1,
4,
0,
'/data/folder/with space/and unicode 😃/project/folder with unicode 😃/file name with "spaces" and unicode 😃.php:2:Bar' . "\n" .
'/data/folder/with space/and unicode 😃/project/folder with unicode 😃/file name with "spaces" and unicode 😃.php:2:Bar' . "\nBar2\n" .
'/data/folder/with space/and unicode 😃/project/folder with unicode 😃/file name with "spaces" and unicode 😃.php:4:Foo' . "\n" .
'/data/folder/with space/and unicode 😃/project/foo.php:1:Foo' . "\n" .
'/data/folder/with space/and unicode 😃/project/foo.php:5:Bar' . "\n",
'/data/folder/with space/and unicode 😃/project/foo.php:5:Bar' . "\nBar2\n",
];

yield [
Expand All @@ -60,10 +60,10 @@ public function dataFormatterOutputProvider(): iterable
2,
'?:?:first generic error' . "\n" .
'?:?:second generic error' . "\n" .
'/data/folder/with space/and unicode 😃/project/folder with unicode 😃/file name with "spaces" and unicode 😃.php:2:Bar' . "\n" .
'/data/folder/with space/and unicode 😃/project/folder with unicode 😃/file name with "spaces" and unicode 😃.php:2:Bar' . "\nBar2\n" .
'/data/folder/with space/and unicode 😃/project/folder with unicode 😃/file name with "spaces" and unicode 😃.php:4:Foo' . "\n" .
'/data/folder/with space/and unicode 😃/project/foo.php:1:Foo' . "\n" .
'/data/folder/with space/and unicode 😃/project/foo.php:5:Bar' . "\n",
'/data/folder/with space/and unicode 😃/project/foo.php:5:Bar' . "\nBar2\n",
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function dataFormatterOutputProvider(): iterable
Line folder with unicode 😃/file name with "spaces" and unicode 😃.php
------ -----------------------------------------------------------------
2 Bar
Bar2
4 Foo
------ -----------------------------------------------------------------
Expand All @@ -70,6 +71,7 @@ public function dataFormatterOutputProvider(): iterable
------ ---------
1 Foo
5 Bar
Bar2
------ ---------
[ERROR] Found 4 errors
Expand Down Expand Up @@ -103,6 +105,7 @@ public function dataFormatterOutputProvider(): iterable
Line folder with unicode 😃/file name with "spaces" and unicode 😃.php
------ -----------------------------------------------------------------
2 Bar
Bar2
4 Foo
------ -----------------------------------------------------------------
Expand All @@ -111,6 +114,7 @@ public function dataFormatterOutputProvider(): iterable
------ ---------
1 Foo
5 Bar
Bar2
------ ---------
-- ----------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public function dataFormatterOutputProvider(): iterable
4,
0,
'##teamcity[inspectionType id=\'phpstan\' name=\'phpstan\' category=\'phpstan\' description=\'phpstan Inspection\']
##teamcity[inspection typeId=\'phpstan\' message=\'Bar\' file=\'folder with unicode 😃/file name with "spaces" and unicode 😃.php\' line=\'2\' SEVERITY=\'ERROR\' ignorable=\'1\' tip=\'\']
##teamcity[inspection typeId=\'phpstan\' message=\'Bar||nBar2\' file=\'folder with unicode 😃/file name with "spaces" and unicode 😃.php\' line=\'2\' SEVERITY=\'ERROR\' ignorable=\'1\' tip=\'\']
##teamcity[inspection typeId=\'phpstan\' message=\'Foo\' file=\'folder with unicode 😃/file name with "spaces" and unicode 😃.php\' line=\'4\' SEVERITY=\'ERROR\' ignorable=\'1\' tip=\'\']
##teamcity[inspection typeId=\'phpstan\' message=\'Foo\' file=\'foo.php\' line=\'1\' SEVERITY=\'ERROR\' ignorable=\'1\' tip=\'\']
##teamcity[inspection typeId=\'phpstan\' message=\'Bar\' file=\'foo.php\' line=\'5\' SEVERITY=\'ERROR\' ignorable=\'1\' tip=\'\']
##teamcity[inspection typeId=\'phpstan\' message=\'Bar||nBar2\' file=\'foo.php\' line=\'5\' SEVERITY=\'ERROR\' ignorable=\'1\' tip=\'\']
',
];

Expand All @@ -68,10 +68,10 @@ public function dataFormatterOutputProvider(): iterable
4,
2,
'##teamcity[inspectionType id=\'phpstan\' name=\'phpstan\' category=\'phpstan\' description=\'phpstan Inspection\']
##teamcity[inspection typeId=\'phpstan\' message=\'Bar\' file=\'folder with unicode 😃/file name with "spaces" and unicode 😃.php\' line=\'2\' SEVERITY=\'ERROR\' ignorable=\'1\' tip=\'\']
##teamcity[inspection typeId=\'phpstan\' message=\'Bar||nBar2\' file=\'folder with unicode 😃/file name with "spaces" and unicode 😃.php\' line=\'2\' SEVERITY=\'ERROR\' ignorable=\'1\' tip=\'\']
##teamcity[inspection typeId=\'phpstan\' message=\'Foo\' file=\'folder with unicode 😃/file name with "spaces" and unicode 😃.php\' line=\'4\' SEVERITY=\'ERROR\' ignorable=\'1\' tip=\'\']
##teamcity[inspection typeId=\'phpstan\' message=\'Foo\' file=\'foo.php\' line=\'1\' SEVERITY=\'ERROR\' ignorable=\'1\' tip=\'\']
##teamcity[inspection typeId=\'phpstan\' message=\'Bar\' file=\'foo.php\' line=\'5\' SEVERITY=\'ERROR\' ignorable=\'1\' tip=\'\']
##teamcity[inspection typeId=\'phpstan\' message=\'Bar||nBar2\' file=\'foo.php\' line=\'5\' SEVERITY=\'ERROR\' ignorable=\'1\' tip=\'\']
##teamcity[inspection typeId=\'phpstan\' message=\'first generic error\' file=\'./\' SEVERITY=\'ERROR\']
##teamcity[inspection typeId=\'phpstan\' message=\'second generic error\' file=\'./\' SEVERITY=\'ERROR\']
',
Expand Down

0 comments on commit c99b7f8

Please sign in to comment.