Skip to content

Commit

Permalink
Allow --logger-github to be explicitly disabled with =false
Browse files Browse the repository at this point in the history
By switching from InputOption::VALUE_NONE to InputOption::VALUE_OPTIONAL
and providing the default value `false`, we can detect if the option
was provided or not or if the user explicitly wants to disable it.

This required to populate `useGitHubLogger` as nullable throughout to
still support auto-detecting it by default (i.e. when not provided).

TL;DR:
- not provided: auto-detected
- `--logger-github` : enabled
- `--logger-github=true` : enabled
- `--logger-github=false` : disabled
  • Loading branch information
mfn committed Jan 30, 2022
1 parent 80b74e8 commit 788a432
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
25 changes: 22 additions & 3 deletions src/Command/RunCommand.php
Expand Up @@ -263,8 +263,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 <comment>true</comment> to force-enable or <comment>false</comment> to force-disable it).',
false
)
->addOption(
self::OPTION_LOGGER_HTML,
Expand Down Expand Up @@ -437,6 +438,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(),
Expand Down Expand Up @@ -481,7 +500,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)
Expand Down
6 changes: 3 additions & 3 deletions src/Configuration/ConfigurationFactory.php
Expand Up @@ -119,7 +119,7 @@ public function create(
?string $gitDiffFilter,
bool $isForGitDiffLines,
?string $gitDiffBase,
bool $useGitHubLogger,
?bool $useGitHubLogger,
?string $htmlLogFilePath,
bool $useNoopMutators,
bool $executeOnlyCoveringTestCases
Expand Down Expand Up @@ -317,9 +317,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();
}

Expand Down
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 @@ -745,7 +745,7 @@ public function withValues(
?string $gitDiffFilter,
bool $isForGitDiffLines,
?string $gitDiffBase,
bool $useGitHubLogger,
?bool $useGitHubLogger,
?string $htmlLogFilePath,
bool $useNoopMutators,
bool $executeOnlyCoveringTestCases
Expand Down
24 changes: 18 additions & 6 deletions tests/phpunit/Configuration/ConfigurationFactoryTest.php
Expand Up @@ -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,
Expand Down Expand Up @@ -399,25 +399,37 @@ 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
Expand Down Expand Up @@ -1271,7 +1283,7 @@ private static function createValueForNoProgress(
}

private static function createValueForGithubActionsDetected(
bool $inputUseGitHubAnnotationsLogger,
?bool $inputUseGitHubAnnotationsLogger,
bool $githubActionsDetected,
bool $useGitHubAnnotationsLogger
): array {
Expand Down

0 comments on commit 788a432

Please sign in to comment.