From abfcf73018f6d0f7a1a804abdb571735b0e0c171 Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Mon, 31 Dec 2018 16:04:09 +0000 Subject: [PATCH] Get tag defaults from the environment --- src/Cli/Command/DefaultTag.php | 16 ++++++++++++++++ src/Cli/Command/InstallCommand.php | 5 +++-- src/Cli/Command/ListCommand.php | 6 ++++-- src/Cli/Command/TestCommand.php | 5 +++-- tests/Cli/Command/InstallCommandTest.php | 18 ++++++++++++++++++ tests/Cli/Command/ListCommandTest.php | 17 +++++++++++++++++ tests/Cli/Command/TestCommandTest.php | 17 +++++++++++++++++ 7 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 src/Cli/Command/DefaultTag.php diff --git a/src/Cli/Command/DefaultTag.php b/src/Cli/Command/DefaultTag.php new file mode 100644 index 00000000..994c50b5 --- /dev/null +++ b/src/Cli/Command/DefaultTag.php @@ -0,0 +1,16 @@ +setDescription('Installs tools'); $this->addOption('dry-run', null, InputOption::VALUE_NONE, 'Output the command without executing it'); $this->addOption('target-dir', null, InputOption::VALUE_REQUIRED, 'The target installation directory', $this->defaultTargetDir()); - $this->addOption('exclude-tag', 'e', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Tool tags to exclude'); - $this->addOption('tag', 't', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Tool tags to filter by'); + $this->addOption('exclude-tag', 'e', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Tool tags to exclude', $this->defaultExcludeTag()); + $this->addOption('tag', 't', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Tool tags to filter by', $this->defaultTag()); } protected function execute(InputInterface $input, OutputInterface $output) diff --git a/src/Cli/Command/ListCommand.php b/src/Cli/Command/ListCommand.php index edbe322a..354e8494 100644 --- a/src/Cli/Command/ListCommand.php +++ b/src/Cli/Command/ListCommand.php @@ -14,6 +14,8 @@ final class ListCommand extends Command { + use DefaultTag; + public const NAME = 'list-tools'; private $listTools; @@ -28,8 +30,8 @@ public function __construct(ListTools $listTools) protected function configure() { $this->setDescription('Lists available tools'); - $this->addOption('exclude-tag', 'e', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Tool tags to exclude'); - $this->addOption('tag', 't', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Tool tags to filter by'); + $this->addOption('exclude-tag', 'e', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Tool tags to exclude', $this->defaultExcludeTag()); + $this->addOption('tag', 't', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Tool tags to filter by', $this->defaultTag()); } protected function execute(InputInterface $input, OutputInterface $output) diff --git a/src/Cli/Command/TestCommand.php b/src/Cli/Command/TestCommand.php index 7eae674b..5192b834 100644 --- a/src/Cli/Command/TestCommand.php +++ b/src/Cli/Command/TestCommand.php @@ -12,6 +12,7 @@ final class TestCommand extends Command { + use DefaultTag; use DefaultTargetDir; public const NAME = 'test'; @@ -32,8 +33,8 @@ protected function configure() $this->setDescription('Runs basic tests to verify tools are installed'); $this->addOption('dry-run', null, InputOption::VALUE_NONE, 'Output the command without executing it'); $this->addOption('target-dir', null, InputOption::VALUE_REQUIRED, 'The target installation directory', $this->defaultTargetDir()); - $this->addOption('exclude-tag', 'e', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Tool tags to exclude'); - $this->addOption('tag', 't', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Tool tags to filter by'); + $this->addOption('exclude-tag', 'e', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Tool tags to exclude', $this->defaultExcludeTag()); + $this->addOption('tag', 't', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Tool tags to filter by', $this->defaultTag()); } protected function execute(InputInterface $input, OutputInterface $output) diff --git a/tests/Cli/Command/InstallCommandTest.php b/tests/Cli/Command/InstallCommandTest.php index 848ee4ff..2873bfa4 100644 --- a/tests/Cli/Command/InstallCommandTest.php +++ b/tests/Cli/Command/InstallCommandTest.php @@ -87,11 +87,29 @@ public function test_it_takes_the_target_dir_option_default_from_environment_if_ public function test_it_defines_exclude_tag_option() { $this->assertTrue($this->cliCommand()->getDefinition()->hasOption('exclude-tag')); + $this->assertSame([\sprintf('exclude-php:%s.%s', PHP_MAJOR_VERSION, PHP_MINOR_VERSION)], $this->cliCommand()->getDefinition()->getOption('exclude-tag')->getDefault()); + } + + /** + * @putenv TOOLBOX_EXCLUDED_TAGS=foo,bar,baz + */ + public function test_it_takes_the_excluded_tag_option_default_from_environment_if_present() + { + $this->assertSame(['foo', 'bar', 'baz'], $this->cliCommand()->getDefinition()->getOption('exclude-tag')->getDefault()); } public function test_it_defines_tag_option() { $this->assertTrue($this->cliCommand()->getDefinition()->hasOption('tag')); + $this->assertSame([], $this->cliCommand()->getDefinition()->getOption('tag')->getDefault()); + } + + /** + * @putenv TOOLBOX_TAGS=foo,bar,baz + */ + public function test_it_takes_the_tag_option_default_from_environment_if_present() + { + $this->assertSame(['foo', 'bar', 'baz'], $this->cliCommand()->getDefinition()->getOption('tag')->getDefault()); } protected function getContainerTestDoubles(): array diff --git a/tests/Cli/Command/ListCommandTest.php b/tests/Cli/Command/ListCommandTest.php index 839d3c22..85584041 100644 --- a/tests/Cli/Command/ListCommandTest.php +++ b/tests/Cli/Command/ListCommandTest.php @@ -53,6 +53,15 @@ public function test_it_filters_by_tags() public function test_it_defines_exclude_tag_option() { $this->assertTrue($this->cliCommand()->getDefinition()->hasOption('exclude-tag')); + $this->assertSame([\sprintf('exclude-php:%s.%s', PHP_MAJOR_VERSION, PHP_MINOR_VERSION)], $this->cliCommand()->getDefinition()->getOption('exclude-tag')->getDefault()); + } + + /** + * @putenv TOOLBOX_EXCLUDED_TAGS=foo,bar,baz + */ + public function test_it_takes_the_excluded_tag_option_default_from_environment_if_present() + { + $this->assertSame(['foo', 'bar', 'baz'], $this->cliCommand()->getDefinition()->getOption('exclude-tag')->getDefault()); } public function test_it_defines_tag_option() @@ -60,6 +69,14 @@ public function test_it_defines_tag_option() $this->assertTrue($this->cliCommand()->getDefinition()->hasOption('tag')); } + /** + * @putenv TOOLBOX_TAGS=foo,bar,baz + */ + public function test_it_takes_the_tag_option_default_from_environment_if_present() + { + $this->assertSame(['foo', 'bar', 'baz'], $this->cliCommand()->getDefinition()->getOption('tag')->getDefault()); + } + protected function getContainerTestDoubles(): array { return [ diff --git a/tests/Cli/Command/TestCommandTest.php b/tests/Cli/Command/TestCommandTest.php index 0e3eb4d6..7ead3d28 100644 --- a/tests/Cli/Command/TestCommandTest.php +++ b/tests/Cli/Command/TestCommandTest.php @@ -87,6 +87,15 @@ public function test_it_takes_the_target_dir_option_default_from_environment_if_ public function test_it_defines_exclude_tag_option() { $this->assertTrue($this->cliCommand()->getDefinition()->hasOption('exclude-tag')); + $this->assertSame([\sprintf('exclude-php:%s.%s', PHP_MAJOR_VERSION, PHP_MINOR_VERSION)], $this->cliCommand()->getDefinition()->getOption('exclude-tag')->getDefault()); + } + + /** + * @putenv TOOLBOX_EXCLUDED_TAGS=foo,bar,baz + */ + public function test_it_takes_the_excluded_tag_option_default_from_environment_if_present() + { + $this->assertSame(['foo', 'bar', 'baz'], $this->cliCommand()->getDefinition()->getOption('exclude-tag')->getDefault()); } public function test_it_defines_tag_option() @@ -94,6 +103,14 @@ public function test_it_defines_tag_option() $this->assertTrue($this->cliCommand()->getDefinition()->hasOption('tag')); } + /** + * @putenv TOOLBOX_TAGS=foo,bar,baz + */ + public function test_it_takes_the_tag_option_default_from_environment_if_present() + { + $this->assertSame(['foo', 'bar', 'baz'], $this->cliCommand()->getDefinition()->getOption('tag')->getDefault()); + } + protected function getContainerTestDoubles(): array { return [