From 69178f91960b4e63d9338c4e5af956235a13059a Mon Sep 17 00:00:00 2001 From: darthf1 <17253332+darthf1@users.noreply.github.com> Date: Sun, 24 Mar 2024 14:06:47 +0100 Subject: [PATCH 1/4] feat: use CI variables to detect project path --- src/Logger/GitHubAnnotationsLogger.php | 6 +- src/Logger/GitLabCodeQualityLogger.php | 6 +- .../Logger/GitHubAnnotationsLoggerTest.php | 62 ++++++++++++++++ .../Logger/GitLabCodeQualityLoggerTest.php | 74 +++++++++++++++++++ 4 files changed, 146 insertions(+), 2 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/GitHubAnnotationsLoggerTest.php b/tests/phpunit/Logger/GitHubAnnotationsLoggerTest.php index cd840f314..388017f4f 100644 --- a/tests/phpunit/Logger/GitHubAnnotationsLoggerTest.php +++ b/tests/phpunit/Logger/GitHubAnnotationsLoggerTest.php @@ -37,6 +37,13 @@ use Infection\Logger\GitHubAnnotationsLogger; use Infection\Metrics\ResultsCollector; +use Infection\Mutant\DetectionStatus; +use Infection\Mutant\MutantExecutionResult; +use Infection\Mutator\Loop\For_; +use Infection\Tests\EnvVariableManipulation\BacksUpEnvironmentVariables; +use Infection\Tests\Mutator\MutatorName; +use function Infection\Tests\normalize_trailing_spaces; +use function Later\now; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\TestCase; @@ -44,8 +51,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 +93,44 @@ public static function metricsProvider(): iterable ], ]; } + + public function test_it_logs_correctly_with_ci_project_dir(): void + { + \Safe\putenv('GITHUB_WORKSPACE=/my/project/dir'); + + $resultsCollector = new ResultsCollector(); + $resultsCollector->collect( + new MutantExecutionResult( + 'bin/phpunit --configuration infection-tmp-phpunit.xml --filter "tests/Acme/FooTest.php"', + 'process output', + DetectionStatus::ESCAPED, + now(normalize_trailing_spaces( + <<assertSame([ + "::warning file=foo/bar,line=10::Escaped Mutant for Mutator \"For_\":%0A%0A--- Original%0A+++ New%0A@@ @@%0A%0A- echo 'original';%0A+ echo 'escaped#0';%0A\n", + ], $logger->getLogLines()); + } } diff --git a/tests/phpunit/Logger/GitLabCodeQualityLoggerTest.php b/tests/phpunit/Logger/GitLabCodeQualityLoggerTest.php index efb588325..101a15821 100644 --- a/tests/phpunit/Logger/GitLabCodeQualityLoggerTest.php +++ b/tests/phpunit/Logger/GitLabCodeQualityLoggerTest.php @@ -38,8 +38,13 @@ use Infection\Logger\GitLabCodeQualityLogger; use Infection\Metrics\ResultsCollector; use Infection\Mutant\DetectionStatus; +use Infection\Mutant\MutantExecutionResult; use Infection\Mutator\Loop\For_; +use Infection\Tests\EnvVariableManipulation\BacksUpEnvironmentVariables; +use Infection\Tests\Mutator\MutatorName; +use function Infection\Tests\normalize_trailing_spaces; use const JSON_THROW_ON_ERROR; +use function Later\now; use const PHP_EOL; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; @@ -51,8 +56,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 +148,60 @@ public static function metricsProvider(): iterable ]; } + public function test_it_logs_correctly_with_ci_project_dir(): void + { + \Safe\putenv('CI_PROJECT_DIR=/my/project/dir'); + + $resultsCollector = new ResultsCollector(); + $resultsCollector->collect( + new MutantExecutionResult( + 'bin/phpunit --configuration infection-tmp-phpunit.xml --filter "tests/Acme/FooTest.php"', + 'process output', + DetectionStatus::ESCAPED, + now(normalize_trailing_spaces( + <<assertLoggedContentIs([ + [ + 'type' => 'issue', + 'fingerprint' => 'a1b2c3', + 'check_name' => 'For_', + 'description' => 'Escaped Mutant for Mutator For_', + 'content' => str_replace("\n", PHP_EOL, "--- Original\n+++ New\n@@ @@\n\n- echo 'original';\n+ echo 'escaped#0';"), + 'categories' => ['Escaped Mutant'], + 'location' => [ + 'path' => 'foo/bar', + 'lines' => [ + 'begin' => 10, + ], + ], + 'severity' => 'major', + ], + ], $logger); + } + private function assertLoggedContentIs(array $expectedJson, GitLabCodeQualityLogger $logger): void { $this->assertSame($expectedJson, json_decode($logger->getLogLines()[0], true, JSON_THROW_ON_ERROR)); 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 2/4] 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)); From 4f7bd7cc0bf8453b3425d9ec7679af91fae5e8f1 Mon Sep 17 00:00:00 2001 From: Alexey Kopytko Date: Mon, 25 Mar 2024 10:35:33 +0900 Subject: [PATCH 3/4] CS --- tests/phpunit/Logger/GitLabCodeQualityLoggerTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/phpunit/Logger/GitLabCodeQualityLoggerTest.php b/tests/phpunit/Logger/GitLabCodeQualityLoggerTest.php index 7cad04704..7d971f0d4 100644 --- a/tests/phpunit/Logger/GitLabCodeQualityLoggerTest.php +++ b/tests/phpunit/Logger/GitLabCodeQualityLoggerTest.php @@ -38,11 +38,9 @@ use Infection\Logger\GitLabCodeQualityLogger; use Infection\Metrics\ResultsCollector; use Infection\Mutant\DetectionStatus; -use Infection\Mutant\MutantExecutionResult; use Infection\Mutator\Loop\For_; use Infection\Tests\EnvVariableManipulation\BacksUpEnvironmentVariables; use const JSON_THROW_ON_ERROR; -use function Later\now; use const PHP_EOL; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; From 29f54d456c0d85f48c703c59131186ce0aa6b0f3 Mon Sep 17 00:00:00 2001 From: darthf1 <17253332+darthf1@users.noreply.github.com> Date: Mon, 25 Mar 2024 09:25:36 +0100 Subject: [PATCH 4/4] refactor: rename variable and reset value after each test --- tests/phpunit/Logger/CreateMetricsCalculator.php | 14 ++++++++++++-- .../phpunit/Logger/GitHubAnnotationsLoggerTest.php | 3 ++- .../phpunit/Logger/GitLabCodeQualityLoggerTest.php | 3 ++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/Logger/CreateMetricsCalculator.php b/tests/phpunit/Logger/CreateMetricsCalculator.php index 40b30f8ea..8952be8c8 100644 --- a/tests/phpunit/Logger/CreateMetricsCalculator.php +++ b/tests/phpunit/Logger/CreateMetricsCalculator.php @@ -48,7 +48,7 @@ trait CreateMetricsCalculator { - private static $pathPrefix = ''; + private static $originalFilePrefix = ''; private static function createCompleteMetricsCalculator(): MetricsCalculator { @@ -193,7 +193,7 @@ private static function createMutantExecutionResult( )), 'a1b2c3', MutatorName::getName($mutatorClassName), - self::$pathPrefix . 'foo/bar', + self::$originalFilePrefix . 'foo/bar', 10 - $i, 20 - $i, 10 - $i, @@ -203,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 053e49cc8..4d044a47d 100644 --- a/tests/phpunit/Logger/GitHubAnnotationsLoggerTest.php +++ b/tests/phpunit/Logger/GitHubAnnotationsLoggerTest.php @@ -62,6 +62,7 @@ protected function tearDown(): void parent::tearDown(); $this->restoreEnvironmentVariables(); + self::resetOriginalFilePrefix(); } #[DataProvider('metricsProvider')] @@ -93,7 +94,7 @@ 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/'; + self::setOriginalFilePrefix('/my/project/dir/'); $resultsCollector = self::createCompleteResultsCollector(); diff --git a/tests/phpunit/Logger/GitLabCodeQualityLoggerTest.php b/tests/phpunit/Logger/GitLabCodeQualityLoggerTest.php index 5edaf44c3..c2f9162a5 100644 --- a/tests/phpunit/Logger/GitLabCodeQualityLoggerTest.php +++ b/tests/phpunit/Logger/GitLabCodeQualityLoggerTest.php @@ -69,6 +69,7 @@ protected function tearDown(): void parent::tearDown(); $this->restoreEnvironmentVariables(); + self::resetOriginalFilePrefix(); } #[DataProvider('metricsProvider')] @@ -149,7 +150,7 @@ 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/'; + self::setOriginalFilePrefix('/my/project/dir/'); $resultsCollector = self::createCompleteResultsCollector();