Merge pull request #238 from meadsteve/command-execution

Move shell command execution out of builder into separate class and fix issue #218
This commit is contained in:
Steve B 2013-12-20 01:38:03 -08:00
commit bc838e0c43
3 changed files with 206 additions and 55 deletions

View file

@ -9,6 +9,7 @@
namespace PHPCI;
use PHPCI\Helper\CommandExecutor;
use PHPCI\Helper\MailerFactory;
use PHPCI\Model\Build;
use b8\Store;
@ -96,6 +97,11 @@ class Builder implements LoggerAwareInterface, BuildLogger
*/
protected $pluginExecutor;
/**
* @var Helper\CommandExecutor
*/
protected $commandExecutor;
/**
* Set up the builder.
* @param \PHPCI\Model\Build $build
@ -109,6 +115,8 @@ class Builder implements LoggerAwareInterface, BuildLogger
$this->build = $build;
$this->store = Store\Factory::getStore('Build');
$this->pluginExecutor = new Plugin\Util\Executor($this->buildPluginFactory($build), $this);
$this->commandExecutor = new CommandExecutor($this, PHPCI_DIR, $this->quiet, $this->verbose);
}
/**
@ -209,27 +217,7 @@ class Builder implements LoggerAwareInterface, BuildLogger
*/
public function executeCommand()
{
$command = call_user_func_array('sprintf', func_get_args());
if (!$this->quiet) {
$this->log('Executing: ' . $command);
}
$status = 0;
exec($command, $this->lastOutput, $status);
if (!empty($this->lastOutput) && ($this->verbose || $status != 0)) {
$this->log($this->lastOutput);
}
$rtn = false;
if ($status == 0) {
$rtn = true;
}
return $rtn;
return $this->commandExecutor->buildAndExecuteCommand(func_get_args());
}
/**
@ -237,7 +225,17 @@ class Builder implements LoggerAwareInterface, BuildLogger
*/
public function getLastOutput()
{
return implode(PHP_EOL, $this->lastOutput);
return $this->commandExecutor->getLastOutput();
}
/**
* Find a binary required by a plugin.
* @param $binary
* @return null|string
*/
public function findBinary($binary)
{
return $this->commandExecutor->findBinary($binary);
}
/**
@ -375,39 +373,6 @@ class Builder implements LoggerAwareInterface, BuildLogger
return true;
}
/**
* Find a binary required by a plugin.
* @param $binary
* @return null|string
*/
public function findBinary($binary)
{
if (is_string($binary)) {
$binary = array($binary);
}
foreach ($binary as $bin) {
// Check project root directory:
if (is_file(PHPCI_DIR . $bin)) {
return PHPCI_DIR . $bin;
}
// Check Composer bin dir:
if (is_file(PHPCI_DIR . 'vendor/bin/' . $bin)) {
return PHPCI_DIR . 'vendor/bin/' . $bin;
}
// Use "which"
$which = trim(shell_exec('which ' . $bin));
if (!empty($which)) {
return $which;
}
}
return null;
}
/**
* Sets a logger instance on the object
*

View file

@ -0,0 +1,131 @@
<?php
namespace PHPCI\Helper;
use PHPCI\BuildLogger;
class CommandExecutor
{
/**
* @var \PHPCI\BuildLogger
*/
protected $logger;
/**
* @var bool
*/
protected $quiet;
/**
* @var bool
*/
protected $verbose;
protected $lastOutput;
/**
* The path which findBinary will look in.
* @var string
*/
protected $rootDir;
/**
* @param BuildLogger $logger
* @param $rootDir
* @param bool $quiet
* @param bool $verbose
*/
public function __construct(BuildLogger $logger, $rootDir, &$quiet = false, &$verbose = false)
{
$this->logger = $logger;
$this->quiet = $quiet;
$this->verbose = $verbose;
$this->lastOutput = array();
$this->rootDir = $rootDir;
}
/**
* Executes shell commands. Accepts multiple arguments the first
* is the template and everything else is inserted in. c.f. sprintf
* @return bool Indicates success
*/
public function executeCommand()
{
return $this->buildAndExecuteCommand(func_get_args());
}
/**
* Executes shell commands.
* @param array $args
* @return bool Indicates success
*/
public function buildAndExecuteCommand($args = array())
{
$this->lastOutput = array();
$command = call_user_func_array('sprintf', $args);
if ($this->quiet) {
$this->logger->log('Executing: ' . $command);
}
$status = 0;
exec($command, $this->lastOutput, $status);
if (!empty($this->lastOutput) && ($this->verbose|| $status != 0)) {
$this->logger->log($this->lastOutput);
}
$rtn = false;
if ($status == 0) {
$rtn = true;
}
return $rtn;
}
/**
* Returns the output from the last command run.
*/
public function getLastOutput()
{
return implode(PHP_EOL, $this->lastOutput);
}
/**
* Find a binary required by a plugin.
* @param $binary
* @return null|string
*/
public function findBinary($binary)
{
if (is_string($binary)) {
$binary = array($binary);
}
foreach ($binary as $bin) {
// Check project root directory:
if (is_file($this->rootDir . $bin)) {
return $this->rootDir . $bin;
}
// Check Composer bin dir:
if (is_file($this->rootDir . 'vendor/bin/' . $bin)) {
return $this->rootDir . 'vendor/bin/' . $bin;
}
// Use "which"
$which = trim(shell_exec('which ' . $bin));
if (!empty($which)) {
return $which;
}
}
return null;
}
}

View file

@ -0,0 +1,55 @@
<?php
namespace PHPCI\Plugin\Tests\Helper;
use PHPCI\Helper\CommandExecutor;
use \Prophecy\PhpUnit\ProphecyTestCase;
class CommandExecutorTest extends ProphecyTestCase
{
/**
* @var CommandExecutor
*/
protected $testedExecutor;
protected function setUp()
{
parent::setUp();
$mockBuildLogger = $this->prophesize('\PHPCI\BuildLogger');
$this->testedExecutor = new CommandExecutor($mockBuildLogger->reveal(), __DIR__ . "/");
}
public function testGetLastOutput_ReturnsOutputOfCommand()
{
$this->testedExecutor->buildAndExecuteCommand(array('echo "%s"', 'Hello World'));
$output = $this->testedExecutor->getLastOutput();
$this->assertEquals("Hello World", $output);
}
public function testGetLastOutput_ForgetsPreviousCommandOutput()
{
$this->testedExecutor->buildAndExecuteCommand(array('echo "%s"', 'Hello World'));
$this->testedExecutor->buildAndExecuteCommand(array('echo "%s"', 'Hello Tester'));
$output = $this->testedExecutor->getLastOutput();
$this->assertEquals("Hello Tester", $output);
}
public function testExecuteCommand_ReturnsTrueForValidCommands()
{
$returnValue = $this->testedExecutor->buildAndExecuteCommand(array('echo "%s"', 'Hello World'));
$this->assertTrue($returnValue);
}
public function testExecuteCommand_ReturnsFalseForInvalidCommands()
{
$returnValue = $this->testedExecutor->buildAndExecuteCommand(array('eerfdcvcho "%s"', 'Hello World'));
$this->assertFalse($returnValue);
}
public function testFindBinary_ReturnsPathInSpecifiedRoot()
{
$thisFileName = "CommandExecutorTest.php";
$returnValue = $this->testedExecutor->findBinary($thisFileName);
$this->assertEquals(__DIR__ . "/" . $thisFileName, $returnValue);
}
}