From 616b45d92a8f1a739de191740131cdd4a1177ffa Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Thu, 23 Apr 2020 15:54:30 +0300 Subject: [PATCH 1/6] ISAICP-5825: Add a new 'exec' task for dynamic commands. --- .../CollectionFactory/CollectionFactory.php | 11 ++++++ tests/Tasks/CollectionFactoryTest.php | 37 ++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/Tasks/CollectionFactory/CollectionFactory.php b/src/Tasks/CollectionFactory/CollectionFactory.php index ff430f20..6783b618 100644 --- a/src/Tasks/CollectionFactory/CollectionFactory.php +++ b/src/Tasks/CollectionFactory/CollectionFactory.php @@ -95,6 +95,7 @@ public function getHelp() protected function taskFactory($task) { if (is_string($task)) { + @trigger_error('Defining a task as a plain text is deprecated in openeuropa/task-runner:1.0.0 and is removed from openeuropa/task-runner:2.0.0. Use the "exec" task and pass arguments and options.', E_USER_DEPRECATED); return $this->taskExec($task); } @@ -181,6 +182,16 @@ protected function taskFactory($task) return $this->collectionBuilder()->addTaskList($tasks); + case 'exec': + $taskExec = $this->taskExec($task['command']); + if (!empty($task['arguments'])) { + $taskExec->args($task['arguments']); + } + if (!empty($task['options'])) { + $taskExec->options($task['options']); + } + return $taskExec; + default: throw new TaskException($this, "Task '{$task['task']}' not supported."); } diff --git a/tests/Tasks/CollectionFactoryTest.php b/tests/Tasks/CollectionFactoryTest.php index 1ede1272..27fc307b 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,42 @@ public function testProcessPhpTask($type, $override, $destinationExists, $source $this->assertEquals(trim($expected), trim(file_get_contents($destinationFile))); } + /** + * Tests the exec task and old style exec task deprecation. + * + * @expectedDeprecation Defining a task as a plain text is deprecated. Use the "exec" task and pass arguments and options. + * @group legacy + */ + public function testExecTask(): void + { + $tasks = [ + // This form is deprecated. + 'touch -t 198006062359.59 '.$this->getSandboxFilepath('deprecated.txt'), + // Valid usage. + [ + 'task' => 'exec', + 'command' => 'touch', + 'arguments' => [ + 'current.txt', + ], + 'options' => [ + // 1980-06-06 23:59:59. + '-t' => '198006062359.59' + ], + 'dir' => $this->getSandboxRoot(), + ], + ]; + $this->taskCollectionFactory($tasks)->run(); + + $this->assertFileExists($this->getSandboxFilepath('deprecated.txt')); + $mtime = gmdate('Y-m-d H:i:s', filemtime($this->getSandboxFilepath('deprecated.txt'))); + $this->assertSame('1980-06-06 23:59:59', $mtime); + + $this->assertFileExists($this->getSandboxFilepath('current.txt')); + $mtime = gmdate('Y-m-d H:i:s', filemtime($this->getSandboxFilepath('current.txt'))); + $this->assertSame('1980-06-06 23:59:59', $mtime); + } + /** * @return array */ From 700b971c3a82beff0326601104e0da31f0a22ba9 Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Fri, 24 Apr 2020 13:20:18 +0300 Subject: [PATCH 2/6] ISAICP-5825: Allow to pass a directory where the command should be executed. --- src/Tasks/CollectionFactory/CollectionFactory.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Tasks/CollectionFactory/CollectionFactory.php b/src/Tasks/CollectionFactory/CollectionFactory.php index 6783b618..0704759d 100644 --- a/src/Tasks/CollectionFactory/CollectionFactory.php +++ b/src/Tasks/CollectionFactory/CollectionFactory.php @@ -190,6 +190,9 @@ protected function taskFactory($task) if (!empty($task['options'])) { $taskExec->options($task['options']); } + if (!empty($task['dir'])) { + $taskExec->dir($task['dir']); + } return $taskExec; default: From a588875810c3086a6ba8ca5df1afd9cad49a2424 Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Sat, 25 Apr 2020 20:45:04 +0300 Subject: [PATCH 3/6] ISAICP-5825: Prevent 'TTY mode requires /dev/tty to be read/writable.' error. --- .../CollectionFactory/CollectionFactory.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Tasks/CollectionFactory/CollectionFactory.php b/src/Tasks/CollectionFactory/CollectionFactory.php index 0704759d..d65dc7df 100644 --- a/src/Tasks/CollectionFactory/CollectionFactory.php +++ b/src/Tasks/CollectionFactory/CollectionFactory.php @@ -95,8 +95,8 @@ public function getHelp() protected function taskFactory($task) { if (is_string($task)) { - @trigger_error('Defining a task as a plain text is deprecated in openeuropa/task-runner:1.0.0 and is removed from openeuropa/task-runner:2.0.0. Use the "exec" task and pass arguments and options.', E_USER_DEPRECATED); - return $this->taskExec($task); + @trigger_error('Defining a task as a plain text is depracted. Use the "exec" task and pass arguments and options.', E_USER_DEPRECATED); + return $this->taskExec($task)->interactive($this->isTtySupported()); } $this->secureOption($task, 'force', false); @@ -183,7 +183,7 @@ protected function taskFactory($task) return $this->collectionBuilder()->addTaskList($tasks); case 'exec': - $taskExec = $this->taskExec($task['command']); + $taskExec = $this->taskExec($task['command'])->interactive($this->isTtySupported()); if (!empty($task['arguments'])) { $taskExec->args($task['arguments']); } @@ -211,4 +211,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); + } } From 8be1b2efcaa2bad7aacee137afee9fff9a525150 Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Fri, 15 May 2020 11:50:39 +0300 Subject: [PATCH 4/6] ISAICP-5825: Fix typo. --- src/Tasks/CollectionFactory/CollectionFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tasks/CollectionFactory/CollectionFactory.php b/src/Tasks/CollectionFactory/CollectionFactory.php index d65dc7df..59b6409c 100644 --- a/src/Tasks/CollectionFactory/CollectionFactory.php +++ b/src/Tasks/CollectionFactory/CollectionFactory.php @@ -95,7 +95,7 @@ public function getHelp() protected function taskFactory($task) { if (is_string($task)) { - @trigger_error('Defining a task as a plain text is depracted. Use the "exec" task and pass arguments and options.', E_USER_DEPRECATED); + @trigger_error('Defining a task as a plain text is deprecated. Use the "exec" task and pass arguments and options.', E_USER_DEPRECATED); return $this->taskExec($task)->interactive($this->isTtySupported()); } From ab2e92227bd6212c6c0d31d6e4f58950acf98b05 Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Fri, 15 May 2020 15:59:40 +0300 Subject: [PATCH 5/6] ISAICP-5825: Provide test coverage. --- tests/Tasks/CollectionFactoryTest.php | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/tests/Tasks/CollectionFactoryTest.php b/tests/Tasks/CollectionFactoryTest.php index 27fc307b..a8ff5d23 100644 --- a/tests/Tasks/CollectionFactoryTest.php +++ b/tests/Tasks/CollectionFactoryTest.php @@ -75,21 +75,15 @@ public function testProcessPhpTask($type, $override, $destinationExists, $source /** * Tests the exec task and old style exec task deprecation. - * - * @expectedDeprecation Defining a task as a plain text is deprecated. Use the "exec" task and pass arguments and options. - * @group legacy */ public function testExecTask(): void { $tasks = [ - // This form is deprecated. - 'touch -t 198006062359.59 '.$this->getSandboxFilepath('deprecated.txt'), - // Valid usage. [ 'task' => 'exec', 'command' => 'touch', 'arguments' => [ - 'current.txt', + 'file.txt', ], 'options' => [ // 1980-06-06 23:59:59. @@ -100,12 +94,8 @@ public function testExecTask(): void ]; $this->taskCollectionFactory($tasks)->run(); - $this->assertFileExists($this->getSandboxFilepath('deprecated.txt')); - $mtime = gmdate('Y-m-d H:i:s', filemtime($this->getSandboxFilepath('deprecated.txt'))); - $this->assertSame('1980-06-06 23:59:59', $mtime); - - $this->assertFileExists($this->getSandboxFilepath('current.txt')); - $mtime = gmdate('Y-m-d H:i:s', filemtime($this->getSandboxFilepath('current.txt'))); + $this->assertFileExists($this->getSandboxFilepath('file.txt')); + $mtime = gmdate('Y-m-d H:i:s', filemtime($this->getSandboxFilepath('file.txt'))); $this->assertSame('1980-06-06 23:59:59', $mtime); } From 36dc563477db9f3a61cdc1f94d764f2c7c47383a Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Fri, 15 May 2020 16:06:18 +0300 Subject: [PATCH 6/6] ISAICP-5825: Better deprecation message. --- src/Tasks/CollectionFactory/CollectionFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tasks/CollectionFactory/CollectionFactory.php b/src/Tasks/CollectionFactory/CollectionFactory.php index 59b6409c..fdb78086 100644 --- a/src/Tasks/CollectionFactory/CollectionFactory.php +++ b/src/Tasks/CollectionFactory/CollectionFactory.php @@ -95,7 +95,7 @@ public function getHelp() protected function taskFactory($task) { if (is_string($task)) { - @trigger_error('Defining a task as a plain text is deprecated. Use the "exec" task and pass arguments and options.', E_USER_DEPRECATED); + @trigger_error('Defining a task as a plain text is deprecated in openeuropa/task-runner:1.0.0 and is removed from openeuropa/task-runner:2.0.0. Use the "exec" task and pass arguments and options.', E_USER_DEPRECATED); return $this->taskExec($task)->interactive($this->isTtySupported()); }