Skip to content

Commit

Permalink
feat: introduce --logger-project-root-directory command option (#1978)
Browse files Browse the repository at this point in the history
* fix: introduce --logger-project-root-directory option

* chore: update changelog

* fix: fix CS

* tests: fix and add some tests

* fix: fix PHPStan

* Update CHANGELOG.md

* Update CHANGELOG.md

---------

Co-authored-by: Maks Rafalko <maks.rafalko@gmail.com>
  • Loading branch information
vincentchalamon and maks-rafalko committed Jun 7, 2024
1 parent 99e03a7 commit cb975d3
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 21 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# Change Log

## [0.29.0](https://github.com/infection/infection/tree/0.28.0) (2024-05-28)
## [0.29.4](https://github.com/infection/infection/tree/0.29.4) (2024-06-08)

[Full Changelog](https://github.com/infection/infection/compare/0.29.0...0.29.4)

**Added:**

* Introduce `--logger-project-root-directory` by @vincentchalamon in https://github.com/infection/infection/pull/1978

## [0.29.0](https://github.com/infection/infection/tree/0.29.0) (2024-05-28)

[Full Changelog](https://github.com/infection/infection/compare/0.28.1...0.29.0)

Expand Down
24 changes: 23 additions & 1 deletion src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
use function getenv;
use function implode;
use function in_array;
use Infection\Configuration\Configuration;
use Infection\Configuration\Schema\SchemaConfigurationLoader;
use Infection\Console\ConsoleOutput;
use Infection\Console\Input\MsiParser;
Expand All @@ -66,6 +65,7 @@
use function max;
use const PHP_SAPI;
use Psr\Log\LoggerInterface;
use function Safe\shell_exec;
use function sprintf;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -131,6 +131,8 @@ final class RunCommand extends BaseCommand
/** @var string */
private const OPTION_LOGGER_GITLAB = 'logger-gitlab';

private const OPTION_LOGGER_PROJECT_ROOT_DIRECTORY = 'logger-project-root-directory';

private const OPTION_LOGGER_HTML = 'logger-html';

private const OPTION_USE_NOOP_MUTATORS = 'noop';
Expand Down Expand Up @@ -293,6 +295,12 @@ protected function configure(): void
InputOption::VALUE_OPTIONAL,
'Path to log escaped Mutants in the GitLab (Code Climate) JSON format.',
)
->addOption(
self::OPTION_LOGGER_PROJECT_ROOT_DIRECTORY,
null,
InputOption::VALUE_REQUIRED,
'Custom path to project root directory used on the log report generation (auto-detected if not set).',
)
->addOption(
self::OPTION_LOGGER_HTML,
null,
Expand Down Expand Up @@ -425,6 +433,19 @@ private function createContainer(IO $io, LoggerInterface $logger): Container
$initialTestsPhpOptions = trim((string) $input->getOption(self::OPTION_INITIAL_TESTS_PHP_OPTIONS));
$gitlabFileLogPath = trim((string) $input->getOption(self::OPTION_LOGGER_GITLAB));
$htmlFileLogPath = trim((string) $input->getOption(self::OPTION_LOGGER_HTML));
$loggerProjectRootDirectory = $input->getOption(self::OPTION_LOGGER_PROJECT_ROOT_DIRECTORY);

// auto-detect project-root-directory on GitHub and GitLab if not manually set
// default retrieve it using git rev-parse
if ($loggerProjectRootDirectory === null) {
if (($githubWorkspace = getenv('GITHUB_WORKSPACE')) !== false) {
$loggerProjectRootDirectory = $githubWorkspace;
} elseif (($gitlabCiProjectDir = getenv('CI_PROJECT_DIR')) !== false) {
$loggerProjectRootDirectory = $gitlabCiProjectDir;
} else {
$loggerProjectRootDirectory = trim(shell_exec('git rev-parse --show-toplevel'));
}
}

/** @var string|null $minMsi */
$minMsi = $input->getOption(self::OPTION_MIN_MSI);
Expand Down Expand Up @@ -514,6 +535,7 @@ private function createContainer(IO $io, LoggerInterface $logger): Container
(bool) $input->getOption(self::OPTION_USE_NOOP_MUTATORS),
(bool) $input->getOption(self::OPTION_EXECUTE_ONLY_COVERING_TEST_CASES),
$this->getMapSourceClassToTest($input),
$loggerProjectRootDirectory,
);
}

Expand Down
6 changes: 6 additions & 0 deletions src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public function __construct(
private readonly bool $isForGitDiffLines,
private readonly ?string $gitDiffBase,
private readonly ?string $mapSourceClassToTestStrategy,
private readonly ?string $loggerProjectRootDirectory,
) {
Assert::nullOrGreaterThanEq($timeout, 0);
Assert::allString($sourceDirectories);
Expand Down Expand Up @@ -296,4 +297,9 @@ public function getMapSourceClassToTestStrategy(): ?string
{
return $this->mapSourceClassToTestStrategy;
}

public function getLoggerProjectRootDirectory(): ?string
{
return $this->loggerProjectRootDirectory;
}
}
2 changes: 2 additions & 0 deletions src/Configuration/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public function create(
bool $useNoopMutators,
bool $executeOnlyCoveringTestCases,
?string $mapSourceClassToTestStrategy,
?string $loggerProjectRootDirectory,
): Configuration {
$configDir = dirname($schema->getFile());

Expand Down Expand Up @@ -173,6 +174,7 @@ public function create(
$isForGitDiffLines,
$gitDiffBase,
$mapSourceClassToTestStrategy,
$loggerProjectRootDirectory,
);
}

Expand Down
5 changes: 5 additions & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ final class Container
public const DEFAULT_GIT_DIFF_BASE = null;
public const DEFAULT_USE_GITHUB_LOGGER = null;
public const DEFAULT_GITLAB_LOGGER_PATH = null;
public const DEFAULT_LOGGER_PROJECT_ROOT_DIRECTORY = 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 @@ -467,6 +468,7 @@ public static function create(): self
$config->mutateOnlyCoveredCode(),
$container->getLogger(),
$container->getStrykerHtmlReportBuilder(),
$config->getLoggerProjectRootDirectory(),
);
},
MutationTestingResultsLogger::class => static fn (self $container): MutationTestingResultsLogger => new FederatedLogger(...array_filter([
Expand Down Expand Up @@ -583,6 +585,7 @@ public static function create(): self
self::DEFAULT_USE_NOOP_MUTATORS,
self::DEFAULT_EXECUTE_ONLY_COVERING_TEST_CASES,
self::DEFAULT_MAP_SOURCE_CLASS_TO_TEST_STRATEGY,
self::DEFAULT_LOGGER_PROJECT_ROOT_DIRECTORY,
);
}

Expand Down Expand Up @@ -619,6 +622,7 @@ public function withValues(
bool $useNoopMutators,
bool $executeOnlyCoveringTestCases,
?string $mapSourceClassToTestStrategy,
?string $loggerProjectRootDirectory,
): self {
$clone = clone $this;

Expand Down Expand Up @@ -689,6 +693,7 @@ public function withValues(
$useNoopMutators,
$executeOnlyCoveringTestCases,
$mapSourceClassToTestStrategy,
$loggerProjectRootDirectory,
),
);

Expand Down
6 changes: 3 additions & 3 deletions src/Logger/FileLoggerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
*/
class FileLoggerFactory
{
public function __construct(private readonly MetricsCalculator $metricsCalculator, private readonly ResultsCollector $resultsCollector, private readonly Filesystem $filesystem, private readonly string $logVerbosity, private readonly bool $debugMode, private readonly bool $onlyCoveredCode, private readonly LoggerInterface $logger, private readonly StrykerHtmlReportBuilder $strykerHtmlReportBuilder)
public function __construct(private readonly MetricsCalculator $metricsCalculator, private readonly ResultsCollector $resultsCollector, private readonly Filesystem $filesystem, private readonly string $logVerbosity, private readonly bool $debugMode, private readonly bool $onlyCoveredCode, private readonly LoggerInterface $logger, private readonly StrykerHtmlReportBuilder $strykerHtmlReportBuilder, private readonly ?string $loggerProjectRootDirectory)
{
}

Expand Down Expand Up @@ -142,12 +142,12 @@ private function createJsonLogger(): LineMutationTestingResultsLogger

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

private function createGitHubAnnotationsLogger(): LineMutationTestingResultsLogger
{
return new GitHubAnnotationsLogger($this->resultsCollector);
return new GitHubAnnotationsLogger($this->resultsCollector, $this->loggerProjectRootDirectory);
}

private function createDebugLogger(): LineMutationTestingResultsLogger
Expand Down
15 changes: 9 additions & 6 deletions src/Logger/GitHubAnnotationsLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,20 @@ final class GitHubAnnotationsLogger implements LineMutationTestingResultsLogger
{
public const DEFAULT_OUTPUT = 'php://stdout';

public function __construct(private readonly ResultsCollector $resultsCollector)
public function __construct(private readonly ResultsCollector $resultsCollector, private ?string $loggerProjectRootDirectory)
{
if ($loggerProjectRootDirectory === null) {
if (($projectRootDirectory = getenv('GITHUB_WORKSPACE')) === false) {
$projectRootDirectory = trim(shell_exec('git rev-parse --show-toplevel'));
}
$this->loggerProjectRootDirectory = $projectRootDirectory;
}
}

public function getLogLines(): array
{
$lines = [];

if (($projectRootDirectory = getenv('GITHUB_WORKSPACE')) === false) {
$projectRootDirectory = trim(shell_exec('git rev-parse --show-toplevel'));
}

foreach ($this->resultsCollector->getEscapedExecutionResults() as $escapedExecutionResult) {
$error = [
'line' => $escapedExecutionResult->getOriginalStartingLine(),
Expand All @@ -73,7 +75,8 @@ public function getLogLines(): array
];
$lines[] = $this->buildAnnotation(
Path::makeRelative($escapedExecutionResult->getOriginalFilePath(), $projectRootDirectory),
/* @phpstan-ignore-next-line expects string, string|null given */
Path::makeRelative($escapedExecutionResult->getOriginalFilePath(), $this->loggerProjectRootDirectory),
$error,
);
}
Expand Down
15 changes: 9 additions & 6 deletions src/Logger/GitLabCodeQualityLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,20 @@
*/
final class GitLabCodeQualityLogger implements LineMutationTestingResultsLogger
{
public function __construct(private readonly ResultsCollector $resultsCollector)
public function __construct(private readonly ResultsCollector $resultsCollector, private ?string $loggerProjectRootDirectory)
{
if ($loggerProjectRootDirectory === null) {
if (($projectRootDirectory = getenv('CI_PROJECT_DIR')) === false) {
$projectRootDirectory = trim(shell_exec('git rev-parse --show-toplevel'));
}
$this->loggerProjectRootDirectory = $projectRootDirectory;
}
}

public function getLogLines(): array
{
$lines = [];

if (($projectRootDirectory = getenv('CI_PROJECT_DIR')) === false) {
$projectRootDirectory = trim(shell_exec('git rev-parse --show-toplevel'));
}

foreach ($this->resultsCollector->getEscapedExecutionResults() as $escapedExecutionResult) {
$lines[] = [
'type' => 'issue',
Expand All @@ -70,7 +72,8 @@ public function getLogLines(): array
'content' => Str::convertToUtf8(Str::trimLineReturns($escapedExecutionResult->getMutantDiff())),
'categories' => ['Escaped Mutant'],
'location' => [
'path' => Path::makeRelative($escapedExecutionResult->getOriginalFilePath(), $projectRootDirectory),
/* @phpstan-ignore-next-line expects string, string|null given */
'path' => Path::makeRelative($escapedExecutionResult->getOriginalFilePath(), $this->loggerProjectRootDirectory),
'lines' => [
'begin' => $escapedExecutionResult->getOriginalStartingLine(),
],
Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/Configuration/ConfigurationAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ private function assertConfigurationStateIs(
bool $expectedIsForGitDiffLines,
?string $expectedGitDiffBase,
?string $expectedMapSourceClassToTest,
?string $expectedLoggerProjectRootDirectory,
): void {
$this->assertSame($expectedTimeout, $configuration->getProcessTimeout());
$this->assertSame($expectedSourceDirectories, $configuration->getSourceDirectories());
Expand Down Expand Up @@ -143,6 +144,7 @@ private function assertConfigurationStateIs(
$this->assertSame($expectedIsForGitDiffLines, $configuration->isForGitDiffLines());
$this->assertSame($expectedGitDiffBase, $configuration->getGitDiffBase());
$this->assertSame($expectedMapSourceClassToTest, $configuration->getMapSourceClassToTestStrategy());
$this->assertSame($expectedLoggerProjectRootDirectory, $configuration->getLoggerProjectRootDirectory());
}

/**
Expand Down
Loading

0 comments on commit cb975d3

Please sign in to comment.