Skip to content

Commit

Permalink
Merge 9c85cf4 into 75b4b0e
Browse files Browse the repository at this point in the history
  • Loading branch information
sabbelasichon committed Mar 1, 2020
2 parents 75b4b0e + 9c85cf4 commit 09ce0e5
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/Task/LocalShellTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
* file that was distributed with this source code.
*/

use Symfony\Component\OptionsResolver\OptionsResolver;
use TYPO3\Surf\Domain\Model\Application;
use TYPO3\Surf\Domain\Model\Deployment;
use TYPO3\Surf\Domain\Model\Node;
use TYPO3\Surf\Domain\Model\Task;
use TYPO3\Surf\Domain\Service\ShellCommandServiceAwareInterface;
use TYPO3\Surf\Domain\Service\ShellCommandServiceAwareTrait;
use TYPO3\Surf\Exception\InvalidConfigurationException;

/**
* A shell task for local packaging.
Expand All @@ -40,22 +40,16 @@ class LocalShellTask extends Task implements ShellCommandServiceAwareInterface

public function execute(Node $node, Application $application, Deployment $deployment, array $options = [])
{
$options = $this->configureOptions($options);
$replacePaths = [];
$replacePaths['{workspacePath}'] = escapeshellarg($deployment->getWorkspacePath($application));

if (!isset($options['command'])) {
throw new InvalidConfigurationException('Missing "command" option for LocalShellTask', 1311168045);
}
$command = $options['command'];
$command = str_replace(array_keys($replacePaths), $replacePaths, $command);

$ignoreErrors = isset($options['ignoreErrors']) && $options['ignoreErrors'] === true;
$logOutput = !(isset($options['logOutput']) && $options['logOutput'] === false);
$command = str_replace(array_keys($replacePaths), $replacePaths, $options['command']);

$localhost = new Node('localhost');
$localhost->onLocalhost();

$this->shell->executeOrSimulate($command, $localhost, $deployment, $ignoreErrors, $logOutput);
$this->shell->executeOrSimulate($command, $localhost, $deployment, $options['ignoreErrors'], $options['logOutput']);
}

public function simulate(Node $node, Application $application, Deployment $deployment, array $options = [])
Expand All @@ -68,15 +62,23 @@ public function rollback(Node $node, Application $application, Deployment $deplo
$replacePaths = [];
$replacePaths['{workspacePath}'] = escapeshellarg($deployment->getWorkspacePath($application));

if (!isset($options['rollbackCommand'])) {
if (null === $options['rollbackCommand']) {
return;
}
$command = $options['rollbackCommand'];
$command = str_replace(array_keys($replacePaths), $replacePaths, $command);

$command = str_replace(array_keys($replacePaths), $replacePaths, $options['rollbackCommand']);

$localhost = new Node('localhost');
$localhost->onLocalhost();

$this->shell->execute($command, $localhost, $deployment, true);
}

protected function resolveOptions(OptionsResolver $resolver)
{
$resolver->setRequired(['command']);
$resolver->setDefault('rollbackCommand', null);
$resolver->setDefault('ignoreErrors', false);
$resolver->setDefault('logOutput', false);
}
}
72 changes: 72 additions & 0 deletions tests/Unit/Task/LocalShellTaskTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace TYPO3\Surf\Tests\Unit\Task;

/*
* This file is part of TYPO3 Surf.
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

use TYPO3\Surf\Domain\Model\Task;
use TYPO3\Surf\Exception\InvalidConfigurationException;
use TYPO3\Surf\Task\LocalShellTask;

class LocalShellTaskTest extends BaseTaskTest
{

/**
* @test
*/
public function executeThrowsExceptionNoCommandGiven(): void
{
$this->expectException(InvalidConfigurationException::class);
$this->task->execute($this->node, $this->application, $this->deployment, []);
}

/**
* @test
* @dataProvider commands
*/
public function executeSomeCommandSuccessfully(string $command, string $expectedCommand): void
{
$this->task->execute(
$this->node,
$this->application,
$this->deployment,
['command' => $command]
);
$this->assertCommandExecuted($expectedCommand);
}

/**
* @test
* @dataProvider commands
*/
public function rollbackSomeCommandSuccessfully(string $command, string $expectedCommand): void
{
$this->task->rollback(
$this->node,
$this->application,
$this->deployment,
['rollbackCommand' => $command, 'command' => 'someCommand']
);
$this->assertCommandExecuted($expectedCommand);
}

public function commands(): array
{
return [
['ln -s {workspacePath}', sprintf('ln -s %s', escapeshellarg('./Data/Surf/TestDeployment/TestApplication'))],
];
}

/**
* @return LocalShellTask|Task
*/
protected function createTask()
{
return new LocalShellTask();
}
}

0 comments on commit 09ce0e5

Please sign in to comment.