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
…1645)

* Auto-detect Github Actions CI and activate github logger accordingly

Fixes #1560

* Remove explicit --logger-github flag, not that it is auto-detected in CI

* Lower min-msi so it passes

See feedback in #1645 (comment)

* Add doc to option itself that it's auto-detected

* Add `--logger-github=false` to E2E tests

* Import CiDetector class for const usage

* Restore accidently removed commits

* Fix incorrect condition after refactoring, fix tests

* Do not use deprecated `Safe\sprintf`

* Decrease MSI

* Try to catch all types of exception, including "sh: 1: git: not found"

* Simplify code, remove nested `elseif`. Disable GH logger for e2e tests

* Set env var differently

* Set env var differently 2

* Fix CS

Co-authored-by: maks-rafalko <b0rn@list.ru>
  • Loading branch information
mfn and maks-rafalko committed May 15, 2022
1 parent 134853c commit 38004b5
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 23 deletions.
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

0 comments on commit 38004b5

Please sign in to comment.