From fc851dd6618e7c14e65a578b39dfae99f9a75c72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Sat, 25 Feb 2017 19:31:25 -0300 Subject: [PATCH] [Nostromo] Exec task --- CHANGELOG.md | 1 + src/Task/BuiltIn/ExecTask.php | 26 +++---- tests/Command/BuiltIn/ExecTaskTest.php | 90 ------------------------ tests/Task/BuiltIn/ExecTaskTest.php | 97 ++++++++++++++++++++++++++ 4 files changed, 107 insertions(+), 107 deletions(-) delete mode 100644 tests/Command/BuiltIn/ExecTaskTest.php create mode 100644 tests/Task/BuiltIn/ExecTaskTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 9612350..e0c24a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ CHANGELOG for 3.X ================= * 3.X (2017-XX-XX) + * Add new Exec task to execute arbitrary shell commands * Add new Composer task, to update phar (composer/self-update) * [#344] Allow to flag Filesystem tasks * [PR#346] Add new File System task, to change file's modes (fs/chmod) diff --git a/src/Task/BuiltIn/ExecTask.php b/src/Task/BuiltIn/ExecTask.php index 095add9..0c7fc17 100644 --- a/src/Task/BuiltIn/ExecTask.php +++ b/src/Task/BuiltIn/ExecTask.php @@ -12,6 +12,7 @@ namespace Mage\Task\BuiltIn; use Mage\Task\Exception\ErrorException; use Mage\Task\AbstractTask; +use Symfony\Component\Process\Process; /** * Exec task. Allows you to execute arbitrary commands. @@ -35,11 +36,11 @@ class ExecTask extends AbstractTask { $options = $this->getOptions(); - if ('' !== $options['descr']) { - return (string) $options['descr']; + if ($options['desc']) { + return '[Exec] ' . $options['desc']; } - return '[Exec] Executing custom command'; + return '[Exec] Custom command'; } /** @@ -51,16 +52,11 @@ class ExecTask extends AbstractTask { $options = $this->getOptions(); - if ('' === $options['cmd']) { - throw new ErrorException('What about if you gave me a command to execute?'); - } - - // If not jailed, it must run as remote command - if (false === $options['jail']) { - $process = $this->runtime->runRemoteCommand($options['cmd'], false, $options['timeout']); - return $process->isSuccessful(); + if (!$options['cmd']) { + throw new ErrorException('Parameter "cmd" is not defined'); } + /** @var Process $process */ $process = $this->runtime->runCommand($options['cmd'], $options['timeout']); return $process->isSuccessful(); } @@ -70,12 +66,8 @@ class ExecTask extends AbstractTask */ protected function getOptions() { - $options = array_merge([ - 'cmd' => '', - 'descr' => '', - 'jail' => true, - 'timeout' => 120 - ], + $options = array_merge( + ['cmd' => '', 'desc' => '', 'timeout' => 120], $this->options ); diff --git a/tests/Command/BuiltIn/ExecTaskTest.php b/tests/Command/BuiltIn/ExecTaskTest.php deleted file mode 100644 index ea1bae8..0000000 --- a/tests/Command/BuiltIn/ExecTaskTest.php +++ /dev/null @@ -1,90 +0,0 @@ -assertSame('exec', $task->getName()); - $this->assertSame('[Exec] Executing custom command', $task->getDescription()); - } - - public function testCustomDescription() - { - $task = new ExecTask(); - $task->setOptions(['descr' => '[My project] This is my wonderful task']); - $this->assertSame('[My project] This is my wonderful task', $task->getDescription()); - } - - /** - * @expectedException \Mage\Task\Exception\ErrorException - */ - public function testNoCommandProvided() - { - $task = new ExecTask(); - $task->execute(); - } - - public function testNonJailedCommand() - { - $runtime = $this->getMockBuilder(Runtime::class) - ->setMethods(['runRemoteCommand']) - ->getMock(); - - $runtime - ->expects($this->once()) - ->method('runRemoteCommand') - ->with('rm -rf /') - ->willReturn($this->mockProcess(true)); - - - $task = $this->getTask($runtime); - $task->setOptions(['cmd' => 'rm -rf /', 'jail' => false]); - $this->assertTrue($task->execute()); - } - - public function testRegularCommand() - { - $runtime = $this->getMockBuilder(Runtime::class) - ->setMethods(['runCommand']) - ->getMock(); - - $runtime - ->expects($this->once()) - ->method('runCommand') - ->with('rm -rf /', 10) - ->willReturn($this->mockProcess(true)); - - $task = $this->getTask($runtime); - $task->setOptions(['cmd' => 'rm -rf /', 'timeout' => 10]); - $task->execute(); - } - - private function getTask($runtime) - { - $task = new ExecTask(); - $task->setRuntime($runtime); - - return $task; - } - - private function mockProcess($successful) - { - $process = $this->getMockBuilder(Process::class) - ->disableOriginalConstructor() - ->getMock(); - $process - ->expects($this->any()) - ->method('isSuccessful') - ->willReturn($successful); - - return $process; - } -} diff --git a/tests/Task/BuiltIn/ExecTaskTest.php b/tests/Task/BuiltIn/ExecTaskTest.php new file mode 100644 index 0000000..d548176 --- /dev/null +++ b/tests/Task/BuiltIn/ExecTaskTest.php @@ -0,0 +1,97 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Mage\Tests\Task\BuiltIn; + +use Mage\Task\Exception\ErrorException; +use Mage\Task\BuiltIn\ExecTask; +use Exception; +use Mage\Tests\Runtime\RuntimeMockup; +use PHPUnit_Framework_TestCase as TestCase; + +class ExecTest extends TestCase +{ + public function testSimpleCommand() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new ExecTask(); + $task->setOptions(['cmd' => 'ls -l', 'desc' => 'Loading docker']); + $task->setRuntime($runtime); + + $this->assertContains('[Exec] Loading docker', $task->getDescription()); + $task->execute(); + + $ranCommands = $runtime->getRanCommands(); + + $testCase = array( + 0 => 'ls -l', + ); + + // Check total of Executed Commands + $this->assertEquals(count($testCase), count($ranCommands)); + + // Check Generated Commands + foreach ($testCase as $index => $command) { + $this->assertEquals($command, $ranCommands[$index]); + } + } + + public function testCommandWithoutDescription() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new ExecTask(); + $task->setOptions(['cmd' => 'ls -la']); + $task->setRuntime($runtime); + + $this->assertContains('[Exec] Custom command', $task->getDescription()); + $task->execute(); + + $ranCommands = $runtime->getRanCommands(); + + $testCase = array( + 0 => 'ls -la', + ); + + // Check total of Executed Commands + $this->assertEquals(count($testCase), count($ranCommands)); + + // Check Generated Commands + foreach ($testCase as $index => $command) { + $this->assertEquals($command, $ranCommands[$index]); + } + } + + public function testWithoutCommand() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new ExecTask(); + $task->setOptions(['desc' => 'Loading docker']); + $task->setRuntime($runtime); + + $this->assertContains('[Exec] Loading docker', $task->getDescription()); + + try { + $task->execute(); + $this->assertTrue(false, 'Task did not failed'); + } catch (Exception $exception) { + $this->assertTrue($exception instanceof ErrorException); + $this->assertEquals('Parameter "cmd" is not defined', $exception->getMessage()); + } + } +}