Skip to content

Commit

Permalink
Auto-detect Github Actions CI and activate github logger accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
mfn authored and maks-rafalko committed May 15, 2022
1 parent 134853c commit 7ef2cae
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 6 deletions.
20 changes: 20 additions & 0 deletions src/Configuration/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
}
}
137 changes: 134 additions & 3 deletions tests/phpunit/Configuration/ConfigurationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -217,6 +218,7 @@ public function valueProvider(): iterable
$expectedLogs->setUseGitHubAnnotationsLogger(true);

yield 'minimal' => [
false,
false,
new SchemaConfiguration(
'/path/to/infection.json',
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -686,6 +712,7 @@ public function valueProvider(): iterable
);

yield 'with source files' => [
false,
false,
new SchemaConfiguration(
'/path/to/infection.json',
Expand Down Expand Up @@ -759,6 +786,7 @@ public function valueProvider(): iterable
];

yield 'complete' => [
false,
false,
new SchemaConfiguration(
'/path/to/infection.json',
Expand Down Expand Up @@ -865,6 +893,7 @@ private static function createValueForTimeout(
int $expectedTimeOut
): array {
return [
false,
false,
new SchemaConfiguration(
'/path/to/infection.json',
Expand Down Expand Up @@ -940,6 +969,7 @@ private static function createValueForTmpDir(
?string $expectedTmpDir
): array {
return [
false,
false,
new SchemaConfiguration(
'/path/to/infection.json',
Expand Down Expand Up @@ -1016,6 +1046,7 @@ private static function createValueForCoveragePath(
string $expectedCoveragePath
): array {
return [
false,
false,
new SchemaConfiguration(
'/path/to/infection.json',
Expand Down Expand Up @@ -1091,6 +1122,7 @@ private static function createValueForPhpUnitConfigDir(
?string $expectedPhpUnitConfigDir
): array {
return [
false,
false,
new SchemaConfiguration(
'/path/to/infection.json',
Expand Down Expand Up @@ -1168,6 +1200,7 @@ private static function createValueForNoProgress(
): array {
return [
$ciDetected,
false,
new SchemaConfiguration(
'/path/to/infection.json',
null,
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -1319,6 +1441,7 @@ private static function createValueForMinMsi(
?float $expectedMinMsi
): array {
return [
false,
false,
new SchemaConfiguration(
'/path/to/infection.json',
Expand Down Expand Up @@ -1395,6 +1518,7 @@ private static function createValueForMinCoveredMsi(
?float $expectedMinCoveredMsi
): array {
return [
false,
false,
new SchemaConfiguration(
'/path/to/infection.json',
Expand Down Expand Up @@ -1472,6 +1596,7 @@ private static function createValueForTestFramework(
string $expectedTestFrameworkExtraOptions
): array {
return [
false,
false,
new SchemaConfiguration(
'/path/to/infection.json',
Expand Down Expand Up @@ -1548,6 +1673,7 @@ private static function createValueForInitialTestsPhpOptions(
?string $expectedInitialTestPhpOptions
): array {
return [
false,
false,
new SchemaConfiguration(
'/path/to/infection.json',
Expand Down Expand Up @@ -1625,6 +1751,7 @@ private static function createValueForTestFrameworkExtraOptions(
string $expectedTestFrameworkExtraOptions
): array {
return [
false,
false,
new SchemaConfiguration(
'/path/to/infection.json',
Expand Down Expand Up @@ -1701,6 +1828,7 @@ private static function createValueForTestFrameworkKey(
string $expectedTestFrameworkExtraOptions
): array {
return [
false,
false,
new SchemaConfiguration(
'/path/to/infection.json',
Expand Down Expand Up @@ -1781,6 +1909,7 @@ private static function createValueForMutators(
array $expectedMutators
): array {
return [
false,
false,
new SchemaConfiguration(
'/path/to/infection.json',
Expand Down Expand Up @@ -1860,6 +1989,7 @@ private static function createValueForIgnoreSourceCodeByRegex(
array $expectedIgnoreSourceCodeMutatorsMap
): array {
return [
false,
false,
new SchemaConfiguration(
'/path/to/infection.json',
Expand Down Expand Up @@ -1946,6 +2076,7 @@ private static function createValueForHtmlLogFilePath(?string $htmlFileLogPathIn
);

return [
false,
false,
new SchemaConfiguration(
'/path/to/infection.json',
Expand Down Expand Up @@ -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);
Expand All @@ -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
);
}
Expand Down
13 changes: 10 additions & 3 deletions tests/phpunit/Fixtures/DummyCiDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');
}
}

0 comments on commit 7ef2cae

Please sign in to comment.