From dc48f19b265150c4cc584cf6c0726d7cb78cef17 Mon Sep 17 00:00:00 2001 From: Yanick Witschi Date: Wed, 1 Feb 2017 15:23:31 +0100 Subject: [PATCH 01/24] Added a a helper method to the runtime that merges env specific options with global ones --- src/Runtime/Runtime.php | 21 +++++++++++++++++++ src/Task/BuiltIn/Symfony/AsseticDumpTask.php | 5 +---- .../BuiltIn/Symfony/AssetsInstallTask.php | 5 +---- src/Task/BuiltIn/Symfony/CacheClearTask.php | 5 +---- src/Task/BuiltIn/Symfony/CacheWarmupTask.php | 5 +---- 5 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/Runtime/Runtime.php b/src/Runtime/Runtime.php index e03cc48..8952c37 100644 --- a/src/Runtime/Runtime.php +++ b/src/Runtime/Runtime.php @@ -229,6 +229,27 @@ class Runtime return $default; } + /** + * Shortcut to get the the configuration option for a specific environment and merge it with + * the global one (environment specific overrides the global one if present). + * + * @param $key + * @param null $defaultEnv + * @param null $defaultConfig + * + * @return array + */ + public function getMergedEnvAndConfigOption($key, $defaultEnv = null, $defaultConfig = null) + { + $userGlobalOptions = $this->getConfigOption($key, $defaultConfig); + $userEnvOptions = $this->getEnvOption($key, $defaultEnv); + + return array_merge( + (is_array($userGlobalOptions) ? $userGlobalOptions : []), + (is_array($userEnvOptions) ? $userEnvOptions : []) + ); + } + /** * Overwrites an Environment Configuration Option * diff --git a/src/Task/BuiltIn/Symfony/AsseticDumpTask.php b/src/Task/BuiltIn/Symfony/AsseticDumpTask.php index 4c4404c..3229721 100644 --- a/src/Task/BuiltIn/Symfony/AsseticDumpTask.php +++ b/src/Task/BuiltIn/Symfony/AsseticDumpTask.php @@ -43,12 +43,9 @@ class AsseticDumpTask extends AbstractTask protected function getOptions() { - $userGlobalOptions = $this->runtime->getConfigOption('symfony', []); - $userEnvOptions = $this->runtime->getEnvOption('symfony', []); $options = array_merge( ['console' => 'bin/console', 'env' => 'dev', 'flags' => ''], - (is_array($userGlobalOptions) ? $userGlobalOptions : []), - (is_array($userEnvOptions) ? $userEnvOptions : []), + $this->runtime->getMergedEnvAndConfigOption('symfony', [], []), $this->options ); diff --git a/src/Task/BuiltIn/Symfony/AssetsInstallTask.php b/src/Task/BuiltIn/Symfony/AssetsInstallTask.php index a6a63c4..8f313ef 100644 --- a/src/Task/BuiltIn/Symfony/AssetsInstallTask.php +++ b/src/Task/BuiltIn/Symfony/AssetsInstallTask.php @@ -43,12 +43,9 @@ class AssetsInstallTask extends AbstractTask protected function getOptions() { - $userGlobalOptions = $this->runtime->getConfigOption('symfony', []); - $userEnvOptions = $this->runtime->getEnvOption('symfony', []); $options = array_merge( ['console' => 'bin/console', 'env' => 'dev', 'target' => 'web', 'flags' => '--symlink --relative'], - (is_array($userGlobalOptions) ? $userGlobalOptions : []), - (is_array($userEnvOptions) ? $userEnvOptions : []), + $this->runtime->getMergedEnvAndConfigOption('symfony', [], []), $this->options ); diff --git a/src/Task/BuiltIn/Symfony/CacheClearTask.php b/src/Task/BuiltIn/Symfony/CacheClearTask.php index bd5b77a..f9df326 100644 --- a/src/Task/BuiltIn/Symfony/CacheClearTask.php +++ b/src/Task/BuiltIn/Symfony/CacheClearTask.php @@ -43,12 +43,9 @@ class CacheClearTask extends AbstractTask protected function getOptions() { - $userGlobalOptions = $this->runtime->getConfigOption('symfony', []); - $userEnvOptions = $this->runtime->getEnvOption('symfony', []); $options = array_merge( ['console' => 'bin/console', 'env' => 'dev', 'flags' => ''], - (is_array($userGlobalOptions) ? $userGlobalOptions : []), - (is_array($userEnvOptions) ? $userEnvOptions : []), + $this->runtime->getMergedEnvAndConfigOption('symfony', [], []), $this->options ); diff --git a/src/Task/BuiltIn/Symfony/CacheWarmupTask.php b/src/Task/BuiltIn/Symfony/CacheWarmupTask.php index a2e30f0..f0b6229 100644 --- a/src/Task/BuiltIn/Symfony/CacheWarmupTask.php +++ b/src/Task/BuiltIn/Symfony/CacheWarmupTask.php @@ -43,12 +43,9 @@ class CacheWarmupTask extends AbstractTask protected function getOptions() { - $userGlobalOptions = $this->runtime->getConfigOption('symfony', []); - $userEnvOptions = $this->runtime->getEnvOption('symfony', []); $options = array_merge( ['console' => 'bin/console', 'env' => 'dev', 'flags' => ''], - (is_array($userGlobalOptions) ? $userGlobalOptions : []), - (is_array($userEnvOptions) ? $userEnvOptions : []), + $this->runtime->getMergedEnvAndConfigOption('symfony', [], []), $this->options ); From b2099105fcce18eca0db71b6d471351c1dbf9361 Mon Sep 17 00:00:00 2001 From: Yanick Witschi Date: Wed, 1 Feb 2017 15:23:44 +0100 Subject: [PATCH 02/24] Added support for environment specific composer settings --- .../BuiltIn/Composer/DumpAutoloadTask.php | 3 +- src/Task/BuiltIn/Composer/InstallTask.php | 3 +- .../BuiltIn/DeployCommandMiscTasksTest.php | 30 +++++++++++++++++++ tests/Resources/composer-env.yml | 19 ++++++++++++ 4 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 tests/Resources/composer-env.yml diff --git a/src/Task/BuiltIn/Composer/DumpAutoloadTask.php b/src/Task/BuiltIn/Composer/DumpAutoloadTask.php index b778413..39e44da 100644 --- a/src/Task/BuiltIn/Composer/DumpAutoloadTask.php +++ b/src/Task/BuiltIn/Composer/DumpAutoloadTask.php @@ -43,10 +43,9 @@ class DumpAutoloadTask extends AbstractTask protected function getOptions() { - $userOptions = $this->runtime->getConfigOption('composer', []); $options = array_merge( ['path' => 'composer', 'flags' => '--optimize'], - (is_array($userOptions) ? $userOptions : []), + $this->runtime->getMergedEnvAndConfigOption('composer', [], []), $this->options ); diff --git a/src/Task/BuiltIn/Composer/InstallTask.php b/src/Task/BuiltIn/Composer/InstallTask.php index ce97361..5596864 100644 --- a/src/Task/BuiltIn/Composer/InstallTask.php +++ b/src/Task/BuiltIn/Composer/InstallTask.php @@ -43,10 +43,9 @@ class InstallTask extends AbstractTask protected function getOptions() { - $userOptions = $this->runtime->getConfigOption('composer', []); $options = array_merge( ['path' => 'composer', 'flags' => '--optimize-autoloader'], - (is_array($userOptions) ? $userOptions : []), + $this->runtime->getMergedEnvAndConfigOption('composer', [], []), $this->options ); diff --git a/tests/Command/BuiltIn/DeployCommandMiscTasksTest.php b/tests/Command/BuiltIn/DeployCommandMiscTasksTest.php index 4d5efd6..a4beb90 100644 --- a/tests/Command/BuiltIn/DeployCommandMiscTasksTest.php +++ b/tests/Command/BuiltIn/DeployCommandMiscTasksTest.php @@ -80,6 +80,36 @@ class DeployCommandMiscTasksTest extends TestCase $this->assertEquals(0, $tester->getStatusCode()); } + public function testComposerEnvFlags() + { + $application = new MageApplicationMockup(__DIR__ . '/../../Resources/composer-env.yml'); + + /** @var AbstractCommand $command */ + $command = $application->find('deploy'); + $this->assertTrue($command instanceof DeployCommand); + + $tester = new CommandTester($command); + $tester->execute(['command' => $command->getName(), 'environment' => 'test']); + + $ranCommands = $application->getRuntime()->getRanCommands(); + + $testCase = array( + 0 => '/usr/foobar/composer install --prefer-source', + 1 => '/usr/foobar/composer dump-autoload --no-scripts', + 2 => 'rsync -e "ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" -avz --exclude=.git --exclude=./var/cache/* --exclude=./var/log/* --exclude=./web/app_dev.php ./ tester@testhost:/var/www/test', + ); + + // Check total of Executed Commands + $this->assertEquals(count($testCase), count($ranCommands)); + + // Check Generated Commands + foreach ($testCase as $index => $command) { + $this->assertEquals($command, $ranCommands[$index]); + } + + $this->assertEquals(0, $tester->getStatusCode()); + } + public function testInvalidTaskName() { $application = new MageApplicationMockup(__DIR__ . '/../../Resources/invalid-task.yml'); diff --git a/tests/Resources/composer-env.yml b/tests/Resources/composer-env.yml new file mode 100644 index 0000000..2421fdf --- /dev/null +++ b/tests/Resources/composer-env.yml @@ -0,0 +1,19 @@ +magephp: + log_dir: /tmp + composer: + path: /usr/bin/composer.phar + environments: + test: + composer: + path: /usr/foobar/composer + user: tester + host_path: /var/www/test + exclude: + - ./var/cache/* + - ./var/log/* + - ./web/app_dev.php + hosts: + - testhost + pre-deploy: + - composer/install: { flags: '--prefer-source' } + - composer/dump-autoload: { flags: '--no-scripts' } From b09089c27075e6b07602105d2dbaea0dc675ffaf Mon Sep 17 00:00:00 2001 From: Toni Date: Thu, 2 Feb 2017 10:38:57 +0100 Subject: [PATCH 03/24] Update example-config.yml Because this is a production environment example I would recommend to use the symfony prod env as well. --- docs/example-config.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/example-config.yml b/docs/example-config.yml index 86bcdd3..a3036b7 100644 --- a/docs/example-config.yml +++ b/docs/example-config.yml @@ -19,9 +19,9 @@ magephp: - composer/install - composer/generate-autoload on-deploy: - - symfony/cache-warmup: { env: 'dev' } - - symfony/assets-install: { env: 'dev' } - - symfony/assetic-dump: { env: 'dev' } + - symfony/cache-warmup: { env: 'prod' } + - symfony/assets-install: { env: 'prod' } + - symfony/assetic-dump: { env: 'prod' } on-release: post-release: post-deploy: From 532a3146b07ea9eeeaf979e8b0228c4d0d4895d3 Mon Sep 17 00:00:00 2001 From: Yanick Witschi Date: Fri, 3 Feb 2017 08:43:24 +0100 Subject: [PATCH 04/24] Renamed method --- src/Runtime/Runtime.php | 9 ++++----- src/Task/BuiltIn/Composer/DumpAutoloadTask.php | 2 +- src/Task/BuiltIn/Composer/InstallTask.php | 2 +- src/Task/BuiltIn/Symfony/AsseticDumpTask.php | 2 +- src/Task/BuiltIn/Symfony/AssetsInstallTask.php | 2 +- src/Task/BuiltIn/Symfony/CacheClearTask.php | 2 +- src/Task/BuiltIn/Symfony/CacheWarmupTask.php | 2 +- 7 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/Runtime/Runtime.php b/src/Runtime/Runtime.php index 8952c37..a9b2c24 100644 --- a/src/Runtime/Runtime.php +++ b/src/Runtime/Runtime.php @@ -233,15 +233,14 @@ class Runtime * Shortcut to get the the configuration option for a specific environment and merge it with * the global one (environment specific overrides the global one if present). * - * @param $key - * @param null $defaultEnv - * @param null $defaultConfig + * @param $key + * @param array $defaultEnv * * @return array */ - public function getMergedEnvAndConfigOption($key, $defaultEnv = null, $defaultConfig = null) + public function getMergedOption($key, $defaultEnv = []) { - $userGlobalOptions = $this->getConfigOption($key, $defaultConfig); + $userGlobalOptions = $this->getConfigOption($key, $defaultEnv); $userEnvOptions = $this->getEnvOption($key, $defaultEnv); return array_merge( diff --git a/src/Task/BuiltIn/Composer/DumpAutoloadTask.php b/src/Task/BuiltIn/Composer/DumpAutoloadTask.php index 39e44da..9078283 100644 --- a/src/Task/BuiltIn/Composer/DumpAutoloadTask.php +++ b/src/Task/BuiltIn/Composer/DumpAutoloadTask.php @@ -45,7 +45,7 @@ class DumpAutoloadTask extends AbstractTask { $options = array_merge( ['path' => 'composer', 'flags' => '--optimize'], - $this->runtime->getMergedEnvAndConfigOption('composer', [], []), + $this->runtime->getMergedOption('composer'), $this->options ); diff --git a/src/Task/BuiltIn/Composer/InstallTask.php b/src/Task/BuiltIn/Composer/InstallTask.php index 5596864..7a63672 100644 --- a/src/Task/BuiltIn/Composer/InstallTask.php +++ b/src/Task/BuiltIn/Composer/InstallTask.php @@ -45,7 +45,7 @@ class InstallTask extends AbstractTask { $options = array_merge( ['path' => 'composer', 'flags' => '--optimize-autoloader'], - $this->runtime->getMergedEnvAndConfigOption('composer', [], []), + $this->runtime->getMergedOption('composer'), $this->options ); diff --git a/src/Task/BuiltIn/Symfony/AsseticDumpTask.php b/src/Task/BuiltIn/Symfony/AsseticDumpTask.php index 3229721..3f23d19 100644 --- a/src/Task/BuiltIn/Symfony/AsseticDumpTask.php +++ b/src/Task/BuiltIn/Symfony/AsseticDumpTask.php @@ -45,7 +45,7 @@ class AsseticDumpTask extends AbstractTask { $options = array_merge( ['console' => 'bin/console', 'env' => 'dev', 'flags' => ''], - $this->runtime->getMergedEnvAndConfigOption('symfony', [], []), + $this->runtime->getMergedOption('symfony'), $this->options ); diff --git a/src/Task/BuiltIn/Symfony/AssetsInstallTask.php b/src/Task/BuiltIn/Symfony/AssetsInstallTask.php index 8f313ef..001d100 100644 --- a/src/Task/BuiltIn/Symfony/AssetsInstallTask.php +++ b/src/Task/BuiltIn/Symfony/AssetsInstallTask.php @@ -45,7 +45,7 @@ class AssetsInstallTask extends AbstractTask { $options = array_merge( ['console' => 'bin/console', 'env' => 'dev', 'target' => 'web', 'flags' => '--symlink --relative'], - $this->runtime->getMergedEnvAndConfigOption('symfony', [], []), + $this->runtime->getMergedOption('symfony'), $this->options ); diff --git a/src/Task/BuiltIn/Symfony/CacheClearTask.php b/src/Task/BuiltIn/Symfony/CacheClearTask.php index f9df326..a0b6475 100644 --- a/src/Task/BuiltIn/Symfony/CacheClearTask.php +++ b/src/Task/BuiltIn/Symfony/CacheClearTask.php @@ -45,7 +45,7 @@ class CacheClearTask extends AbstractTask { $options = array_merge( ['console' => 'bin/console', 'env' => 'dev', 'flags' => ''], - $this->runtime->getMergedEnvAndConfigOption('symfony', [], []), + $this->runtime->getMergedOption('symfony'), $this->options ); diff --git a/src/Task/BuiltIn/Symfony/CacheWarmupTask.php b/src/Task/BuiltIn/Symfony/CacheWarmupTask.php index f0b6229..8584beb 100644 --- a/src/Task/BuiltIn/Symfony/CacheWarmupTask.php +++ b/src/Task/BuiltIn/Symfony/CacheWarmupTask.php @@ -45,7 +45,7 @@ class CacheWarmupTask extends AbstractTask { $options = array_merge( ['console' => 'bin/console', 'env' => 'dev', 'flags' => ''], - $this->runtime->getMergedEnvAndConfigOption('symfony', [], []), + $this->runtime->getMergedOption('symfony'), $this->options ); From 18fd988c2156abf1cd6b4a5cfeb1afe21111f3fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Sat, 4 Feb 2017 16:14:23 -0300 Subject: [PATCH 05/24] Update Mage.php --- src/Mage.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mage.php b/src/Mage.php index 18d62eb..4944c52 100644 --- a/src/Mage.php +++ b/src/Mage.php @@ -17,6 +17,6 @@ namespace Mage; */ class Mage { - const VERSION = '3.0.0'; + const VERSION = '3.x-dev'; const CODENAME = 'Nostromo'; } From c6ec69b7f2043f7205efec1e5c973874e8d56abd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Sat, 4 Feb 2017 16:40:40 -0300 Subject: [PATCH 06/24] [Nostromo] Update Changelog --- CHANGELOG.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef6b473..3cd88a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ CHANGELOG for 3.x ================= -* 3.0.0 (2017-01-xx) +* 3.0.X (2017-XX-XX) + * [PR#330] Allow Composer task options to be overwritten at environment level + * [PR#330] Add new method Runtime::getMergedOption to merge ConfigOption and EnvOption + * [Documentation] [PR#333] Improve example config file + +* 3.0.0 (2017-01-31) * v3 series release From e2bb29334ddb1c5e4ce505b5f5e527a840abae94 Mon Sep 17 00:00:00 2001 From: thePanz Date: Sun, 5 Feb 2017 00:26:56 +0100 Subject: [PATCH 07/24] Rsync task: ignore empty 'exclude' parameters --- src/Task/BuiltIn/Deploy/RsyncTask.php | 2 +- src/Task/BuiltIn/Deploy/Tar/PrepareTask.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Task/BuiltIn/Deploy/RsyncTask.php b/src/Task/BuiltIn/Deploy/RsyncTask.php index f3343b6..1dce74e 100644 --- a/src/Task/BuiltIn/Deploy/RsyncTask.php +++ b/src/Task/BuiltIn/Deploy/RsyncTask.php @@ -55,7 +55,7 @@ class RsyncTask extends AbstractTask protected function getExcludes() { $excludes = $this->runtime->getEnvOption('exclude', []); - $excludes = array_merge(['.git'], $excludes); + $excludes = array_merge(['.git'], array_filter($excludes)); foreach ($excludes as &$exclude) { $exclude = '--exclude=' . $exclude; diff --git a/src/Task/BuiltIn/Deploy/Tar/PrepareTask.php b/src/Task/BuiltIn/Deploy/Tar/PrepareTask.php index d3b9d28..e694f8f 100644 --- a/src/Task/BuiltIn/Deploy/Tar/PrepareTask.php +++ b/src/Task/BuiltIn/Deploy/Tar/PrepareTask.php @@ -52,7 +52,7 @@ class PrepareTask extends AbstractTask protected function getExcludes() { $excludes = $this->runtime->getEnvOption('exclude', []); - $excludes = array_merge(['.git'], $excludes); + $excludes = array_merge(['.git'], array_filter($excludes)); foreach ($excludes as &$exclude) { $exclude = '--exclude="' . $exclude . '"'; From b36f90e80a2a1c5906c8ed21ab74fe21ef5d1485 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Sun, 5 Feb 2017 16:25:58 -0300 Subject: [PATCH 08/24] [Nostromo] Improve tests and update Changelog --- CHANGELOG.md | 1 + tests/Resources/testhost.yml | 2 ++ 2 files changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cd88a8..5effb5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ CHANGELOG for 3.x ================= * 3.0.X (2017-XX-XX) + * [BUGFIX] [PR#342] Ignore empty exclude lines * [PR#330] Allow Composer task options to be overwritten at environment level * [PR#330] Add new method Runtime::getMergedOption to merge ConfigOption and EnvOption * [Documentation] [PR#333] Improve example config file diff --git a/tests/Resources/testhost.yml b/tests/Resources/testhost.yml index c20a614..f55f9e9 100644 --- a/tests/Resources/testhost.yml +++ b/tests/Resources/testhost.yml @@ -10,6 +10,8 @@ magephp: - ./var/cache/* - ./var/log/* - ./web/app_dev.php + - + - hosts: - testhost pre-deploy: From 49d55871275eb9110e744fb8c2ada812588e5d4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marian=20B=C3=A4uerle?= Date: Sun, 5 Feb 2017 21:52:16 +0100 Subject: [PATCH 09/24] Add change mode task with test --- src/Task/BuiltIn/FS/ChangeModeTask.php | 53 ++++++++++++ tests/Task/BuiltIn/ChangeModeTaskTest.php | 100 ++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 src/Task/BuiltIn/FS/ChangeModeTask.php create mode 100644 tests/Task/BuiltIn/ChangeModeTaskTest.php diff --git a/src/Task/BuiltIn/FS/ChangeModeTask.php b/src/Task/BuiltIn/FS/ChangeModeTask.php new file mode 100644 index 0000000..883fce5 --- /dev/null +++ b/src/Task/BuiltIn/FS/ChangeModeTask.php @@ -0,0 +1,53 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Mage\Task\BuiltIn\FS; + +use Symfony\Component\Process\Process; +use Exception; + +/** + * File System Task - Copy a File + * + * @author Marian Bäuerle + */ +class ChangeModeTask extends AbstractFileTask +{ + public function getName() + { + return 'fs/chmod'; + } + + public function getDescription() + { + try { + return sprintf('[FS] Change mode of "%s" to "%s" with flags "%s"', $this->getFile('file'), $this->options['mode'], $this->options['flags']); + } catch (Exception $exception) { + return '[FS] Chmod [missing parameters]'; + } + } + + public function execute() + { + $file = $this->getFile('file'); + + $cmd = sprintf('chmod %s %s %s', $this->options['flags'], $this->options['mode'], $file); + + /** @var Process $process */ + $process = $this->runtime->runCommand($cmd); + + return $process->isSuccessful(); + } + + protected function getParameters() + { + return ['file', 'mode', 'flags']; + } +} diff --git a/tests/Task/BuiltIn/ChangeModeTaskTest.php b/tests/Task/BuiltIn/ChangeModeTaskTest.php new file mode 100644 index 0000000..5408339 --- /dev/null +++ b/tests/Task/BuiltIn/ChangeModeTaskTest.php @@ -0,0 +1,100 @@ + + * + * 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\FS\ChangeModeTask; +use Exception; +use Mage\Tests\Runtime\RuntimeMockup; +use PHPUnit_Framework_TestCase as TestCase; + +class ChangeModeTest extends TestCase +{ + public function testChangeModeTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new ChangeModeTask(); + $task->setOptions(['file' => 'a.txt', 'flags' => '-R', 'mode' => 'o+w']); + $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', + ); + + // 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 testChangeModeReplaceTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new ChangeModeTask(); + $task->setOptions(['file' => '%environment%.txt', 'flags' => '-R', 'mode' => 'o+w']); + $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', + ); + + // 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 testChangeModeBadOptionsTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new ChangeModeTask(); + $task->setOptions(['from' => 'a.txt', 'flags' => '-R', 'mode' => 'o+w']); + $task->setRuntime($runtime); + + try { + $this->assertContains('[missing parameters]', $task->getDescription()); + $task->execute(); + $this->assertTrue(false, 'Task should have raised an exception'); + } catch (Exception $exception) { + $this->assertTrue($exception instanceof ErrorException); + $this->assertEquals('Parameter "file" is not defined', $exception->getMessage()); + } + } +} From 33364f5e2466a57a0b3d6c578289bc383f48f59b Mon Sep 17 00:00:00 2001 From: thePanz Date: Tue, 7 Feb 2017 23:27:00 +0100 Subject: [PATCH 10/24] 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 From eba1e61d47f172e51a64ca0cd8d8c9ff92e4a0f4 Mon Sep 17 00:00:00 2001 From: Yanick Witschi Date: Fri, 10 Feb 2017 16:48:16 +0100 Subject: [PATCH 11/24] Added composer selfupdate command --- src/Task/BuiltIn/Composer/SelfUpdateTask.php | 146 ++++++++++++++++ .../BuiltIn/Composer/SelfUpdateTaskTest.php | 157 ++++++++++++++++++ 2 files changed, 303 insertions(+) create mode 100644 src/Task/BuiltIn/Composer/SelfUpdateTask.php create mode 100644 tests/Command/BuiltIn/Composer/SelfUpdateTaskTest.php diff --git a/src/Task/BuiltIn/Composer/SelfUpdateTask.php b/src/Task/BuiltIn/Composer/SelfUpdateTask.php new file mode 100644 index 0000000..41dbac7 --- /dev/null +++ b/src/Task/BuiltIn/Composer/SelfUpdateTask.php @@ -0,0 +1,146 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Mage\Task\BuiltIn\Composer; + +use Symfony\Component\Process\Process; +use Mage\Task\AbstractTask; + +/** + * Composer Task - Self update + * + * @author Yanick Witschi + */ +class SelfUpdateTask extends AbstractTask +{ + /** + * Only used for unit tests. + * + * @var \DateTime + */ + private $dateToCompare; + + /** + * @return string + */ + public function getName() + { + return 'composer/selfupdate'; + } + + /** + * @return string + */ + public function getDescription() + { + return '[Composer] Selfupdate'; + } + + /** + * @return bool + */ + public function execute() + { + $options = $this->getOptions(); + $days = $options['days']; + $versionCommand = sprintf('%s --version', $options['path']); + + /** @var Process $process */ + $process = $this->runtime->runCommand(trim($versionCommand)); + + if (!$process->isSuccessful()) { + return false; + } + + $dt = $this->extractDate($process->getOutput()); + + // Date could not be extracted, always run update + if (false === $dt) { + return $this->selfUpdate($options); + } + + // Check age + if (!$this->isOlderThan($dt, $days)) { + return true; + } + + return $this->selfUpdate($options); + } + + /** + * This tasks obviously always takes the current date to compare the age + * of the composer.phar. This method is used for unit test purposes + * only. + * + * @param \DateTime $dateToCompare + */ + public function setDateToCompare(\DateTime $dateToCompare) + { + $this->dateToCompare = $dateToCompare; + } + + /** + * @param \DateTime $dt + * @param int $days + * + * @return bool + */ + protected function isOlderThan(\DateTime $dt, $days) + { + $dtComp = new \DateTime($days . ' days ago'); + + if (null !== $this->dateToCompare) { + $dtComp = $this->dateToCompare; + } + + return $dt < $dtComp; + } + + /** + * @param array $options + * + * @return bool + */ + protected function selfUpdate(array $options) + { + $selfupdateCommand = sprintf('%s selfupdate %s', $options['path'], $options['release']); + + /** @var Process $process */ + $process = $this->runtime->runCommand(trim($selfupdateCommand)); + + return $process->isSuccessful(); + } + + /** + * @param string $output + * + * @return \DateTime|false + */ + protected function extractDate($output) + { + $date = substr($output, -19); + + return \DateTime::createFromFormat('Y-m-d H:i:s', $date); + } + + /** + * @return array + */ + protected function getOptions() + { + $options = array_merge( + ['path' => 'composer', 'release' => '', 'days' => 30], + $this->runtime->getMergedOption('composer'), + $this->options + ); + + return $options; + } +} diff --git a/tests/Command/BuiltIn/Composer/SelfUpdateTaskTest.php b/tests/Command/BuiltIn/Composer/SelfUpdateTaskTest.php new file mode 100644 index 0000000..c394c27 --- /dev/null +++ b/tests/Command/BuiltIn/Composer/SelfUpdateTaskTest.php @@ -0,0 +1,157 @@ +assertSame('composer/selfupdate', $task->getName()); + $this->assertSame('[Composer] Selfupdate', $task->getDescription()); + } + + public function testExecuteWithFailingVersionDoesNotCallSelfupdate() + { + $runtime = $this->getMockBuilder(Runtime::class) + ->setMethods(['runCommand']) + ->getMock(); + + $runtime + ->expects($this->once()) + ->method('runCommand') + ->with('composer --version') + ->willReturn($this->mockProcess(false)); + + $task = $this->getTask($runtime); + $this->assertFalse($task->execute()); + } + + public function testExecuteWithNoDateVersionDoesCallSelfupdate() + { + $runtime = $this->getMockBuilder(Runtime::class) + ->setMethods(['runCommand']) + ->getMock(); + + $runtime + ->expects($this->exactly(2)) + ->method('runCommand') + ->withConsecutive( + ['composer --version'], + ['composer selfupdate'] + ) + ->willReturnOnConsecutiveCalls( + $this->mockProcess(true, 'whatever-without-valid-date'), + $this->mockProcess(true) + ); + + $task = $this->getTask($runtime); + $this->assertTrue($task->execute()); + } + + public function testExecuteShouldUpdate() + { + $runtime = $this->getMockBuilder(Runtime::class) + ->setMethods(['runCommand']) + ->getMock(); + + $runtime + ->expects($this->exactly(2)) + ->method('runCommand') + ->withConsecutive( + ['composer --version'], + ['composer selfupdate'] + ) + ->willReturnOnConsecutiveCalls( + $this->mockProcess(true, 'Composer version 1.3.2 2017-01-01 18:23:41'), + $this->mockProcess(true) + ); + + $task = $this->getTask($runtime); + $task->setOptions(['days' => 30]); + $this->assertTrue($task->execute()); + } + + public function testExecuteShouldNotUpdate() + { + $runtime = $this->getMockBuilder(Runtime::class) + ->setMethods(['runCommand']) + ->getMock(); + + $runtime + ->expects($this->exactly(1)) + ->method('runCommand') + ->with('composer --version') + ->willReturn($this->mockProcess(true, 'Composer version 1.3.2 2017-01-01 18:23:41')); + + $task = $this->getTask($runtime); + $task->setDateToCompare(\DateTime::createFromFormat('Y-m-d H:i:s', '2016-12-10 18:23:41')); + $task->setOptions(['days' => 30]); + $this->assertTrue($task->execute()); + } + + public function testWithRelease() + { + $runtime = $this->getMockBuilder(Runtime::class) + ->setMethods(['runCommand']) + ->getMock(); + + $runtime + ->expects($this->exactly(2)) + ->method('runCommand') + ->withConsecutive( + ['composer --version'], + ['composer selfupdate 1.3.1'] + ) + ->willReturnOnConsecutiveCalls( + $this->mockProcess(true, 'Composer version 1.3.2 2017-01-01 18:23:41'), + $this->mockProcess(true) + ); + + $task = $this->getTask($runtime); + $task->setOptions(['days' => 30, 'release' => '1.3.1']); + $this->assertTrue($task->execute()); + } + + private function getTask($runtime) + { + $config = [ + 'magephp' => [ + 'composer' => [ + 'path' => 'composer.phar' + ] + ] + ]; + + /** @var Runtime $runtime */ + $runtime->setConfiguration($config); + + $task = new SelfUpdateTask(); + $task->setRuntime($runtime); + + return $task; + } + + private function mockProcess($successful, $output = '') + { + $process = $this->getMockBuilder(Process::class) + ->disableOriginalConstructor() + ->getMock(); + $process + ->expects($this->any()) + ->method('isSuccessful') + ->willReturn($successful); + + $process + ->expects($this->any()) + ->method('getOutput') + ->willReturn($output); + + return $process; + } +} From 50354a36242accec277204280072699961f5b660 Mon Sep 17 00:00:00 2001 From: Yanick Witschi Date: Fri, 10 Feb 2017 17:42:22 +0100 Subject: [PATCH 12/24] Added task to execute arbitrary commands --- src/Task/BuiltIn/ExecTask.php | 84 ++++++++++++++++++++++++ tests/Command/BuiltIn/ExecTaskTest.php | 90 ++++++++++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 src/Task/BuiltIn/ExecTask.php create mode 100644 tests/Command/BuiltIn/ExecTaskTest.php diff --git a/src/Task/BuiltIn/ExecTask.php b/src/Task/BuiltIn/ExecTask.php new file mode 100644 index 0000000..095add9 --- /dev/null +++ b/src/Task/BuiltIn/ExecTask.php @@ -0,0 +1,84 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Mage\Task\BuiltIn; + +use Mage\Task\Exception\ErrorException; +use Mage\Task\AbstractTask; + +/** + * Exec task. Allows you to execute arbitrary commands. + * + * @author Yanick Witschi + */ +class ExecTask extends AbstractTask +{ + /** + * @return string + */ + public function getName() + { + return 'exec'; + } + + /** + * @return string + */ + public function getDescription() + { + $options = $this->getOptions(); + + if ('' !== $options['descr']) { + return (string) $options['descr']; + } + + return '[Exec] Executing custom command'; + } + + /** + * @return bool + * + * @throws ErrorException + */ + public function execute() + { + $options = $this->getOptions(); + + if ('' === $options['cmd']) { + throw new ErrorException('What about if you gave me a command to execute?'); + } + + // 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(); + } + + $process = $this->runtime->runCommand($options['cmd'], $options['timeout']); + return $process->isSuccessful(); + } + + /** + * @return array + */ + protected function getOptions() + { + $options = array_merge([ + 'cmd' => '', + 'descr' => '', + 'jail' => true, + 'timeout' => 120 + ], + $this->options + ); + + return $options; + } +} diff --git a/tests/Command/BuiltIn/ExecTaskTest.php b/tests/Command/BuiltIn/ExecTaskTest.php new file mode 100644 index 0000000..ea1bae8 --- /dev/null +++ b/tests/Command/BuiltIn/ExecTaskTest.php @@ -0,0 +1,90 @@ +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; + } +} From 5f4236c208badee9112668a6bbdf385bc3d4a5d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Fri, 10 Feb 2017 23:49:59 -0300 Subject: [PATCH 13/24] Update Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb159ab..25ce595 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ CHANGELOG for 3.x ================= * 3.0.X (2017-XX-XX) + * [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 * [PR#330] Add new method Runtime::getMergedOption to merge ConfigOption and EnvOption From 83f047aab09679a1c36835809f229e4852093248 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Fri, 10 Feb 2017 23:57:34 -0300 Subject: [PATCH 14/24] Allow to define default options in the tasks (eg: flags) --- src/Task/AbstractTask.php | 12 +++++++++++- src/Task/BuiltIn/FS/AbstractFileTask.php | 4 +++- src/Task/BuiltIn/FS/ChangeModeTask.php | 5 +++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Task/AbstractTask.php b/src/Task/AbstractTask.php index 0ee7c2f..395dd5f 100644 --- a/src/Task/AbstractTask.php +++ b/src/Task/AbstractTask.php @@ -61,7 +61,8 @@ abstract class AbstractTask if (!is_array($options)) { $options = []; } - $this->options = $options; + + $this->options = array_merge($options, $this->getDefaults()); return $this; } @@ -76,4 +77,13 @@ abstract class AbstractTask $this->runtime = $runtime; return $this; } + + /** + * Return Default options + * @return array + */ + public function getDefaults() + { + return []; + } } diff --git a/src/Task/BuiltIn/FS/AbstractFileTask.php b/src/Task/BuiltIn/FS/AbstractFileTask.php index c2e4685..eb782b2 100644 --- a/src/Task/BuiltIn/FS/AbstractFileTask.php +++ b/src/Task/BuiltIn/FS/AbstractFileTask.php @@ -29,8 +29,10 @@ abstract class AbstractFileTask extends AbstractTask protected function getOptions() { $mandatory = $this->getParameters(); + $defaults = array_keys($this->getDefaults()); + $missing = array_diff($mandatory, $defaults); - foreach ($mandatory as $parameter) { + foreach ($missing as $parameter) { if (!array_key_exists($parameter, $this->options)) { throw new ErrorException(sprintf('Parameter "%s" is not defined', $parameter)); } diff --git a/src/Task/BuiltIn/FS/ChangeModeTask.php b/src/Task/BuiltIn/FS/ChangeModeTask.php index 883fce5..3ad05ba 100644 --- a/src/Task/BuiltIn/FS/ChangeModeTask.php +++ b/src/Task/BuiltIn/FS/ChangeModeTask.php @@ -50,4 +50,9 @@ class ChangeModeTask extends AbstractFileTask { return ['file', 'mode', 'flags']; } + + public function getDefaults() + { + return ['flags' => null]; + } } From abf10b05f81469a55a4e0f9fb131cc733e87a6a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Sat, 11 Feb 2017 00:03:38 -0300 Subject: [PATCH 15/24] Allow to define default options in the tasks (eg: flags) --- src/Task/AbstractTask.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Task/AbstractTask.php b/src/Task/AbstractTask.php index 395dd5f..ee03134 100644 --- a/src/Task/AbstractTask.php +++ b/src/Task/AbstractTask.php @@ -62,7 +62,7 @@ abstract class AbstractTask $options = []; } - $this->options = array_merge($options, $this->getDefaults()); + $this->options = array_merge($this->getDefaults(), $options); return $this; } From 55faaaa8f0a83b3ed12205aa58acea3c4d9daefe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Sat, 11 Feb 2017 00:06:57 -0300 Subject: [PATCH 16/24] [fs/chmod] Allow to not use flags --- src/Task/BuiltIn/FS/ChangeModeTask.php | 10 +++++--- tests/Task/BuiltIn/ChangeModeTaskTest.php | 30 +++++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/Task/BuiltIn/FS/ChangeModeTask.php b/src/Task/BuiltIn/FS/ChangeModeTask.php index 3ad05ba..2f5c5bd 100644 --- a/src/Task/BuiltIn/FS/ChangeModeTask.php +++ b/src/Task/BuiltIn/FS/ChangeModeTask.php @@ -28,7 +28,11 @@ class ChangeModeTask extends AbstractFileTask public function getDescription() { try { - return sprintf('[FS] Change mode of "%s" to "%s" with flags "%s"', $this->getFile('file'), $this->options['mode'], $this->options['flags']); + $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; } catch (Exception $exception) { return '[FS] Chmod [missing parameters]'; } @@ -36,9 +40,7 @@ class ChangeModeTask extends AbstractFileTask public function execute() { - $file = $this->getFile('file'); - - $cmd = sprintf('chmod %s %s %s', $this->options['flags'], $this->options['mode'], $file); + $cmd = sprintf('chmod %s %s %s', $this->options['flags'], $this->options['mode'], $this->getFile('file')); /** @var Process $process */ $process = $this->runtime->runCommand($cmd); diff --git a/tests/Task/BuiltIn/ChangeModeTaskTest.php b/tests/Task/BuiltIn/ChangeModeTaskTest.php index 5408339..d0481a6 100644 --- a/tests/Task/BuiltIn/ChangeModeTaskTest.php +++ b/tests/Task/BuiltIn/ChangeModeTaskTest.php @@ -48,6 +48,36 @@ class ChangeModeTest extends TestCase } } + public function testChangeModeTaskWithoutFlags() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new ChangeModeTask(); + $task->setOptions(['file' => 'a.txt', 'mode' => 'o+w']); + $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', + ); + + // 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 testChangeModeReplaceTask() { $runtime = new RuntimeMockup(); 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 17/24] [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(); From d8f2359d12f1870618e8bb0adcc290194d888d4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Sat, 11 Feb 2017 01:48:26 -0300 Subject: [PATCH 18/24] [Nostromo] Refactor Composer Update, organize task tests better --- .../BuiltIn/Composer/DumpAutoloadTask.php | 4 +- src/Task/BuiltIn/Composer/InstallTask.php | 4 +- src/Task/BuiltIn/Composer/SelfUpdateTask.php | 117 ++--- .../BuiltIn/Composer/SelfUpdateTaskTest.php | 157 ------ tests/Runtime/ProcessMockup.php | 23 + .../BuiltIn/Composer/SelfUpdateTaskTest.php | 173 +++++++ .../{ => FileSystem}/ChangeModeTaskTest.php | 2 +- .../Task/BuiltIn/FileSystem/CopyTaskTest.php | 158 ++++++ .../Task/BuiltIn/FileSystem/LinkTaskTest.php | 127 +++++ .../Task/BuiltIn/FileSystem/MoveTaskTest.php | 127 +++++ .../BuiltIn/FileSystem/RemoveTaskTest.php | 124 +++++ tests/Task/BuiltIn/FileSystemTaskTest.php | 479 ------------------ 12 files changed, 769 insertions(+), 726 deletions(-) delete mode 100644 tests/Command/BuiltIn/Composer/SelfUpdateTaskTest.php create mode 100644 tests/Task/BuiltIn/Composer/SelfUpdateTaskTest.php rename tests/Task/BuiltIn/{ => FileSystem}/ChangeModeTaskTest.php (98%) create mode 100644 tests/Task/BuiltIn/FileSystem/CopyTaskTest.php create mode 100644 tests/Task/BuiltIn/FileSystem/LinkTaskTest.php create mode 100644 tests/Task/BuiltIn/FileSystem/MoveTaskTest.php create mode 100644 tests/Task/BuiltIn/FileSystem/RemoveTaskTest.php delete mode 100644 tests/Task/BuiltIn/FileSystemTaskTest.php diff --git a/src/Task/BuiltIn/Composer/DumpAutoloadTask.php b/src/Task/BuiltIn/Composer/DumpAutoloadTask.php index 9078283..af66d1e 100644 --- a/src/Task/BuiltIn/Composer/DumpAutoloadTask.php +++ b/src/Task/BuiltIn/Composer/DumpAutoloadTask.php @@ -33,10 +33,10 @@ class DumpAutoloadTask extends AbstractTask public function execute() { $options = $this->getOptions(); - $command = sprintf('%s dump-autoload %s', $options['path'], $options['flags']); + $cmd = sprintf('%s dump-autoload %s', $options['path'], $options['flags']); /** @var Process $process */ - $process = $this->runtime->runCommand(trim($command)); + $process = $this->runtime->runCommand(trim($cmd)); return $process->isSuccessful(); } diff --git a/src/Task/BuiltIn/Composer/InstallTask.php b/src/Task/BuiltIn/Composer/InstallTask.php index 7a63672..4229992 100644 --- a/src/Task/BuiltIn/Composer/InstallTask.php +++ b/src/Task/BuiltIn/Composer/InstallTask.php @@ -33,10 +33,10 @@ class InstallTask extends AbstractTask public function execute() { $options = $this->getOptions(); - $command = sprintf('%s install %s', $options['path'], $options['flags']); + $cmd = sprintf('%s install %s', $options['path'], $options['flags']); /** @var Process $process */ - $process = $this->runtime->runCommand(trim($command)); + $process = $this->runtime->runCommand(trim($cmd)); return $process->isSuccessful(); } diff --git a/src/Task/BuiltIn/Composer/SelfUpdateTask.php b/src/Task/BuiltIn/Composer/SelfUpdateTask.php index 41dbac7..e5f2245 100644 --- a/src/Task/BuiltIn/Composer/SelfUpdateTask.php +++ b/src/Task/BuiltIn/Composer/SelfUpdateTask.php @@ -10,8 +10,10 @@ namespace Mage\Task\BuiltIn\Composer; +use Mage\Task\Exception\SkipException; use Symfony\Component\Process\Process; use Mage\Task\AbstractTask; +use DateTime; /** * Composer Task - Self update @@ -20,123 +22,68 @@ use Mage\Task\AbstractTask; */ class SelfUpdateTask extends AbstractTask { - /** - * Only used for unit tests. - * - * @var \DateTime - */ - private $dateToCompare; - - /** - * @return string - */ public function getName() { - return 'composer/selfupdate'; + return 'composer/self-update'; } - /** - * @return string - */ public function getDescription() { - return '[Composer] Selfupdate'; + return '[Composer] Self Update'; } - /** - * @return bool - */ public function execute() { $options = $this->getOptions(); - $days = $options['days']; - $versionCommand = sprintf('%s --version', $options['path']); - + $cmdVersion = sprintf('%s --version', $options['path']); /** @var Process $process */ - $process = $this->runtime->runCommand(trim($versionCommand)); - + $process = $this->runtime->runCommand(trim($cmdVersion)); if (!$process->isSuccessful()) { return false; } - $dt = $this->extractDate($process->getOutput()); - - // Date could not be extracted, always run update - if (false === $dt) { - return $this->selfUpdate($options); + $buildDate = $this->getBuildDate($process->getOutput()); + if (!$buildDate instanceof DateTime) { + return false; } - // Check age - if (!$this->isOlderThan($dt, $days)) { - return true; + $compareDate = $this->getCompareDate(); + if ($buildDate >= $compareDate) { + throw new SkipException(); } - return $this->selfUpdate($options); - } - - /** - * This tasks obviously always takes the current date to compare the age - * of the composer.phar. This method is used for unit test purposes - * only. - * - * @param \DateTime $dateToCompare - */ - public function setDateToCompare(\DateTime $dateToCompare) - { - $this->dateToCompare = $dateToCompare; - } - - /** - * @param \DateTime $dt - * @param int $days - * - * @return bool - */ - protected function isOlderThan(\DateTime $dt, $days) - { - $dtComp = new \DateTime($days . ' days ago'); - - if (null !== $this->dateToCompare) { - $dtComp = $this->dateToCompare; - } - - return $dt < $dtComp; - } - - /** - * @param array $options - * - * @return bool - */ - protected function selfUpdate(array $options) - { - $selfupdateCommand = sprintf('%s selfupdate %s', $options['path'], $options['release']); - + $cmdUpdate = sprintf('%s self-update %s', $options['path'], $options['release']); /** @var Process $process */ - $process = $this->runtime->runCommand(trim($selfupdateCommand)); + $process = $this->runtime->runCommand(trim($cmdUpdate)); return $process->isSuccessful(); } - /** - * @param string $output - * - * @return \DateTime|false - */ - protected function extractDate($output) + protected function getBuildDate($output) { - $date = substr($output, -19); + $buildDate = null; + $output = explode(PHP_EOL, $output); + foreach ($output as $row) { + if (strpos($row, 'Composer version ') === 0) { + $buildDate = DateTime::createFromFormat('Y-m-d H:i:s', substr(trim($row), -19)); + } + } - return \DateTime::createFromFormat('Y-m-d H:i:s', $date); + return $buildDate; + } + + protected function getCompareDate() + { + $options = $this->getOptions(); + $compareDate = new DateTime(); + $compareDate->modify(sprintf('now -%d days', $options['days'])); + return $compareDate; } - /** - * @return array - */ protected function getOptions() { $options = array_merge( - ['path' => 'composer', 'release' => '', 'days' => 30], + ['path' => 'composer', 'release' => '', 'days' => 60], $this->runtime->getMergedOption('composer'), $this->options ); diff --git a/tests/Command/BuiltIn/Composer/SelfUpdateTaskTest.php b/tests/Command/BuiltIn/Composer/SelfUpdateTaskTest.php deleted file mode 100644 index c394c27..0000000 --- a/tests/Command/BuiltIn/Composer/SelfUpdateTaskTest.php +++ /dev/null @@ -1,157 +0,0 @@ -assertSame('composer/selfupdate', $task->getName()); - $this->assertSame('[Composer] Selfupdate', $task->getDescription()); - } - - public function testExecuteWithFailingVersionDoesNotCallSelfupdate() - { - $runtime = $this->getMockBuilder(Runtime::class) - ->setMethods(['runCommand']) - ->getMock(); - - $runtime - ->expects($this->once()) - ->method('runCommand') - ->with('composer --version') - ->willReturn($this->mockProcess(false)); - - $task = $this->getTask($runtime); - $this->assertFalse($task->execute()); - } - - public function testExecuteWithNoDateVersionDoesCallSelfupdate() - { - $runtime = $this->getMockBuilder(Runtime::class) - ->setMethods(['runCommand']) - ->getMock(); - - $runtime - ->expects($this->exactly(2)) - ->method('runCommand') - ->withConsecutive( - ['composer --version'], - ['composer selfupdate'] - ) - ->willReturnOnConsecutiveCalls( - $this->mockProcess(true, 'whatever-without-valid-date'), - $this->mockProcess(true) - ); - - $task = $this->getTask($runtime); - $this->assertTrue($task->execute()); - } - - public function testExecuteShouldUpdate() - { - $runtime = $this->getMockBuilder(Runtime::class) - ->setMethods(['runCommand']) - ->getMock(); - - $runtime - ->expects($this->exactly(2)) - ->method('runCommand') - ->withConsecutive( - ['composer --version'], - ['composer selfupdate'] - ) - ->willReturnOnConsecutiveCalls( - $this->mockProcess(true, 'Composer version 1.3.2 2017-01-01 18:23:41'), - $this->mockProcess(true) - ); - - $task = $this->getTask($runtime); - $task->setOptions(['days' => 30]); - $this->assertTrue($task->execute()); - } - - public function testExecuteShouldNotUpdate() - { - $runtime = $this->getMockBuilder(Runtime::class) - ->setMethods(['runCommand']) - ->getMock(); - - $runtime - ->expects($this->exactly(1)) - ->method('runCommand') - ->with('composer --version') - ->willReturn($this->mockProcess(true, 'Composer version 1.3.2 2017-01-01 18:23:41')); - - $task = $this->getTask($runtime); - $task->setDateToCompare(\DateTime::createFromFormat('Y-m-d H:i:s', '2016-12-10 18:23:41')); - $task->setOptions(['days' => 30]); - $this->assertTrue($task->execute()); - } - - public function testWithRelease() - { - $runtime = $this->getMockBuilder(Runtime::class) - ->setMethods(['runCommand']) - ->getMock(); - - $runtime - ->expects($this->exactly(2)) - ->method('runCommand') - ->withConsecutive( - ['composer --version'], - ['composer selfupdate 1.3.1'] - ) - ->willReturnOnConsecutiveCalls( - $this->mockProcess(true, 'Composer version 1.3.2 2017-01-01 18:23:41'), - $this->mockProcess(true) - ); - - $task = $this->getTask($runtime); - $task->setOptions(['days' => 30, 'release' => '1.3.1']); - $this->assertTrue($task->execute()); - } - - private function getTask($runtime) - { - $config = [ - 'magephp' => [ - 'composer' => [ - 'path' => 'composer.phar' - ] - ] - ]; - - /** @var Runtime $runtime */ - $runtime->setConfiguration($config); - - $task = new SelfUpdateTask(); - $task->setRuntime($runtime); - - return $task; - } - - private function mockProcess($successful, $output = '') - { - $process = $this->getMockBuilder(Process::class) - ->disableOriginalConstructor() - ->getMock(); - $process - ->expects($this->any()) - ->method('isSuccessful') - ->willReturn($successful); - - $process - ->expects($this->any()) - ->method('getOutput') - ->willReturn($output); - - return $process; - } -} diff --git a/tests/Runtime/ProcessMockup.php b/tests/Runtime/ProcessMockup.php index 7e88048..918a127 100644 --- a/tests/Runtime/ProcessMockup.php +++ b/tests/Runtime/ProcessMockup.php @@ -68,6 +68,29 @@ class ProcessMockup extends Process return '* master'; } + // Make composer build 20 days old + if ($this->commandline == 'composer --version') { + $date = date('Y-m-d H:i:s', strtotime('now -20 days')); + return 'Composer version 1.3.0 ' . $date; + } + + // Make ./composer build 20 days old + if ($this->commandline == './composer --version') { + $date = date('Y-m-d H:i:s', strtotime('now -20 days')); + return 'Do not run Composer as root/super user! See https://getcomposer.org/root for details' . PHP_EOL . 'Composer version 1.3.0 ' . $date; + } + + // Make composer.phar build 90 days old + if ($this->commandline == 'composer.phar --version') { + $date = date('Y-m-d H:i:s', strtotime('now -90 days')); + return 'Composer version 1.3.0 ' . $date; + } + + // Make php composer has wrong output + if ($this->commandline == 'php composer --version') { + return 'Composer version 1.3.0 no build'; + } + if ($this->commandline == 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "ls -1 /var/www/test/releases"') { return implode(PHP_EOL, ['20170101015110', '20170101015111', '20170101015112', '20170101015113', '20170101015114', '20170101015115', '20170101015116', '20170101015117']); } diff --git a/tests/Task/BuiltIn/Composer/SelfUpdateTaskTest.php b/tests/Task/BuiltIn/Composer/SelfUpdateTaskTest.php new file mode 100644 index 0000000..72c5543 --- /dev/null +++ b/tests/Task/BuiltIn/Composer/SelfUpdateTaskTest.php @@ -0,0 +1,173 @@ +setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new SelfUpdateTask(); + $task->setOptions(['path' => 'composer']); + $task->setRuntime($runtime); + + try { + $task->execute(); + } catch (Exception $exception) { + $this->assertTrue($exception instanceof SkipException, 'Update should been skipped'); + } + + $ranCommands = $runtime->getRanCommands(); + $testCase = array( + 0 => 'composer --version', + ); + + // 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 testSelfUpdateAsRootTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new SelfUpdateTask(); + $task->setOptions(['path' => './composer']); + $task->setRuntime($runtime); + + try { + $task->execute(); + } catch (Exception $exception) { + $this->assertTrue($exception instanceof SkipException, 'Update should been skipped'); + } + + $ranCommands = $runtime->getRanCommands(); + $testCase = array( + 0 => './composer --version', + ); + + // 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 testSelfUpdateMustUpdateTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new SelfUpdateTask(); + $task->setOptions(['path' => 'composer.phar']); + $task->setRuntime($runtime); + + try { + $result = $task->execute(); + $this->assertTrue($result, 'Result should be successful'); + } catch (Exception $exception) { + if ($exception instanceof SkipException) { + $this->assertTrue(false, 'Update should not have been skipped'); + } + } + + $ranCommands = $runtime->getRanCommands(); + $testCase = array( + 0 => 'composer.phar --version', + 1 => 'composer.phar self-update', + ); + + // 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 testSelfUpdateWrongOutputTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new SelfUpdateTask(); + $task->setOptions(['path' => 'php composer']); + $task->setRuntime($runtime); + + try { + $result = $task->execute(); + $this->assertFalse($result, 'Result should be failure'); + } catch (Exception $exception) { + if ($exception instanceof SkipException) { + $this->assertTrue(false, 'Update should not have been skipped'); + } + } + + $ranCommands = $runtime->getRanCommands(); + $testCase = array( + 0 => 'php composer --version', + ); + + // 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 testSelfUpdateFailExecTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new SelfUpdateTask(); + $task->setOptions(['path' => 'composer']); + $task->setRuntime($runtime); + $runtime->forceFail('composer --version'); + + try { + $result = $task->execute(); + $this->assertFalse($result, 'Result should be failure'); + } catch (Exception $exception) { + if ($exception instanceof SkipException) { + $this->assertTrue(false, 'Update should not have been skipped'); + } + } + + $ranCommands = $runtime->getRanCommands(); + $testCase = array( + 0 => 'composer --version', + ); + + // Check total of Executed Commands + $this->assertEquals(count($testCase), count($ranCommands)); + + // Check Generated Commands + foreach ($testCase as $index => $command) { + $this->assertEquals($command, $ranCommands[$index]); + } + } +} diff --git a/tests/Task/BuiltIn/ChangeModeTaskTest.php b/tests/Task/BuiltIn/FileSystem/ChangeModeTaskTest.php similarity index 98% rename from tests/Task/BuiltIn/ChangeModeTaskTest.php rename to tests/Task/BuiltIn/FileSystem/ChangeModeTaskTest.php index 2548742..79db322 100644 --- a/tests/Task/BuiltIn/ChangeModeTaskTest.php +++ b/tests/Task/BuiltIn/FileSystem/ChangeModeTaskTest.php @@ -8,7 +8,7 @@ * file that was distributed with this source code. */ -namespace Mage\Tests\Task\BuiltIn; +namespace Mage\Tests\Task\BuiltIn\FileSystem; use Mage\Task\Exception\ErrorException; use Mage\Task\BuiltIn\FS\ChangeModeTask; diff --git a/tests/Task/BuiltIn/FileSystem/CopyTaskTest.php b/tests/Task/BuiltIn/FileSystem/CopyTaskTest.php new file mode 100644 index 0000000..1d91d1a --- /dev/null +++ b/tests/Task/BuiltIn/FileSystem/CopyTaskTest.php @@ -0,0 +1,158 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Mage\Tests\Task\BuiltIn\FileSystem; + +use Mage\Task\Exception\ErrorException; +use Mage\Task\BuiltIn\FS\CopyTask; +use Exception; +use Mage\Tests\Runtime\RuntimeMockup; +use PHPUnit_Framework_TestCase as TestCase; + +class CopyTaskTest extends TestCase +{ + public function testCopyTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new CopyTask(); + $task->setOptions(['from' => 'a.txt', 'to' => 'b.txt']); + $task->setRuntime($runtime); + + $this->assertContains('a.txt', $task->getDescription()); + $this->assertContains('b.txt', $task->getDescription()); + $task->execute(); + + $ranCommands = $runtime->getRanCommands(); + + $testCase = array( + 0 => 'cp -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 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(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new CopyTask(); + $task->setOptions(['from' => '%environment%.txt', 'to' => 'b.txt']); + $task->setRuntime($runtime); + + $this->assertContains('test.txt', $task->getDescription()); + $this->assertContains('b.txt', $task->getDescription()); + $task->execute(); + + $ranCommands = $runtime->getRanCommands(); + + $testCase = array( + 0 => 'cp -p "test.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 testCopyMultipleReplaceTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + $runtime->setReleaseId('1234'); + $runtime->setWorkingHost('localhost'); + + $task = new CopyTask(); + $task->setOptions(['from' => '%host%.txt', 'to' => '%release%.yml']); + $task->setRuntime($runtime); + + $this->assertContains('localhost.txt', $task->getDescription()); + $this->assertContains('1234.yml', $task->getDescription()); + $task->execute(); + + $ranCommands = $runtime->getRanCommands(); + + $testCase = array( + 0 => 'cp -p "localhost.txt" "1234.yml"', + ); + + // 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 testCopyBadOptionsTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new CopyTask(); + $task->setOptions(['form' => 'a.txt', 'to' => 'b.txt']); + $task->setRuntime($runtime); + + try { + $this->assertContains('[missing parameters]', $task->getDescription()); + $task->execute(); + $this->assertTrue(false, 'Task did not failed'); + } catch (Exception $exception) { + $this->assertTrue($exception instanceof ErrorException); + $this->assertEquals('Parameter "from" is not defined', $exception->getMessage()); + } + } +} diff --git a/tests/Task/BuiltIn/FileSystem/LinkTaskTest.php b/tests/Task/BuiltIn/FileSystem/LinkTaskTest.php new file mode 100644 index 0000000..5869709 --- /dev/null +++ b/tests/Task/BuiltIn/FileSystem/LinkTaskTest.php @@ -0,0 +1,127 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Mage\Tests\Task\BuiltIn\FileSystem; + +use Mage\Task\Exception\ErrorException; +use Mage\Task\BuiltIn\FS\LinkTask; +use Exception; +use Mage\Tests\Runtime\RuntimeMockup; +use PHPUnit_Framework_TestCase as TestCase; + +class LinkTaskTest extends TestCase +{ + public function testLinkTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new LinkTask(); + $task->setOptions(['from' => 'a.txt', 'to' => 'b.txt']); + $task->setRuntime($runtime); + + $this->assertContains('a.txt', $task->getDescription()); + $this->assertContains('b.txt', $task->getDescription()); + $task->execute(); + + $ranCommands = $runtime->getRanCommands(); + + $testCase = array( + 0 => 'ln -snf "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 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(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new LinkTask(); + $task->setOptions(['from' => '%environment%.txt', 'to' => 'b.txt']); + $task->setRuntime($runtime); + + $this->assertContains('test.txt', $task->getDescription()); + $this->assertContains('b.txt', $task->getDescription()); + $task->execute(); + + $ranCommands = $runtime->getRanCommands(); + + $testCase = array( + 0 => 'ln -snf "test.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 testLinkBadOptionsTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new LinkTask(); + $task->setOptions(['form' => 'a.txt', 'to' => 'b.txt']); + $task->setRuntime($runtime); + + try { + $this->assertContains('[missing parameters]', $task->getDescription()); + $task->execute(); + $this->assertTrue(false, 'Task did not failed'); + } catch (Exception $exception) { + $this->assertTrue($exception instanceof ErrorException); + $this->assertEquals('Parameter "from" is not defined', $exception->getMessage()); + } + } +} diff --git a/tests/Task/BuiltIn/FileSystem/MoveTaskTest.php b/tests/Task/BuiltIn/FileSystem/MoveTaskTest.php new file mode 100644 index 0000000..4fdab3d --- /dev/null +++ b/tests/Task/BuiltIn/FileSystem/MoveTaskTest.php @@ -0,0 +1,127 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Mage\Tests\Task\BuiltIn\FileSystem; + +use Mage\Task\Exception\ErrorException; +use Mage\Task\BuiltIn\FS\MoveTask; +use Exception; +use Mage\Tests\Runtime\RuntimeMockup; +use PHPUnit_Framework_TestCase as TestCase; + +class MoveTaskTest extends TestCase +{ + public function testMoveTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new MoveTask(); + $task->setOptions(['from' => 'a.txt', 'to' => 'b.txt']); + $task->setRuntime($runtime); + + $this->assertContains('a.txt', $task->getDescription()); + $this->assertContains('b.txt', $task->getDescription()); + $task->execute(); + + $ranCommands = $runtime->getRanCommands(); + + $testCase = array( + 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 + $this->assertEquals(count($testCase), count($ranCommands)); + + // Check Generated Commands + foreach ($testCase as $index => $command) { + $this->assertEquals($command, $ranCommands[$index]); + } + } + + public function testMoveReplaceTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new MoveTask(); + $task->setOptions(['from' => '%environment%.txt', 'to' => 'b.txt']); + $task->setRuntime($runtime); + + $this->assertContains('test.txt', $task->getDescription()); + $this->assertContains('b.txt', $task->getDescription()); + $task->execute(); + + $ranCommands = $runtime->getRanCommands(); + + $testCase = array( + 0 => 'mv "test.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 testMoveBadOptionsTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new MoveTask(); + $task->setOptions(['form' => 'a.txt', 'to' => 'b.txt']); + $task->setRuntime($runtime); + + try { + $this->assertContains('[missing parameters]', $task->getDescription()); + $task->execute(); + $this->assertTrue(false, 'Task did not failed'); + } catch (Exception $exception) { + $this->assertTrue($exception instanceof ErrorException); + $this->assertEquals('Parameter "from" is not defined', $exception->getMessage()); + } + } +} diff --git a/tests/Task/BuiltIn/FileSystem/RemoveTaskTest.php b/tests/Task/BuiltIn/FileSystem/RemoveTaskTest.php new file mode 100644 index 0000000..4b050a4 --- /dev/null +++ b/tests/Task/BuiltIn/FileSystem/RemoveTaskTest.php @@ -0,0 +1,124 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Mage\Tests\Task\BuiltIn\FileSystem; + +use Mage\Task\Exception\ErrorException; +use Mage\Task\BuiltIn\FS\RemoveTask; +use Exception; +use Mage\Tests\Runtime\RuntimeMockup; +use PHPUnit_Framework_TestCase as TestCase; + +class RemoveTaskTest extends TestCase +{ + public function testRemoveTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new RemoveTask(); + $task->setOptions(['file' => 'a.txt']); + $task->setRuntime($runtime); + + $this->assertContains('a.txt', $task->getDescription()); + $task->execute(); + + $ranCommands = $runtime->getRanCommands(); + + $testCase = array( + 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 + $this->assertEquals(count($testCase), count($ranCommands)); + + // Check Generated Commands + foreach ($testCase as $index => $command) { + $this->assertEquals($command, $ranCommands[$index]); + } + } + + public function testRemoveReplaceTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new RemoveTask(); + $task->setOptions(['file' => '%environment%.txt']); + $task->setRuntime($runtime); + + $this->assertContains('test.txt', $task->getDescription()); + $task->execute(); + + $ranCommands = $runtime->getRanCommands(); + + $testCase = array( + 0 => 'rm "test.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 testRemoveBadOptionsTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new RemoveTask(); + $task->setOptions(['from' => 'a.txt']); + $task->setRuntime($runtime); + + try { + $this->assertContains('[missing parameters]', $task->getDescription()); + $task->execute(); + $this->assertTrue(false, 'Task did not failed'); + } catch (Exception $exception) { + $this->assertTrue($exception instanceof ErrorException); + $this->assertEquals('Parameter "file" is not defined', $exception->getMessage()); + } + } +} diff --git a/tests/Task/BuiltIn/FileSystemTaskTest.php b/tests/Task/BuiltIn/FileSystemTaskTest.php deleted file mode 100644 index 49bfd60..0000000 --- a/tests/Task/BuiltIn/FileSystemTaskTest.php +++ /dev/null @@ -1,479 +0,0 @@ - - * - * 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\FS\CopyTask; -use Mage\Task\BuiltIn\FS\LinkTask; -use Mage\Task\BuiltIn\FS\MoveTask; -use Mage\Task\BuiltIn\FS\RemoveTask; -use Exception; -use Mage\Tests\Runtime\RuntimeMockup; -use PHPUnit_Framework_TestCase as TestCase; - -class FileSystemTaskTest extends TestCase -{ - public function testCopyTask() - { - $runtime = new RuntimeMockup(); - $runtime->setConfiguration(['environments' => ['test' => []]]); - $runtime->setEnvironment('test'); - - $task = new CopyTask(); - $task->setOptions(['from' => 'a.txt', 'to' => 'b.txt']); - $task->setRuntime($runtime); - - $this->assertContains('a.txt', $task->getDescription()); - $this->assertContains('b.txt', $task->getDescription()); - $task->execute(); - - $ranCommands = $runtime->getRanCommands(); - - $testCase = array( - 0 => 'cp -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 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(); - $runtime->setConfiguration(['environments' => ['test' => []]]); - $runtime->setEnvironment('test'); - - $task = new CopyTask(); - $task->setOptions(['from' => '%environment%.txt', 'to' => 'b.txt']); - $task->setRuntime($runtime); - - $this->assertContains('test.txt', $task->getDescription()); - $this->assertContains('b.txt', $task->getDescription()); - $task->execute(); - - $ranCommands = $runtime->getRanCommands(); - - $testCase = array( - 0 => 'cp -p "test.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 testCopyMultipleReplaceTask() - { - $runtime = new RuntimeMockup(); - $runtime->setConfiguration(['environments' => ['test' => []]]); - $runtime->setEnvironment('test'); - $runtime->setReleaseId('1234'); - $runtime->setWorkingHost('localhost'); - - $task = new CopyTask(); - $task->setOptions(['from' => '%host%.txt', 'to' => '%release%.yml']); - $task->setRuntime($runtime); - - $this->assertContains('localhost.txt', $task->getDescription()); - $this->assertContains('1234.yml', $task->getDescription()); - $task->execute(); - - $ranCommands = $runtime->getRanCommands(); - - $testCase = array( - 0 => 'cp -p "localhost.txt" "1234.yml"', - ); - - // 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 testCopyBadOptionsTask() - { - $runtime = new RuntimeMockup(); - $runtime->setConfiguration(['environments' => ['test' => []]]); - $runtime->setEnvironment('test'); - - $task = new CopyTask(); - $task->setOptions(['form' => 'a.txt', 'to' => 'b.txt']); - $task->setRuntime($runtime); - - try { - $this->assertContains('[missing parameters]', $task->getDescription()); - $task->execute(); - $this->assertTrue(false, 'Task did not failed'); - } catch (Exception $exception) { - $this->assertTrue($exception instanceof ErrorException); - $this->assertEquals('Parameter "from" is not defined', $exception->getMessage()); - } - } - - public function testMoveTask() - { - $runtime = new RuntimeMockup(); - $runtime->setConfiguration(['environments' => ['test' => []]]); - $runtime->setEnvironment('test'); - - $task = new MoveTask(); - $task->setOptions(['from' => 'a.txt', 'to' => 'b.txt']); - $task->setRuntime($runtime); - - $this->assertContains('a.txt', $task->getDescription()); - $this->assertContains('b.txt', $task->getDescription()); - $task->execute(); - - $ranCommands = $runtime->getRanCommands(); - - $testCase = array( - 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 - $this->assertEquals(count($testCase), count($ranCommands)); - - // Check Generated Commands - foreach ($testCase as $index => $command) { - $this->assertEquals($command, $ranCommands[$index]); - } - } - - public function testMoveReplaceTask() - { - $runtime = new RuntimeMockup(); - $runtime->setConfiguration(['environments' => ['test' => []]]); - $runtime->setEnvironment('test'); - - $task = new MoveTask(); - $task->setOptions(['from' => '%environment%.txt', 'to' => 'b.txt']); - $task->setRuntime($runtime); - - $this->assertContains('test.txt', $task->getDescription()); - $this->assertContains('b.txt', $task->getDescription()); - $task->execute(); - - $ranCommands = $runtime->getRanCommands(); - - $testCase = array( - 0 => 'mv "test.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 testMoveBadOptionsTask() - { - $runtime = new RuntimeMockup(); - $runtime->setConfiguration(['environments' => ['test' => []]]); - $runtime->setEnvironment('test'); - - $task = new MoveTask(); - $task->setOptions(['form' => 'a.txt', 'to' => 'b.txt']); - $task->setRuntime($runtime); - - try { - $this->assertContains('[missing parameters]', $task->getDescription()); - $task->execute(); - $this->assertTrue(false, 'Task did not failed'); - } catch (Exception $exception) { - $this->assertTrue($exception instanceof ErrorException); - $this->assertEquals('Parameter "from" is not defined', $exception->getMessage()); - } - } - - public function testRemoveTask() - { - $runtime = new RuntimeMockup(); - $runtime->setConfiguration(['environments' => ['test' => []]]); - $runtime->setEnvironment('test'); - - $task = new RemoveTask(); - $task->setOptions(['file' => 'a.txt']); - $task->setRuntime($runtime); - - $this->assertContains('a.txt', $task->getDescription()); - $task->execute(); - - $ranCommands = $runtime->getRanCommands(); - - $testCase = array( - 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 - $this->assertEquals(count($testCase), count($ranCommands)); - - // Check Generated Commands - foreach ($testCase as $index => $command) { - $this->assertEquals($command, $ranCommands[$index]); - } - } - - public function testRemoveReplaceTask() - { - $runtime = new RuntimeMockup(); - $runtime->setConfiguration(['environments' => ['test' => []]]); - $runtime->setEnvironment('test'); - - $task = new RemoveTask(); - $task->setOptions(['file' => '%environment%.txt']); - $task->setRuntime($runtime); - - $this->assertContains('test.txt', $task->getDescription()); - $task->execute(); - - $ranCommands = $runtime->getRanCommands(); - - $testCase = array( - 0 => 'rm "test.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 testRemoveBadOptionsTask() - { - $runtime = new RuntimeMockup(); - $runtime->setConfiguration(['environments' => ['test' => []]]); - $runtime->setEnvironment('test'); - - $task = new RemoveTask(); - $task->setOptions(['from' => 'a.txt']); - $task->setRuntime($runtime); - - try { - $this->assertContains('[missing parameters]', $task->getDescription()); - $task->execute(); - $this->assertTrue(false, 'Task did not failed'); - } catch (Exception $exception) { - $this->assertTrue($exception instanceof ErrorException); - $this->assertEquals('Parameter "file" is not defined', $exception->getMessage()); - } - } - - public function testLinkTask() - { - $runtime = new RuntimeMockup(); - $runtime->setConfiguration(['environments' => ['test' => []]]); - $runtime->setEnvironment('test'); - - $task = new LinkTask(); - $task->setOptions(['from' => 'a.txt', 'to' => 'b.txt']); - $task->setRuntime($runtime); - - $this->assertContains('a.txt', $task->getDescription()); - $this->assertContains('b.txt', $task->getDescription()); - $task->execute(); - - $ranCommands = $runtime->getRanCommands(); - - $testCase = array( - 0 => 'ln -snf "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 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(); - $runtime->setConfiguration(['environments' => ['test' => []]]); - $runtime->setEnvironment('test'); - - $task = new LinkTask(); - $task->setOptions(['from' => '%environment%.txt', 'to' => 'b.txt']); - $task->setRuntime($runtime); - - $this->assertContains('test.txt', $task->getDescription()); - $this->assertContains('b.txt', $task->getDescription()); - $task->execute(); - - $ranCommands = $runtime->getRanCommands(); - - $testCase = array( - 0 => 'ln -snf "test.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 testLinkBadOptionsTask() - { - $runtime = new RuntimeMockup(); - $runtime->setConfiguration(['environments' => ['test' => []]]); - $runtime->setEnvironment('test'); - - $task = new LinkTask(); - $task->setOptions(['form' => 'a.txt', 'to' => 'b.txt']); - $task->setRuntime($runtime); - - try { - $this->assertContains('[missing parameters]', $task->getDescription()); - $task->execute(); - $this->assertTrue(false, 'Task did not failed'); - } catch (Exception $exception) { - $this->assertTrue($exception instanceof ErrorException); - $this->assertEquals('Parameter "from" is not defined', $exception->getMessage()); - } - } -} From f7448e39e7855dc2756a0c14a39aa622ce27c29e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Sat, 11 Feb 2017 01:53:22 -0300 Subject: [PATCH 19/24] [Nostromo] Refactor Composer SelfUpdate --- CHANGELOG.md | 1 + src/Task/BuiltIn/Composer/SelfUpdateTask.php | 4 +-- .../BuiltIn/Composer/SelfUpdateTaskTest.php | 34 +++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0be809f..f327780 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ CHANGELOG for 3.x ================= * 3.0.X (2017-XX-XX) + * Add new Composer task, to update phar (composer/self-update) * [#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 diff --git a/src/Task/BuiltIn/Composer/SelfUpdateTask.php b/src/Task/BuiltIn/Composer/SelfUpdateTask.php index e5f2245..ea11164 100644 --- a/src/Task/BuiltIn/Composer/SelfUpdateTask.php +++ b/src/Task/BuiltIn/Composer/SelfUpdateTask.php @@ -52,7 +52,7 @@ class SelfUpdateTask extends AbstractTask throw new SkipException(); } - $cmdUpdate = sprintf('%s self-update %s', $options['path'], $options['release']); + $cmdUpdate = sprintf('%s self-update %s', $options['path'], $options['version']); /** @var Process $process */ $process = $this->runtime->runCommand(trim($cmdUpdate)); @@ -83,7 +83,7 @@ class SelfUpdateTask extends AbstractTask protected function getOptions() { $options = array_merge( - ['path' => 'composer', 'release' => '', 'days' => 60], + ['path' => 'composer', 'version' => '', 'days' => 60], $this->runtime->getMergedOption('composer'), $this->options ); diff --git a/tests/Task/BuiltIn/Composer/SelfUpdateTaskTest.php b/tests/Task/BuiltIn/Composer/SelfUpdateTaskTest.php index 72c5543..6d8abee 100644 --- a/tests/Task/BuiltIn/Composer/SelfUpdateTaskTest.php +++ b/tests/Task/BuiltIn/Composer/SelfUpdateTaskTest.php @@ -104,6 +104,40 @@ class SelfUpdateTaskTest extends TestCase } } + public function testSelfUpdateMustUpdateToVersionTask() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + + $task = new SelfUpdateTask(); + $task->setOptions(['path' => 'composer.phar', 'version' => '1.2.0']); + $task->setRuntime($runtime); + + try { + $result = $task->execute(); + $this->assertTrue($result, 'Result should be successful'); + } catch (Exception $exception) { + if ($exception instanceof SkipException) { + $this->assertTrue(false, 'Update should not have been skipped'); + } + } + + $ranCommands = $runtime->getRanCommands(); + $testCase = array( + 0 => 'composer.phar --version', + 1 => 'composer.phar self-update 1.2.0', + ); + + // 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 testSelfUpdateWrongOutputTask() { $runtime = new RuntimeMockup(); From 67c3a23bbb334e8ab0dfda36b66c3df13d38a5b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Sat, 11 Feb 2017 02:01:49 -0300 Subject: [PATCH 20/24] [Nostromo] Refactor Composer SelfUpdate --- src/Task/BuiltIn/Composer/SelfUpdateTask.php | 4 +-- .../BuiltIn/Composer/SelfUpdateTaskTest.php | 34 ------------------- 2 files changed, 2 insertions(+), 36 deletions(-) diff --git a/src/Task/BuiltIn/Composer/SelfUpdateTask.php b/src/Task/BuiltIn/Composer/SelfUpdateTask.php index ea11164..f07fd51 100644 --- a/src/Task/BuiltIn/Composer/SelfUpdateTask.php +++ b/src/Task/BuiltIn/Composer/SelfUpdateTask.php @@ -52,7 +52,7 @@ class SelfUpdateTask extends AbstractTask throw new SkipException(); } - $cmdUpdate = sprintf('%s self-update %s', $options['path'], $options['version']); + $cmdUpdate = sprintf('%s self-update', $options['path']); /** @var Process $process */ $process = $this->runtime->runCommand(trim($cmdUpdate)); @@ -83,7 +83,7 @@ class SelfUpdateTask extends AbstractTask protected function getOptions() { $options = array_merge( - ['path' => 'composer', 'version' => '', 'days' => 60], + ['path' => 'composer', 'days' => 60], $this->runtime->getMergedOption('composer'), $this->options ); diff --git a/tests/Task/BuiltIn/Composer/SelfUpdateTaskTest.php b/tests/Task/BuiltIn/Composer/SelfUpdateTaskTest.php index 6d8abee..72c5543 100644 --- a/tests/Task/BuiltIn/Composer/SelfUpdateTaskTest.php +++ b/tests/Task/BuiltIn/Composer/SelfUpdateTaskTest.php @@ -104,40 +104,6 @@ class SelfUpdateTaskTest extends TestCase } } - public function testSelfUpdateMustUpdateToVersionTask() - { - $runtime = new RuntimeMockup(); - $runtime->setConfiguration(['environments' => ['test' => []]]); - $runtime->setEnvironment('test'); - - $task = new SelfUpdateTask(); - $task->setOptions(['path' => 'composer.phar', 'version' => '1.2.0']); - $task->setRuntime($runtime); - - try { - $result = $task->execute(); - $this->assertTrue($result, 'Result should be successful'); - } catch (Exception $exception) { - if ($exception instanceof SkipException) { - $this->assertTrue(false, 'Update should not have been skipped'); - } - } - - $ranCommands = $runtime->getRanCommands(); - $testCase = array( - 0 => 'composer.phar --version', - 1 => 'composer.phar self-update 1.2.0', - ); - - // 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 testSelfUpdateWrongOutputTask() { $runtime = new RuntimeMockup(); From 9d6163669ac97ddafe6d2deadcbdd3b5ecf12d92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Sat, 11 Feb 2017 02:04:16 -0300 Subject: [PATCH 21/24] [Nostromo] PHP CS Fixer --- src/MageApplication.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/MageApplication.php b/src/MageApplication.php index 70b144c..cb20d30 100644 --- a/src/MageApplication.php +++ b/src/MageApplication.php @@ -77,7 +77,6 @@ class MageApplication extends Application } if (array_key_exists('magephp', $config) && is_array($config['magephp'])) { - $logger = null; if (array_key_exists('log_dir', $config['magephp']) && file_exists($config['magephp']['log_dir']) && is_dir($config['magephp']['log_dir'])) { $logfile = sprintf('%s/%s.log', $config['magephp']['log_dir'], date('Ymd_His')); From b45d85c0306b9aef8b4a2d803fb7dcb289867324 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Sat, 11 Feb 2017 02:07:08 -0300 Subject: [PATCH 22/24] [Nostromo] Improve coverage --- CHANGELOG.md | 4 ++-- tests/Task/BuiltIn/Composer/SelfUpdateTaskTest.php | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f327780..9612350 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ -CHANGELOG for 3.x +CHANGELOG for 3.X ================= -* 3.0.X (2017-XX-XX) +* 3.X (2017-XX-XX) * Add new Composer task, to update phar (composer/self-update) * [#344] Allow to flag Filesystem tasks * [PR#346] Add new File System task, to change file's modes (fs/chmod) diff --git a/tests/Task/BuiltIn/Composer/SelfUpdateTaskTest.php b/tests/Task/BuiltIn/Composer/SelfUpdateTaskTest.php index 72c5543..1721e8b 100644 --- a/tests/Task/BuiltIn/Composer/SelfUpdateTaskTest.php +++ b/tests/Task/BuiltIn/Composer/SelfUpdateTaskTest.php @@ -19,6 +19,7 @@ class SelfUpdateTaskTest extends TestCase $task = new SelfUpdateTask(); $task->setOptions(['path' => 'composer']); $task->setRuntime($runtime); + $this->assertEquals('[Composer] Self Update', $task->getDescription()); try { $task->execute(); From fc851dd6618e7c14e65a578b39dfae99f9a75c72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Sat, 25 Feb 2017 19:31:25 -0300 Subject: [PATCH 23/24] [Nostromo] Exec task --- CHANGELOG.md | 1 + src/Task/BuiltIn/ExecTask.php | 26 +++---- tests/Command/BuiltIn/ExecTaskTest.php | 90 ------------------------ tests/Task/BuiltIn/ExecTaskTest.php | 97 ++++++++++++++++++++++++++ 4 files changed, 107 insertions(+), 107 deletions(-) delete mode 100644 tests/Command/BuiltIn/ExecTaskTest.php create mode 100644 tests/Task/BuiltIn/ExecTaskTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 9612350..e0c24a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ CHANGELOG for 3.X ================= * 3.X (2017-XX-XX) + * Add new Exec task to execute arbitrary shell commands * Add new Composer task, to update phar (composer/self-update) * [#344] Allow to flag Filesystem tasks * [PR#346] Add new File System task, to change file's modes (fs/chmod) diff --git a/src/Task/BuiltIn/ExecTask.php b/src/Task/BuiltIn/ExecTask.php index 095add9..0c7fc17 100644 --- a/src/Task/BuiltIn/ExecTask.php +++ b/src/Task/BuiltIn/ExecTask.php @@ -12,6 +12,7 @@ namespace Mage\Task\BuiltIn; use Mage\Task\Exception\ErrorException; use Mage\Task\AbstractTask; +use Symfony\Component\Process\Process; /** * Exec task. Allows you to execute arbitrary commands. @@ -35,11 +36,11 @@ class ExecTask extends AbstractTask { $options = $this->getOptions(); - if ('' !== $options['descr']) { - return (string) $options['descr']; + if ($options['desc']) { + return '[Exec] ' . $options['desc']; } - return '[Exec] Executing custom command'; + return '[Exec] Custom command'; } /** @@ -51,16 +52,11 @@ class ExecTask extends AbstractTask { $options = $this->getOptions(); - if ('' === $options['cmd']) { - throw new ErrorException('What about if you gave me a command to execute?'); - } - - // 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(); + if (!$options['cmd']) { + throw new ErrorException('Parameter "cmd" is not defined'); } + /** @var Process $process */ $process = $this->runtime->runCommand($options['cmd'], $options['timeout']); return $process->isSuccessful(); } @@ -70,12 +66,8 @@ class ExecTask extends AbstractTask */ protected function getOptions() { - $options = array_merge([ - 'cmd' => '', - 'descr' => '', - 'jail' => true, - 'timeout' => 120 - ], + $options = array_merge( + ['cmd' => '', 'desc' => '', 'timeout' => 120], $this->options ); diff --git a/tests/Command/BuiltIn/ExecTaskTest.php b/tests/Command/BuiltIn/ExecTaskTest.php deleted file mode 100644 index ea1bae8..0000000 --- a/tests/Command/BuiltIn/ExecTaskTest.php +++ /dev/null @@ -1,90 +0,0 @@ -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; - } -} diff --git a/tests/Task/BuiltIn/ExecTaskTest.php b/tests/Task/BuiltIn/ExecTaskTest.php new file mode 100644 index 0000000..d548176 --- /dev/null +++ b/tests/Task/BuiltIn/ExecTaskTest.php @@ -0,0 +1,97 @@ + + * + * 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()); + } + } +} From 97911357c3c74843701c1b68c1ba6eebbbef5d80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Sat, 25 Feb 2017 19:57:40 -0300 Subject: [PATCH 24/24] Changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0c24a1..f642a44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ CHANGELOG for 3.X ================= -* 3.X (2017-XX-XX) +* 3.1.0 (2017-02-25) * Add new Exec task to execute arbitrary shell commands * Add new Composer task, to update phar (composer/self-update) * [#344] Allow to flag Filesystem tasks