Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-detect Github Actions CI and activate github logger accordingly #1645

Merged
merged 15 commits into from May 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/mt-annotations.yaml
Expand Up @@ -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
2 changes: 1 addition & 1 deletion .github/workflows/mt.yaml
Expand Up @@ -11,7 +11,7 @@ on:
- master

env:
MIN_MSI: 71.05
MIN_MSI: 71.39
MIN_COVERED_MSI: 86.78

jobs:
Expand Down
45 changes: 41 additions & 4 deletions src/Command/RunCommand.php
Expand Up @@ -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;
Expand All @@ -61,8 +62,9 @@
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\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use function trim;

Expand Down Expand Up @@ -264,8 +266,9 @@ protected function configure(): void
->addOption(
self::OPTION_LOGGER_GITHUB,
null,
InputOption::VALUE_NONE,
'Log escaped Mutants as GitHub Annotations.',
InputOption::VALUE_OPTIONAL,
'Log escaped Mutants as GitHub Annotations (automatically detected on Github Actions itself, use <comment>true</comment> to force-enable or <comment>false</comment> to force-disable it).',
false
)
->addOption(
self::OPTION_LOGGER_HTML,
Expand Down Expand Up @@ -482,7 +485,7 @@ private function createContainer(IO $io, LoggerInterface $logger): Container
$gitDiffFilter,
$isForGitDiffLines,
$gitDiffBase,
(bool) $input->getOption(self::OPTION_LOGGER_GITHUB),
$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)
Expand Down Expand Up @@ -606,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
));
}
}
21 changes: 19 additions & 2 deletions src/Configuration/ConfigurationFactory.php
Expand Up @@ -52,7 +52,9 @@
use Infection\Mutator\MutatorParser;
use Infection\Mutator\MutatorResolver;
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;
Expand Down Expand Up @@ -118,7 +120,7 @@ public function create(
?string $gitDiffFilter,
bool $isForGitDiffLines,
?string $gitDiffBase,
bool $useGitHubLogger,
?bool $useGitHubLogger,
?string $htmlLogFilePath,
bool $useNoopMutators,
bool $executeOnlyCoveringTestCases
Expand Down Expand Up @@ -316,8 +318,12 @@ 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 === null) {
$useGitHubLogger = $this->detectCiGithubActions();
}

if ($useGitHubLogger) {
$logs->setUseGitHubAnnotationsLogger($useGitHubLogger);
}
Expand All @@ -328,4 +334,15 @@ private function retrieveLogs(Logs $logs, bool $useGitHubLogger, ?string $htmlLo

return $logs;
}

private function detectCiGithubActions(): bool
{
try {
$ci = $this->ciDetector->detect();
} catch (CiNotDetectedException $e) {
return false;
}

return $ci->getCiName() === CiDetector::CI_GITHUB_ACTIONS;
}
}
4 changes: 2 additions & 2 deletions src/Container.php
Expand Up @@ -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;
Expand Down Expand Up @@ -610,7 +610,7 @@ public function withValues(
?string $gitDiffFilter,
bool $isForGitDiffLines,
?string $gitDiffBase,
bool $useGitHubLogger,
?bool $useGitHubLogger,
?string $htmlLogFilePath,
bool $useNoopMutators,
bool $executeOnlyCoveringTestCases
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e_tests
Expand Up @@ -23,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 ]
Expand Down