Skip to content

Commit

Permalink
[BUGFIX] Fix code coverage calucation (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
mteu authored Sep 9, 2024
1 parent 4cbd233 commit dcd595f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 130 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
with:
php-version: ${{ matrix.php-version }}
tools: composer:v${{ matrix.composer-version }}
coverage: none
coverage: pcov

# Install dependencies
- name: Install Composer dependencies
Expand All @@ -38,7 +38,7 @@ jobs:

# Run tests
- name: Run tests
run: composer test
run: composer test:coverage

coverage:
name: Test coverage
Expand All @@ -52,7 +52,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.3
php-version: ${{ matrix.php-version }}
tools: composer:v2
coverage: pcov

Expand All @@ -69,12 +69,12 @@ jobs:
# Upload artifact
- name: Fix coverage path
working-directory: .build/coverage
run: sed -i 's#/home/runner/work/typo3-stream-writer/typo3-stream-writer#${{ github.workspace }}#g' clover.xml
run: sed -i 's#/home/runner/work/typo3-stream-writer/typo3-stream-writer#${{ github.workspace }}#g' merged.clover.xml
- name: Upload coverage artifact
uses: actions/upload-artifact@v4
with:
name: coverage
path: .build/coverage/clover.xml
path: .build/coverage/merged.clover.xml
retention-days: 7

coverage-report:
Expand All @@ -101,10 +101,10 @@ jobs:
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
with:
coverageLocations: |
${{ steps.download.outputs.download-path }}/clover.xml:clover
${{ steps.download.outputs.download-path }}/merged.clover.xml:clover
# Coveralls
- name: Coveralls report
uses: coverallsapp/github-action@v2
with:
file: ${{ steps.download.outputs.download-path }}/clover.xml
file: ${{ steps.download.outputs.download-path }}/merged.clover.xml
172 changes: 52 additions & 120 deletions Tests/Unit/Log/Writer/StreamWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use mteu\StreamWriter\Log\Config\StandardStream;
use mteu\StreamWriter\Log\Writer\StreamWriter;
use PHPUnit\Framework;
use Psr\Log\LogLevel;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
use TYPO3\CMS\Core\Log\Exception\InvalidLogWriterConfigurationException;
Expand All @@ -42,23 +41,6 @@
#[Framework\Attributes\CoversClass(Src\Writer\StreamWriter::class)]
final class StreamWriterTest extends Framework\TestCase
{
/**
* @throws \Exception
*/
#[Framework\Attributes\Test]
#[Framework\Attributes\DataProvider('generateLogRecordsForStandardStream')]
public function writeLogSucceedsInWritingToStream(
StandardStream $stream,
LogRecord $record,
string $expected,
): void {

self::assertSame(
$expected,
$this->writeToStreamInSeparateProcess($stream, $record),
);
}

/**
* @throws \Exception
*/
Expand All @@ -81,6 +63,7 @@ public function writeLogRespectsMaximumLevelBoundary(
$stream,
$record,
$potentialMaxLevel,
__METHOD__,
),
),
// level is within bounds or null
Expand All @@ -90,6 +73,7 @@ public function writeLogRespectsMaximumLevelBoundary(
$stream,
$record,
$potentialMaxLevel,
__METHOD__,
),
),
};
Expand All @@ -99,14 +83,14 @@ public function writeLogRespectsMaximumLevelBoundary(
private function writeToStreamInSeparateProcess(
StandardStream $stream,
LogRecord $record,
Src\LogLevel $maxLevel = Src\LogLevel::EMERGENCY,
Src\LogLevel $maxLevel,
string $trigger,
): string {
$tempFile = tempnam(sys_get_temp_dir(), 'stream_writer_test_script_');
$tempOutputFile = tempnam(sys_get_temp_dir(), 'stream_writer_test_script_');

file_put_contents($tempFile, $this->generatePhpScriptForLogWriting($stream, $record, $maxLevel));
file_put_contents($tempOutputFile, $this->generatePhpScriptForLogWriting($stream, $record, $maxLevel, $trigger));

// @todo: ensure this path is included in the coverage report
$process = new Process([PHP_BINARY, $tempFile]);
$process = new Process([PHP_BINARY, $tempOutputFile]);
$process->run();

if (!$process->isSuccessful()) {
Expand All @@ -115,17 +99,20 @@ private function writeToStreamInSeparateProcess(

$output = $stream === StandardStream::Out ? trim($process->getOutput()) : trim($process->getErrorOutput());

unlink($tempFile);
unlink($tempOutputFile);

return $output;
}

private function generatePhpScriptForLogWriting(
StandardStream $stream,
LogRecord $record,
Src\LogLevel $maxLevel = Src\LogLevel::DEBUG,
Src\LogLevel $maxLevel,
string $trigger,
): string {
$autoload = dirname(__DIR__, 4) . '/.build/vendor/autoload.php';
$classFileName = dirname(__DIR__, 4) . '/Classes/Log/Writer/StreamWriter.php';
$coverageFile = dirname(__DIR__, 4) . '/.build/coverage/sub-process_' . uniqid() . '.cov';

return <<<PHP
<?php
Expand All @@ -134,8 +121,23 @@ private function generatePhpScriptForLogWriting(
use mteu\StreamWriter\Log\Config\StandardStream;
use mteu\StreamWriter\Log\Writer\StreamWriter;
use SebastianBergmann\CodeCoverage\Filter;
use SebastianBergmann\CodeCoverage\Driver\Selector;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Report\PHP as PhpReport;
use TYPO3\CMS\Core\Log\LogRecord;
\$filter = new Filter;
\$filter->includeFiles(['$classFileName']);
\$coverage = new CodeCoverage(
(new Selector)->forLineCoverage(\$filter),
\$filter
);
\$coverage->start('{$trigger}');
\$logWriter = new StreamWriter(
[
'outputStream' => StandardStream::from('{$stream->value}'),
Expand All @@ -151,6 +153,9 @@ private function generatePhpScriptForLogWriting(
'{$record->getRequestId()}',
),
);
\$coverage->stop();
(new PhpReport)->process(\$coverage, '$coverageFile');
PHP;
}

Expand Down Expand Up @@ -218,100 +223,27 @@ public function writeLogCreationThrowsExceptionForEmptyOutputStreamValue(): void
*/
public static function generateLogRecordsForStandardStream(): \Generator
{
yield 'emergency' => [
StandardStream::Error,
new LogRecord(
'EmergencyComponent',
LogLevel::EMERGENCY,
'EmergencyMessage',
[],
'requestId_emergency',
),
'[EMERGENCY] EmergencyComponent: EmergencyMessage',
];

yield 'alert' => [
StandardStream::Error,
new LogRecord(
'AlertComponent',
LogLevel::ALERT,
'AlertMessage',
[],
'requestId_alert',
),
'[ALERT] AlertComponent: AlertMessage',
];

yield 'critical' => [
StandardStream::Error,
new LogRecord(
'CriticalComponent',
LogLevel::CRITICAL,
'CriticalMessage',
[],
'requestId_critical',
),
'[CRITICAL] CriticalComponent: CriticalMessage',
];

yield 'error' => [
StandardStream::Error,
new LogRecord(
'ErrorComponent',
LogLevel::ERROR,
'ErrorMessage',
[],
'requestId_error',
),
'[ERROR] ErrorComponent: ErrorMessage',
];

yield 'warning' => [
StandardStream::Out,
new LogRecord(
'WarningComponent',
LogLevel::WARNING,
'WarningMessage',
[],
'requestId_warning',
),
'[WARNING] WarningComponent: WarningMessage',
];

yield 'notice' => [
StandardStream::Out,
new LogRecord(
'NoticeComponent',
LogLevel::NOTICE,
'NoticeMessage',
[],
'requestId_notice',
),
'[NOTICE] NoticeComponent: NoticeMessage',
];

yield 'info' => [
StandardStream::Out,
new LogRecord(
'InfoComponent',
LogLevel::INFO,
'InfoMessage',
[],
'requestId_info',
),
'[INFO] InfoComponent: InfoMessage',
];

yield 'debug' => [
StandardStream::Out,
new LogRecord(
'DebugComponent',
LogLevel::DEBUG,
'DebugMessage',
[],
'requestId_debug',
),
'[DEBUG] DebugComponent: DebugMessage',
];
foreach (StandardStream::cases() as $stream) {
$streamKey = strtolower($stream->name);

foreach (Src\LogLevel::cases() as $logLevel) {
yield 'Write ' . $logLevel->value . ' to ' . $streamKey => [
$stream,
new LogRecord(
$logLevel->value . 'Component',
$logLevel->value,
$logLevel->value . 'Message',
[],
'requestId_' . $streamKey,
),
sprintf(
'[%s] %s: %s',
strtoupper($logLevel->value),
$logLevel->value . 'Component',
$logLevel->value . 'Message',
),
];
}
}
}
}
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"phpstan/extension-installer": "^1.4",
"phpstan/phpstan": "^1.11",
"phpstan/phpstan-phpunit": "^1.4",
"phpunit/php-code-coverage": "^10.1",
"phpunit/phpcov": "^9.0",
"phpunit/phpunit": "^10.5",
"saschaegerer/phpstan-typo3": "^1.10",
Expand Down Expand Up @@ -91,9 +92,11 @@
"@test:unit"
],
"test:coverage": [
"@test:coverage:unit"
"@test:coverage:unit",
"@test:coverage:merge"
],
"test:coverage:merge": "phpcov merge --html .build/coverage/merged_html --clover .build/coverage/merged.clover.xml --text php://stdout .build/coverage/ ",
"test:coverage:unit": "phpunit -c phpunit.unit.xml",
"test:unit": "@test:coverage:unit --no-coverage"
"test:unit": "@test:coverage:unit"
}
}
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions phpunit.unit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
</php>
<coverage>
<report>
<php outputFile=".build/coverage/main-process.cov"/>
<clover outputFile=".build/coverage/clover.xml"/>
<html outputDirectory=".build/coverage/html"/>
<text outputFile="php://stdout" showOnlySummary="true"/>
Expand Down

0 comments on commit dcd595f

Please sign in to comment.