Skip to content

Commit

Permalink
feat: use CI variables to detect project path
Browse files Browse the repository at this point in the history
  • Loading branch information
darthf1 committed Mar 24, 2024
1 parent c7b185c commit b0a8e3c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
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 __construct(private readonly ResultsCollector $resultsCollector)
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 __construct(private readonly ResultsCollector $resultsCollector)
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/';

$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

0 comments on commit b0a8e3c

Please sign in to comment.