Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add filename and line number to phpunit output #2775

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/Command/ErrorFormatter/JunitErrorFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function formatErrors(

$result = '<?xml version="1.0" encoding="UTF-8"?>';
$result .= sprintf(
'<testsuite failures="%d" name="phpstan" tests="%d" 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">',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you remove this? Maybe it's important

'<testsuite failures="%d" name="phpstan" tests="%d">',
$totalFailuresCount,
$totalTestsCount,
);
Expand All @@ -38,6 +38,8 @@ public function formatErrors(
sprintf('%s:%s', $fileName, (string) $fileSpecificError->getLine()),
'ERROR',
$this->escape($fileSpecificError->getMessage()),
$fileName,
$fileSpecificError->getLine(),
);
}

Expand Down Expand Up @@ -65,9 +67,16 @@ public function formatErrors(
*
*
*/
private function createTestCase(string $reference, string $type, ?string $message = null): string
private function createTestCase(string $reference, string $type, ?string $message = null, ?string $fileName = null, ?int $lineNumber = null): string
{
$result = sprintf('<testcase name="%s">', $this->escape($reference));
$result = sprintf('<testcase name="%s"', $this->escape($reference));
if ($fileName !== null) {
$result .= sprintf(' file="%s"', $this->escape($fileName));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about absolute vs. relative path to the file? Typically you don't want to print the whole absolute path, but trim the beginning. But I'm not sure what PHPUnit does and what tools that read the output expect.

Anyway, this is usually done with the help of RelativePathHelper. Check the other formatters.

}
if ($lineNumber !== null) {
$result .= sprintf(' line="%s"', (string) $lineNumber);
}
$result .= '>';

if ($message !== null) {
$result .= sprintf('<failure type="%s" message="%s" />', $this->escape($type), $this->escape($message));
Expand Down
35 changes: 15 additions & 20 deletions tests/PHPStan/Command/ErrorFormatter/JunitErrorFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function dataFormatterOutputProvider(): Generator
0,
0,
'<?xml version="1.0" encoding="UTF-8"?>
<testsuite failures="0" name="phpstan" tests="1" 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">
<testsuite failures="0" name="phpstan" tests="1">
<testcase name="phpstan"/>
</testsuite>
',
Expand All @@ -40,8 +40,8 @@ public function dataFormatterOutputProvider(): Generator
1,
0,
'<?xml version="1.0" encoding="UTF-8"?>
<testsuite failures="1" name="phpstan" tests="1" 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:4">
<testsuite failures="1" name="phpstan" tests="1">
<testcase name="folder with unicode &#x1F603;/file name with &quot;spaces&quot; and unicode &#x1F603;.php:4" file="folder with unicode &#x1F603;/file name with &quot;spaces&quot; and unicode &#x1F603;.php" line="4">
<failure type="ERROR" message="Foo" />
</testcase>
</testsuite>
Expand All @@ -53,7 +53,7 @@ public function dataFormatterOutputProvider(): Generator
0,
1,
'<?xml version="1.0" encoding="UTF-8"?>
<testsuite failures="1" name="phpstan" tests="1" 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">
<testsuite failures="1" name="phpstan" tests="1">
<testcase name="General error">
<failure type="ERROR" message="first generic error" />
</testcase>
Expand All @@ -66,17 +66,17 @@ public function dataFormatterOutputProvider(): Generator
4,
0,
'<?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">
<testsuite failures="4" name="phpstan" tests="4">
<testcase name="folder with unicode &#x1F603;/file name with &quot;spaces&quot; and unicode &#x1F603;.php:2" file="folder with unicode &#x1F603;/file name with &quot;spaces&quot; and unicode &#x1F603;.php" line="2">
<failure type="ERROR" message="Bar Bar2" />
</testcase>
<testcase name="folder with unicode &#x1F603;/file name with &quot;spaces&quot; and unicode &#x1F603;.php:4">
<testcase name="folder with unicode &#x1F603;/file name with &quot;spaces&quot; and unicode &#x1F603;.php:4" file="folder with unicode &#x1F603;/file name with &quot;spaces&quot; and unicode &#x1F603;.php" line="4">
<failure type="ERROR" message="Foo" />
</testcase>
<testcase name="foo.php:1">
<testcase name="foo.php:1" file="foo.php" line="1">
<failure type="ERROR" message="Foo"/>
</testcase>
<testcase name="foo.php:5">
<testcase name="foo.php:5" file="foo.php" line="5">
<failure type="ERROR" message="Bar Bar2"/>
</testcase>
</testsuite>
Expand All @@ -88,7 +88,7 @@ public function dataFormatterOutputProvider(): Generator
0,
2,
'<?xml version="1.0" encoding="UTF-8"?>
<testsuite failures="2" name="phpstan" tests="2" 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">
<testsuite failures="2" name="phpstan" tests="2">
<testcase name="General error">
<failure type="ERROR" message="first generic error" />
</testcase>
Expand All @@ -104,17 +104,17 @@ public function dataFormatterOutputProvider(): Generator
4,
2,
'<?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">
<testsuite failures="6" name="phpstan" tests="6">
<testcase name="folder with unicode &#x1F603;/file name with &quot;spaces&quot; and unicode &#x1F603;.php:2" file="folder with unicode &#x1F603;/file name with &quot;spaces&quot; and unicode &#x1F603;.php" line="2">
<failure type="ERROR" message="Bar Bar2" />
</testcase>
<testcase name="folder with unicode &#x1F603;/file name with &quot;spaces&quot; and unicode &#x1F603;.php:4">
<testcase name="folder with unicode &#x1F603;/file name with &quot;spaces&quot; and unicode &#x1F603;.php:4" file="folder with unicode &#x1F603;/file name with &quot;spaces&quot; and unicode &#x1F603;.php" line="4">
<failure type="ERROR" message="Foo" />
</testcase>
<testcase name="foo.php:1">
<testcase name="foo.php:1" file="foo.php" line="1">
<failure type="ERROR" message="Foo"/>
</testcase>
<testcase name="foo.php:5">
<testcase name="foo.php:5" file="foo.php" line="5">
<failure type="ERROR" message="Bar Bar2"/>
</testcase>
<testcase name="General error">
Expand Down Expand Up @@ -152,11 +152,6 @@ public function testFormatErrors(
$xml = new DOMDocument();
$xml->loadXML($this->getOutputContent());

$this->assertTrue(
$xml->schemaValidate(__DIR__ . '/junit-schema.xsd'),
'Schema do not validate',
);

$this->assertXmlStringEqualsXmlString(
$expected,
$this->getOutputContent(),
Expand Down
118 changes: 0 additions & 118 deletions tests/PHPStan/Command/ErrorFormatter/junit-schema.xsd

This file was deleted.

Loading