From fcf6a860f82d1da70832fcc0b137845dd35c6ad9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Goetz?= Date: Thu, 23 Apr 2020 22:19:12 +0200 Subject: [PATCH] Fix sonarCloud reports output --- .github/workflows/php.yml | 7 +++--- composer.json | 1 + scripts/fix_report.php | 52 +++++++++++++++++++++++++++++++++++++++ scripts/fix_reports.sh | 7 ++++++ 4 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 scripts/fix_report.php create mode 100755 scripts/fix_reports.sh diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 99dfe7c..59ee060 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -43,7 +43,7 @@ jobs: runs-on: ubuntu-latest if: github.event_name != 'pull_request' steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v1 - name: Setup PHP uses: shivammathur/setup-php@master with: @@ -60,9 +60,8 @@ jobs: - name: Run test suite run: composer run-script test -- --coverage-clover=coverage.clover --log-junit=test-report.xml - # https://community.sonarsource.com/t/code-coverage-doesnt-work-with-github-action/16747/5 - - name: fix code coverage paths - run: sed -i 's/\/home\/runner\/work\/daux.io\/daux.io\//\/github\/workspace\//g' coverage.clover + - name: Fix reports + run: scripts/fix_reports.sh - name: SonarCloud Scan uses: SonarSource/sonarcloud-github-action@v1.1 diff --git a/composer.json b/composer.json index eb7e6ac..ddb8b8d 100644 --- a/composer.json +++ b/composer.json @@ -47,6 +47,7 @@ }, "scripts": { "test": "build/phpunit", + "test:coverage-html": "build/phpunit --coverage-html=build/coverage", "lint": "build/php-cs-fixer fix --config=.php_cs --dry-run -v", "lint:fix": "build/php-cs-fixer fix --config=.php_cs" } diff --git a/scripts/fix_report.php b/scripts/fix_report.php new file mode 100644 index 0000000..4f568d5 --- /dev/null +++ b/scripts/fix_report.php @@ -0,0 +1,52 @@ +loadXML($report); + +function hasDataSetTestCase(DomNode $node) { + foreach ($node->childNodes as $child) { + if ($child->nodeName === "testcase" && strpos($child->attributes->getNamedItem("name")->textContent, "with data set #" ) !== false) { + return $child; + } + } + + return false; +} + +function drillDownTestSuite(DomDocument $document, DomNode $node) { + if ($dataset = hasDataSetTestCase($node)) { + $childAttributes = $dataset->attributes; + $nodeAttributes= $node->attributes; + + $case = $document->createElement('testcase'); + $case->setAttribute('name', $childAttributes->getNamedItem('name')->textContent); + $case->setAttribute('class', $childAttributes->getNamedItem('class')->textContent); + $case->setAttribute('classname', $childAttributes->getNamedItem('classname')->textContent); + $case->setAttribute('file', $childAttributes->getNamedItem('file')->textContent); + $case->setAttribute('line', $childAttributes->getNamedItem('line')->textContent); + $case->setAttribute('assertions', $nodeAttributes->getNamedItem('assertions')->textContent); + $case->setAttribute('time', $nodeAttributes->getNamedItem('time')->textContent); + + $node->parentNode->replaceChild($case, $node); + return true; + } + + /** @var DomNode $child */ + for ($i=0; $i< $node->childNodes->length; $i++) { + $child = $node->childNodes->item($i); + if ($child->localName === "testsuite") { + if (drillDownTestSuite($document, $child)) { + $i--; + } + } + } + + return false; +} + +drillDownTestSuite($doc, $doc->firstChild); + +file_put_contents(dirname(__DIR__) . "/test-report.xml", $doc->saveXML()); +echo "Done\n"; diff --git a/scripts/fix_reports.sh b/scripts/fix_reports.sh new file mode 100755 index 0000000..90ab60c --- /dev/null +++ b/scripts/fix_reports.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +# https://community.sonarsource.com/t/code-coverage-doesnt-work-with-github-action/16747/5 +echo "fix code coverage paths" +sed -i 's/\/home\/runner\/work\/daux.io\/daux.io\//\/github\/workspace\//g' coverage.clover + +php scripts/fix_report.php