Skip to content

Commit

Permalink
add junit compatible report
Browse files Browse the repository at this point in the history
  • Loading branch information
FO-nTTaX committed Mar 23, 2022
1 parent a053447 commit 8d4d4ca
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 3 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ composer require liquipedia/sqllint
./vendor/bin/sqllint
```

```sh
./vendor/bin/sqllint --report=junit > report-junit.xml
```

### Via composer script

Add this to your composer.json
Expand All @@ -34,6 +38,9 @@ Add this to your composer.json
"scripts": {
"sqllint": [
"sqllint"
],
"sqllint-junit": [
"sqllint --report=junit > report-junit.xml"
]
}
}
Expand All @@ -44,5 +51,11 @@ and run
```sh
composer sqllint
```

### Parameter
| Name | Values |
|----------|--------------------------|
| --report | "cli" (default), "junit" |

[1]:https://github.com/phpmyadmin/sql-parser
[2]:https://getcomposer.org/
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "liquipedia/sqllint",
"description": "A thin wrapper around the SqlParser from the phpMyAdmin project which can be used to lint any amount of sql files from the command line.",
"version": "1.0.3",
"version": "1.1.0",
"type": "library",
"license": "GPL-2.0-or-later",
"keywords": [ "sql", "lint" ],
"keywords": [ "sql", "lint", "junit" ],
"authors": [
{
"name": "Alex Winkler",
Expand Down
5 changes: 5 additions & 0 deletions src/Report/IReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

interface IReport {

public const REPORT_TYPES = [
'cli' => CLI::class,
'junit' => JUnit::class,
];

/**
* Set total amount of files to be linted so we can show a progress bar
* @param int $amount
Expand Down
71 changes: 71 additions & 0 deletions src/Report/JUnit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare( strict_types=1 );

namespace Liquipedia\SqlLint\Report;

use SimpleXMLElement;

class JUnit extends Report {

/**
* @var SimpleXMLElement
*/
private $outputXml;

public function __construct() {
$this->outputXml = new SimpleXMLElement(
'<testsuite'
. ' name="sqllint"'
. ' 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"'
. ' />'
);
}

/**
* @param string $fileName
*/
public function addSuccess( string $fileName ): void {
$testCase = $this->outputXml->addChild( 'testcase' );
$finalFileName = realpath( $fileName );
if ( $finalFileName === false ) {
// File somehow does not exist anymore
$finalFileName = $fileName;
}
$testCase->addAttribute( 'name', $finalFileName );
}

/**
* @param string $fileName
* @param array<int, array<int, mixed>> $errors
*/
public function addError( string $fileName, array $errors ): void {
$this->exitCode = 1;
$finalFileName = realpath( $fileName );
if ( $finalFileName === false ) {
// File somehow does not exist anymore
$finalFileName = $fileName;
}
foreach ( $errors as $i => $error ) {
$testCase = $this->outputXml->addChild( 'testcase' );
$testCase->addAttribute( 'name', $finalFileName . ' (' . ( $i + 1 ) . ')' );
$failure = $testCase->addChild( 'failure' );
$failure->addAttribute( 'type', 'ERROR' );
$failure->addAttribute(
'message',
htmlspecialchars( strval( $error[ 0 ] ) )
. ' (near \'' . htmlspecialchars( strval( $error[ 2 ] ) ) . '\''
. ' at position ' . htmlspecialchars( strval( $error[ 3 ] ) ) . ')'
);
}
}

public function printBody(): void {
echo $this->output;
echo $this->outputXml->asXML();
}

}
19 changes: 18 additions & 1 deletion src/SqlLint.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,30 @@

namespace Liquipedia\SqlLint;

use Liquipedia\SqlLint\Report\IReport;

class SqlLint {

/**
* @return int
*/
public static function lint(): int {
$report = new Report\CLI;
$args = getopt( '', [ 'report::' ] );
$reportType = 'cli';
if ( array_key_exists( 'report', $args ) ) {
if ( array_key_exists( strval( $args[ 'report' ] ), IReport::REPORT_TYPES ) ) {
$reportType = strval( $args[ 'report' ] );
} else {
die(
PHP_EOL . 'ERROR: Unknown report type "' . strval( $args[ 'report' ] ) . '",'
. ' should be one of "' . implode( '", "', array_keys( IReport::REPORT_TYPES ) ) . '"'
. PHP_EOL . PHP_EOL
);
}
}
$reportClass = IReport::REPORT_TYPES[ $reportType ];

$report = new $reportClass;
$runner = new Runner( $report );
return $runner->run();
}
Expand Down

0 comments on commit 8d4d4ca

Please sign in to comment.