From 33364f5e2466a57a0b3d6c578289bc383f48f59b Mon Sep 17 00:00:00 2001 From: thePanz Date: Tue, 7 Feb 2017 23:27:00 +0100 Subject: [PATCH] Added Flags to FS tasks (closes #344) --- src/Task/BuiltIn/FS/AbstractFileTask.php | 27 +++++++++++++ src/Task/BuiltIn/FS/CopyTask.php | 11 +++++- src/Task/BuiltIn/FS/LinkTask.php | 11 +++++- src/Task/BuiltIn/FS/MoveTask.php | 5 ++- src/Task/BuiltIn/FS/RemoveTask.php | 5 ++- tests/Task/BuiltIn/FileSystemTaskTest.php | 46 ++++++++++++++++++----- 6 files changed, 88 insertions(+), 17 deletions(-) diff --git a/src/Task/BuiltIn/FS/AbstractFileTask.php b/src/Task/BuiltIn/FS/AbstractFileTask.php index c2e4685..3cb2a92 100644 --- a/src/Task/BuiltIn/FS/AbstractFileTask.php +++ b/src/Task/BuiltIn/FS/AbstractFileTask.php @@ -46,6 +46,33 @@ abstract class AbstractFileTask extends AbstractTask */ abstract protected function getParameters(); + /** + * Returns the default "flags". + * + * @return null|string + */ + protected function getDefaultFlags() + { + return null; + } + + /** + * Returns the flags for the current command. + * + * @return string + */ + protected function getFlags() + { + $options = $this->getOptions(); + $flags = $this->getDefaultFlags(); + + if (array_key_exists('flags', $options) && !empty($options['flags'])) { + $flags = trim($options['flags']); + } + + return empty($flags) ? '' : $flags.' '; + } + /** * Returns a file with the placeholders replaced * diff --git a/src/Task/BuiltIn/FS/CopyTask.php b/src/Task/BuiltIn/FS/CopyTask.php index fc7eaf4..1eebb18 100644 --- a/src/Task/BuiltIn/FS/CopyTask.php +++ b/src/Task/BuiltIn/FS/CopyTask.php @@ -28,7 +28,7 @@ class CopyTask extends AbstractFileTask public function getDescription() { try { - return sprintf('[FS] Copy "%s" to "%s"', $this->getFile('from'), $this->getFile('to')); + return sprintf('[FS] Copy %s"%s" to "%s"', $this->getFlags(), $this->getFile('from'), $this->getFile('to')); } catch (Exception $exception) { return '[FS] Copy [missing parameters]'; } @@ -39,7 +39,9 @@ class CopyTask extends AbstractFileTask $copyFrom = $this->getFile('from'); $copyTo = $this->getFile('to'); - $cmd = sprintf('cp -p %s %s', $copyFrom, $copyTo); + $flags = $this->getFlags(); + + $cmd = sprintf('cp %s"%s" "%s"', $flags, $copyFrom, $copyTo); /** @var Process $process */ $process = $this->runtime->runCommand($cmd); @@ -51,4 +53,9 @@ class CopyTask extends AbstractFileTask { return ['from', 'to']; } + + protected function getDefaultFlags() + { + return '-p'; + } } diff --git a/src/Task/BuiltIn/FS/LinkTask.php b/src/Task/BuiltIn/FS/LinkTask.php index 9cf6994..564fb69 100644 --- a/src/Task/BuiltIn/FS/LinkTask.php +++ b/src/Task/BuiltIn/FS/LinkTask.php @@ -28,7 +28,7 @@ class LinkTask extends AbstractFileTask public function getDescription() { try { - return sprintf('[FS] Link "%s" to "%s"', $this->getFile('from'), $this->getFile('to')); + return sprintf('[FS] Link %s"%s" to "%s"', $this->getFlags(), $this->getFile('from'), $this->getFile('to')); } catch (Exception $exception) { return '[FS] Link [missing parameters]'; } @@ -39,7 +39,9 @@ class LinkTask extends AbstractFileTask $linkFrom = $this->getFile('from'); $linkTo = $this->getFile('to'); - $cmd = sprintf('ln -snf %s %s', $linkFrom, $linkTo); + $flags = $this->getFlags(); + + $cmd = sprintf('ln %s"%s" "%s"', $flags, $linkFrom, $linkTo); /** @var Process $process */ $process = $this->runtime->runCommand($cmd); @@ -51,4 +53,9 @@ class LinkTask extends AbstractFileTask { return ['from', 'to']; } + + protected function getDefaultFlags() + { + return '-snf'; + } } diff --git a/src/Task/BuiltIn/FS/MoveTask.php b/src/Task/BuiltIn/FS/MoveTask.php index 4da7146..e54e057 100644 --- a/src/Task/BuiltIn/FS/MoveTask.php +++ b/src/Task/BuiltIn/FS/MoveTask.php @@ -28,7 +28,7 @@ class MoveTask extends AbstractFileTask public function getDescription() { try { - return sprintf('[FS] Move "%s" to "%s"', $this->getFile('from'), $this->getFile('to')); + return sprintf('[FS] Move %s"%s" to "%s"', $this->getFlags(), $this->getFile('from'), $this->getFile('to')); } catch (Exception $exception) { return '[FS] Move [missing parameters]'; } @@ -38,8 +38,9 @@ class MoveTask extends AbstractFileTask { $moveFrom = $this->getFile('from'); $moveTo = $this->getFile('to'); + $flags = $this->getFlags(); - $cmd = sprintf('mv %s %s', $moveFrom, $moveTo); + $cmd = sprintf('mv %s"%s" "%s"', $flags, $moveFrom, $moveTo); /** @var Process $process */ $process = $this->runtime->runCommand($cmd); diff --git a/src/Task/BuiltIn/FS/RemoveTask.php b/src/Task/BuiltIn/FS/RemoveTask.php index c55c4a2..ba27ef4 100644 --- a/src/Task/BuiltIn/FS/RemoveTask.php +++ b/src/Task/BuiltIn/FS/RemoveTask.php @@ -28,7 +28,7 @@ class RemoveTask extends AbstractFileTask public function getDescription() { try { - return sprintf('[FS] Remove "%s"', $this->getFile('file')); + return sprintf('[FS] Remove %s"%s"', $this->getFlags(), $this->getFile('file')); } catch (Exception $exception) { return '[FS] Remove [missing parameters]'; } @@ -37,8 +37,9 @@ class RemoveTask extends AbstractFileTask public function execute() { $file = $this->getFile('file'); + $flags = $this->getFlags(); - $cmd = sprintf('rm %s', $file); + $cmd = sprintf('rm %s"%s"', $flags, $file); /** @var Process $process */ $process = $this->runtime->runCommand($cmd); diff --git a/tests/Task/BuiltIn/FileSystemTaskTest.php b/tests/Task/BuiltIn/FileSystemTaskTest.php index c28c3d5..c32d4aa 100644 --- a/tests/Task/BuiltIn/FileSystemTaskTest.php +++ b/tests/Task/BuiltIn/FileSystemTaskTest.php @@ -38,7 +38,7 @@ class FileSystemTaskTest extends TestCase $ranCommands = $runtime->getRanCommands(); $testCase = array( - 0 => 'cp -p a.txt b.txt', + 0 => 'cp -p "a.txt" "b.txt"', ); // Check total of Executed Commands @@ -67,7 +67,7 @@ class FileSystemTaskTest extends TestCase $ranCommands = $runtime->getRanCommands(); $testCase = array( - 0 => 'cp -p test.txt b.txt', + 0 => 'cp -p "test.txt" "b.txt"', ); // Check total of Executed Commands @@ -98,7 +98,7 @@ class FileSystemTaskTest extends TestCase $ranCommands = $runtime->getRanCommands(); $testCase = array( - 0 => 'cp -p localhost.txt 1234.yml', + 0 => 'cp -p "localhost.txt" "1234.yml"', ); // Check total of Executed Commands @@ -147,7 +147,7 @@ class FileSystemTaskTest extends TestCase $ranCommands = $runtime->getRanCommands(); $testCase = array( - 0 => 'mv a.txt b.txt', + 0 => 'mv "a.txt" "b.txt"', ); // Check total of Executed Commands @@ -176,7 +176,7 @@ class FileSystemTaskTest extends TestCase $ranCommands = $runtime->getRanCommands(); $testCase = array( - 0 => 'mv test.txt b.txt', + 0 => 'mv "test.txt" "b.txt"', ); // Check total of Executed Commands @@ -224,7 +224,35 @@ class FileSystemTaskTest extends TestCase $ranCommands = $runtime->getRanCommands(); $testCase = array( - 0 => 'rm a.txt', + 0 => 'rm "a.txt"', + ); + + // 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 testRemoveWithFlagsTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new RemoveTask(); + $task->setOptions(['file' => 'a.txt', 'flags' => '-fr']); + $task->setRuntime($runtime); + + $this->assertContains('a.txt', $task->getDescription()); + $task->execute(); + + $ranCommands = $runtime->getRanCommands(); + + $testCase = array( + 0 => 'rm -fr "a.txt"', ); // Check total of Executed Commands @@ -252,7 +280,7 @@ class FileSystemTaskTest extends TestCase $ranCommands = $runtime->getRanCommands(); $testCase = array( - 0 => 'rm test.txt', + 0 => 'rm "test.txt"', ); // Check total of Executed Commands @@ -301,7 +329,7 @@ class FileSystemTaskTest extends TestCase $ranCommands = $runtime->getRanCommands(); $testCase = array( - 0 => 'ln -snf a.txt b.txt', + 0 => 'ln -snf "a.txt" "b.txt"', ); // Check total of Executed Commands @@ -330,7 +358,7 @@ class FileSystemTaskTest extends TestCase $ranCommands = $runtime->getRanCommands(); $testCase = array( - 0 => 'ln -snf test.txt b.txt', + 0 => 'ln -snf "test.txt" "b.txt"', ); // Check total of Executed Commands