Skip to content

Commit

Permalink
Provide new logger "summaryJson". (#1808)
Browse files Browse the repository at this point in the history
* Provide new logger "summaryJson".

* Fix order of expectations of defined loggers in a unit-test.

Co-authored-by: Nicolas Giraud <nicolas.giraud@aareon.com>
  • Loading branch information
niconoe- and niconoe- committed Jan 20, 2023
1 parent c39fa15 commit cc56af8
Show file tree
Hide file tree
Showing 15 changed files with 410 additions and 28 deletions.
4 changes: 4 additions & 0 deletions resources/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@
"github": {
"type": "boolean",
"description": "GitHub Annotations for escaped Mutants in the added/modifies files"
},
"summaryJson": {
"type": "string",
"definition": "Summary JSON log file, which contains only the statistics from the complete JSON file."
}
}
},
Expand Down
11 changes: 10 additions & 1 deletion src/Configuration/Entry/Logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Logs
private ?string $perMutatorFilePath;
private bool $useGitHubAnnotationsLogger;
private ?StrykerConfig $strykerConfig;
private ?string $summaryJsonLogFilePath;

public function __construct(
?string $textLogFilePath,
Expand All @@ -58,7 +59,8 @@ public function __construct(
?string $debugLogFilePath,
?string $perMutatorFilePath,
bool $useGitHubAnnotationsLogger,
?StrykerConfig $strykerConfig
?StrykerConfig $strykerConfig,
?string $summaryJsonLogFilePath
) {
$this->textLogFilePath = $textLogFilePath;
$this->htmlLogFilePath = $htmlLogFilePath;
Expand All @@ -68,6 +70,7 @@ public function __construct(
$this->perMutatorFilePath = $perMutatorFilePath;
$this->useGitHubAnnotationsLogger = $useGitHubAnnotationsLogger;
$this->strykerConfig = $strykerConfig;
$this->summaryJsonLogFilePath = $summaryJsonLogFilePath;
}

public static function createEmpty(): self
Expand All @@ -80,6 +83,7 @@ public static function createEmpty(): self
null,
null,
false,
null,
null
);
}
Expand Down Expand Up @@ -133,4 +137,9 @@ public function getStrykerConfig(): ?StrykerConfig
{
return $this->strykerConfig;
}

public function getSummaryJsonLogFilePath(): ?string
{
return $this->summaryJsonLogFilePath;
}
}
3 changes: 2 additions & 1 deletion src/Configuration/Schema/SchemaConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ private static function createLogs(stdClass $logs): Logs
self::normalizeString($logs->debug ?? null),
self::normalizeString($logs->perMutator ?? null),
$logs->github ?? false,
self::createStrykerConfig($logs->stryker ?? null)
self::createStrykerConfig($logs->stryker ?? null),
self::normalizeString($logs->summaryJson ?? null),
);
}

Expand Down
7 changes: 7 additions & 0 deletions src/Logger/FileLoggerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ private function createLineLoggers(Logs $logConfig): iterable

yield $logConfig->getPerMutatorFilePath() => $this->createPerMutatorLogger();

yield $logConfig->getSummaryJsonLogFilePath() => $this->createSummaryJsonLogger();

if ($logConfig->getUseGitHubAnnotationsLogger()) {
yield GitHubAnnotationsLogger::DEFAULT_OUTPUT => $this->createGitHubAnnotationsLogger();
}
Expand Down Expand Up @@ -157,4 +159,9 @@ private function createPerMutatorLogger(): LineMutationTestingResultsLogger
$this->resultsCollector
);
}

private function createSummaryJsonLogger(): LineMutationTestingResultsLogger
{
return new SummaryJsonLogger($this->metricsCalculator);
}
}
75 changes: 75 additions & 0 deletions src/Logger/SummaryJsonLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, Maks Rafalko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

declare(strict_types=1);

namespace Infection\Logger;

use Infection\Metrics\MetricsCalculator;
use function json_encode;
use const JSON_THROW_ON_ERROR;

