Skip to content

Commit

Permalink
Add JSON logger, useful for CI and analyzing results of Infection pro…
Browse files Browse the repository at this point in the history
…grammatically
  • Loading branch information
maks-rafalko committed Jul 4, 2020
1 parent 3f4221c commit 9bee64b
Show file tree
Hide file tree
Showing 28 changed files with 576 additions and 30 deletions.
4 changes: 4 additions & 0 deletions resources/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
"type": "string",
"definition": "Summary log file, which display the amount of mutants per category, (Killed, Errored, Escaped, Timed Out & Not Covered). More intended for internal purposes."
},
"json": {
"type": "string",
"definition": "JSON log file, which contains information about all mutants, as well as the source and mutated code and test framework output. Useful for using on CI servers to be able to programmatically analyze it."
},
"debug": {
"type": "string",
"description": "Debug log file, which displays what mutations were found on what line, per category. More intended for internal purposes."
Expand Down
9 changes: 9 additions & 0 deletions src/Configuration/Entry/Logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,22 @@ final class Logs
{
private $textLogFilePath;
private $summaryLogFilePath;
private $jsonLogFilePath;
private $debugLogFilePath;
private $perMutatorFilePath;
private $badge;

public function __construct(
?string $textLogFilePath,
?string $summaryLogFilePath,
?string $jsonLogFilePath,
?string $debugLogFilePath,
?string $perMutatorFilePath,
?Badge $badge
) {
$this->textLogFilePath = $textLogFilePath;
$this->summaryLogFilePath = $summaryLogFilePath;
$this->jsonLogFilePath = $jsonLogFilePath;
$this->debugLogFilePath = $debugLogFilePath;
$this->perMutatorFilePath = $perMutatorFilePath;
$this->badge = $badge;
Expand All @@ -67,6 +70,7 @@ public static function createEmpty(): self
null,
null,
null,
null,
null
);
}
Expand All @@ -81,6 +85,11 @@ public function getSummaryLogFilePath(): ?string
return $this->summaryLogFilePath;
}

public function getJsonLogFilePath(): ?string
{
return $this->jsonLogFilePath;
}

