Skip to content

Commit

Permalink
Merge pull request #654 from mad-briller/add-junit-formatter
Browse files Browse the repository at this point in the history
Added junit formatter.
  • Loading branch information
Ocramius authored Oct 12, 2022
2 parents 0d93ab8 + 512629f commit e164a07
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Command/AssertBackwardsCompatible.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Roave\BackwardCompatibility\CompareApi;
use Roave\BackwardCompatibility\Factory\ComposerInstallationReflectorFactory;
use Roave\BackwardCompatibility\Formatter\GithubActionsFormatter;
use Roave\BackwardCompatibility\Formatter\JunitFormatter;
use Roave\BackwardCompatibility\Formatter\MarkdownPipedToSymfonyConsoleFormatter;
use Roave\BackwardCompatibility\Formatter\SymfonyConsoleTextFormatter;
use Roave\BackwardCompatibility\Git\CheckedOutRepository;
Expand Down Expand Up @@ -69,8 +70,9 @@ protected function configure(): void
'format',
null,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'Currently supports "console", "markdown" or "github-actions"',
'Currently supports "console", "markdown", "github-actions" or "junit"',
['console'],
['console', 'markdown', 'github-actions', 'junit'],
)
->addOption(
'install-development-dependencies',
Expand All @@ -82,7 +84,7 @@ protected function configure(): void
<<<'USAGE'
Without arguments, this command will attempt to detect the
Without arguments, this command will attempt to detect the
latest stable git tag ("release", according to this tool)
of the repository in your CWD (current working directory),
and will use it as baseline for the defined API.
Expand Down Expand Up @@ -152,13 +154,15 @@ public function execute(InputInterface $input, OutputInterface $output): int
'console' => new SymfonyConsoleTextFormatter($stdErr),
'markdown' => new MarkdownPipedToSymfonyConsoleFormatter($output),
'github-actions' => new GithubActionsFormatter($output, $toPath),
'junit' => new JunitFormatter($output, $toPath),
];

