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..8952be8c8 100644 --- a/tests/phpunit/Logger/CreateMetricsCalculator.php +++ b/tests/phpunit/Logger/CreateMetricsCalculator.php @@ -48,6 +48,8 @@ trait CreateMetricsCalculator { + private static $originalFilePrefix = ''; + private static function createCompleteMetricsCalculator(): MetricsCalculator { $calculator = new MetricsCalculator(2); @@ -191,7 +193,7 @@ private static function createMutantExecutionResult( )), 'a1b2c3', MutatorName::getName($mutatorClassName), - 'foo/bar', + self::$originalFilePrefix . 'foo/bar', 10 - $i, 20 - $i, 10 - $i, @@ -201,4 +203,14 @@ private static function createMutantExecutionResult( [], ); } + + private static function setOriginalFilePrefix(string $pathPrefix): void + { + self::$originalFilePrefix = $pathPrefix; + } + + private static function resetOriginalFilePrefix(): void + { + self::$originalFilePrefix = ''; + } } diff --git a/tests/phpunit/Logger/GitHubAnnotationsLoggerTest.php b/tests/phpunit/Logger/GitHubAnnotationsLoggerTest.php index 6d74e94c1..4d044a47d 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\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; @@ -46,8 +47,24 @@ #[CoversClass(GitHubAnnotationsLogger::class)] 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(); + self::resetOriginalFilePrefix(); + } + #[DataProvider('metricsProvider')] public function test_it_logs_correctly_with_mutations( ResultsCollector $resultsCollector, @@ -73,4 +90,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::setOriginalFilePrefix('/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 0b9ecb05d..c2f9162a5 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\CoversClass; @@ -53,8 +54,24 @@ #[CoversClass(GitLabCodeQualityLogger::class)] 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(); + self::resetOriginalFilePrefix(); + } + #[DataProvider('metricsProvider')] public function test_it_logs_correctly_with_mutations( ResultsCollector $resultsCollector, @@ -130,6 +147,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::setOriginalFilePrefix('/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));