Skip to content

Commit c290816

Browse files
korelstarstaabm
andauthored
add options gracefulWarnings and colorize (#33)
* add options gracefulWarnings and colorize * Apply suggestions from code review Co-Authored-By: Markus Staab <[email protected]> * fix phpdoc, add tests * check annotateType instead of type Co-authored-by: Markus Staab <[email protected]>
1 parent 1fd8e90 commit c290816

File tree

7 files changed

+90
-9
lines changed

7 files changed

+90
-9
lines changed

cs2pr

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,43 @@ gc_disable();
1818

1919
$version = '1.0.6-dev';
2020

21-
if ($argc === 1) {
21+
// options
22+
$colorize = false;
23+
$gracefulWarnings = false;
24+
25+
// parameters
26+
$params = [];
27+
foreach ($argv as $arg) {
28+
if (substr($arg, 0, 2) === '--') {
29+
$option = substr($arg, 2);
30+
switch ($option) {
31+
case 'graceful-warnings':
32+
$gracefulWarnings = true;
33+
break;
34+
case 'colorize':
35+
$colorize = true;
36+
break;
37+
default:
38+
echo "Unknown option ".$option."\n";
39+
exit(9);
40+
}
41+
} else {
42+
$params[] = $arg;
43+
}
44+
}
45+
46+
if (count($params) === 1) {
2247
$xml = stream_get_contents(STDIN);
23-
} elseif ($argc === 2 && file_exists($argv[1])) {
24-
$xml = file_get_contents($argv[1]);
48+
} elseif (count($params) === 2 && file_exists($params[1])) {
49+
$xml = file_get_contents($params[1]);
2550
} else {
2651
echo "cs2pr $version\n";
2752
echo "Annotate a Github Pull Request based on a Checkstyle XML-report.\n";
28-
echo "Usage: ". $argv[0] ." <filename>\n";
53+
echo "Usage: ". $params[0] ." [OPTION]... <filename>\n";
54+
echo "\n";
55+
echo "Supported options:\n";
56+
echo " --graceful-warnings Don't exit with error codes if there are only warnings.\n";
57+
echo " --colorize Colorize the output (still compatible with Github Annotations)\n";
2958
exit(9);
3059
}
3160

@@ -58,8 +87,12 @@ foreach ($root as $file) {
5887
$line = (string) $error['line'];
5988
$message = (string) $error['message'];
6089

61-
annotateCheck(annotateType($type), relativePath($filename), $line, $message);
62-
$exit = 1;
90+
$annotateType = annotateType($type);
91+
annotateCheck($annotateType, relativePath($filename), $line, $message, $colorize);
92+
93+
if (!$gracefulWarnings || $annotateType === 'error') {
94+
$exit = 1;
95+
}
6396
}
6497
}
6598

@@ -70,10 +103,17 @@ exit($exit);
70103
* @param string $filename
71104
* @param int $line
72105
* @param string $message
106+
* @param boolean $colorize
73107
*/
74-
function annotateCheck($type, $filename, $line, $message)
108+
function annotateCheck($type, $filename, $line, $message, $colorize)
75109
{
110+
if ($colorize) {
111+
echo "\033[".($type==='error' ? '91' : '93')."m\n";
112+
}
76113
echo "::{$type} file={$filename},line={$line}::{$message}\n";
114+
if ($colorize) {
115+
echo "\033[0m";
116+
}
77117
}
78118

79119
function relativePath($path)

tests/errors/mixed-colors.expect

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+

2+
::error file=redaxo\src\addons\2factor_auth\boot.php,line=6::Call to static method getInstance() on an unknown class rex_one_time_password.
3+

4+
::warning file=redaxo\src\addons\2factor_auth\boot.php,line=9::Call to static method getInstance() on an unknown class rex_minibar.
5+

6+
::error file=redaxo\src\addons\2factor_auth\lib\one_time_password.php,line=0::Class rex_one_time_password was not found while trying to analyse it - autoloading is probably not configured properly.
7+


tests/errors/mixed.expect

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
::error file=redaxo\src\addons\2factor_auth\boot.php,line=6::Call to static method getInstance() on an unknown class rex_one_time_password.
2+
::warning file=redaxo\src\addons\2factor_auth\boot.php,line=9::Call to static method getInstance() on an unknown class rex_minibar.
3+
::error file=redaxo\src\addons\2factor_auth\lib\one_time_password.php,line=0::Class rex_one_time_password was not found while trying to analyse it - autoloading is probably not configured properly.

tests/errors/mixed.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<checkstyle>
3+
<file name="redaxo\src\addons\2factor_auth\boot.php">
4+
<error line="6" column="1" severity="failure" message="Call to static method getInstance() on an unknown class rex_one_time_password." />
5+
<error line="9" column="1" severity="warning" message="Call to static method getInstance() on an unknown class rex_minibar." />
6+
</file>
7+
<file name="redaxo\src\addons\2factor_auth\lib\one_time_password.php">
8+
<error line="0" column="1" severity="failure" message="Class rex_one_time_password was not found while trying to analyse it - autoloading is probably not configured properly." />
9+
</file>
10+
</checkstyle>

tests/errors/warning-only.expect

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
::warning file=redaxo/src/addons/2factor_auth/boot.php,line=6::Call to static method getInstance() on an unknown class rex_one_time_password.
2+
::warning file=redaxo/src/addons/2factor_auth/boot.php,line=9::Call to static method getInstance() on an unknown class rex_minibar.
3+
::warning file=redaxo/src/addons/2factor_auth/lib/one_time_password.php,line=0::Class rex_one_time_password was not found while trying to analyse it - autoloading is probably not configured properly.

tests/errors/warning-only.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<checkstyle>
3+
<file name="redaxo/src/addons/2factor_auth/boot.php">
4+
<error line="6" column="1" severity="warning" message="Call to static method getInstance() on an unknown class rex_one_time_password." />
5+
<error line="9" column="1" severity="warning" message="Call to static method getInstance() on an unknown class rex_minibar." />
6+
</file>
7+
<file name="redaxo/src/addons/2factor_auth/lib/one_time_password.php">
8+
<error line="0" column="1" severity="warning" message="Class rex_one_time_password was not found while trying to analyse it - autoloading is probably not configured properly." />
9+
</file>
10+
</checkstyle>

tests/tests.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
* https://github.com/staabm/annotate-pull-request-from-checkstyle
1212
*/
1313

14-
function testXml($xmlPath, $expectedExit, $expectedOutput = null)
14+
function testXml($xmlPath, $expectedExit, $expectedOutput = null, $options = '')
1515
{
16-
exec('cat '.$xmlPath .' | php '. __DIR__ .'/../cs2pr 2>&1', $output, $exit);
16+
exec('cat '.$xmlPath .' | php '. __DIR__ .'/../cs2pr '.$options.' 2>&1', $output, $exit);
1717
$output = implode("\n", $output);
1818

1919
if ($exit != $expectedExit) {
@@ -42,6 +42,14 @@ function testXml($xmlPath, $expectedExit, $expectedOutput = null)
4242
testXml(__DIR__.'/fail/multiple-suites.xml', 2, "Error: Extra content at the end of the document on line 8, column 1\n\n" .file_get_contents(__DIR__.'/fail/multiple-suites.xml'));
4343

4444
testXml(__DIR__.'/errors/minimal.xml', 1, file_get_contents(__DIR__.'/errors/minimal.expect'));
45+
testXml(__DIR__.'/errors/mixed.xml', 1, file_get_contents(__DIR__.'/errors/mixed.expect'));
46+
testXml(__DIR__.'/errors/warning-only.xml', 1, file_get_contents(__DIR__.'/errors/warning-only.expect'));
47+
48+
testXml(__DIR__.'/errors/minimal.xml', 1, file_get_contents(__DIR__.'/errors/minimal.expect'), '--graceful-warnings');
49+
testXml(__DIR__.'/errors/mixed.xml', 1, file_get_contents(__DIR__.'/errors/mixed.expect'), '--graceful-warnings');
50+
testXml(__DIR__.'/errors/warning-only.xml', 0, file_get_contents(__DIR__.'/errors/warning-only.expect'), '--graceful-warnings');
51+
52+
testXml(__DIR__.'/errors/mixed.xml', 1, file_get_contents(__DIR__.'/errors/mixed-colors.expect'), '--colorize');
4553

4654
testXml(__DIR__.'/noerrors/only-header.xml', 0, file_get_contents(__DIR__.'/noerrors/only-header.expect'));
4755
testXml(__DIR__.'/noerrors/only-header-php-cs-fixer.xml', 0, file_get_contents(__DIR__.'/noerrors/only-header-php-cs-fixer.expect'));

0 commit comments

Comments
 (0)