From b25559f60673b906af7ea2e9d33dba027f11bb90 Mon Sep 17 00:00:00 2001 From: Markus Podar Date: Sun, 30 Jan 2022 22:12:48 +0100 Subject: [PATCH] Allow --logger-github to be explicitly disabled 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). --- src/Command/RunCommand.php | 25 ++++++++++++++++--- src/Configuration/ConfigurationFactory.php | 6 ++--- src/Container.php | 4 +-- .../ConfigurationFactoryTest.php | 24 +++++++++++++----- 4 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/Command/RunCommand.php b/src/Command/RunCommand.php index aabc63c05b..e9b9f9a813 100644 --- a/src/Command/RunCommand.php +++ b/src/Command/RunCommand.php @@ -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 true to force-enable or false to force-disable it).', + false ) ->addOption( self::OPTION_LOGGER_HTML, @@ -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(), @@ -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) diff --git a/src/Configuration/ConfigurationFactory.php b/src/Configuration/ConfigurationFactory.php index 03d2596bf8..8f34f35e7a 100644 --- a/src/Configuration/ConfigurationFactory.php +++ b/src/Configuration/ConfigurationFactory.php @@ -119,7 +119,7 @@ public function create( ?string $gitDiffFilter, bool $isForGitDiffLines, ?string $gitDiffBase, - bool $useGitHubLogger, + ?bool $useGitHubLogger, ?string $htmlLogFilePath, bool $useNoopMutators, bool $executeOnlyCoveringTestCases @@ -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(); } diff --git a/src/Container.php b/src/Container.php index f72d0a675c..e590efe390 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; @@ -745,7 +745,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 0aa99a8154..191b773696 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,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 @@ -1271,7 +1283,7 @@ private static function createValueForNoProgress( } private static function createValueForGithubActionsDetected( - bool $inputUseGitHubAnnotationsLogger, + ?bool $inputUseGitHubAnnotationsLogger, bool $githubActionsDetected, bool $useGitHubAnnotationsLogger ): array {