Browse Source

[Nostromo] Exec task

pull/1/head
Andrés Montañez 8 years ago
parent
commit
fc851dd661
  1. 1
      CHANGELOG.md
  2. 26
      src/Task/BuiltIn/ExecTask.php
  3. 90
      tests/Command/BuiltIn/ExecTaskTest.php
  4. 97
      tests/Task/BuiltIn/ExecTaskTest.php

1
CHANGELOG.md

@ -2,6 +2,7 @@ CHANGELOG for 3.X
================= =================
* 3.X (2017-XX-XX) * 3.X (2017-XX-XX)
* Add new Exec task to execute arbitrary shell commands
* Add new Composer task, to update phar (composer/self-update) * Add new Composer task, to update phar (composer/self-update)
* [#344] Allow to flag Filesystem tasks * [#344] Allow to flag Filesystem tasks
* [PR#346] Add new File System task, to change file's modes (fs/chmod) * [PR#346] Add new File System task, to change file's modes (fs/chmod)

26
src/Task/BuiltIn/ExecTask.php

@ -12,6 +12,7 @@ namespace Mage\Task\BuiltIn;
use Mage\Task\Exception\ErrorException; use Mage\Task\Exception\ErrorException;
use Mage\Task\AbstractTask; use Mage\Task\AbstractTask;
use Symfony\Component\Process\Process;
/** /**
* Exec task. Allows you to execute arbitrary commands. * Exec task. Allows you to execute arbitrary commands.
@ -35,11 +36,11 @@ class ExecTask extends AbstractTask
{ {
$options = $this->getOptions(); $options = $this->getOptions();
if ('' !== $options['descr']) { if ($options['desc']) {
return (string) $options['descr']; return '[Exec] ' . $options['desc'];
} }
return '[Exec] Executing custom command'; return '[Exec] Custom command';
} }
/** /**
@ -51,16 +52,11 @@ class ExecTask extends AbstractTask
{ {
$options = $this->getOptions(); $options = $this->getOptions();
if ('' === $options['cmd']) { if (!$options['cmd']) {
throw new ErrorException('What about if you gave me a command to execute?'); throw new ErrorException('Parameter "cmd" is not defined');
}
// 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();
} }
/** @var Process $process */
$process = $this->runtime->runCommand($options['cmd'], $options['timeout']); $process = $this->runtime->runCommand($options['cmd'], $options['timeout']);
return $process->isSuccessful(); return $process->isSuccessful();
} }
@ -70,12 +66,8 @@ class ExecTask extends AbstractTask
*/ */
protected function getOptions() protected function getOptions()
{ {
$options = array_merge([ $options = array_merge(
'cmd' => '', ['cmd' => '', 'desc' => '', 'timeout' => 120],
'descr' => '',
'jail' => true,
'timeout' => 120
],
$this->options $this->options
); );

90
tests/Command/BuiltIn/ExecTaskTest.php

@ -1,90 +0,0 @@
<?php
namespace Mage\Tests\Command\BuiltIn;
use Mage\Runtime\Runtime;
use Mage\Task\BuiltIn\ExecTask;
use PHPUnit_Framework_TestCase as TestCase;
use Symfony\Component\Process\Process;
class ExecTaskTest extends TestCase
{
public function testBasics()
{
$task = new ExecTask();
$this->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;
}
}

97
tests/Task/BuiltIn/ExecTaskTest.php

@ -0,0 +1,97 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* 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());
}
}
}
Loading…
Cancel
Save