From f6886f3d28984512a79dc09bdbc7088869d59a4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Sat, 11 Feb 2017 00:30:49 -0300 Subject: [PATCH] [Nostromo] Refactor FS Tasks flagging system --- CHANGELOG.md | 1 + src/Task/BuiltIn/FS/AbstractFileTask.php | 27 ------- src/Task/BuiltIn/FS/ChangeModeTask.php | 12 +-- src/Task/BuiltIn/FS/CopyTask.php | 13 ++-- src/Task/BuiltIn/FS/LinkTask.php | 13 ++-- src/Task/BuiltIn/FS/MoveTask.php | 13 +++- src/Task/BuiltIn/FS/RemoveTask.php | 13 +++- tests/Task/BuiltIn/ChangeModeTaskTest.php | 9 +-- tests/Task/BuiltIn/FileSystemTaskTest.php | 95 ++++++++++++++++++++++- 9 files changed, 131 insertions(+), 65 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25ce595..0be809f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ CHANGELOG for 3.x ================= * 3.0.X (2017-XX-XX) + * [#344] Allow to flag Filesystem tasks * [PR#346] Add new File System task, to change file's modes (fs/chmod) * [BUGFIX] [PR#342] Ignore empty exclude lines * [PR#330] Allow Composer task options to be overwritten at environment level diff --git a/src/Task/BuiltIn/FS/AbstractFileTask.php b/src/Task/BuiltIn/FS/AbstractFileTask.php index 302ef24..eb782b2 100644 --- a/src/Task/BuiltIn/FS/AbstractFileTask.php +++ b/src/Task/BuiltIn/FS/AbstractFileTask.php @@ -48,33 +48,6 @@ 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/ChangeModeTask.php b/src/Task/BuiltIn/FS/ChangeModeTask.php index 2f5c5bd..1422c3f 100644 --- a/src/Task/BuiltIn/FS/ChangeModeTask.php +++ b/src/Task/BuiltIn/FS/ChangeModeTask.php @@ -28,11 +28,7 @@ class ChangeModeTask extends AbstractFileTask public function getDescription() { try { - $description = sprintf('[FS] Change mode of "%s" to "%s"', $this->getFile('file'), $this->options['mode']); - if ($this->options['flags'] != null) { - $description = sprintf('[FS] Change mode of "%s" to "%s" with flags "%s"', $this->getFile('file'), $this->options['mode'], $this->options['flags']); - } - return $description; + return sprintf('[FS] Change mode of "%s" to "%s"', $this->getFile('file'), $this->options['mode']); } catch (Exception $exception) { return '[FS] Chmod [missing parameters]'; } @@ -40,7 +36,11 @@ class ChangeModeTask extends AbstractFileTask public function execute() { - $cmd = sprintf('chmod %s %s %s', $this->options['flags'], $this->options['mode'], $this->getFile('file')); + $file = $this->getFile('file'); + $mode = $this->options['mode']; + $flags = $this->options['flags']; + + $cmd = sprintf('chmod %s %s "%s"', $flags, $mode, $file); /** @var Process $process */ $process = $this->runtime->runCommand($cmd); diff --git a/src/Task/BuiltIn/FS/CopyTask.php b/src/Task/BuiltIn/FS/CopyTask.php index 1eebb18..469b19c 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"%s" to "%s"', $this->getFlags(), $this->getFile('from'), $this->getFile('to')); + return sprintf('[FS] Copy "%s" to "%s"', $this->getFile('from'), $this->getFile('to')); } catch (Exception $exception) { return '[FS] Copy [missing parameters]'; } @@ -38,10 +38,9 @@ class CopyTask extends AbstractFileTask { $copyFrom = $this->getFile('from'); $copyTo = $this->getFile('to'); + $flags = $this->options['flags']; - $flags = $this->getFlags(); - - $cmd = sprintf('cp %s"%s" "%s"', $flags, $copyFrom, $copyTo); + $cmd = sprintf('cp %s "%s" "%s"', $flags, $copyFrom, $copyTo); /** @var Process $process */ $process = $this->runtime->runCommand($cmd); @@ -51,11 +50,11 @@ class CopyTask extends AbstractFileTask protected function getParameters() { - return ['from', 'to']; + return ['from', 'to', 'flags']; } - protected function getDefaultFlags() + public function getDefaults() { - return '-p'; + return ['flags' => '-p']; } } diff --git a/src/Task/BuiltIn/FS/LinkTask.php b/src/Task/BuiltIn/FS/LinkTask.php index 564fb69..12721e0 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"%s" to "%s"', $this->getFlags(), $this->getFile('from'), $this->getFile('to')); + return sprintf('[FS] Link "%s" to "%s"', $this->getFile('from'), $this->getFile('to')); } catch (Exception $exception) { return '[FS] Link [missing parameters]'; } @@ -38,10 +38,9 @@ class LinkTask extends AbstractFileTask { $linkFrom = $this->getFile('from'); $linkTo = $this->getFile('to'); + $flags = $this->options['flags']; - $flags = $this->getFlags(); - - $cmd = sprintf('ln %s"%s" "%s"', $flags, $linkFrom, $linkTo); + $cmd = sprintf('ln %s "%s" "%s"', $flags, $linkFrom, $linkTo); /** @var Process $process */ $process = $this->runtime->runCommand($cmd); @@ -51,11 +50,11 @@ class LinkTask extends AbstractFileTask protected function getParameters() { - return ['from', 'to']; + return ['from', 'to', 'flags']; } - protected function getDefaultFlags() + public function getDefaults() { - return '-snf'; + return ['flags' => '-snf']; } } diff --git a/src/Task/BuiltIn/FS/MoveTask.php b/src/Task/BuiltIn/FS/MoveTask.php index e54e057..a13ecc1 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"%s" to "%s"', $this->getFlags(), $this->getFile('from'), $this->getFile('to')); + return sprintf('[FS] Move "%s" to "%s"', $this->getFile('from'), $this->getFile('to')); } catch (Exception $exception) { return '[FS] Move [missing parameters]'; } @@ -38,9 +38,9 @@ class MoveTask extends AbstractFileTask { $moveFrom = $this->getFile('from'); $moveTo = $this->getFile('to'); - $flags = $this->getFlags(); + $flags = $this->options['flags']; - $cmd = sprintf('mv %s"%s" "%s"', $flags, $moveFrom, $moveTo); + $cmd = sprintf('mv %s "%s" "%s"', $flags, $moveFrom, $moveTo); /** @var Process $process */ $process = $this->runtime->runCommand($cmd); @@ -50,6 +50,11 @@ class MoveTask extends AbstractFileTask protected function getParameters() { - return ['from', 'to']; + return ['from', 'to', 'flags']; + } + + public function getDefaults() + { + return ['flags' => null]; } } diff --git a/src/Task/BuiltIn/FS/RemoveTask.php b/src/Task/BuiltIn/FS/RemoveTask.php index ba27ef4..1f90724 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"%s"', $this->getFlags(), $this->getFile('file')); + return sprintf('[FS] Remove "%s"', $this->getFile('file')); } catch (Exception $exception) { return '[FS] Remove [missing parameters]'; } @@ -37,9 +37,9 @@ class RemoveTask extends AbstractFileTask public function execute() { $file = $this->getFile('file'); - $flags = $this->getFlags(); + $flags = $this->options['flags']; - $cmd = sprintf('rm %s"%s"', $flags, $file); + $cmd = sprintf('rm %s "%s"', $flags, $file); /** @var Process $process */ $process = $this->runtime->runCommand($cmd); @@ -49,6 +49,11 @@ class RemoveTask extends AbstractFileTask protected function getParameters() { - return ['file']; + return ['file', 'flags']; + } + + public function getDefaults() + { + return ['flags' => null]; } } diff --git a/tests/Task/BuiltIn/ChangeModeTaskTest.php b/tests/Task/BuiltIn/ChangeModeTaskTest.php index d0481a6..2548742 100644 --- a/tests/Task/BuiltIn/ChangeModeTaskTest.php +++ b/tests/Task/BuiltIn/ChangeModeTaskTest.php @@ -29,14 +29,13 @@ class ChangeModeTest extends TestCase $task->setRuntime($runtime); $this->assertContains('a.txt', $task->getDescription()); - $this->assertContains('-R', $task->getDescription()); $this->assertContains('o+w', $task->getDescription()); $task->execute(); $ranCommands = $runtime->getRanCommands(); $testCase = array( - 0 => 'chmod -R o+w a.txt', + 0 => 'chmod -R o+w "a.txt"', ); // Check total of Executed Commands @@ -59,14 +58,13 @@ class ChangeModeTest extends TestCase $task->setRuntime($runtime); $this->assertContains('a.txt', $task->getDescription()); - $this->assertNotContains('-R', $task->getDescription()); $this->assertContains('o+w', $task->getDescription()); $task->execute(); $ranCommands = $runtime->getRanCommands(); $testCase = array( - 0 => 'chmod o+w a.txt', + 0 => 'chmod o+w "a.txt"', ); // Check total of Executed Commands @@ -89,14 +87,13 @@ class ChangeModeTest extends TestCase $task->setRuntime($runtime); $this->assertContains('test.txt', $task->getDescription()); - $this->assertContains('-R', $task->getDescription()); $this->assertContains('o+w', $task->getDescription()); $task->execute(); $ranCommands = $runtime->getRanCommands(); $testCase = array( - 0 => 'chmod -R o+w test.txt', + 0 => 'chmod -R o+w "test.txt"', ); // Check total of Executed Commands diff --git a/tests/Task/BuiltIn/FileSystemTaskTest.php b/tests/Task/BuiltIn/FileSystemTaskTest.php index c32d4aa..49bfd60 100644 --- a/tests/Task/BuiltIn/FileSystemTaskTest.php +++ b/tests/Task/BuiltIn/FileSystemTaskTest.php @@ -50,6 +50,35 @@ class FileSystemTaskTest extends TestCase } } + public function testCopyTaskWithFlags() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new CopyTask(); + $task->setOptions(['from' => 'a.txt', 'to' => 'b.txt', 'flags' => '-rp']); + $task->setRuntime($runtime); + + $this->assertContains('a.txt', $task->getDescription()); + $this->assertContains('b.txt', $task->getDescription()); + $task->execute(); + + $ranCommands = $runtime->getRanCommands(); + + $testCase = array( + 0 => 'cp -rp "a.txt" "b.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 testCopyReplaceTask() { $runtime = new RuntimeMockup(); @@ -147,7 +176,36 @@ 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 + $this->assertEquals(count($testCase), count($ranCommands)); + + // Check Generated Commands + foreach ($testCase as $index => $command) { + $this->assertEquals($command, $ranCommands[$index]); + } + } + + public function testMoveWithFlagsTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new MoveTask(); + $task->setOptions(['from' => 'a.txt', 'to' => 'b.txt', 'flags' => '-n']); + $task->setRuntime($runtime); + + $this->assertContains('a.txt', $task->getDescription()); + $this->assertContains('b.txt', $task->getDescription()); + $task->execute(); + + $ranCommands = $runtime->getRanCommands(); + + $testCase = array( + 0 => 'mv -n "a.txt" "b.txt"', ); // Check total of Executed Commands @@ -176,7 +234,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 +282,7 @@ class FileSystemTaskTest extends TestCase $ranCommands = $runtime->getRanCommands(); $testCase = array( - 0 => 'rm "a.txt"', + 0 => 'rm "a.txt"', ); // Check total of Executed Commands @@ -280,7 +338,7 @@ class FileSystemTaskTest extends TestCase $ranCommands = $runtime->getRanCommands(); $testCase = array( - 0 => 'rm "test.txt"', + 0 => 'rm "test.txt"', ); // Check total of Executed Commands @@ -341,6 +399,35 @@ class FileSystemTaskTest extends TestCase } } + public function testLinkTaskWithFlags() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new LinkTask(); + $task->setOptions(['from' => 'a.txt', 'to' => 'b.txt', 'flags' => '-P']); + $task->setRuntime($runtime); + + $this->assertContains('a.txt', $task->getDescription()); + $this->assertContains('b.txt', $task->getDescription()); + $task->execute(); + + $ranCommands = $runtime->getRanCommands(); + + $testCase = array( + 0 => 'ln -P "a.txt" "b.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 testLinkReplaceTask() { $runtime = new RuntimeMockup();