Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take2 #2

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions resources/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@
"type": "boolean",
"description": "GitHub Annotations for escaped Mutants in the added/modifies files"
},
"gitlab": {
"type": "string",
"definition": "GitLab (Code Climate) code quality json file."
},
"summaryJson": {
"type": "string",
"definition": "Summary JSON log file, which contains only the statistics from the complete JSON file."
Expand Down
11 changes: 11 additions & 0 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@
/** @var string */
private const OPTION_LOGGER_GITHUB = 'logger-github';

/** @var string */
private const OPTION_LOGGER_GITLAB = 'logger-gitlab';

private const OPTION_LOGGER_HTML = 'logger-html';

private const OPTION_USE_NOOP_MUTATORS = 'noop';
Expand Down Expand Up @@ -274,6 +277,12 @@
'Log escaped Mutants as GitHub Annotations (automatically detected on Github Actions itself, use <comment>true</comment> to force-enable or <comment>false</comment> to force-disable it).',
false
)
->addOption(
self::OPTION_LOGGER_GITLAB,
null,
InputOption::VALUE_OPTIONAL,
'Path to log escaped Mutants in the GitLab (Code Climate) JSON format.',
)
->addOption(
self::OPTION_LOGGER_HTML,
null,
Expand Down Expand Up @@ -404,6 +413,7 @@
$testFramework = trim((string) $input->getOption(self::OPTION_TEST_FRAMEWORK));
$testFrameworkExtraOptions = trim((string) $input->getOption(self::OPTION_TEST_FRAMEWORK_OPTIONS));
$initialTestsPhpOptions = trim((string) $input->getOption(self::OPTION_INITIAL_TESTS_PHP_OPTIONS));
$gitlabFileLogPath = trim((string) $input->getOption(self::OPTION_LOGGER_GITLAB));

Check warning on line 416 in src/Command/RunCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "UnwrapTrim": --- Original +++ New @@ @@ $testFramework = trim((string) $input->getOption(self::OPTION_TEST_FRAMEWORK)); $testFrameworkExtraOptions = trim((string) $input->getOption(self::OPTION_TEST_FRAMEWORK_OPTIONS)); $initialTestsPhpOptions = trim((string) $input->getOption(self::OPTION_INITIAL_TESTS_PHP_OPTIONS)); - $gitlabFileLogPath = trim((string) $input->getOption(self::OPTION_LOGGER_GITLAB)); + $gitlabFileLogPath = (string) $input->getOption(self::OPTION_LOGGER_GITLAB); $htmlFileLogPath = trim((string) $input->getOption(self::OPTION_LOGGER_HTML)); /** @var string|null $minMsi */ $minMsi = $input->getOption(self::OPTION_MIN_MSI);

Check warning on line 416 in src/Command/RunCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "UnwrapTrim": --- Original +++ New @@ @@ $testFramework = trim((string) $input->getOption(self::OPTION_TEST_FRAMEWORK)); $testFrameworkExtraOptions = trim((string) $input->getOption(self::OPTION_TEST_FRAMEWORK_OPTIONS)); $initialTestsPhpOptions = trim((string) $input->getOption(self::OPTION_INITIAL_TESTS_PHP_OPTIONS)); - $gitlabFileLogPath = trim((string) $input->getOption(self::OPTION_LOGGER_GITLAB)); + $gitlabFileLogPath = (string) $input->getOption(self::OPTION_LOGGER_GITLAB); $htmlFileLogPath = trim((string) $input->getOption(self::OPTION_LOGGER_HTML)); /** @var string|null $minMsi */ $minMsi = $input->getOption(self::OPTION_MIN_MSI);
$htmlFileLogPath = trim((string) $input->getOption(self::OPTION_LOGGER_HTML));

/** @var string|null $minMsi */
Expand Down Expand Up @@ -489,6 +499,7 @@
$isForGitDiffLines,
$gitDiffBase,
$this->getUseGitHubLogger($input),
$gitlabFileLogPath === '' ? Container::DEFAULT_GITLAB_LOGGER_PATH : $gitlabFileLogPath,

Check warning on line 502 in src/Command/RunCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "Identical": --- Original +++ New @@ @@ $isForGitDiffLines, $gitDiffBase, $this->getUseGitHubLogger($input), - $gitlabFileLogPath === '' ? Container::DEFAULT_GITLAB_LOGGER_PATH : $gitlabFileLogPath, + $gitlabFileLogPath !== '' ? Container::DEFAULT_GITLAB_LOGGER_PATH : $gitlabFileLogPath, $htmlFileLogPath === '' ? Container::DEFAULT_HTML_LOGGER_PATH : $htmlFileLogPath, (bool) $input->getOption(self::OPTION_USE_NOOP_MUTATORS), (bool) $input->getOption(self::OPTION_EXECUTE_ONLY_COVERING_TEST_CASES)

Check warning on line 502 in src/Command/RunCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "Ternary": --- Original +++ New @@ @@ $isForGitDiffLines, $gitDiffBase, $this->getUseGitHubLogger($input), - $gitlabFileLogPath === '' ? Container::DEFAULT_GITLAB_LOGGER_PATH : $gitlabFileLogPath, + $gitlabFileLogPath === '' ? $gitlabFileLogPath : Container::DEFAULT_GITLAB_LOGGER_PATH, $htmlFileLogPath === '' ? Container::DEFAULT_HTML_LOGGER_PATH : $htmlFileLogPath, (bool) $input->getOption(self::OPTION_USE_NOOP_MUTATORS), (bool) $input->getOption(self::OPTION_EXECUTE_ONLY_COVERING_TEST_CASES)

Check warning on line 502 in src/Command/RunCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "Identical": --- Original +++ New @@ @@ $isForGitDiffLines, $gitDiffBase, $this->getUseGitHubLogger($input), - $gitlabFileLogPath === '' ? Container::DEFAULT_GITLAB_LOGGER_PATH : $gitlabFileLogPath, + $gitlabFileLogPath !== '' ? Container::DEFAULT_GITLAB_LOGGER_PATH : $gitlabFileLogPath, $htmlFileLogPath === '' ? Container::DEFAULT_HTML_LOGGER_PATH : $htmlFileLogPath, (bool) $input->getOption(self::OPTION_USE_NOOP_MUTATORS), (bool) $input->getOption(self::OPTION_EXECUTE_ONLY_COVERING_TEST_CASES)

Check warning on line 502 in src/Command/RunCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "Ternary": --- Original +++ New @@ @@ $isForGitDiffLines, $gitDiffBase, $this->getUseGitHubLogger($input), - $gitlabFileLogPath === '' ? Container::DEFAULT_GITLAB_LOGGER_PATH : $gitlabFileLogPath, + $gitlabFileLogPath === '' ? $gitlabFileLogPath : Container::DEFAULT_GITLAB_LOGGER_PATH, $htmlFileLogPath === '' ? Container::DEFAULT_HTML_LOGGER_PATH : $htmlFileLogPath, (bool) $input->getOption(self::OPTION_USE_NOOP_MUTATORS), (bool) $input->getOption(self::OPTION_EXECUTE_ONLY_COVERING_TEST_CASES)
$htmlFileLogPath === '' ? Container::DEFAULT_HTML_LOGGER_PATH : $htmlFileLogPath,
(bool) $input->getOption(self::OPTION_USE_NOOP_MUTATORS),
(bool) $input->getOption(self::OPTION_EXECUTE_ONLY_COVERING_TEST_CASES)
Expand Down
10 changes: 8 additions & 2 deletions src/Configuration/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public function create(
bool $isForGitDiffLines,
?string $gitDiffBase,
?bool $useGitHubLogger,
?string $gitlabLogFilePath,
?string $htmlLogFilePath,
bool $useNoopMutators,
bool $executeOnlyCoveringTestCases
Expand Down Expand Up @@ -140,7 +141,7 @@ public function create(
),
$this->retrieveFilter($filter, $gitDiffFilter, $isForGitDiffLines, $gitDiffBase, $schema->getSource()->getDirectories()),
$schema->getSource()->getExcludes(),
$this->retrieveLogs($schema->getLogs(), $configDir, $useGitHubLogger, $htmlLogFilePath),
$this->retrieveLogs($schema->getLogs(), $configDir, $useGitHubLogger, $gitlabLogFilePath, $htmlLogFilePath),
$logVerbosity,
$namespacedTmpDir,
$this->retrievePhpUnit($schema, $configDir),
Expand Down Expand Up @@ -308,7 +309,7 @@ private function retrieveFilter(string $filter, ?string $gitDiffFilter, bool $is
return $this->gitDiffFileProvider->provide($gitDiffFilter, $baseBranch, $sourceDirectories);
}

private function retrieveLogs(Logs $logs, string $configDir, ?bool $useGitHubLogger, ?string $htmlLogFilePath): Logs
private function retrieveLogs(Logs $logs, string $configDir, ?bool $useGitHubLogger, ?string $gitlabLogFilePath, ?string $htmlLogFilePath): Logs
{
if ($useGitHubLogger === null) {
$useGitHubLogger = $this->detectCiGithubActions();
Expand All @@ -318,6 +319,10 @@ private function retrieveLogs(Logs $logs, string $configDir, ?bool $useGitHubLog
$logs->setUseGitHubAnnotationsLogger($useGitHubLogger);
}

if ($gitlabLogFilePath !== null) {
$logs->setGitlabLogFilePath($gitlabLogFilePath);
}

if ($htmlLogFilePath !== null) {
$logs->setHtmlLogFilePath($htmlLogFilePath);
}
Expand All @@ -327,6 +332,7 @@ private function retrieveLogs(Logs $logs, string $configDir, ?bool $useGitHubLog
self::pathToAbsolute($logs->getHtmlLogFilePath(), $configDir),
self::pathToAbsolute($logs->getSummaryLogFilePath(), $configDir),
self::pathToAbsolute($logs->getJsonLogFilePath(), $configDir),
self::pathToAbsolute($logs->getGitlabLogFilePath(), $configDir),
self::pathToAbsolute($logs->getDebugLogFilePath(), $configDir),
self::pathToAbsolute($logs->getPerMutatorFilePath(), $configDir),
$logs->getUseGitHubAnnotationsLogger(),
Expand Down
12 changes: 12 additions & 0 deletions src/Configuration/Entry/Logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function __construct(
private ?string $htmlLogFilePath,
private readonly ?string $summaryLogFilePath,
private readonly ?string $jsonLogFilePath,
private ?string $gitlabLogFilePath,
private readonly ?string $debugLogFilePath,
private readonly ?string $perMutatorFilePath,
private bool $useGitHubAnnotationsLogger,
Expand All @@ -63,6 +64,7 @@ public static function createEmpty(): self
null,
null,
null,
null,
false,
null,
null
Expand All @@ -79,6 +81,11 @@ public function getHtmlLogFilePath(): ?string
return $this->htmlLogFilePath;
}

public function setGitlabLogFilePath(string $gitlabLogFilePath): void
{
$this->gitlabLogFilePath = $gitlabLogFilePath;
}

public function setHtmlLogFilePath(string $htmlLogFilePath): void
{
$this->htmlLogFilePath = $htmlLogFilePath;
Expand All @@ -94,6 +101,11 @@ public function getJsonLogFilePath(): ?string
return $this->jsonLogFilePath;
}

public function getGitlabLogFilePath(): ?string
{
return $this->gitlabLogFilePath;
}

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 @@ -85,6 +85,7 @@ private static function createLogs(stdClass $logs): Logs
self::normalizeString($logs->html ?? null),
self::normalizeString($logs->summary ?? null),
self::normalizeString($logs->json ?? null),
self::normalizeString($logs->gitlab ?? null),
self::normalizeString($logs->debug ?? null),
self::normalizeString($logs->perMutator ?? null),
$logs->github ?? false,
Expand Down
4 changes: 4 additions & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ final class Container
public const DEFAULT_GIT_DIFF_LINES = false;
public const DEFAULT_GIT_DIFF_BASE = null;
public const DEFAULT_USE_GITHUB_LOGGER = null;
public const DEFAULT_GITLAB_LOGGER_PATH = null;
public const DEFAULT_HTML_LOGGER_PATH = null;
public const DEFAULT_USE_NOOP_MUTATORS = false;
public const DEFAULT_EXECUTE_ONLY_COVERING_TEST_CASES = false;
Expand Down Expand Up @@ -588,6 +589,7 @@ public static function create(): self
self::DEFAULT_GIT_DIFF_LINES,
self::DEFAULT_GIT_DIFF_BASE,
self::DEFAULT_USE_GITHUB_LOGGER,
self::DEFAULT_GITLAB_LOGGER_PATH,
self::DEFAULT_HTML_LOGGER_PATH,
self::DEFAULT_USE_NOOP_MUTATORS,
self::DEFAULT_EXECUTE_ONLY_COVERING_TEST_CASES
Expand Down Expand Up @@ -622,6 +624,7 @@ public function withValues(
bool $isForGitDiffLines,
?string $gitDiffBase,
?bool $useGitHubLogger,
?string $gitlabLogFilePath,
?string $htmlLogFilePath,
bool $useNoopMutators,
bool $executeOnlyCoveringTestCases
Expand Down Expand Up @@ -690,6 +693,7 @@ public function withValues(
$isForGitDiffLines,
$gitDiffBase,
$useGitHubLogger,
$gitlabLogFilePath,
$htmlLogFilePath,
$useNoopMutators,
$executeOnlyCoveringTestCases
Expand Down
7 changes: 7 additions & 0 deletions src/Logger/FileLoggerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ private function createLineLoggers(Logs $logConfig): iterable

yield $logConfig->getJsonLogFilePath() => $this->createJsonLogger();

yield $logConfig->getGitlabLogFilePath() => $this->createGitlabLogger();

yield $logConfig->getDebugLogFilePath() => $this->createDebugLogger();

yield $logConfig->getPerMutatorFilePath() => $this->createPerMutatorLogger();
Expand Down Expand Up @@ -138,6 +140,11 @@ private function createJsonLogger(): LineMutationTestingResultsLogger
);
}

private function createGitlabLogger(): LineMutationTestingResultsLogger
{
return new GitLabCodeQualityLogger($this->resultsCollector);
}

private function createGitHubAnnotationsLogger(): LineMutationTestingResultsLogger
{
return new GitHubAnnotationsLogger($this->resultsCollector);
Expand Down
80 changes: 80 additions & 0 deletions src/Logger/GitLabCodeQualityLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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\ResultsCollector;
use Infection\Str;
use function json_encode;
use const JSON_THROW_ON_ERROR;
use function Safe\shell_exec;
use Symfony\Component\Filesystem\Path;
use function trim;

/**
* @internal
*/
final class GitLabCodeQualityLogger implements LineMutationTestingResultsLogger
{
public function __construct(private readonly ResultsCollector $resultsCollector)
{
}

public function getLogLines(): array
{
$lines = [];
$projectRootDirectory = trim(shell_exec('git rev-parse --show-toplevel'));

Check warning on line 58 in src/Logger/GitLabCodeQualityLogger.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "UnwrapTrim": --- Original +++ New @@ @@ public function getLogLines() : array { $lines = []; - $projectRootDirectory = trim(shell_exec('git rev-parse --show-toplevel')); + $projectRootDirectory = shell_exec('git rev-parse --show-toplevel'); foreach ($this->resultsCollector->getEscapedExecutionResults() as $escapedExecutionResult) { $lines[] = ['type' => 'issue', 'fingerprint' => $escapedExecutionResult->getMutantHash(), 'check_name' => $escapedExecutionResult->getMutatorName(), 'description' => 'Escaped Mutant for Mutator ' . $escapedExecutionResult->getMutatorName(), 'content' => Str::convertToUtf8(Str::trimLineReturns($escapedExecutionResult->getMutantDiff())), 'categories' => ['Escaped Mutant'], 'location' => ['path' => Path::makeRelative($escapedExecutionResult->getOriginalFilePath(), $projectRootDirectory), 'lines' => ['begin' => $escapedExecutionResult->getOriginalStartingLine()]], 'severity' => 'major']; }

Check warning on line 58 in src/Logger/GitLabCodeQualityLogger.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "UnwrapTrim": --- Original +++ New @@ @@ public function getLogLines() : array { $lines = []; - $projectRootDirectory = trim(shell_exec('git rev-parse --show-toplevel')); + $projectRootDirectory = shell_exec('git rev-parse --show-toplevel'); foreach ($this->resultsCollector->getEscapedExecutionResults() as $escapedExecutionResult) { $lines[] = ['type' => 'issue', 'fingerprint' => $escapedExecutionResult->getMutantHash(), 'check_name' => $escapedExecutionResult->getMutatorName(), 'description' => 'Escaped Mutant for Mutator ' . $escapedExecutionResult->getMutatorName(), 'content' => Str::convertToUtf8(Str::trimLineReturns($escapedExecutionResult->getMutantDiff())), 'categories' => ['Escaped Mutant'], 'location' => ['path' => Path::makeRelative($escapedExecutionResult->getOriginalFilePath(), $projectRootDirectory), 'lines' => ['begin' => $escapedExecutionResult->getOriginalStartingLine()]], 'severity' => 'major']; }

foreach ($this->resultsCollector->getEscapedExecutionResults() as $escapedExecutionResult) {
$lines[] = [
'type' => 'issue',
'fingerprint' => $escapedExecutionResult->getMutantHash(),
'check_name' => $escapedExecutionResult->getMutatorName(),
'description' => 'Escaped Mutant for Mutator ' . $escapedExecutionResult->getMutatorName(),
'content' => Str::convertToUtf8(Str::trimLineReturns($escapedExecutionResult->getMutantDiff())),
'categories' => ['Escaped Mutant'],
'location' => [
'path' => Path::makeRelative($escapedExecutionResult->getOriginalFilePath(), $projectRootDirectory),
'lines' => [
'begin' => $escapedExecutionResult->getOriginalStartingLine(),
],
],
'severity' => 'major',
];
}

return [json_encode($lines, JSON_THROW_ON_ERROR)];
}
}
1 change: 1 addition & 0 deletions tests/benchmark/Tracing/provide-traces-closure.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
Container::DEFAULT_GIT_DIFF_LINES,
Container::DEFAULT_GIT_DIFF_BASE,
Container::DEFAULT_USE_GITHUB_LOGGER,
Container::DEFAULT_GITLAB_LOGGER_PATH,
Container::DEFAULT_HTML_LOGGER_PATH,
true,
Container::DEFAULT_EXECUTE_ONLY_COVERING_TEST_CASES
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 @@ -102,6 +102,7 @@ private function assertConfigurationStateIs(
$expectedLogs->getHtmlLogFilePath(),
$expectedLogs->getSummaryLogFilePath(),
$expectedLogs->getJsonLogFilePath(),
$expectedLogs->getGitlabLogFilePath(),
$expectedLogs->getDebugLogFilePath(),
$expectedLogs->getPerMutatorFilePath(),
$expectedLogs->getUseGitHubAnnotationsLogger(),
Expand Down
Loading