From 7daef907270c4ca48a99431fc4a86ec1b7556bdb Mon Sep 17 00:00:00 2001 From: Dane Powell Date: Fri, 17 Nov 2023 12:42:47 -0800 Subject: [PATCH] CLI-1221: TypeError and warning in remote:alises:download (#1636) * CLI-1221: TypeError and warning in remote:alises:download * add test --- src/Command/CommandBase.php | 17 +++++++++-------- .../src/Commands/InferApplicationTest.php | 16 ++++++++++------ .../Commands/Ssh/SshKeyUploadCommandTest.php | 6 +++--- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/Command/CommandBase.php b/src/Command/CommandBase.php index 691a7c5c1..bff811383 100644 --- a/src/Command/CommandBase.php +++ b/src/Command/CommandBase.php @@ -48,6 +48,7 @@ use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerAwareTrait; use Psr\Log\LoggerInterface; +use Safe\Exceptions\FilesystemException; use stdClass; use Symfony\Component\Cache\Adapter\FilesystemAdapter; use Symfony\Component\Console\Command\Command; @@ -454,16 +455,12 @@ public function promptChooseFromObjectsOrArrays(array|ArrayObject $items, string /** * Load configuration from .git/config. * - * @return string[][]|null + * @return string[][] * A multidimensional array keyed by file section. */ - private function getGitConfig(): ?array { + private function getGitConfig(): array { $filePath = $this->projectDir . '/.git/config'; - if (file_exists($filePath)) { - return parse_ini_file($filePath, TRUE); - } - - return NULL; + return @\Safe\parse_ini_file($filePath, TRUE); } /** @@ -574,7 +571,8 @@ protected function inferCloudAppFromLocalGitConfig( $answer = $this->io->confirm('Would you like Acquia CLI to search for a Cloud application that matches your local git config?'); if ($answer) { $this->output->writeln('Searching for a matching Cloud application...'); - if ($gitConfig = $this->getGitConfig()) { + try { + $gitConfig = $this->getGitConfig(); $localGitRemotes = $this->getGitRemotes($gitConfig); if ($cloudApplication = $this->findCloudApplicationByGitUrl($acquiaCloudClient, $localGitRemotes)) { @@ -585,6 +583,9 @@ protected function inferCloudAppFromLocalGitConfig( $this->output->writeln('Could not find a matching Cloud application.'); return NULL; } + catch (FilesystemException $e) { + throw new AcquiaCliException($e->getMessage()); + } } } diff --git a/tests/phpunit/src/Commands/InferApplicationTest.php b/tests/phpunit/src/Commands/InferApplicationTest.php index 1d3617902..7a5ceb219 100644 --- a/tests/phpunit/src/Commands/InferApplicationTest.php +++ b/tests/phpunit/src/Commands/InferApplicationTest.php @@ -5,6 +5,7 @@ namespace Acquia\Cli\Tests\Commands; use Acquia\Cli\Command\App\LinkCommand; +use Acquia\Cli\Exception\AcquiaCliException; use Acquia\Cli\Tests\CommandTestBase; use Symfony\Component\Console\Command\Command; @@ -20,13 +21,8 @@ protected function createCommand(): Command { return $this->injectCommand(LinkCommand::class); } - public function setUp(mixed $output = NULL): void { - parent::setUp(); - $this->createMockGitConfigFile(); - } - public function testInfer(): void { - + $this->createMockGitConfigFile(); $applicationsResponse = $this->mockApplicationsRequest(); $this->mockApplicationRequest(); $environmentResponse = $this->getMockEnvironmentResponse(); @@ -56,6 +52,7 @@ public function testInfer(): void { } public function testInferFailure(): void { + $this->createMockGitConfigFile(); $applicationsResponse = $this->mockApplicationsRequest(); $this->mockApplicationRequest(); @@ -87,4 +84,11 @@ public function testInferFailure(): void { $this->assertStringContainsString('The Cloud application Sample application 1 has been linked', $output); } + public function testInferInvalidGitConfig(): void { + $this->expectException(AcquiaCliException::class); + $this->executeCommand([], [ + 'y', + ]); + } + } diff --git a/tests/phpunit/src/Commands/Ssh/SshKeyUploadCommandTest.php b/tests/phpunit/src/Commands/Ssh/SshKeyUploadCommandTest.php index 2a5e3fc00..47f9696ba 100644 --- a/tests/phpunit/src/Commands/Ssh/SshKeyUploadCommandTest.php +++ b/tests/phpunit/src/Commands/Ssh/SshKeyUploadCommandTest.php @@ -36,7 +36,7 @@ public function providerTestUpload(): array { // Would you like to wait until Cloud Platform is ready? (yes/no) 'y', // Would you like Acquia CLI to search for a Cloud application that matches your local git config? (yes/no) - 'y', + 'n', ], // Perms. TRUE, @@ -52,7 +52,7 @@ public function providerTestUpload(): array { // Would you like to wait until Cloud Platform is ready? (yes/no) 'y', // Would you like Acquia CLI to search for a Cloud application that matches your local git config? (yes/no) - 'y', + 'n', ], // Perms. FALSE, @@ -63,7 +63,7 @@ public function providerTestUpload(): array { /** * @dataProvider providerTestUpload */ - public function testUpload(mixed $args, mixed $inputs, mixed $perms): void { + public function testUpload(array $args, array $inputs, bool $perms): void { $sshKeysRequestBody = $this->getMockRequestBodyFromSpec('/account/ssh-keys'); $body = [ 'json' => [