From 5da0a06cdd128d262cb308cfc29a23ce42694ead Mon Sep 17 00:00:00 2001 From: Adam Malone Date: Sat, 4 Apr 2020 02:06:20 +1100 Subject: [PATCH] Adds a test for the lock and skip lock functionality. (#64) * Adds a test for the lock. * Adds functionality for users who do not want to use the lock. --- src/Cli/AcquiaCli.php | 52 +++++++++++++++++------------- src/Cli/CloudApi.php | 2 +- src/Cli/Config.php | 2 +- src/Commands/AcquiaCommand.php | 1 - tests/AcquiaCliApplicationTest.php | 20 ++++++++++++ 5 files changed, 51 insertions(+), 26 deletions(-) diff --git a/src/Cli/AcquiaCli.php b/src/Cli/AcquiaCli.php index 1d56270..0c0bc99 100644 --- a/src/Cli/AcquiaCli.php +++ b/src/Cli/AcquiaCli.php @@ -3,18 +3,18 @@ namespace AcquiaCli\Cli; use Robo\Robo; -use Robo\Common\ConfigAwareTrait; -use Symfony\Component\Console\Command\LockableTrait; -use Robo\Runner as RoboRunner; use Robo\Application; -use Symfony\Component\Console\Input\InputInterface; +use Robo\Runner as RoboRunner; +use Robo\Common\ConfigAwareTrait; +use AcquiaCloudApi\Connector\Client; +use AcquiaLogstream\LogstreamManager; +use AcquiaCli\Injector\AcquiaCliInjector; +use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Command\LockableTrait; use Symfony\Component\Console\Output\OutputInterface; use Consolidation\AnnotatedCommand\CommandFileDiscovery; -use AcquiaCloudApi\Connector\Client; -use AcquiaCli\Injector\AcquiaCliInjector; -use AcquiaCli\Cli\CloudApi; -use AcquiaLogstream\LogstreamManager; /** * Class AcquiaCli @@ -75,6 +75,12 @@ public function __construct( InputOption::VALUE_NONE, 'Automatically respond "yes" to all confirmation questions.' ), + new InputOption( + '--no-lock', + null, + InputOption::VALUE_NONE, + 'Run commands without locking. Allows multiple instances of commands to run concurrently.' + ), new InputOption( '--limit', '-l', @@ -138,27 +144,27 @@ public function injectParameters($container) $parameterInjection = $container->get('parameterInjection'); $parameterInjection->register('AcquiaCli\Cli\CloudApi', new AcquiaCliInjector()); $parameterInjection->register('AcquiaCli\Cli\Config', new AcquiaCliInjector()); - $parameterInjection->register('AcquiaCloudApi\Connector\Client', new AcquiaCliInjector()); $parameterInjection->register('AcquiaLogstream\LogstreamManager', new AcquiaCliInjector()); + $parameterInjection->register('AcquiaCloudApi\Connector\Client', new AcquiaCliInjector()); + $parameterInjection->register('AcquiaCloudApi\Endpoints\Account', new AcquiaCliInjector()); $parameterInjection->register('AcquiaCloudApi\Endpoints\Applications', new AcquiaCliInjector()); - $parameterInjection->register('AcquiaCloudApi\Endpoints\Environments', new AcquiaCliInjector()); - $parameterInjection->register('AcquiaCloudApi\Endpoints\Databases', new AcquiaCliInjector()); - $parameterInjection->register('AcquiaCloudApi\Endpoints\Servers', new AcquiaCliInjector()); - $parameterInjection->register('AcquiaCloudApi\Endpoints\Domains', new AcquiaCliInjector()); $parameterInjection->register('AcquiaCloudApi\Endpoints\Code', new AcquiaCliInjector()); - $parameterInjection->register('AcquiaCloudApi\Endpoints\DatabaseBackups', new AcquiaCliInjector()); $parameterInjection->register('AcquiaCloudApi\Endpoints\Crons', new AcquiaCliInjector()); - $parameterInjection->register('AcquiaCloudApi\Endpoints\Account', new AcquiaCliInjector()); - $parameterInjection->register('AcquiaCloudApi\Endpoints\Roles', new AcquiaCliInjector()); - $parameterInjection->register('AcquiaCloudApi\Endpoints\Permissions', new AcquiaCliInjector()); - $parameterInjection->register('AcquiaCloudApi\Endpoints\Teams', new AcquiaCliInjector()); - $parameterInjection->register('AcquiaCloudApi\Endpoints\Variables', new AcquiaCliInjector()); - $parameterInjection->register('AcquiaCloudApi\Endpoints\Logs', new AcquiaCliInjector()); - $parameterInjection->register('AcquiaCloudApi\Endpoints\Notifications', new AcquiaCliInjector()); + $parameterInjection->register('AcquiaCloudApi\Endpoints\DatabaseBackups', new AcquiaCliInjector()); + $parameterInjection->register('AcquiaCloudApi\Endpoints\Databases', new AcquiaCliInjector()); + $parameterInjection->register('AcquiaCloudApi\Endpoints\Domains', new AcquiaCliInjector()); + $parameterInjection->register('AcquiaCloudApi\Endpoints\Environments', new AcquiaCliInjector()); $parameterInjection->register('AcquiaCloudApi\Endpoints\Insights', new AcquiaCliInjector()); $parameterInjection->register('AcquiaCloudApi\Endpoints\LogForwardingDestinations', new AcquiaCliInjector()); - $parameterInjection->register('AcquiaCloudApi\Endpoints\SslCertificates', new AcquiaCliInjector()); + $parameterInjection->register('AcquiaCloudApi\Endpoints\Logs', new AcquiaCliInjector()); + $parameterInjection->register('AcquiaCloudApi\Endpoints\Notifications', new AcquiaCliInjector()); $parameterInjection->register('AcquiaCloudApi\Endpoints\Organizations', new AcquiaCliInjector()); + $parameterInjection->register('AcquiaCloudApi\Endpoints\Permissions', new AcquiaCliInjector()); + $parameterInjection->register('AcquiaCloudApi\Endpoints\Roles', new AcquiaCliInjector()); + $parameterInjection->register('AcquiaCloudApi\Endpoints\Servers', new AcquiaCliInjector()); + $parameterInjection->register('AcquiaCloudApi\Endpoints\SslCertificates', new AcquiaCliInjector()); + $parameterInjection->register('AcquiaCloudApi\Endpoints\Teams', new AcquiaCliInjector()); + $parameterInjection->register('AcquiaCloudApi\Endpoints\Variables', new AcquiaCliInjector()); } /** @@ -169,7 +175,7 @@ public function injectParameters($container) public function run(InputInterface $input, OutputInterface $output) { // Obtain a lock and exit if the command is already running. - if (!$this->lock('acquia-cli-command')) { + if (!$input->hasParameterOption('--no-lock') && !$this->lock('acquia-cli-command')) { $output->writeln('The command is already running in another process.'); return 0; diff --git a/src/Cli/CloudApi.php b/src/Cli/CloudApi.php index 1661112..41c1d75 100644 --- a/src/Cli/CloudApi.php +++ b/src/Cli/CloudApi.php @@ -2,6 +2,7 @@ namespace AcquiaCli\Cli; +use Robo\Config\Config; use AcquiaCloudApi\Connector\Client; use AcquiaCloudApi\Connector\Connector; use AcquiaCloudApi\Endpoints\Applications; @@ -9,7 +10,6 @@ use AcquiaCloudApi\Endpoints\Organizations; use AcquiaCloudApi\Response\EnvironmentResponse; use AcquiaCloudApi\Response\OrganizationResponse; -use Robo\Config\Config; use Symfony\Component\Console\Input\InputInterface; /** diff --git a/src/Cli/Config.php b/src/Cli/Config.php index a90ba87..6050726 100644 --- a/src/Cli/Config.php +++ b/src/Cli/Config.php @@ -3,9 +3,9 @@ namespace AcquiaCli\Cli; use Robo\Config\Config as RoboConfig; -use Robo\Config\GlobalOptionDefaultValuesInterface; use Consolidation\Config\Loader\ConfigProcessor; use Consolidation\Config\Loader\YamlConfigLoader; +use Robo\Config\GlobalOptionDefaultValuesInterface; /** * Class Config diff --git a/src/Commands/AcquiaCommand.php b/src/Commands/AcquiaCommand.php index d063b16..9ea6123 100644 --- a/src/Commands/AcquiaCommand.php +++ b/src/Commands/AcquiaCommand.php @@ -124,7 +124,6 @@ public function validateApiOptionsHook(CommandData $commandData) */ public function initUuidHook(InputInterface $input, AnnotationData $annotationData) { - if ($input->hasArgument('uuid')) { $uuid = $input->getArgument('uuid'); diff --git a/tests/AcquiaCliApplicationTest.php b/tests/AcquiaCliApplicationTest.php index fb75547..48d0afb 100644 --- a/tests/AcquiaCliApplicationTest.php +++ b/tests/AcquiaCliApplicationTest.php @@ -8,9 +8,12 @@ use AcquiaCli\Cli\AcquiaCli; use Symfony\Component\Console\Output\BufferedOutput; use Symfony\Component\Stopwatch\Stopwatch; +use Symfony\Component\Console\Command\LockableTrait; class AcquiaCliApplicationTest extends AcquiaCliTestCase { + use LockableTrait; + private $root; public function setUp() @@ -49,6 +52,23 @@ public function testVersion() $this->assertContains('AcquiaCli 2', $actualValue); } + public function testLock() + { + // Obtain an identical lock and then attempt to run acquiacli. + $this->lock('acquia-cli-command'); + $command = ['--version']; + $actualValue = $this->execute($command); + $this->assertContains('The command is already running in another process.', $actualValue); + + // Use --no-lock to override the lock. + $command[] = '--no-lock'; + $actualValue = $this->execute($command); + $this->assertContains('AcquiaCli', $actualValue); + + // Unlock to ensure tests are able to continue. + $this->release(); + } + public function testClientOptions() { $command = ['application:list', '--sort=label', '--filter=label=@*sample*', '--limit=2'];