From af999b806aace75703fdce68772de89ffd64282a Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Tue, 21 Apr 2020 14:16:20 +0300 Subject: [PATCH 1/6] ISAICP-5825: Task 'run' should allow arguments and options. --- README.md | 2 +- .../CollectionFactory/CollectionFactory.php | 9 +++++- tests/Tasks/CollectionFactoryTest.php | 26 ++++++++++++++++- .../src/TaskRunner/Commands/TestCommands.php | 28 +++++++++++++++++++ 4 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 tests/custom/src/TaskRunner/Commands/TestCommands.php diff --git a/README.md b/README.md index 6edc2fe6..e73e3e3f 100644 --- a/README.md +++ b/README.md @@ -172,7 +172,7 @@ At the moment the following tasks are supported (optional argument default value | `process-php` | `taskAppendConfiguration()` | `type: append`, `config`, `source`, `destination`, `override` (false) | | `process-php` | `taskPrependConfiguration()` | `type: prepend`, `config`, `source`, `destination`, `override` (false) | | `process-php` | `taskWriteConfiguration()` | `type: write`, `config`, `source`, `destination`, `override` (false) | -| `run` | `taskExec()` | `command` (will run `./vendor/bin/run [command]`) | +| `run` | `taskExec()` | `command`, `arguments`, `options` (will run `./vendor/bin/run [command] [argument1] [argument2] ... --[option1]=[value1] --[option2]=[value2] ...`) | Tasks provided as plain-text strings will be executed as is in the current working directory. diff --git a/src/Tasks/CollectionFactory/CollectionFactory.php b/src/Tasks/CollectionFactory/CollectionFactory.php index ff430f20..4f828532 100644 --- a/src/Tasks/CollectionFactory/CollectionFactory.php +++ b/src/Tasks/CollectionFactory/CollectionFactory.php @@ -150,7 +150,14 @@ protected function taskFactory($task) ]); case "run": - return $this->taskExec($this->getConfig()->get('runner.bin_dir').'/run')->arg($task['command']); + $taskExec = $this->taskExec($this->getConfig()->get('runner.bin_dir').'/run')->arg($task['command']); + if (!empty($task['arguments'])) { + $taskExec->args($task['arguments']); + } + if (!empty($task['options'])) { + $taskExec->options($task['options'], '='); + } + return $taskExec; case "process-php": $this->secureOption($task, 'override', false); diff --git a/tests/Tasks/CollectionFactoryTest.php b/tests/Tasks/CollectionFactoryTest.php index 1ede1272..7f10428d 100644 --- a/tests/Tasks/CollectionFactoryTest.php +++ b/tests/Tasks/CollectionFactoryTest.php @@ -4,7 +4,6 @@ use OpenEuropa\TaskRunner\Tasks\CollectionFactory\loadTasks; use OpenEuropa\TaskRunner\Tests\AbstractTaskTest; -use Robo\Config\Config; /** * Class CollectionFactoryTest @@ -74,6 +73,31 @@ public function testProcessPhpTask($type, $override, $destinationExists, $source $this->assertEquals(trim($expected), trim(file_get_contents($destinationFile))); } + /** + * Tests the 'run' task. + */ + public function testRunTask() + { + + $filePath = $this->getSandboxFilepath('test-file.txt'); + $tasks = [ + [ + 'task' => 'run', + 'command' => 'custom:test', + 'options' => [ + 'filepath' => $filePath, + 'content' => __METHOD__, + ], + ], + ]; + + $collectionFactory = $this->taskCollectionFactory($tasks); + $collectionFactory->getConfig()->set('runner.bin_dir', realpath(__DIR__ . '/../../bin')); + $collectionFactory->run(); + + $this->assertSame(__METHOD__, file_get_contents($filePath)); + } + /** * @return array */ diff --git a/tests/custom/src/TaskRunner/Commands/TestCommands.php b/tests/custom/src/TaskRunner/Commands/TestCommands.php new file mode 100644 index 00000000..3a35e140 --- /dev/null +++ b/tests/custom/src/TaskRunner/Commands/TestCommands.php @@ -0,0 +1,28 @@ + InputOption::VALUE_REQUIRED, + 'content' => InputOption::VALUE_REQUIRED, + ]) + { + file_put_contents($options['filepath'], $options['content']); + } +} From 978b7b5243d0a6e51646ba2b3d4d053d8199ff2e Mon Sep 17 00:00:00 2001 From: Pieter Frenssen Date: Mon, 11 May 2020 17:58:56 +0300 Subject: [PATCH 2/6] ISAICP-5825: Formatting. --- tests/Tasks/CollectionFactoryTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Tasks/CollectionFactoryTest.php b/tests/Tasks/CollectionFactoryTest.php index 7f10428d..90bf55ac 100644 --- a/tests/Tasks/CollectionFactoryTest.php +++ b/tests/Tasks/CollectionFactoryTest.php @@ -78,7 +78,6 @@ public function testProcessPhpTask($type, $override, $destinationExists, $source */ public function testRunTask() { - $filePath = $this->getSandboxFilepath('test-file.txt'); $tasks = [ [ From 02caf02ab336aa736f739f2693dc2abec5a79127 Mon Sep 17 00:00:00 2001 From: Pieter Frenssen Date: Mon, 11 May 2020 18:20:53 +0300 Subject: [PATCH 3/6] ISAICP-5825: Fix formatting. --- tests/Tasks/CollectionFactoryTest.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/Tasks/CollectionFactoryTest.php b/tests/Tasks/CollectionFactoryTest.php index 90bf55ac..d4b9b473 100644 --- a/tests/Tasks/CollectionFactoryTest.php +++ b/tests/Tasks/CollectionFactoryTest.php @@ -80,14 +80,14 @@ public function testRunTask() { $filePath = $this->getSandboxFilepath('test-file.txt'); $tasks = [ - [ - 'task' => 'run', - 'command' => 'custom:test', - 'options' => [ - 'filepath' => $filePath, - 'content' => __METHOD__, - ], - ], + [ + 'task' => 'run', + 'command' => 'custom:test', + 'options' => [ + 'filepath' => $filePath, + 'content' => __METHOD__, + ], + ], ]; $collectionFactory = $this->taskCollectionFactory($tasks); From 4aad503abe1abcac4e5cbc11610c22170d522c18 Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Thu, 14 May 2020 19:39:26 +0300 Subject: [PATCH 4/6] ISAICP-5825: Fix test command class dockblock. --- tests/custom/src/TaskRunner/Commands/TestCommands.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/custom/src/TaskRunner/Commands/TestCommands.php b/tests/custom/src/TaskRunner/Commands/TestCommands.php index 3a35e140..c0ac4830 100644 --- a/tests/custom/src/TaskRunner/Commands/TestCommands.php +++ b/tests/custom/src/TaskRunner/Commands/TestCommands.php @@ -6,7 +6,7 @@ use Symfony\Component\Console\Input\InputOption; /** - * Class TestCommands. + * Commands for testing CollectionFactory tasks. */ class TestCommands extends AbstractCommands { From cf004560e3376d7fe9d36f60ff43f36b76481fc8 Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Thu, 14 May 2020 19:40:01 +0300 Subject: [PATCH 5/6] ISAICP-5825: Test also arguments. --- tests/Tasks/CollectionFactoryTest.php | 23 +++++++++---------- .../src/TaskRunner/Commands/TestCommands.php | 15 ++++++------ tests/sandbox/.gitkeep | 0 3 files changed, 19 insertions(+), 19 deletions(-) mode change 100644 => 100755 tests/sandbox/.gitkeep diff --git a/tests/Tasks/CollectionFactoryTest.php b/tests/Tasks/CollectionFactoryTest.php index d4b9b473..8cb69810 100644 --- a/tests/Tasks/CollectionFactoryTest.php +++ b/tests/Tasks/CollectionFactoryTest.php @@ -79,20 +79,19 @@ public function testProcessPhpTask($type, $override, $destinationExists, $source public function testRunTask() { $filePath = $this->getSandboxFilepath('test-file.txt'); - $tasks = [ - [ - 'task' => 'run', - 'command' => 'custom:test', - 'options' => [ - 'filepath' => $filePath, - 'content' => __METHOD__, - ], + + $tasks = []; + $tasks[] = [ + 'task' => 'run', + 'command' => 'custom:test', + 'arguments' => [ + __METHOD__, + ], + 'options' => [ + 'filepath' => $filePath, ], ]; - - $collectionFactory = $this->taskCollectionFactory($tasks); - $collectionFactory->getConfig()->set('runner.bin_dir', realpath(__DIR__ . '/../../bin')); - $collectionFactory->run(); + $this->taskCollectionFactory($tasks)->run(); $this->assertSame(__METHOD__, file_get_contents($filePath)); } diff --git a/tests/custom/src/TaskRunner/Commands/TestCommands.php b/tests/custom/src/TaskRunner/Commands/TestCommands.php index c0ac4830..334931bd 100644 --- a/tests/custom/src/TaskRunner/Commands/TestCommands.php +++ b/tests/custom/src/TaskRunner/Commands/TestCommands.php @@ -1,5 +1,7 @@ InputOption::VALUE_REQUIRED, - 'content' => InputOption::VALUE_REQUIRED, - ]) + ]): void { - file_put_contents($options['filepath'], $options['content']); + file_put_contents($options['filepath'], $content); } } diff --git a/tests/sandbox/.gitkeep b/tests/sandbox/.gitkeep old mode 100644 new mode 100755 From cd59de5ee132bac723ee2921ec0680b86898851b Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Fri, 15 May 2020 17:24:16 +0300 Subject: [PATCH 6/6] ISAICP-5825: Fix the test. --- src/Tasks/CollectionFactory/CollectionFactory.php | 14 +++++++++++++- tests/AbstractTaskTest.php | 1 + tests/Tasks/CollectionFactoryTest.php | 1 - 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Tasks/CollectionFactory/CollectionFactory.php b/src/Tasks/CollectionFactory/CollectionFactory.php index 4f828532..bedef742 100644 --- a/src/Tasks/CollectionFactory/CollectionFactory.php +++ b/src/Tasks/CollectionFactory/CollectionFactory.php @@ -150,7 +150,9 @@ protected function taskFactory($task) ]); case "run": - $taskExec = $this->taskExec($this->getConfig()->get('runner.bin_dir').'/run')->arg($task['command']); + $taskExec = $this->taskExec($this->getConfig()->get('runner.bin_dir').'/run') + ->arg($task['command']) + ->interactive($this->isTtySupported()); if (!empty($task['arguments'])) { $taskExec->args($task['arguments']); } @@ -204,4 +206,14 @@ protected function secureOption(array &$task, $name, $default) { $task[$name] = isset($task[$name]) ? $task[$name] : $default; } + + /** + * Checks if the TTY mode is supported + * + * @return bool + */ + protected function isTtySupported() + { + return PHP_OS !== 'WINNT' && (bool) @proc_open('echo 1 >/dev/null', [['file', '/dev/tty', 'r'], ['file', '/dev/tty', 'w'], ['file', '/dev/tty', 'w']], $pipes); + } } diff --git a/tests/AbstractTaskTest.php b/tests/AbstractTaskTest.php index 0370dfda..da1a3abf 100644 --- a/tests/AbstractTaskTest.php +++ b/tests/AbstractTaskTest.php @@ -33,6 +33,7 @@ public function setup() $this->output = new BufferedOutput(); $runner = new TaskRunner(new StringInput(''), $this->output, $this->getClassLoader()); $this->setContainer($runner->getContainer()); + $this->getContainer()->get('config')->set('runner.bin_dir', realpath(__DIR__.'/../bin')); } /** diff --git a/tests/Tasks/CollectionFactoryTest.php b/tests/Tasks/CollectionFactoryTest.php index 8cb69810..ba218bad 100644 --- a/tests/Tasks/CollectionFactoryTest.php +++ b/tests/Tasks/CollectionFactoryTest.php @@ -92,7 +92,6 @@ public function testRunTask() ], ]; $this->taskCollectionFactory($tasks)->run(); - $this->assertSame(__METHOD__, file_get_contents($filePath)); }