public function getDebugLogFilePath(): ?string
{
return $this->debugLogFilePath;
Expand Down
1 change: 1 addition & 0 deletions src/Configuration/Schema/SchemaConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ private static function createLogs(stdClass $logs): Logs
return new Logs(
self::normalizeString($logs->text ?? null),
self::normalizeString($logs->summary ?? null),
self::normalizeString($logs->json ?? null),
self::normalizeString($logs->debug ?? null),
self::normalizeString($logs->perMutator ?? null),
self::createBadge($logs->badge ?? null)
Expand Down
112 changes: 112 additions & 0 deletions src/Logger/JsonLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?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 Infection\Mutant\MutantExecutionResult;
use Infection\Str;
use function json_encode;
use const JSON_THROW_ON_ERROR;

/**
* @internal
*/
final class JsonLogger implements LineMutationTestingResultsLogger
{
private $metricsCalculator;
private $onlyCoveredMode;

public function __construct(
MetricsCalculator $metricsCalculator,
bool $onlyCoveredMode
) {
$this->metricsCalculator = $metricsCalculator;
$this->onlyCoveredMode = $onlyCoveredMode;
}

/**
* @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(),
'timeOutCount' => $this->metricsCalculator->getTimedOutCount(),
'msi' => $this->metricsCalculator->getMutationScoreIndicator(),
'mutationCodeCoverage' => $this->metricsCalculator->getCoverageRate(),
'coveredCodeMsi' => $this->metricsCalculator->getCoveredCodeMutationScoreIndicator(),
],
'escaped' => $this->getResultsLine($this->metricsCalculator->getEscapedExecutionResults()),
'timeouted' => $this->getResultsLine($this->metricsCalculator->getTimedOutExecutionResults()),
'killed' => $this->getResultsLine($this->metricsCalculator->getKilledExecutionResults()),
'errored' => $this->getResultsLine($this->metricsCalculator->getErrorExecutionResults()),
'uncovered' => $this->onlyCoveredMode ? [] : $this->getResultsLine($this->metricsCalculator->getNotCoveredExecutionResults()),
];

return [json_encode($data, JSON_THROW_ON_ERROR)];
}

/**
* @param MutantExecutionResult[] $executionResults
*
* @return array<int, array{mutator: array, diff: string, processOutput: string}>
*/
private function getResultsLine(array $executionResults): array
{
$mutatorRows = [];

foreach ($executionResults as $index => $mutantProcess) {
$mutatorRows[] = [
'mutator' => [
'mutatorName' => $mutantProcess->getMutatorName(),
'originalSourceCode' => $mutantProcess->getOriginalCode(),
'mutatedSourceCode' => $mutantProcess->getMutatedCode(),
'originalFilePath' => $mutantProcess->getOriginalFilePath(),
'originalStartLine' => $mutantProcess->getOriginalStartingLine(),
],
'diff' => Str::trimLineReturns($mutantProcess->getMutantDiff()),
'processOutput' => Str::trimLineReturns($mutantProcess->getProcessOutput()),
];
}

return $mutatorRows;
}
}
14 changes: 14 additions & 0 deletions src/Logger/LoggerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function createFromLogEntries(Logs $logConfig): MutationTestingResultsLog
[
$this->createTextLogger($logConfig->getTextLogFilePath()),
$this->createSummaryLogger($logConfig->getSummaryLogFilePath()),
$this->createJsonLogger($logConfig->getJsonLogFilePath()),
$this->createDebugLogger($logConfig->getDebugLogFilePath()),
$this->createPerMutatorLogger($logConfig->getPerMutatorFilePath()),
$this->createBadgeLogger($logConfig->getBadge()),
Expand Down Expand Up @@ -129,6 +130,19 @@ private function createSummaryLogger(?string $filePath): ?FileLogger
;
}

private function createJsonLogger(?string $filePath): ?FileLogger
{
return $filePath === null
? null
: new FileLogger(
$filePath,
$this->filesystem,
new JsonLogger($this->metricsCalculator, $this->onlyCoveredCode),
$this->logger
)
;
}

private function createDebugLogger(?string $filePath): ?FileLogger
{
return $filePath === null
Expand Down
10 changes: 9 additions & 1 deletion src/Mutant/Mutant.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,20 @@ class Mutant
private $mutation;
private $mutatedCode;
private $diff;
private $prettyPrintedOriginalCode;

public function __construct(
string $mutantFilePath,
Mutation $mutation,
string $mutatedCode,
string $diff
string $diff,
string $prettyPrintedOriginalCode
) {
$this->mutantFilePath = $mutantFilePath;
$this->mutation = $mutation;
$this->mutatedCode = $mutatedCode;
$this->diff = $diff;
$this->prettyPrintedOriginalCode = $prettyPrintedOriginalCode;
}

public function getFilePath(): string
Expand All @@ -76,6 +79,11 @@ public function getMutatedCode(): string
return $this->mutatedCode;
}

public function getPrettyPrintedOriginalCode(): string
{
return $this->prettyPrintedOriginalCode;
}

public function getDiff(): string
{
return $this->diff;
Expand Down
22 changes: 20 additions & 2 deletions src/Mutant/MutantExecutionResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class MutantExecutionResult
private $mutatorName;
private $originalFilePath;
private $originalStartingLine;
private $originalCode;
private $mutatedCode;

public function __construct(
string $processCommandLine,
Expand All @@ -60,7 +62,9 @@ public function __construct(
string $mutantDiff,
string $mutatorName,
string $originalFilePath,
int $originalStartingLine
int $originalStartingLine,
string $originalCode,
string $mutatedCode
) {
Assert::oneOf($detectionStatus, DetectionStatus::ALL);
Assert::oneOf($mutatorName, array_keys(ProfileList::ALL_MUTATORS));
Expand All @@ -72,6 +76,8 @@ public function __construct(
$this->mutatorName = $mutatorName;
$this->originalFilePath = $originalFilePath;
$this->originalStartingLine = $originalStartingLine;
$this->originalCode = $originalCode;
$this->mutatedCode = $mutatedCode;
}

public static function createFromNonCoveredMutant(Mutant $mutant): self
Expand All @@ -85,7 +91,9 @@ public static function createFromNonCoveredMutant(Mutant $mutant): self
$mutant->getDiff(),
$mutant->getMutation()->getMutatorName(),
$mutation->getOriginalFilePath(),
$mutation->getOriginalStartingLine()
$mutation->getOriginalStartingLine(),
$mutant->getPrettyPrintedOriginalCode(),
$mutant->getMutatedCode()
);
}

Expand Down Expand Up @@ -123,4 +131,14 @@ public function getOriginalStartingLine(): int
{
return $this->originalStartingLine;
}

public function getOriginalCode(): string
{
return $this->originalCode;
}

public function getMutatedCode(): string
{
return $this->mutatedCode;
}
}
4 changes: 3 additions & 1 deletion src/Mutant/MutantExecutionResultFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ public function createFromProcess(MutantProcess $mutantProcess): MutantExecution
$mutant->getDiff(),
$mutation->getMutatorName(),
$mutation->getOriginalFilePath(),
$mutation->getOriginalStartingLine()
$mutation->getOriginalStartingLine(),
$mutant->getPrettyPrintedOriginalCode(),
$mutant->getMutatedCode()
);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Mutant/MutantFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public function create(Mutation $mutation): Mutant
$mutantFilePath,
$mutation,
$mutatedCode,
$this->createMutantDiff($mutation, $mutatedCode)
$this->createMutantDiff($mutation, $mutatedCode),
$this->getOriginalPrettyPrintedFile($mutation->getOriginalFilePath(), $mutation->getOriginalFileAst())
);
}

Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/Configuration/ConfigurationAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ private function assertConfigurationStateIs(
$configuration->getLogs(),
$expectedLogs->getTextLogFilePath(),
$expectedLogs->getSummaryLogFilePath(),
$expectedLogs->getJsonLogFilePath(),
$expectedLogs->getDebugLogFilePath(),
$expectedLogs->getPerMutatorFilePath(),
$expectedLogs->getBadge()
Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/Configuration/ConfigurationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ public function valueProvider(): iterable
new Logs(
'text.log',
'summary.log',
'json.log',
'debug.log',
'mutator.log',
new Badge('master')
Expand Down Expand Up @@ -705,6 +706,7 @@ public function valueProvider(): iterable
new Logs(
'text.log',
'summary.log',
'json.log',
'debug.log',
'mutator.log',
new Badge('master')
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/Configuration/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ public function valueProvider(): iterable
new Logs(
'text.log',
'summary.log',
'json.log',
'debug.log',
'mutator.log',
new Badge('master')
Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/Configuration/Entry/LogsAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ private function assertLogsStateIs(
Logs $logs,
?string $expectedTextLogFilePath,
?string $expectedSummaryLogFilePath,
?string $expectedJsonLogFilePath,
?string $expectedDebugLogFilePath,
?string $expectedPerMutatorFilePath,
?Badge $expectedBadge
): void {
$this->assertSame($expectedTextLogFilePath, $logs->getTextLogFilePath());
$this->assertSame($expectedSummaryLogFilePath, $logs->getSummaryLogFilePath());
$this->assertSame($expectedJsonLogFilePath, $logs->getJsonLogFilePath());
$this->assertSame($expectedDebugLogFilePath, $logs->getDebugLogFilePath());
$this->assertSame($expectedPerMutatorFilePath, $logs->getPerMutatorFilePath());

Expand Down

0 comments on commit 9bee64b

Please sign in to comment.