Skip to content

Commit 9649452

Browse files
staabmlocalheinz
andauthored
Improve error handling, added testcoverage (#4)
* Improve error handling * added tests * try to fix github-action * fixed expected exit-codes * fix typo * Fix: Merge push.yml into continuous-integration.yml * Run tests seperately * Try-and-error * Update continuous-integration.yml * Update cs2pr * Update continuous-integration.yml * Update continuous-integration.yml * Update continuous-integration.yml * Update continuous-integration.yml * Update continuous-integration.yml * Update continuous-integration.yml * Update continuous-integration.yml * fixed testsuite * use fixed versions * fix CS * simplify build Co-authored-by: Andreas Möller <am@localheinz.com>
1 parent 4fcfeda commit 9649452

File tree

10 files changed

+123
-21
lines changed

10 files changed

+123
-21
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,35 @@ jobs:
1616

1717
steps:
1818
- name: "Checkout"
19-
uses: "actions/checkout@v2.0.0"
19+
uses: "actions/checkout@v2"
2020

2121
- name: "Install PHP with extensions"
22-
uses: "shivammathur/setup-php@1.8.1"
22+
uses: "shivammathur/setup-php@v1"
2323
with:
2424
coverage: "none"
2525
extensions: "simplexml"
2626
php-version: "7.0"
2727

28-
- name: "Cache dependencies installed with composer"
29-
uses: "actions/cache@v1.0.3"
30-
with:
31-
path: "~/.composer/cache"
32-
key: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('**/composer.json') }}"
33-
restore-keys: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-"
34-
3528
- name: "Install dependencies with composer"
3629
run: "composer install --no-interaction --no-progress --no-suggest"
3730

3831
- name: "Run friendsofphp/php-cs-fixer"
3932
run: "vendor/bin/php-cs-fixer fix --config=.php_cs --diff --diff-format=udiff --dry-run --verbose"
33+
34+
tests:
35+
name: "Tests"
36+
37+
runs-on: "ubuntu-latest"
38+
39+
steps:
40+
- name: "Checkout"
41+
uses: "actions/checkout@v2"
42+
43+
- name: "Install PHP with extensions"
44+
uses: "shivammathur/setup-php@v1"
45+
with:
46+
coverage: "none"
47+
extensions: "simplexml"
48+
php-version: "7.0"
49+
50+
- run: php tests/tests.php

cs2pr

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ error_reporting(E_ALL);
1616
ini_set('display_errors', 'stderr');
1717
gc_disable();
1818

19-
$version = '1.0-dev';
19+
$version = '1.0.2';
2020

2121
if ($argc === 1) {
2222
$xml = stream_get_contents(STDIN);
@@ -29,21 +29,32 @@ if ($argc === 1) {
2929
exit(9);
3030
}
3131

32+
$root = @simplexml_load_string($xml);
33+
34+
if (!$root) {
35+
$errors = libxml_get_errors();
36+
if ($errors) {
37+
fwrite(STDERR, $errors[0]->message.' on line '.$errors[0]->line.', column '.$errors[0]->column);
38+
} elseif (stripos($xml, '<?xml') !== 0) {
39+
fwrite(STDERR, 'expecting xml stream starting with a xml opening tag.');
40+
} else {
41+
fwrite(STDERR, 'unknown error. expecting checkstyle formatted xml input.');
42+
}
43+
exit(2);
44+
}
45+
3246
$exit = 0;
33-
$root = simplexml_load_string($xml);
3447

35-
if ($root) {
36-
foreach ($root as $file) {
37-
$filename = (string)$file['name'];
48+
foreach ($root as $file) {
49+
$filename = (string)$file['name'];
3850

39-
foreach ($file as $error) {
40-
$type = (string) $error['severity'];
41-
$line = (string) $error['line'];
42-
$message = (string) $error['message'];
51+
foreach ($file as $error) {
52+
$type = (string) $error['severity'];
53+
$line = (string) $error['line'];
54+
$message = (string) $error['message'];
4355

44-
annotateCheck(annotateType($type), relativePath($filename), $line, $message);
45-
$exit = 1;
46-
}
56+
annotateCheck(annotateType($type), relativePath($filename), $line, $message);
57+
$exit = 1;
4758
}
4859
}
4960

tests/errors/minimal.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+
::error 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/minimal.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="error" message="Call to static method getInstance() on an unknown class rex_one_time_password." />
5+
<error line="9" column="1" severity="error" 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="error" 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/fail/empty.xml

Whitespace-only changes.

tests/fail/invalid.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello world

tests/fail/multiple-suites.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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="error" message="Call to static method getInstance() on an unknown class rex_one_time_password." />
5+
<error line="9" column="1" severity="error" message="Call to static method getInstance() on an unknown class rex_minibar." />
6+
</file>
7+
</checkstyle>
8+
<checkstyle>
9+
<file name="redaxo\src\addons\thincache\src\thincache\CacheKeyStatic.php">
10+
<error line="0" column="1" severity="error" message="Class CacheKeyStatic was not found while trying to analyse it - autoloading is probably not configured properly." />
11+
</file>
12+
</checkstyle>
13+
<checkstyle>
14+
<file name="redaxo\src\addons\thincache\src\thincache\CacheKeyStatic.php">
15+
<error line="0" column="1" severity="error" message="Class CacheKeyStatic was not found while trying to analyse it - autoloading is probably not configured properly." />
16+
</file>
17+
</checkstyle>

tests/noerrors/only-header.expect

Whitespace-only changes.

tests/noerrors/only-header.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<checkstyle>
3+
</checkstyle>

tests/tests.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* Turns checkstyle based XML-Reports into Github Pull Request Annotations via the Checks API. This script is meant for use within your GithubAction.
5+
*
6+
* (c) Markus Staab <markus.staab@redaxo.org>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* https://github.com/staabm/annotate-pull-request-from-checkstyle
12+
*/
13+
14+
function testXml($xmlPath, $expectedExit, $expectedOutput = null)
15+
{
16+
exec('cat '.$xmlPath .' | php '. __DIR__ .'/../cs2pr 2>&1', $output, $exit);
17+
$output = implode("\n", $output);
18+
19+
if ($exit != $expectedExit) {
20+
var_dump($output);
21+
22+
throw new Exception('Test with ' . $xmlPath . ' failed, expected exit-code ' . $expectedExit . ' got ' . $exit);
23+
} elseif ($expectedOutput && $expectedOutput != $output) {
24+
echo "EXPECTED:\n";
25+
var_dump($expectedOutput);
26+
echo "\n";
27+
28+
echo "GOT:\n";
29+
var_dump($output);
30+
echo "\n";
31+
32+
throw new Exception('Test with ' . $xmlPath . ' failed, output mismatch');
33+
} else {
34+
echo "success: $xmlPath\n\n";
35+
}
36+
}
37+
38+
39+
testXml(__DIR__.'/fail/empty.xml', 2);
40+
testXml(__DIR__.'/fail/invalid.xml', 2);
41+
42+
testXml(__DIR__.'/fail/multiple-suites.xml', 2);
43+
44+
testXml(__DIR__.'/errors/minimal.xml', 1, file_get_contents(__DIR__.'/errors/minimal.expect'));
45+
46+
testXml(__DIR__.'/noerrors/only-header.xml', 0, file_get_contents(__DIR__.'/noerrors/only-header.expect'));

0 commit comments

Comments
 (0)