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

feat: use CI variables to detect project path #1949

Merged
6 changes: 5 additions & 1 deletion src/Logger/GitHubAnnotationsLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

namespace Infection\Logger;

use function getenv;
use Infection\Metrics\ResultsCollector;
use function Safe\shell_exec;
use function str_replace;
Expand All @@ -55,7 +56,10 @@
public function getLogLines(): array
{
$lines = [];
$projectRootDirectory = trim(shell_exec('git rev-parse --show-toplevel'));

if (($projectRootDirectory = getenv('GITHUB_WORKSPACE')) === false) {

Check warning on line 60 in src/Logger/GitHubAnnotationsLogger.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ public function getLogLines(): array { $lines = []; - if (($projectRootDirectory = getenv('GITHUB_WORKSPACE')) === false) { + if (($projectRootDirectory = getenv('GITHUB_WORKSPACE')) === true) { $projectRootDirectory = trim(shell_exec('git rev-parse --show-toplevel')); } foreach ($this->resultsCollector->getEscapedExecutionResults() as $escapedExecutionResult) {
$projectRootDirectory = trim(shell_exec('git rev-parse --show-toplevel'));
}

foreach ($this->resultsCollector->getEscapedExecutionResults() as $escapedExecutionResult) {
$error = [
Expand Down
6 changes: 5 additions & 1 deletion src/Logger/GitLabCodeQualityLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

namespace Infection\Logger;

use function getenv;
use Infection\Metrics\ResultsCollector;
use Infection\Str;
use function json_encode;
Expand All @@ -55,7 +56,10 @@
public function getLogLines(): array
{
$lines = [];
$projectRootDirectory = trim(shell_exec('git rev-parse --show-toplevel'));

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

Check warning on line 61 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 @@ @@ { $lines = []; if (($projectRootDirectory = getenv('CI_PROJECT_DIR')) === false) { - $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[] = [
Expand Down
4 changes: 3 additions & 1 deletion tests/phpunit/Logger/CreateMetricsCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@

trait CreateMetricsCalculator
{
private static $pathPrefix = '';

private static function createCompleteMetricsCalculator(): MetricsCalculator
{
$calculator = new MetricsCalculator(2);
Expand Down Expand Up @@ -191,7 +193,7 @@ private static function createMutantExecutionResult(
)),
'a1b2c3',
MutatorName::getName($mutatorClassName),
'foo/bar',
self::$pathPrefix . 'foo/bar',
10 - $i,
20 - $i,
10 - $i,
Expand Down
28 changes: 28 additions & 0 deletions tests/phpunit/Logger/GitHubAnnotationsLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,31 @@

use Infection\Logger\GitHubAnnotationsLogger;
use Infection\Metrics\ResultsCollector;
use Infection\Tests\EnvVariableManipulation\BacksUpEnvironmentVariables;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

#[Group('integration')]
final class GitHubAnnotationsLoggerTest extends TestCase
{
use BacksUpEnvironmentVariables;
use CreateMetricsCalculator;

protected function setUp(): void
{
$this->backupEnvironmentVariables();

parent::setUp();
}

protected function tearDown(): void
{
parent::tearDown();

$this->restoreEnvironmentVariables();
}

#[DataProvider('metricsProvider')]
public function test_it_logs_correctly_with_mutations(
ResultsCollector $resultsCollector,
Expand All @@ -71,4 +87,16 @@ public static function metricsProvider(): iterable
],
];
}

public function test_it_logs_correctly_with_ci_github_workspace(): void
{
\Safe\putenv('GITHUB_WORKSPACE=/my/project/dir');
self::$pathPrefix = '/my/project/dir/';
darthf1 marked this conversation as resolved.
Show resolved Hide resolved

$resultsCollector = self::createCompleteResultsCollector();

$logger = new GitHubAnnotationsLogger($resultsCollector);

$this->assertStringContainsString('warning file=foo/bar', $logger->getLogLines()[0]);
}
}
28 changes: 28 additions & 0 deletions tests/phpunit/Logger/GitLabCodeQualityLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use Infection\Metrics\ResultsCollector;
use Infection\Mutant\DetectionStatus;
use Infection\Mutator\Loop\For_;
use Infection\Tests\EnvVariableManipulation\BacksUpEnvironmentVariables;
use const JSON_THROW_ON_ERROR;
use const PHP_EOL;
use PHPUnit\Framework\Attributes\DataProvider;
Expand All @@ -51,8 +52,23 @@
#[Group('integration')]
final class GitLabCodeQualityLoggerTest extends TestCase
{
use BacksUpEnvironmentVariables;
use CreateMetricsCalculator;

protected function setUp(): void
{
$this->backupEnvironmentVariables();

parent::setUp();
}

protected function tearDown(): void
{
parent::tearDown();

$this->restoreEnvironmentVariables();
}

#[DataProvider('metricsProvider')]
public function test_it_logs_correctly_with_mutations(
ResultsCollector $resultsCollector,
Expand Down Expand Up @@ -128,6 +144,18 @@ public static function metricsProvider(): iterable
];
}

public function test_it_logs_correctly_with_ci_project_dir(): void
{
\Safe\putenv('CI_PROJECT_DIR=/my/project/dir');
self::$pathPrefix = '/my/project/dir/';

$resultsCollector = self::createCompleteResultsCollector();

$logger = new GitLabCodeQualityLogger($resultsCollector);

$this->assertStringContainsString('"path":"foo\/bar"', $logger->getLogLines()[0]);
}

private function assertLoggedContentIs(array $expectedJson, GitLabCodeQualityLogger $logger): void
{
$this->assertSame($expectedJson, json_decode($logger->getLogLines()[0], true, JSON_THROW_ON_ERROR));
Expand Down