foreach (
Type\vec(Type\union(
Type\literal_scalar('console'),
Type\literal_scalar('markdown'),
Type\literal_scalar('github-actions'),
Type\literal_scalar('junit'),
))->coerce((array) $input->getOption('format')) as $format
) {
$formatters[$format]->write($changes);
Expand Down
70 changes: 70 additions & 0 deletions src/Formatter/JunitFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Roave\BackwardCompatibility\Formatter;

use Psl\Str;
use Roave\BackwardCompatibility\Changes;
use Roave\BackwardCompatibility\Git\CheckedOutRepository;
use Symfony\Component\Console\Output\OutputInterface;

use function count;
use function htmlspecialchars;
use function implode;
use function sprintf;
use function trim;

use const ENT_COMPAT;
use const ENT_XML1;

/**
* String concatenatation is used rather than DOMDocument or simplexml
* as that would cause the formatter to be dependent on the DOM php
* extension, which may not be available in all environments.
*/
final class JunitFormatter implements OutputFormatter
{
public function __construct(
private readonly OutputInterface $output,
private readonly CheckedOutRepository $basePath,
) {
}

public function write(Changes $changes): void
{
$basePath = $this->basePath->__toString() . '/';

$changeCount = count($changes);

$this->output->writeLn('<?xml version="1.0" encoding="UTF-8"?>');
$this->output->writeLn(sprintf(
'<testsuite name="roave/backward-compatibility-check" tests="%d" failures="%d" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/junit-team/junit5/732a5400f80c8f446daa8b43eaa4b41b3da929be/platform-tests/src/test/resources/jenkins-junit.xsd">',
$changeCount,
$changeCount,
));

foreach ($changes as $change) {
$filename = $change->file === null ? null : Str\replace($change->file, $basePath, '');

$name = $this->escapeXmlAttribute(implode(':', [
$filename ?? '',
(string) ($change->line ?? ''),
(string) ($change->column ?? ''),
]));

$this->output->writeLn(sprintf(
' <testcase name="%s"><failure type="error" message="%s"/></testcase>',
$this->escapeXmlAttribute($name),
$this->escapeXmlAttribute(trim($change->__toString())),
));
}

$this->output->writeLn('</testsuite>');
}

private function escapeXmlAttribute(string $value): string
{
return htmlspecialchars($value, ENT_XML1 | ENT_COMPAT, 'UTF-8');
}
}
90 changes: 90 additions & 0 deletions test/unit/Formatter/JunitFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace RoaveTest\BackwardCompatibility\Formatter;

use DOMDocument;
use PHPUnit\Framework\TestCase;
use Psl\Env;
use Psl\Filesystem;
use ReflectionException;
use Roave\BackwardCompatibility\Change;
use Roave\BackwardCompatibility\Changes;
use Roave\BackwardCompatibility\Formatter\JunitFormatter;
use Roave\BackwardCompatibility\Git\CheckedOutRepository;
use Symfony\Component\Console\Output\BufferedOutput;

use function extension_loaded;

/** @covers \Roave\BackwardCompatibility\Formatter\JunitFormatter */
final class JunitFormatterTest extends TestCase
{
/** @throws ReflectionException */
public function testWrite(): void
{
$output = new BufferedOutput();
$temporaryLocation = Filesystem\create_temporary_file(Env\temp_dir(), 'JunitFormatter');

Filesystem\delete_file($temporaryLocation);
Filesystem\create_directory($temporaryLocation . '/foo/bar/.git');

(new JunitFormatter(
$output,
CheckedOutRepository::fromPath($temporaryLocation . '/foo/bar'),
))->write(Changes::fromList(
Change::removed('foo', true),
Change::added('bar', false),
Change::changed('baz', false)
->onFile('baz-file.php'),
Change::changed('tab', false)
->onFile('tab-file.php')
->onLine(5),
Change::changed('taz', false)
->onFile('taz-file.php')
->onLine(6)
->onColumn(15),
Change::changed('tar', false)
->onFile('tar-file.php')
->onLine(-1)
->onColumn(-1),
Change::changed('file-in-checked-out-dir', false)
->onFile($temporaryLocation . '/foo/bar/subpath/file-in-checked-out-dir.php')
->onLine(10)
->onColumn(20),
));

Filesystem\delete_directory($temporaryLocation, true);

$fetchedOutput = $output->fetch();

self::assertSame(
<<<'OUTPUT'
<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="roave/backward-compatibility-check" tests="7" failures="7" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/junit-team/junit5/732a5400f80c8f446daa8b43eaa4b41b3da929be/platform-tests/src/test/resources/jenkins-junit.xsd">
<testcase name="::"><failure type="error" message="[BC] REMOVED: foo"/></testcase>
<testcase name="::"><failure type="error" message="ADDED: bar"/></testcase>
<testcase name="baz-file.php::"><failure type="error" message="CHANGED: baz"/></testcase>
<testcase name="tab-file.php:5:"><failure type="error" message="CHANGED: tab"/></testcase>
<testcase name="taz-file.php:6:15"><failure type="error" message="CHANGED: taz"/></testcase>
<testcase name="tar-file.php:-1:-1"><failure type="error" message="CHANGED: tar"/></testcase>
<testcase name="subpath/file-in-checked-out-dir.php:10:20"><failure type="error" message="CHANGED: file-in-checked-out-dir"/></testcase>
</testsuite>

OUTPUT
,
$fetchedOutput,
);

if (! extension_loaded('dom')) {
return;
}

$dom = new DOMDocument();
$dom->loadXML($fetchedOutput);

self::assertTrue(
$dom->schemaValidate(__DIR__ . '/junit.xsd'),
);
}
}
118 changes: 118 additions & 0 deletions test/unit/Formatter/junit.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Source: https://svn.jenkins-ci.org/trunk/hudson/dtkit/dtkit-format/dtkit-junit-model/src/main/resources/com/thalesgroup/dtkit/junit/model/xsd/junit-4.xsd
This file available under the terms of the MIT License as follows:
*******************************************************************************
* Copyright (c) 2010 Thales Corporate Services SAS *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy *
* of this software and associated documentation files (the "Software"), to deal*
* in the Software without restriction, including without limitation the rights *
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,*
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN *
* THE SOFTWARE. *
********************************************************************************
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="failure">
<xs:complexType mixed="true">
<xs:attribute name="type" type="xs:string" use="optional"/>
<xs:attribute name="message" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>

<xs:element name="error">
<xs:complexType mixed="true">
<xs:attribute name="type" type="xs:string" use="optional"/>
<xs:attribute name="message" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>

<xs:element name="properties">
<xs:complexType>
<xs:sequence>
<xs:element ref="property" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="property">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="value" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>

<xs:element name="skipped" type="xs:string"/>
<xs:element name="system-err" type="xs:string"/>
<xs:element name="system-out" type="xs:string"/>

<xs:element name="testcase">
<xs:complexType>
<xs:sequence>
<xs:element ref="skipped" minOccurs="0" maxOccurs="1"/>
<xs:element ref="error" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="failure" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="system-out" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="system-err" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="assertions" type="xs:string" use="optional"/>
<xs:attribute name="time" type="xs:string" use="optional"/>
<xs:attribute name="classname" type="xs:string" use="optional"/>
<xs:attribute name="status" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>

<xs:element name="testsuite">
<xs:complexType>
<xs:sequence>
<xs:element ref="properties" minOccurs="0" maxOccurs="1"/>
<xs:element ref="testcase" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="system-out" minOccurs="0" maxOccurs="1"/>
<xs:element ref="system-err" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="tests" type="xs:string" use="required"/>
<xs:attribute name="failures" type="xs:string" use="optional"/>
<xs:attribute name="errors" type="xs:string" use="optional"/>
<xs:attribute name="time" type="xs:string" use="optional"/>
<xs:attribute name="disabled" type="xs:string" use="optional"/>
<xs:attribute name="skipped" type="xs:string" use="optional"/>
<xs:attribute name="timestamp" type="xs:string" use="optional"/>
<xs:attribute name="hostname" type="xs:string" use="optional"/>
<xs:attribute name="id" type="xs:string" use="optional"/>
<xs:attribute name="package" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>

<xs:element name="testsuites">
<xs:complexType>
<xs:sequence>
<xs:element ref="testsuite" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="optional"/>
<xs:attribute name="time" type="xs:string" use="optional"/>
<xs:attribute name="tests" type="xs:string" use="optional"/>
<xs:attribute name="failures" type="xs:string" use="optional"/>
<xs:attribute name="disabled" type="xs:string" use="optional"/>
<xs:attribute name="errors" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>


</xs:schema>

0 comments on commit e164a07

Please sign in to comment.