Skip to content

Commit

Permalink
CLI-1365: Warn when logs need to be created
Browse files Browse the repository at this point in the history
  • Loading branch information
danepowell committed Nov 19, 2024
1 parent 7298135 commit 32333a4
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 113 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"symfony/yaml": "^6.3",
"thecodingmachine/safe": "^2.4",
"typhonius/acquia-logstream": "^0.0.14",
"typhonius/acquia-php-sdk-v2": "^3.3.2",
"typhonius/acquia-php-sdk-v2": "^3.4.0",
"vlucas/phpdotenv": "^5.5",
"zumba/amplitude-php": "^1.0.4"
},
Expand Down
206 changes: 105 additions & 101 deletions composer.lock

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/Command/Api/ApiBaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ protected function interact(InputInterface $input, OutputInterface $output): voi
/**
* @throws \Acquia\Cli\Exception\AcquiaCliException
* @throws \JsonException
* @throws \AcquiaCloudApi\Exception\ApiErrorException
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
Expand All @@ -122,8 +123,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$response = $acquiaCloudClient->request($this->method, $path);
$exitCode = 0;
} catch (ApiErrorException $exception) {
// Ignore PhpStorm warning here.
// @see https://youtrack.jetbrains.com/issue/WI-77190/Exception-is-never-thrown-when-thrown-from-submethod
if ($input->isInteractive()) {
throw $exception;

Check warning on line 127 in src/Command/Api/ApiBaseCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/Api/ApiBaseCommand.php#L127

Added line #L127 was not covered by tests
}
$response = $exception->getResponseBody();
$exitCode = 1;
}
Expand Down
6 changes: 5 additions & 1 deletion src/EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\Console\Exception\RuntimeException;
use TypeError;

/**
* Make exceptions warm and cuddly.
Expand Down Expand Up @@ -82,6 +83,9 @@ public function onConsoleError(ConsoleErrorEvent $event): void
}

if ($error instanceof ApiErrorException) {
if (($command = $event->getCommand()) && $error->getResponseBody()->error === 'not_found' && $command->getName() === 'api:environments:log-download') {

Check warning on line 86 in src/EventListener/ExceptionListener.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "Identical": --- Original +++ New @@ @@ } } if ($error instanceof ApiErrorException) { - if (($command = $event->getCommand()) && $error->getResponseBody()->error === 'not_found' && $command->getName() === 'api:environments:log-download') { + if (($command = $event->getCommand()) && $error->getResponseBody()->error === 'not_found' && $command->getName() !== 'api:environments:log-download') { $this->helpMessages[] = "You must create logs (api:environments:log-create) prior to downloading them"; } switch ($errorMessage) {

Check warning on line 86 in src/EventListener/ExceptionListener.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "LogicalAndAllSubExprNegation": --- Original +++ New @@ @@ } } if ($error instanceof ApiErrorException) { - if (($command = $event->getCommand()) && $error->getResponseBody()->error === 'not_found' && $command->getName() === 'api:environments:log-download') { + if (!($command = $event->getCommand()) && !($error->getResponseBody()->error === 'not_found') && !($command->getName() === 'api:environments:log-download')) { $this->helpMessages[] = "You must create logs (api:environments:log-create) prior to downloading them"; } switch ($errorMessage) {
$this->helpMessages[] = "You must create logs (api:environments:log-create) prior to downloading them";

Check warning on line 87 in src/EventListener/ExceptionListener.php

View check run for this annotation

Codecov / codecov/patch

src/EventListener/ExceptionListener.php#L87

Added line #L87 was not covered by tests
}
switch ($errorMessage) {
case "There are no available Cloud IDEs for this application.\n":
$this->helpMessages[] = "Delete an existing IDE via <bg=$this->messagesBgColor;fg=$this->messagesFgColor;options=bold>acli ide:delete</> or contact your Account Manager or Acquia Sales to purchase additional IDEs.";
Expand All @@ -96,7 +100,7 @@ public function onConsoleError(ConsoleErrorEvent $event): void
}
}

if ($error instanceof \TypeError && str_contains($error->getMessage(), 'AcquiaCloudApi\Response')) {
if ($error instanceof TypeError && str_contains($error->getMessage(), 'AcquiaCloudApi\Response')) {

Check warning on line 103 in src/EventListener/ExceptionListener.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ $this->helpMessages[] = "You can learn more about Cloud Platform API at https://docs.acquia.com/cloud-platform/develop/api/"; } } - if ($error instanceof TypeError && str_contains($error->getMessage(), 'AcquiaCloudApi\\Response')) { + if (true && str_contains($error->getMessage(), 'AcquiaCloudApi\\Response')) { $newErrorMessage = 'Cloud Platform API returned an unexpected data type. This could be due to missing permissions or a problem with your Cloud Platform application.'; } if (!empty($this->helpMessages)) {

Check warning on line 103 in src/EventListener/ExceptionListener.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "LogicalAnd": --- Original +++ New @@ @@ $this->helpMessages[] = "You can learn more about Cloud Platform API at https://docs.acquia.com/cloud-platform/develop/api/"; } } - if ($error instanceof TypeError && str_contains($error->getMessage(), 'AcquiaCloudApi\\Response')) { + if ($error instanceof TypeError || str_contains($error->getMessage(), 'AcquiaCloudApi\\Response')) { $newErrorMessage = 'Cloud Platform API returned an unexpected data type. This could be due to missing permissions or a problem with your Cloud Platform application.'; } if (!empty($this->helpMessages)) {

Check warning on line 103 in src/EventListener/ExceptionListener.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ $this->helpMessages[] = "You can learn more about Cloud Platform API at https://docs.acquia.com/cloud-platform/develop/api/"; } } - if ($error instanceof TypeError && str_contains($error->getMessage(), 'AcquiaCloudApi\\Response')) { + if (false && str_contains($error->getMessage(), 'AcquiaCloudApi\\Response')) { $newErrorMessage = 'Cloud Platform API returned an unexpected data type. This could be due to missing permissions or a problem with your Cloud Platform application.'; } if (!empty($this->helpMessages)) {

Check warning on line 103 in src/EventListener/ExceptionListener.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "LogicalAndAllSubExprNegation": --- Original +++ New @@ @@ $this->helpMessages[] = "You can learn more about Cloud Platform API at https://docs.acquia.com/cloud-platform/develop/api/"; } } - if ($error instanceof TypeError && str_contains($error->getMessage(), 'AcquiaCloudApi\\Response')) { + if (!$error instanceof TypeError && !str_contains($error->getMessage(), 'AcquiaCloudApi\\Response')) { $newErrorMessage = 'Cloud Platform API returned an unexpected data type. This could be due to missing permissions or a problem with your Cloud Platform application.'; } if (!empty($this->helpMessages)) {

Check warning on line 103 in src/EventListener/ExceptionListener.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "LogicalAndNegation": --- Original +++ New @@ @@ $this->helpMessages[] = "You can learn more about Cloud Platform API at https://docs.acquia.com/cloud-platform/develop/api/"; } } - if ($error instanceof TypeError && str_contains($error->getMessage(), 'AcquiaCloudApi\\Response')) { + if (!($error instanceof TypeError && str_contains($error->getMessage(), 'AcquiaCloudApi\\Response'))) { $newErrorMessage = 'Cloud Platform API returned an unexpected data type. This could be due to missing permissions or a problem with your Cloud Platform application.'; } if (!empty($this->helpMessages)) {

Check warning on line 103 in src/EventListener/ExceptionListener.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "LogicalAndSingleSubExprNegation": --- Original +++ New @@ @@ $this->helpMessages[] = "You can learn more about Cloud Platform API at https://docs.acquia.com/cloud-platform/develop/api/"; } } - if ($error instanceof TypeError && str_contains($error->getMessage(), 'AcquiaCloudApi\\Response')) { + if ($error instanceof TypeError && !str_contains($error->getMessage(), 'AcquiaCloudApi\\Response')) { $newErrorMessage = 'Cloud Platform API returned an unexpected data type. This could be due to missing permissions or a problem with your Cloud Platform application.'; } if (!empty($this->helpMessages)) {
$newErrorMessage = 'Cloud Platform API returned an unexpected data type. This could be due to missing permissions or a problem with your Cloud Platform application.';
}

Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/src/CommandTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected function setCommand(CommandBase $command): void
* An array of strings representing each input passed to the command input
* stream.
*/
protected function executeCommand(array $args = [], array $inputs = [], int $verbosity = Output::VERBOSITY_VERY_VERBOSE): void
protected function executeCommand(array $args = [], array $inputs = [], int $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE, ?bool $interactive = true): void
{
$cwd = $this->projectDir;
$tester = $this->getCommandTester();
Expand All @@ -96,7 +96,7 @@ protected function executeCommand(array $args = [], array $inputs = [], int $ver
}

try {
$tester->execute($args, ['verbosity' => $verbosity]);
$tester->execute($args, ['verbosity' => $verbosity, 'interactive' => $interactive]);
} catch (Exception $e) {
if (getenv('ACLI_PRINT_COMMAND_OUTPUT')) {
print $this->getDisplay();
Expand Down
9 changes: 7 additions & 2 deletions tests/phpunit/src/Commands/Api/ApiCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Acquia\Cli\Exception\AcquiaCliException;
use Acquia\Cli\Tests\CommandTestBase;
use AcquiaCloudApi\Exception\ApiErrorException;
use Overtrue\PHPLint\Output\OutputInterface;
use Symfony\Component\Console\Exception\MissingInputException;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\Yaml\Yaml;
Expand Down Expand Up @@ -105,6 +106,10 @@ public function testArgumentsInteractionValidationFormat(): void

/**
* Tests invalid UUID.
*
* @throws \JsonException
* @throws \AcquiaCloudApi\Exception\ApiErrorException
* @throws \Exception
*/
public function testApiCommandErrorResponse(): void
{
Expand All @@ -117,7 +122,7 @@ public function testApiCommandErrorResponse(): void
->willThrow(new ApiErrorException($mockBody))
->shouldBeCalled();

// ApiCommandBase::convertApplicationAliastoUuid() will try to convert the invalid string to a uuid:
// ApiCommandBase::convertApplicationAliasToUuid() will try to convert the invalid string to a UUID:
$this->clientProphecy->addQuery('filter', 'hosting=@*:' . $invalidUuid);
$this->clientProphecy->request('get', '/applications')->willReturn([]);

Expand All @@ -128,7 +133,7 @@ public function testApiCommandErrorResponse(): void
'0',
// Would you like to link the Cloud application Sample application to this repository?
'n',
]);
], \Symfony\Component\Console\Output\OutputInterface::VERBOSITY_VERY_VERBOSE, false);

// Assert.
$output = $this->getDisplay();
Expand Down
3 changes: 0 additions & 3 deletions tests/phpunit/src/Commands/Remote/DrushCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ public static function providerTestRemoteDrushCommand(): array
return [
[
[
'-vvv' => '',
'drush_command' => 'status --fields=db-status',
],
],
[
[
'-vvv' => '',
'drush_command' => 'status --fields=db-status',
],
],
Expand All @@ -42,7 +40,6 @@ public static function providerTestRemoteDrushCommand(): array

/**
* @dataProvider providerTestRemoteDrushCommand
* @group serial
*/
public function testRemoteDrushCommand(array $args): void
{
Expand Down
1 change: 0 additions & 1 deletion tests/phpunit/src/Misc/LocalMachineHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public function testExecuteFromCmd(bool $interactive, bool|null $isTty, bool|nul
{
$localMachineHelper = $this->localMachineHelper;
$localMachineHelper->setIsTty($isTty);
$this->input->setInteractive($interactive);
$process = $localMachineHelper->executeFromCmd('echo "hello world"', null, null, $printOutput);
$this->assertTrue($process->isSuccessful());
assert(is_a($this->output, BufferedOutput::class));
Expand Down

0 comments on commit 32333a4

Please sign in to comment.