From 0eb9d2f16e0547004f0a575cf7dff0005bacd897 Mon Sep 17 00:00:00 2001 From: Edward Hartwell Goose Date: Sat, 1 Jan 2022 21:03:06 +0000 Subject: [PATCH 1/2] Add support to prepend the filename --- README.md | 1 + cs2pr | 9 +++++++++ tests/errors/prepend-filename.expect | 3 +++ tests/errors/prepend-filename.xml | 10 ++++++++++ tests/tests.php | 1 + 5 files changed, 24 insertions(+) create mode 100644 tests/errors/prepend-filename.expect create mode 100644 tests/errors/prepend-filename.xml diff --git a/README.md b/README.md index 734ba74..4088329 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ cs2pr /path/to/checkstyle-report.xml - `--graceful-warnings`: Don't exit with error codes if there are only warnings - `--colorize`: Colorize the output. Useful if the same lint script should be used locally on the command line and remote on GitHub Actions. With this option, errors and warnings are better distinguishable on the command line and the output is still compatible with GitHub Annotations - `--notices-as-warnings` Converts notices to warnings. This can be useful because GitHub does not annotate notices. +- `--prepend-filename` Prepend the filename to the output message - `--prepend-source` When the checkstyle generating tool provides a `source` attribute, prepend the source to the output message. diff --git a/cs2pr b/cs2pr index 033c968..ef2e26a 100755 --- a/cs2pr +++ b/cs2pr @@ -22,6 +22,7 @@ $version = '1.7.2-dev'; $colorize = false; $gracefulWarnings = false; $noticeAsWarning = false; +$prependFilename = false; $prependSource = false; // parameters @@ -36,6 +37,9 @@ foreach ($argv as $arg) { case 'colorize': $colorize = true; break; + case 'prepend-filename': + $prependFilename = true; + break; case 'prepend-source': $prependSource = true; break; @@ -64,6 +68,7 @@ if (count($params) === 1) { echo " --graceful-warnings Don't exit with error codes if there are only warnings.\n"; echo " --colorize Colorize the output (still compatible with Github Annotations)\n"; echo " --notices-as-warnings Convert notices to warnings (Github does not annotate notices otherwise).\n"; + echo " --prepend-filename Prepend error 'filename' attribute to the message.\n"; echo " --prepend-source Prepend error 'source' attribute to the message.\n"; exit(9); } @@ -102,6 +107,10 @@ foreach ($root as $file) { $message = $source.': '.$message; } + if ($prependFilename && $filename) { + $message = $filename.': '.$message; + } + $annotateType = annotateType($type, $noticeAsWarning); annotateCheck($annotateType, relativePath($filename), $line, $message, $colorize); diff --git a/tests/errors/prepend-filename.expect b/tests/errors/prepend-filename.expect new file mode 100644 index 0000000..e46841a --- /dev/null +++ b/tests/errors/prepend-filename.expect @@ -0,0 +1,3 @@ +::error file=redaxo\src\addons\2factor_auth\boot.php,line=6::redaxo\src\addons\2factor_auth\boot.php: Call to static method getInstance() on an unknown class rex_one_time_password. +::error file=redaxo\src\addons\2factor_auth\boot.php,line=9::redaxo\src\addons\2factor_auth\boot.php: Call to static method getInstance() on an unknown class rex_minibar. +::error file=redaxo\src\addons\2factor_auth\lib\one_time_password.php,line=0::redaxo\src\addons\2factor_auth\lib\one_time_password.php: Class rex_one_time_password was not found while trying to analyse it - autoloading is probably not configured properly. \ No newline at end of file diff --git a/tests/errors/prepend-filename.xml b/tests/errors/prepend-filename.xml new file mode 100644 index 0000000..b03d995 --- /dev/null +++ b/tests/errors/prepend-filename.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/tests/tests.php b/tests/tests.php index 2158b6b..e0f9151 100644 --- a/tests/tests.php +++ b/tests/tests.php @@ -52,6 +52,7 @@ function testXml($xmlPath, $expectedExit, $expectedOutput = null, $options = '') testXml(__DIR__.'/errors/notices.xml', 1, file_get_contents(__DIR__.'/errors/notices.expect')); testXml(__DIR__.'/errors/notices.xml', 1, file_get_contents(__DIR__.'/errors/notices-as-warnings.expect'), '--notices-as-warnings'); +testXml(__DIR__.'/errors/prepend-filename.xml', 1, file_get_contents(__DIR__.'/errors/prepend-filename.expect'), '--prepend-filename'); testXml(__DIR__.'/errors/mixed-source-attributes.xml', 1, file_get_contents(__DIR__.'/errors/mixed-source-attributes.expect'), '--prepend-source'); testXml(__DIR__.'/errors/mixed.xml', 1, file_get_contents(__DIR__.'/errors/mixed-colors.expect'), '--colorize'); From c4da9189044f8a20d50d1154b7b97e5d3eff1746 Mon Sep 17 00:00:00 2001 From: Edward Hartwell Goose Date: Sun, 2 Jan 2022 16:31:20 +0000 Subject: [PATCH 2/2] Return only the file name instead of the full path to make the messages less verbose --- cs2pr | 10 +++++++++- tests/errors/prepend-filename.expect | 7 ++++--- tests/errors/prepend-filename.xml | 5 ++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/cs2pr b/cs2pr index ef2e26a..b4b5f3d 100755 --- a/cs2pr +++ b/cs2pr @@ -108,7 +108,7 @@ foreach ($root as $file) { } if ($prependFilename && $filename) { - $message = $filename.': '.$message; + $message = filenameOnly($filename).': '.$message; } $annotateType = annotateType($type, $noticeAsWarning); @@ -189,3 +189,11 @@ function escapeProperty($property) return $property; } + +/** + * Get the filename only from a filepath. Built to work across windows & linux. + */ +function filenameOnly($filepath) +{ + return basename(str_replace("\\", "/", $filepath)); +} diff --git a/tests/errors/prepend-filename.expect b/tests/errors/prepend-filename.expect index e46841a..df6fa84 100644 --- a/tests/errors/prepend-filename.expect +++ b/tests/errors/prepend-filename.expect @@ -1,3 +1,4 @@ -::error file=redaxo\src\addons\2factor_auth\boot.php,line=6::redaxo\src\addons\2factor_auth\boot.php: Call to static method getInstance() on an unknown class rex_one_time_password. -::error file=redaxo\src\addons\2factor_auth\boot.php,line=9::redaxo\src\addons\2factor_auth\boot.php: Call to static method getInstance() on an unknown class rex_minibar. -::error file=redaxo\src\addons\2factor_auth\lib\one_time_password.php,line=0::redaxo\src\addons\2factor_auth\lib\one_time_password.php: Class rex_one_time_password was not found while trying to analyse it - autoloading is probably not configured properly. \ No newline at end of file +::error file=redaxo\src\addons\2factor_auth\boot.php,line=6::boot.php: Call to static method getInstance() on an unknown class rex_one_time_password. +::error file=redaxo\src\addons\2factor_auth\boot.php,line=9::boot.php: Call to static method getInstance() on an unknown class rex_minibar. +::error file=redaxo\src\addons\2factor_auth\lib\one_time_password.php,line=0::one_time_password.php: Class rex_one_time_password was not found while trying to analyse it - autoloading is probably not configured properly. +::error file=redaxo/src/addons/2factor_auth/lib/linux_filepath.php,line=0::linux_filepath.php: Class rex_one_time_password was not found while trying to analyse it - autoloading is probably not configured properly. \ No newline at end of file diff --git a/tests/errors/prepend-filename.xml b/tests/errors/prepend-filename.xml index b03d995..ab22fbe 100644 --- a/tests/errors/prepend-filename.xml +++ b/tests/errors/prepend-filename.xml @@ -7,4 +7,7 @@ - \ No newline at end of file + + + +