From b0a8e3c0c5fd52097adc8627191e541e1e6c343b Mon Sep 17 00:00:00 2001 From: darthf1 <17253332+darthf1@users.noreply.github.com> Date: Sun, 24 Mar 2024 15:30:39 +0100 Subject: [PATCH] feat: use CI variables to detect project path --- src/Logger/GitHubAnnotationsLogger.php | 6 +++- src/Logger/GitLabCodeQualityLogger.php | 6 +++- .../Logger/CreateMetricsCalculator.php | 4 ++- .../Logger/GitHubAnnotationsLoggerTest.php | 28 +++++++++++++++++++ .../Logger/GitLabCodeQualityLoggerTest.php | 28 +++++++++++++++++++ 5 files changed, 69 insertions(+), 3 deletions(-) diff --git a/src/Logger/GitHubAnnotationsLogger.php b/src/Logger/GitHubAnnotationsLogger.php index bd8d24cfd..b3358e0f1 100644 --- a/src/Logger/GitHubAnnotationsLogger.php +++ b/src/Logger/GitHubAnnotationsLogger.php @@ -35,6 +35,7 @@ namespace Infection\Logger; +use function getenv; use Infection\Metrics\ResultsCollector; use function Safe\shell_exec; use function str_replace; @@ -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) { + $projectRootDirectory = trim(shell_exec('git rev-parse --show-toplevel')); + } foreach ($this->resultsCollector->getEscapedExecutionResults() as $escapedExecutionResult) { $error = [ diff --git a/src/Logger/GitLabCodeQualityLogger.php b/src/Logger/GitLabCodeQualityLogger.php index c03bcb61a..6eeb1b5b5 100644 --- a/src/Logger/GitLabCodeQualityLogger.php +++ b/src/Logger/GitLabCodeQualityLogger.php @@ -35,6 +35,7 @@ namespace Infection\Logger; +use function getenv; use Infection\Metrics\ResultsCollector; use Infection\Str; use function json_encode; @@ -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')); + } foreach ($this->resultsCollector->getEscapedExecutionResults() as $escapedExecutionResult) { $lines[] = [ diff --git a/tests/phpunit/Logger/CreateMetricsCalculator.php b/tests/phpunit/Logger/CreateMetricsCalculator.php index b480ad1b2..40b30f8ea 100644 --- a/tests/phpunit/Logger/CreateMetricsCalculator.php +++ b/tests/phpunit/Logger/CreateMetricsCalculator.php @@ -48,6 +48,8 @@ trait CreateMetricsCalculator { + private static $pathPrefix = ''; + private static function createCompleteMetricsCalculator(): MetricsCalculator { $calculator = new MetricsCalculator(2); @@ -191,7 +193,7 @@ private static function createMutantExecutionResult( )), 'a1b2c3', MutatorName::getName($mutatorClassName), - 'foo/bar', + self::$pathPrefix . 'foo/bar', 10 - $i, 20 - $i, 10 - $i, diff --git a/tests/phpunit/Logger/GitHubAnnotationsLoggerTest.php b/tests/phpunit/Logger/GitHubAnnotationsLoggerTest.php index cd840f314..a2f2aed3a 100644 --- a/tests/phpunit/Logger/GitHubAnnotationsLoggerTest.php +++ b/tests/phpunit/Logger/GitHubAnnotationsLoggerTest.php @@ -37,6 +37,7 @@ 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; @@ -44,8 +45,23 @@ #[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, @@ -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]); + } } diff --git a/tests/phpunit/Logger/GitLabCodeQualityLoggerTest.php b/tests/phpunit/Logger/GitLabCodeQualityLoggerTest.php index efb588325..7d971f0d4 100644 --- a/tests/phpunit/Logger/GitLabCodeQualityLoggerTest.php +++ b/tests/phpunit/Logger/GitLabCodeQualityLoggerTest.php @@ -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; @@ -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, @@ -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));