From 7ef2cae325a7e624683a2571c490ba44f9f3c0d4 Mon Sep 17 00:00:00 2001 From: Markus Podar Date: Tue, 11 Jan 2022 22:13:32 +0100 Subject: [PATCH 01/15] Auto-detect Github Actions CI and activate github logger accordingly Fixes #1560 --- src/Configuration/ConfigurationFactory.php | 20 +++ .../ConfigurationFactoryTest.php | 137 +++++++++++++++++- tests/phpunit/Fixtures/DummyCiDetector.php | 13 +- 3 files changed, 164 insertions(+), 6 deletions(-) diff --git a/src/Configuration/ConfigurationFactory.php b/src/Configuration/ConfigurationFactory.php index a6909d71d..4928f13b5 100644 --- a/src/Configuration/ConfigurationFactory.php +++ b/src/Configuration/ConfigurationFactory.php @@ -52,6 +52,7 @@ use Infection\Mutator\MutatorParser; use Infection\Mutator\MutatorResolver; use Infection\TestFramework\TestFrameworkTypes; +use OndraM\CiDetector\Exception\CiNotDetectedException; use OndraM\CiDetector\CiDetectorInterface; use function Safe\sprintf; use function sys_get_temp_dir; @@ -318,6 +319,10 @@ private function retrieveFilter(string $filter, ?string $gitDiffFilter, bool $is private function retrieveLogs(Logs $logs, bool $useGitHubLogger, ?string $htmlLogFilePath): Logs { + if ($useGitHubLogger === false) { + $useGitHubLogger = $this->detectCiGithubActions(); + } + if ($useGitHubLogger) { $logs->setUseGitHubAnnotationsLogger($useGitHubLogger); } @@ -328,4 +333,19 @@ private function retrieveLogs(Logs $logs, bool $useGitHubLogger, ?string $htmlLo return $logs; } + + private function detectCiGithubActions(): bool + { + try { + $ci = $this->ciDetector->detect(); + + if ($ci->getCiName() === CiDetector::CI_GITHUB_ACTIONS) { + return true; + } + } catch (CiNotDetectedException $e) { + // deliberately empty + } + + return false; + } } diff --git a/tests/phpunit/Configuration/ConfigurationFactoryTest.php b/tests/phpunit/Configuration/ConfigurationFactoryTest.php index a5810cf38..0aa99a815 100644 --- a/tests/phpunit/Configuration/ConfigurationFactoryTest.php +++ b/tests/phpunit/Configuration/ConfigurationFactoryTest.php @@ -91,6 +91,7 @@ public static function tearDownAfterClass(): void */ public function test_it_can_create_a_configuration( bool $ciDetected, + bool $githubActionsDetected, SchemaConfiguration $schema, ?string $inputExistingCoveragePath, ?string $inputInitialTestsPhpOptions, @@ -144,7 +145,7 @@ public function test_it_can_create_a_configuration( bool $inputExecuteOnlyCoveringTestCases ): void { $config = $this - ->createConfigurationFactory($ciDetected) + ->createConfigurationFactory($ciDetected, $githubActionsDetected) ->create( $schema, $inputExistingCoveragePath, @@ -217,6 +218,7 @@ public function valueProvider(): iterable $expectedLogs->setUseGitHubAnnotationsLogger(true); yield 'minimal' => [ + false, false, new SchemaConfiguration( '/path/to/infection.json', @@ -397,6 +399,30 @@ public function valueProvider(): iterable true ); + yield 'no Github Actions annotation logged in non-Github Actions environment' => self::createValueForGithubActionsDetected( + false, + false, + false + ); + + yield 'no Github Actions annotation logged in Github Actions environment' => self::createValueForGithubActionsDetected( + false, + true, + true + ); + + yield 'Github Actions annotation logged in non-Github Actions environment' => self::createValueForGithubActionsDetected( + true, + false, + true + ); + + yield 'Github Actions annotation logged in Github Actions environment' => self::createValueForGithubActionsDetected( + true, + true, + true + ); + yield 'ignoreMsiWithNoMutations not specified in schema and not specified in input' => self::createValueForIgnoreMsiWithNoMutations( null, null, @@ -686,6 +712,7 @@ public function valueProvider(): iterable ); yield 'with source files' => [ + false, false, new SchemaConfiguration( '/path/to/infection.json', @@ -759,6 +786,7 @@ public function valueProvider(): iterable ]; yield 'complete' => [ + false, false, new SchemaConfiguration( '/path/to/infection.json', @@ -865,6 +893,7 @@ private static function createValueForTimeout( int $expectedTimeOut ): array { return [ + false, false, new SchemaConfiguration( '/path/to/infection.json', @@ -940,6 +969,7 @@ private static function createValueForTmpDir( ?string $expectedTmpDir ): array { return [ + false, false, new SchemaConfiguration( '/path/to/infection.json', @@ -1016,6 +1046,7 @@ private static function createValueForCoveragePath( string $expectedCoveragePath ): array { return [ + false, false, new SchemaConfiguration( '/path/to/infection.json', @@ -1091,6 +1122,7 @@ private static function createValueForPhpUnitConfigDir( ?string $expectedPhpUnitConfigDir ): array { return [ + false, false, new SchemaConfiguration( '/path/to/infection.json', @@ -1168,6 +1200,7 @@ private static function createValueForNoProgress( ): array { return [ $ciDetected, + false, new SchemaConfiguration( '/path/to/infection.json', null, @@ -1237,12 +1270,101 @@ private static function createValueForNoProgress( ]; } + private static function createValueForGithubActionsDetected( + bool $inputUseGitHubAnnotationsLogger, + bool $githubActionsDetected, + bool $useGitHubAnnotationsLogger + ): array { + $expectedLogs = new Logs( + null, + null, + null, + null, + null, + null, + $useGitHubAnnotationsLogger, + null, + ); + + return [ + false, + $githubActionsDetected, + new SchemaConfiguration( + '/path/to/infection.json', + null, + new Source([], []), + Logs::createEmpty(), + '', + new PhpUnit(null, null), + null, + null, + null, + [], + null, + null, + null, + null + ), + null, + null, + false, + 'none', + false, + false, + false, + false, + null, + false, + null, + '', + null, + null, + '', + 0, + false, + null, + false, + 'master', + $inputUseGitHubAnnotationsLogger, + null, + false, + 2, + 10, + [], + [], + '', + [], + $expectedLogs, + 'none', + sys_get_temp_dir() . '/infection', + new PhpUnit('/path/to', null), + self::getDefaultMutators(), + 'phpunit', + null, + null, + false, + '', + sys_get_temp_dir() . '/infection', + false, + false, + false, + false, + false, + null, + false, + null, + [], + false, + ]; + } + private static function createValueForIgnoreMsiWithNoMutations( ?bool $ignoreMsiWithNoMutationsFromSchemaConfiguration, ?bool $ignoreMsiWithNoMutationsFromInput, ?bool $expectedIgnoreMsiWithNoMutations ): array { return [ + false, false, new SchemaConfiguration( '/path/to/infection.json', @@ -1319,6 +1441,7 @@ private static function createValueForMinMsi( ?float $expectedMinMsi ): array { return [ + false, false, new SchemaConfiguration( '/path/to/infection.json', @@ -1395,6 +1518,7 @@ private static function createValueForMinCoveredMsi( ?float $expectedMinCoveredMsi ): array { return [ + false, false, new SchemaConfiguration( '/path/to/infection.json', @@ -1472,6 +1596,7 @@ private static function createValueForTestFramework( string $expectedTestFrameworkExtraOptions ): array { return [ + false, false, new SchemaConfiguration( '/path/to/infection.json', @@ -1548,6 +1673,7 @@ private static function createValueForInitialTestsPhpOptions( ?string $expectedInitialTestPhpOptions ): array { return [ + false, false, new SchemaConfiguration( '/path/to/infection.json', @@ -1625,6 +1751,7 @@ private static function createValueForTestFrameworkExtraOptions( string $expectedTestFrameworkExtraOptions ): array { return [ + false, false, new SchemaConfiguration( '/path/to/infection.json', @@ -1701,6 +1828,7 @@ private static function createValueForTestFrameworkKey( string $expectedTestFrameworkExtraOptions ): array { return [ + false, false, new SchemaConfiguration( '/path/to/infection.json', @@ -1781,6 +1909,7 @@ private static function createValueForMutators( array $expectedMutators ): array { return [ + false, false, new SchemaConfiguration( '/path/to/infection.json', @@ -1860,6 +1989,7 @@ private static function createValueForIgnoreSourceCodeByRegex( array $expectedIgnoreSourceCodeMutatorsMap ): array { return [ + false, false, new SchemaConfiguration( '/path/to/infection.json', @@ -1946,6 +2076,7 @@ private static function createValueForHtmlLogFilePath(?string $htmlFileLogPathIn ); return [ + false, false, new SchemaConfiguration( '/path/to/infection.json', @@ -2045,7 +2176,7 @@ private static function getDefaultMutators(): array return self::$mutators; } - private function createConfigurationFactory(bool $ciDetected): ConfigurationFactory + private function createConfigurationFactory(bool $ciDetected, bool $githubActionsDetected): ConfigurationFactory { /** @var SourceFileCollector&ObjectProphecy $sourceFilesCollectorProphecy */ $sourceFilesCollectorProphecy = $this->prophesize(SourceFileCollector::class); @@ -2071,7 +2202,7 @@ private function createConfigurationFactory(bool $ciDetected): ConfigurationFact SingletonContainer::getContainer()->getMutatorFactory(), new MutatorParser(), $sourceFilesCollectorProphecy->reveal(), - new DummyCiDetector($ciDetected), + new DummyCiDetector($ciDetected, $githubActionsDetected), $gitDiffFilesProviderMock ); } diff --git a/tests/phpunit/Fixtures/DummyCiDetector.php b/tests/phpunit/Fixtures/DummyCiDetector.php index 6f21edcab..a34bb07c1 100644 --- a/tests/phpunit/Fixtures/DummyCiDetector.php +++ b/tests/phpunit/Fixtures/DummyCiDetector.php @@ -6,17 +6,20 @@ use Infection\Tests\UnsupportedMethod; use OndraM\CiDetector\Ci\CiInterface; -use OndraM\CiDetector\CiDetector; +use OndraM\CiDetector\Ci\GitHubActions; use OndraM\CiDetector\CiDetectorInterface; use OndraM\CiDetector\Env; +use OndraM\CiDetector\Exception\CiNotDetectedException; final class DummyCiDetector implements CiDetectorInterface { private bool $ciDetected; + private bool $githubActionsDetected; - public function __construct(bool $ciDetected) + public function __construct(bool $ciDetected, bool $githubActionsDetected = false) { $this->ciDetected = $ciDetected; + $this->githubActionsDetected = $githubActionsDetected; } public static function fromEnvironment(Env $environment): CiDetector @@ -31,6 +34,10 @@ public function isCiDetected(): bool public function detect(): CiInterface { - throw UnsupportedMethod::method(__CLASS__, __FUNCTION__); + if ($this->githubActionsDetected) { + return new GitHubActions(new Env()); + } + + throw new CiNotDetectedException('No CI server detected in current environment'); } } From 39ec5c1f1f8b3ae89b5e85fab73c17822e1fcb2c Mon Sep 17 00:00:00 2001 From: Markus Podar Date: Wed, 12 Jan 2022 07:31:15 +0100 Subject: [PATCH 02/15] Remove explicit --logger-github flag, not that it is auto-detected in CI --- .github/workflows/mt-annotations.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/mt-annotations.yaml b/.github/workflows/mt-annotations.yaml index 9958b3609..96fd35d64 100644 --- a/.github/workflows/mt-annotations.yaml +++ b/.github/workflows/mt-annotations.yaml @@ -49,4 +49,4 @@ jobs: - name: Run Infection for added files only run: | git fetch origin $GITHUB_BASE_REF - php bin/infection -j2 --git-diff-lines --git-diff-base=origin/$GITHUB_BASE_REF --logger-github --ignore-msi-with-no-mutations --only-covered + php bin/infection -j2 --git-diff-lines --git-diff-base=origin/$GITHUB_BASE_REF --ignore-msi-with-no-mutations --only-covered From 355643d06c63d2d7f7f77178ab7c7d4062d33354 Mon Sep 17 00:00:00 2001 From: Markus Podar Date: Wed, 12 Jan 2022 07:32:26 +0100 Subject: [PATCH 03/15] Lower min-msi so it passes See feedback in https://github.com/infection/infection/pull/1645#issuecomment-1010649989 --- .github/workflows/mt.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/mt.yaml b/.github/workflows/mt.yaml index cd235fd2d..f7a5e44db 100644 --- a/.github/workflows/mt.yaml +++ b/.github/workflows/mt.yaml @@ -11,7 +11,7 @@ on: - master env: - MIN_MSI: 71.05 + MIN_MSI: 71.87 MIN_COVERED_MSI: 86.78 jobs: From 62e55f863a487085f698a04969778fa515b72108 Mon Sep 17 00:00:00 2001 From: Markus Podar Date: Wed, 12 Jan 2022 07:37:47 +0100 Subject: [PATCH 04/15] Add doc to option itself that it's auto-detected --- src/Command/RunCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Command/RunCommand.php b/src/Command/RunCommand.php index 3f2ec9481..77ea932a9 100644 --- a/src/Command/RunCommand.php +++ b/src/Command/RunCommand.php @@ -265,7 +265,7 @@ protected function configure(): void self::OPTION_LOGGER_GITHUB, null, InputOption::VALUE_NONE, - 'Log escaped Mutants as GitHub Annotations.', + 'Log escaped Mutants as GitHub Annotations (automatically detected on Github Actions itself).', ) ->addOption( self::OPTION_LOGGER_HTML, From 1dcd2f18a1609759f2a62b1573a1b68e213719ca Mon Sep 17 00:00:00 2001 From: maks-rafalko Date: Sun, 15 May 2022 12:31:06 +0300 Subject: [PATCH 05/15] Add `--logger-github=false` to E2E tests --- tests/phpunit/Console/E2ETest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/phpunit/Console/E2ETest.php b/tests/phpunit/Console/E2ETest.php index 1a5b1e2bf..8ecdd681d 100644 --- a/tests/phpunit/Console/E2ETest.php +++ b/tests/phpunit/Console/E2ETest.php @@ -349,6 +349,7 @@ private function runInfection(int $expectedExitCode, array $argvExtra = []): str 'run', '--verbose', '--no-interaction', + '--logger-github=false', ], $argvExtra)); $output = new BufferedOutput(); From be53a5b366b97b3702e2b34fded9316e85bb69f6 Mon Sep 17 00:00:00 2001 From: maks-rafalko Date: Sun, 15 May 2022 12:36:36 +0300 Subject: [PATCH 06/15] Import CiDetector class for const usage --- src/Configuration/ConfigurationFactory.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Configuration/ConfigurationFactory.php b/src/Configuration/ConfigurationFactory.php index 4928f13b5..a3d9733f9 100644 --- a/src/Configuration/ConfigurationFactory.php +++ b/src/Configuration/ConfigurationFactory.php @@ -52,8 +52,9 @@ use Infection\Mutator\MutatorParser; use Infection\Mutator\MutatorResolver; use Infection\TestFramework\TestFrameworkTypes; -use OndraM\CiDetector\Exception\CiNotDetectedException; +use OndraM\CiDetector\CiDetector; use OndraM\CiDetector\CiDetectorInterface; +use OndraM\CiDetector\Exception\CiNotDetectedException; use function Safe\sprintf; use function sys_get_temp_dir; use Webmozart\Assert\Assert; From bcf71ec217883ceaa7db6de82de19fed369fcf21 Mon Sep 17 00:00:00 2001 From: maks-rafalko Date: Sun, 15 May 2022 12:49:44 +0300 Subject: [PATCH 07/15] Restore accidently removed commits --- src/Command/RunCommand.php | 25 ++++++++++++++-- src/Configuration/ConfigurationFactory.php | 14 ++++----- src/Container.php | 4 +-- .../ConfigurationFactoryTest.php | 30 +++++++++++-------- 4 files changed, 47 insertions(+), 26 deletions(-) diff --git a/src/Command/RunCommand.php b/src/Command/RunCommand.php index 77ea932a9..fb08511bd 100644 --- a/src/Command/RunCommand.php +++ b/src/Command/RunCommand.php @@ -264,8 +264,9 @@ protected function configure(): void ->addOption( self::OPTION_LOGGER_GITHUB, null, - InputOption::VALUE_NONE, - 'Log escaped Mutants as GitHub Annotations (automatically detected on Github Actions itself).', + InputOption::VALUE_OPTIONAL, + 'Log escaped Mutants as GitHub Annotations (automatically detected on Github Actions itself, use true to force-enable or false to force-disable it).', + false ) ->addOption( self::OPTION_LOGGER_HTML, @@ -438,6 +439,24 @@ private function createContainer(IO $io, LoggerInterface $logger): Container ); } + $useGitHubLogger = $input->getOption(self::OPTION_LOGGER_GITHUB); + // `false` means the option was not provided at all -> user does not care and it will be auto-detected + // `null` means the option was provided without any argument -> use wants to enable it + // any string: the argument provided, but only `'true'` and `'false` are supported + if ($useGitHubLogger === false) { + $useGitHubLogger = null; + } elseif ($useGitHubLogger === null) { + $useGitHubLogger = true; + } elseif ($useGitHubLogger === 'true') { + $useGitHubLogger = true; + } elseif ($useGitHubLogger === 'false') { + $useGitHubLogger = false; + } else { + throw new InvalidArgumentException( + sprintf('Cannot pass "%s" to "--%s": only "true", "false" or no argument is supported', $useGitHubLogger, self::OPTION_LOGGER_GITHUB) + ); + } + return $this->getApplication()->getContainer()->withValues( $logger, $io->getOutput(), @@ -482,7 +501,7 @@ private function createContainer(IO $io, LoggerInterface $logger): Container $gitDiffFilter, $isForGitDiffLines, $gitDiffBase, - (bool) $input->getOption(self::OPTION_LOGGER_GITHUB), + $useGitHubLogger, $htmlFileLogPath === '' ? Container::DEFAULT_HTML_LOGGER_PATH : $htmlFileLogPath, (bool) $input->getOption(self::OPTION_USE_NOOP_MUTATORS), (bool) $input->getOption(self::OPTION_EXECUTE_ONLY_COVERING_TEST_CASES) diff --git a/src/Configuration/ConfigurationFactory.php b/src/Configuration/ConfigurationFactory.php index a3d9733f9..c185d03e5 100644 --- a/src/Configuration/ConfigurationFactory.php +++ b/src/Configuration/ConfigurationFactory.php @@ -120,7 +120,7 @@ public function create( ?string $gitDiffFilter, bool $isForGitDiffLines, ?string $gitDiffBase, - bool $useGitHubLogger, + ?bool $useGitHubLogger, ?string $htmlLogFilePath, bool $useNoopMutators, bool $executeOnlyCoveringTestCases @@ -318,9 +318,9 @@ private function retrieveFilter(string $filter, ?string $gitDiffFilter, bool $is return $this->gitDiffFileProvider->provide($gitDiffFilter, $baseBranch); } - private function retrieveLogs(Logs $logs, bool $useGitHubLogger, ?string $htmlLogFilePath): Logs + private function retrieveLogs(Logs $logs, ?bool $useGitHubLogger, ?string $htmlLogFilePath): Logs { - if ($useGitHubLogger === false) { + if ($useGitHubLogger === null) { $useGitHubLogger = $this->detectCiGithubActions(); } @@ -339,14 +339,10 @@ private function detectCiGithubActions(): bool { try { $ci = $this->ciDetector->detect(); - - if ($ci->getCiName() === CiDetector::CI_GITHUB_ACTIONS) { - return true; - } } catch (CiNotDetectedException $e) { - // deliberately empty + return false; } - return false; + return $ci->getCiName() !== CiDetector::CI_GITHUB_ACTIONS; } } diff --git a/src/Container.php b/src/Container.php index f4d4ab69c..541a9a0cd 100644 --- a/src/Container.php +++ b/src/Container.php @@ -169,7 +169,7 @@ final class Container public const DEFAULT_GIT_DIFF_FILTER = null; public const DEFAULT_GIT_DIFF_LINES = false; public const DEFAULT_GIT_DIFF_BASE = null; - public const DEFAULT_USE_GITHUB_LOGGER = false; + public const DEFAULT_USE_GITHUB_LOGGER = null; public const DEFAULT_HTML_LOGGER_PATH = null; public const DEFAULT_USE_NOOP_MUTATORS = false; public const DEFAULT_EXECUTE_ONLY_COVERING_TEST_CASES = false; @@ -610,7 +610,7 @@ public function withValues( ?string $gitDiffFilter, bool $isForGitDiffLines, ?string $gitDiffBase, - bool $useGitHubLogger, + ?bool $useGitHubLogger, ?string $htmlLogFilePath, bool $useNoopMutators, bool $executeOnlyCoveringTestCases diff --git a/tests/phpunit/Configuration/ConfigurationFactoryTest.php b/tests/phpunit/Configuration/ConfigurationFactoryTest.php index 0aa99a815..44869725d 100644 --- a/tests/phpunit/Configuration/ConfigurationFactoryTest.php +++ b/tests/phpunit/Configuration/ConfigurationFactoryTest.php @@ -113,7 +113,7 @@ public function test_it_can_create_a_configuration( ?string $inputGitDiffFilter, bool $inputIsForGitDiffLines, string $inputGitDiffBase, - bool $inputUseGitHubAnnotationsLogger, + ?bool $inputUseGitHubAnnotationsLogger, ?string $inputHtmlLogFilePath, bool $inputUseNoopMutators, int $inputMsiPrecision, @@ -399,36 +399,42 @@ public function valueProvider(): iterable true ); - yield 'no Github Actions annotation logged in non-Github Actions environment' => self::createValueForGithubActionsDetected( + yield 'Github Actions annotation disabled, not logged in non-Github Actions environment' => self::createValueForGithubActionsDetected( false, false, false ); - yield 'no Github Actions annotation logged in Github Actions environment' => self::createValueForGithubActionsDetected( + yield 'Github Actions annotation disabled, not logged in Github Actions environment' => self::createValueForGithubActionsDetected( false, true, + false + ); + + yield 'Github Actions annotation not provided, not logged in non-Github Actions environment' => self::createValueForGithubActionsDetected( + null, + false, + false + ); + + yield 'Github Actions annotation not provided, logged in Github Actions environment' => self::createValueForGithubActionsDetected( + null, + true, true ); - yield 'Github Actions annotation logged in non-Github Actions environment' => self::createValueForGithubActionsDetected( + yield 'Github Actions annotation enabled, logged in non-Github Actions environment' => self::createValueForGithubActionsDetected( true, false, true ); - yield 'Github Actions annotation logged in Github Actions environment' => self::createValueForGithubActionsDetected( + yield 'Github Actions annotation enabled, logged in Github Actions environment' => self::createValueForGithubActionsDetected( true, true, true ); - yield 'ignoreMsiWithNoMutations not specified in schema and not specified in input' => self::createValueForIgnoreMsiWithNoMutations( - null, - null, - false - ); - yield 'ignoreMsiWithNoMutations not specified in schema and true in input' => self::createValueForIgnoreMsiWithNoMutations( null, true, @@ -1271,7 +1277,7 @@ private static function createValueForNoProgress( } private static function createValueForGithubActionsDetected( - bool $inputUseGitHubAnnotationsLogger, + ?bool $inputUseGitHubAnnotationsLogger, bool $githubActionsDetected, bool $useGitHubAnnotationsLogger ): array { From d1ddbda4e362f5c6af4f356c8f18f0c8b00a5f75 Mon Sep 17 00:00:00 2001 From: maks-rafalko Date: Sun, 15 May 2022 13:10:08 +0300 Subject: [PATCH 08/15] Fix incorrect condition after refactoring, fix tests --- src/Command/RunCommand.php | 2 +- src/Configuration/ConfigurationFactory.php | 2 +- tests/phpunit/Configuration/Entry/LogsAssertions.php | 2 +- tests/phpunit/Fixtures/DummyCiDetector.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Command/RunCommand.php b/src/Command/RunCommand.php index fb08511bd..07a1049ca 100644 --- a/src/Command/RunCommand.php +++ b/src/Command/RunCommand.php @@ -441,7 +441,7 @@ private function createContainer(IO $io, LoggerInterface $logger): Container $useGitHubLogger = $input->getOption(self::OPTION_LOGGER_GITHUB); // `false` means the option was not provided at all -> user does not care and it will be auto-detected - // `null` means the option was provided without any argument -> use wants to enable it + // `null` means the option was provided without any argument -> user wants to enable it // any string: the argument provided, but only `'true'` and `'false` are supported if ($useGitHubLogger === false) { $useGitHubLogger = null; diff --git a/src/Configuration/ConfigurationFactory.php b/src/Configuration/ConfigurationFactory.php index c185d03e5..c5ce31b28 100644 --- a/src/Configuration/ConfigurationFactory.php +++ b/src/Configuration/ConfigurationFactory.php @@ -343,6 +343,6 @@ private function detectCiGithubActions(): bool return false; } - return $ci->getCiName() !== CiDetector::CI_GITHUB_ACTIONS; + return $ci->getCiName() === CiDetector::CI_GITHUB_ACTIONS; } } diff --git a/tests/phpunit/Configuration/Entry/LogsAssertions.php b/tests/phpunit/Configuration/Entry/LogsAssertions.php index c4ccd7b70..549cfe05b 100644 --- a/tests/phpunit/Configuration/Entry/LogsAssertions.php +++ b/tests/phpunit/Configuration/Entry/LogsAssertions.php @@ -57,7 +57,7 @@ private function assertLogsStateIs( $this->assertSame($expectedJsonLogFilePath, $logs->getJsonLogFilePath()); $this->assertSame($expectedDebugLogFilePath, $logs->getDebugLogFilePath()); $this->assertSame($expectedPerMutatorFilePath, $logs->getPerMutatorFilePath()); - $this->assertSame($expectedUseGitHubAnnotationsLogger, $logs->getUseGitHubAnnotationsLogger()); + $this->assertSame($expectedUseGitHubAnnotationsLogger, $logs->getUseGitHubAnnotationsLogger(), 'Use GithubAnnotationLogger is incorrect'); $strykerConfig = $logs->getStrykerConfig(); diff --git a/tests/phpunit/Fixtures/DummyCiDetector.php b/tests/phpunit/Fixtures/DummyCiDetector.php index a34bb07c1..603d29219 100644 --- a/tests/phpunit/Fixtures/DummyCiDetector.php +++ b/tests/phpunit/Fixtures/DummyCiDetector.php @@ -22,7 +22,7 @@ public function __construct(bool $ciDetected, bool $githubActionsDetected = fals $this->githubActionsDetected = $githubActionsDetected; } - public static function fromEnvironment(Env $environment): CiDetector + public static function fromEnvironment(Env $environment): CiDetectorInterface { throw UnsupportedMethod::method(__CLASS__, __FUNCTION__); } From 6be5dfa32a7b21d8b7735c11945bc9c5a32eaa00 Mon Sep 17 00:00:00 2001 From: maks-rafalko Date: Sun, 15 May 2022 13:17:59 +0300 Subject: [PATCH 09/15] Do not use deprecated `Safe\sprintf` --- src/Command/RunCommand.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Command/RunCommand.php b/src/Command/RunCommand.php index 07a1049ca..21559c2a5 100644 --- a/src/Command/RunCommand.php +++ b/src/Command/RunCommand.php @@ -61,7 +61,7 @@ use InvalidArgumentException; use const PHP_SAPI; use Psr\Log\LoggerInterface; -use function Safe\sprintf; +use function sprintf; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputOption; use function trim; @@ -452,9 +452,11 @@ private function createContainer(IO $io, LoggerInterface $logger): Container } elseif ($useGitHubLogger === 'false') { $useGitHubLogger = false; } else { - throw new InvalidArgumentException( - sprintf('Cannot pass "%s" to "--%s": only "true", "false" or no argument is supported', $useGitHubLogger, self::OPTION_LOGGER_GITHUB) - ); + throw new InvalidArgumentException(sprintf( + 'Cannot pass "%s" to "--%s": only "true", "false" or no argument is supported', + $useGitHubLogger, + self::OPTION_LOGGER_GITHUB + )); } return $this->getApplication()->getContainer()->withValues( From cf13f8585c4357df5ccba801c750651c3af7e71a Mon Sep 17 00:00:00 2001 From: maks-rafalko Date: Sun, 15 May 2022 13:36:08 +0300 Subject: [PATCH 10/15] Decrease MSI --- .github/workflows/mt.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/mt.yaml b/.github/workflows/mt.yaml index f7a5e44db..a3c1726ff 100644 --- a/.github/workflows/mt.yaml +++ b/.github/workflows/mt.yaml @@ -11,7 +11,7 @@ on: - master env: - MIN_MSI: 71.87 + MIN_MSI: 71.39 MIN_COVERED_MSI: 86.78 jobs: From 6ddc69a1fe00f6c96317917494e933a1467394f8 Mon Sep 17 00:00:00 2001 From: maks-rafalko Date: Sun, 15 May 2022 13:39:53 +0300 Subject: [PATCH 11/15] Try to catch all types of exception, including "sh: 1: git: not found" --- src/Configuration/ConfigurationFactory.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Configuration/ConfigurationFactory.php b/src/Configuration/ConfigurationFactory.php index c5ce31b28..fcc48f8a3 100644 --- a/src/Configuration/ConfigurationFactory.php +++ b/src/Configuration/ConfigurationFactory.php @@ -40,6 +40,7 @@ use function array_unique; use function array_values; use function dirname; +use Exception; use Infection\Configuration\Entry\Logs; use Infection\Configuration\Entry\PhpUnit; use Infection\Configuration\Schema\SchemaConfiguration; @@ -54,7 +55,6 @@ use Infection\TestFramework\TestFrameworkTypes; use OndraM\CiDetector\CiDetector; use OndraM\CiDetector\CiDetectorInterface; -use OndraM\CiDetector\Exception\CiNotDetectedException; use function Safe\sprintf; use function sys_get_temp_dir; use Webmozart\Assert\Assert; @@ -339,7 +339,7 @@ private function detectCiGithubActions(): bool { try { $ci = $this->ciDetector->detect(); - } catch (CiNotDetectedException $e) { + } catch (Exception $e) { return false; } From 8ed290cdb9e8d97d60391816e816b487f674db1c Mon Sep 17 00:00:00 2001 From: maks-rafalko Date: Sun, 15 May 2022 18:00:22 +0300 Subject: [PATCH 12/15] Simplify code, remove nested `elseif`. Disable GH logger for e2e tests --- src/Command/RunCommand.php | 58 ++++++++++++++-------- src/Configuration/ConfigurationFactory.php | 4 +- tests/e2e_tests | 2 + 3 files changed, 41 insertions(+), 23 deletions(-) diff --git a/src/Command/RunCommand.php b/src/Command/RunCommand.php index 21559c2a5..260130b32 100644 --- a/src/Command/RunCommand.php +++ b/src/Command/RunCommand.php @@ -37,6 +37,7 @@ use function extension_loaded; use function file_exists; +use function getenv; use function implode; use Infection\Configuration\Configuration; use Infection\Configuration\Schema\SchemaConfigurationLoader; @@ -63,6 +64,7 @@ use Psr\Log\LoggerInterface; use function sprintf; use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use function trim; @@ -439,26 +441,6 @@ private function createContainer(IO $io, LoggerInterface $logger): Container ); } - $useGitHubLogger = $input->getOption(self::OPTION_LOGGER_GITHUB); - // `false` means the option was not provided at all -> user does not care and it will be auto-detected - // `null` means the option was provided without any argument -> user wants to enable it - // any string: the argument provided, but only `'true'` and `'false` are supported - if ($useGitHubLogger === false) { - $useGitHubLogger = null; - } elseif ($useGitHubLogger === null) { - $useGitHubLogger = true; - } elseif ($useGitHubLogger === 'true') { - $useGitHubLogger = true; - } elseif ($useGitHubLogger === 'false') { - $useGitHubLogger = false; - } else { - throw new InvalidArgumentException(sprintf( - 'Cannot pass "%s" to "--%s": only "true", "false" or no argument is supported', - $useGitHubLogger, - self::OPTION_LOGGER_GITHUB - )); - } - return $this->getApplication()->getContainer()->withValues( $logger, $io->getOutput(), @@ -503,7 +485,7 @@ private function createContainer(IO $io, LoggerInterface $logger): Container $gitDiffFilter, $isForGitDiffLines, $gitDiffBase, - $useGitHubLogger, + $this->getUseGitHubLogger($input), $htmlFileLogPath === '' ? Container::DEFAULT_HTML_LOGGER_PATH : $htmlFileLogPath, (bool) $input->getOption(self::OPTION_USE_NOOP_MUTATORS), (bool) $input->getOption(self::OPTION_EXECUTE_ONLY_COVERING_TEST_CASES) @@ -627,4 +609,38 @@ private function logRunningWithDebugger(ConsoleOutput $consoleOutput): void $consoleOutput->logRunningWithDebugger('PCOV'); } } + + private function getUseGitHubLogger(InputInterface $input): ?bool + { + // on e2e environment, we don't need github logger + if (getenv('INFECTION_E2E_TESTS_ENV') !== false) { + return false; + } + + $useGitHubLogger = $input->getOption(self::OPTION_LOGGER_GITHUB); + // `false` means the option was not provided at all -> user does not care and it will be auto-detected + // `null` means the option was provided without any argument -> user wants to enable it + // any string: the argument provided, but only `'true'` and `'false` are supported + if ($useGitHubLogger === false) { + return null; + } + + if ($useGitHubLogger === null) { + return true; + } + + if ($useGitHubLogger === 'true') { + return true; + } + + if ($useGitHubLogger === 'false') { + return false; + } + + throw new InvalidArgumentException(sprintf( + 'Cannot pass "%s" to "--%s": only "true", "false" or no argument is supported', + $useGitHubLogger, + self::OPTION_LOGGER_GITHUB + )); + } } diff --git a/src/Configuration/ConfigurationFactory.php b/src/Configuration/ConfigurationFactory.php index fcc48f8a3..c5ce31b28 100644 --- a/src/Configuration/ConfigurationFactory.php +++ b/src/Configuration/ConfigurationFactory.php @@ -40,7 +40,6 @@ use function array_unique; use function array_values; use function dirname; -use Exception; use Infection\Configuration\Entry\Logs; use Infection\Configuration\Entry\PhpUnit; use Infection\Configuration\Schema\SchemaConfiguration; @@ -55,6 +54,7 @@ use Infection\TestFramework\TestFrameworkTypes; use OndraM\CiDetector\CiDetector; use OndraM\CiDetector\CiDetectorInterface; +use OndraM\CiDetector\Exception\CiNotDetectedException; use function Safe\sprintf; use function sys_get_temp_dir; use Webmozart\Assert\Assert; @@ -339,7 +339,7 @@ private function detectCiGithubActions(): bool { try { $ci = $this->ciDetector->detect(); - } catch (Exception $e) { + } catch (CiNotDetectedException $e) { return false; } diff --git a/tests/e2e_tests b/tests/e2e_tests index a92a13178..3365ca6e3 100755 --- a/tests/e2e_tests +++ b/tests/e2e_tests @@ -21,6 +21,8 @@ do composer install > /dev/null 2>&1 fi + putenv('INFECTION_E2E_TESTS_ENV=1'); + if [ -f "run_tests.bash" ] then output="$(bash run_tests.bash)" From 6ce26fb1a1d50660d8213b6214ca469abeacec1a Mon Sep 17 00:00:00 2001 From: maks-rafalko Date: Sun, 15 May 2022 18:06:26 +0300 Subject: [PATCH 13/15] Set env var differently --- tests/e2e_tests | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/e2e_tests b/tests/e2e_tests index 3365ca6e3..ace36f4cc 100755 --- a/tests/e2e_tests +++ b/tests/e2e_tests @@ -10,6 +10,7 @@ then fi touch error.log +INFECTION_E2E_TESTS_ENV=1 for dir in */ do @@ -21,8 +22,6 @@ do composer install > /dev/null 2>&1 fi - putenv('INFECTION_E2E_TESTS_ENV=1'); - if [ -f "run_tests.bash" ] then output="$(bash run_tests.bash)" From 63c4963c395ccdbd8b4f93c8177fdfdc45484b3a Mon Sep 17 00:00:00 2001 From: maks-rafalko Date: Sun, 15 May 2022 18:18:07 +0300 Subject: [PATCH 14/15] Set env var differently 2 --- src/Command/RunCommand.php | 1 + tests/e2e_tests | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Command/RunCommand.php b/src/Command/RunCommand.php index 260130b32..80f11023b 100644 --- a/src/Command/RunCommand.php +++ b/src/Command/RunCommand.php @@ -60,6 +60,7 @@ use Infection\TestFramework\Coverage\XmlReport\NoLineExecutedInDiffLinesMode; use Infection\TestFramework\TestFrameworkTypes; use InvalidArgumentException; +use function var_export; use const PHP_SAPI; use Psr\Log\LoggerInterface; use function sprintf; diff --git a/tests/e2e_tests b/tests/e2e_tests index ace36f4cc..f30ec3c9e 100755 --- a/tests/e2e_tests +++ b/tests/e2e_tests @@ -10,7 +10,6 @@ then fi touch error.log -INFECTION_E2E_TESTS_ENV=1 for dir in */ do @@ -24,9 +23,9 @@ do if [ -f "run_tests.bash" ] then - output="$(bash run_tests.bash)" + output="$(INFECTION_E2E_TESTS_ENV=1 bash run_tests.bash)" else - output="$(bash ../standard_script.bash ${1:-bin/infection})" + output="$(INFECTION_E2E_TESTS_ENV=1 bash ../standard_script.bash ${1:-bin/infection})" fi if [ $? != 0 ] From 13e9b6f164ec5247e029d247bcb874394a821f99 Mon Sep 17 00:00:00 2001 From: maks-rafalko Date: Sun, 15 May 2022 18:20:21 +0300 Subject: [PATCH 15/15] Fix CS --- src/Command/RunCommand.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Command/RunCommand.php b/src/Command/RunCommand.php index 80f11023b..260130b32 100644 --- a/src/Command/RunCommand.php +++ b/src/Command/RunCommand.php @@ -60,7 +60,6 @@ use Infection\TestFramework\Coverage\XmlReport\NoLineExecutedInDiffLinesMode; use Infection\TestFramework\TestFrameworkTypes; use InvalidArgumentException; -use function var_export; use const PHP_SAPI; use Psr\Log\LoggerInterface; use function sprintf;