/**
* @internal
*/
final class SummaryJsonLogger implements LineMutationTestingResultsLogger
{
public function __construct(private MetricsCalculator $metricsCalculator)
{
}

/**
* @return array{0: string}
*/
public function getLogLines(): array
{
$data = [
'stats' => [
'totalMutantsCount' => $this->metricsCalculator->getTotalMutantsCount(),
'killedCount' => $this->metricsCalculator->getKilledCount(),
'notCoveredCount' => $this->metricsCalculator->getNotTestedCount(),
'escapedCount' => $this->metricsCalculator->getEscapedCount(),
'errorCount' => $this->metricsCalculator->getErrorCount(),
'syntaxErrorCount' => $this->metricsCalculator->getSyntaxErrorCount(),
'skippedCount' => $this->metricsCalculator->getSkippedCount(),
'ignoredCount' => $this->metricsCalculator->getIgnoredCount(),
'timeOutCount' => $this->metricsCalculator->getTimedOutCount(),
'msi' => $this->metricsCalculator->getMutationScoreIndicator(),
'mutationCodeCoverage' => $this->metricsCalculator->getCoverageRate(),
'coveredCodeMsi' => $this->metricsCalculator->getCoveredCodeMutationScoreIndicator(),
],
];

return [json_encode($data, JSON_THROW_ON_ERROR)];
}
}
3 changes: 2 additions & 1 deletion tests/phpunit/Configuration/ConfigurationAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ private function assertConfigurationStateIs(
$expectedLogs->getDebugLogFilePath(),
$expectedLogs->getPerMutatorFilePath(),
$expectedLogs->getUseGitHubAnnotationsLogger(),
$expectedLogs->getStrykerConfig()
$expectedLogs->getStrykerConfig(),
$expectedLogs->getSummaryJsonLogFilePath()
);
$this->assertSame($expectedLogVerbosity, $configuration->getLogVerbosity());
$this->assertSame($expectedTmpDir, $configuration->getTmpDir());
Expand Down
9 changes: 7 additions & 2 deletions tests/phpunit/Configuration/ConfigurationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,8 @@ public function valueProvider(): iterable
'debug.log',
'mutator.log',
true,
StrykerConfig::forFullReport('master')
StrykerConfig::forFullReport('master'),
'summary.json'
),
'config/tmp',
new PhpUnit(
Expand Down Expand Up @@ -862,7 +863,8 @@ public function valueProvider(): iterable
'debug.log',
'mutator.log',
true,
StrykerConfig::forFullReport('master')
StrykerConfig::forFullReport('master'),
'summary.json'
),
'none',
'/path/to/config/tmp/infection',
Expand Down Expand Up @@ -1290,6 +1292,7 @@ private static function createValueForGithubActionsDetected(
null,
$useGitHubAnnotationsLogger,
null,
null,
);

return [
Expand Down Expand Up @@ -2079,6 +2082,7 @@ private static function createValueForHtmlLogFilePath(?string $htmlFileLogPathIn
null,
true,
null,
null,
);

return [
Expand All @@ -2097,6 +2101,7 @@ private static function createValueForHtmlLogFilePath(?string $htmlFileLogPathIn
null,
false,
null,
null,
),
'',
new PhpUnit(null, null),
Expand Down
3 changes: 2 additions & 1 deletion tests/phpunit/Configuration/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ public function valueProvider(): iterable
'debug.log',
'mutator.log',
true,
StrykerConfig::forBadge('master')
StrykerConfig::forBadge('master'),
'summary.json'
),
'default',
'custom-dir',
Expand Down
4 changes: 3 additions & 1 deletion tests/phpunit/Configuration/Entry/LogsAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ private function assertLogsStateIs(
?string $expectedDebugLogFilePath,
?string $expectedPerMutatorFilePath,
bool $expectedUseGitHubAnnotationsLogger,
?StrykerConfig $expectedStrykerConfig
?StrykerConfig $expectedStrykerConfig,
?string $expectedSummaryJsonLogFilePath,
): void {
$this->assertSame($expectedTextLogFilePath, $logs->getTextLogFilePath());
$this->assertSame($expectedHtmlLogFilePath, $logs->getHtmlLogFilePath());
Expand All @@ -58,6 +59,7 @@ private function assertLogsStateIs(
$this->assertSame($expectedDebugLogFilePath, $logs->getDebugLogFilePath());
$this->assertSame($expectedPerMutatorFilePath, $logs->getPerMutatorFilePath());
$this->assertSame($expectedUseGitHubAnnotationsLogger, $logs->getUseGitHubAnnotationsLogger(), 'Use GithubAnnotationLogger is incorrect');
$this->assertSame($expectedSummaryJsonLogFilePath, $logs->getSummaryJsonLogFilePath());

$strykerConfig = $logs->getStrykerConfig();

Expand Down
12 changes: 9 additions & 3 deletions tests/phpunit/Configuration/Entry/LogsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public function test_it_can_be_instantiated(
?string $debugLogFilePath,
?string $perMutatorFilePath,
bool $useGitHubAnnotationsLogger,
?StrykerConfig $strykerConfig
?StrykerConfig $strykerConfig,
?string $summaryJsonLogFilePath
): void {
$logs = new Logs(
$textLogFilePath,
Expand All @@ -64,7 +65,8 @@ public function test_it_can_be_instantiated(
$debugLogFilePath,
$perMutatorFilePath,
$useGitHubAnnotationsLogger,
$strykerConfig
$strykerConfig,
$summaryJsonLogFilePath
);

$this->assertLogsStateIs(
Expand All @@ -76,7 +78,8 @@ public function test_it_can_be_instantiated(
$debugLogFilePath,
$perMutatorFilePath,
$useGitHubAnnotationsLogger,
$strykerConfig
$strykerConfig,
$summaryJsonLogFilePath
);
}

Expand All @@ -93,6 +96,7 @@ public function test_it_can_be_instantiated_without_any_values(): void
null,
null,
false,
null,
null
);
}
Expand All @@ -108,6 +112,7 @@ public function valuesProvider(): iterable
null,
false,
null,
null,
];

yield 'complete' => [
Expand All @@ -119,6 +124,7 @@ public function valuesProvider(): iterable
'perMutator.log',
true,
StrykerConfig::forBadge('master'),
'summary.json',
];
}
}

0 comments on commit cc56af8

Please sign in to comment.