Skip to content

Commit

Permalink
fix: Do not rethrow the exception in the info command
Browse files Browse the repository at this point in the history
Since `PharInfo` has been introduced, the exception should be clear and
easily understandable so this extra catch logic is no longer necessary.
  • Loading branch information
theofidry committed Oct 16, 2023
1 parent 9a3856c commit f987f7f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 43 deletions.
32 changes: 8 additions & 24 deletions src/Console/Command/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Filesystem\Path;
use Throwable;
use function implode;
use function is_array;
use function realpath;
Expand Down Expand Up @@ -129,30 +128,15 @@ public static function showInfo(string $file, IO $io): int
$maxDepth = self::getMaxDepth($io);
$mode = $io->getOption(self::MODE_OPT)->asStringChoice(self::MODES);

try {
$pharInfo = new PharInfo($file);
$pharInfo = new PharInfo($file);

return self::showPharInfo(
$pharInfo,
$io->getOption(self::LIST_OPT)->asBoolean(),
-1 === $maxDepth ? false : $maxDepth,
'indent' === $mode,
$io,
);
} catch (Throwable $throwable) {
if ($io->isDebug()) {
throw $throwable;
}

$io->error(
sprintf(
'Could not read the file "%s".',
$file,
),
);

return ExitCode::FAILURE;
}
return self::showPharInfo(
$pharInfo,
$io->getOption(self::LIST_OPT)->asBoolean(),
-1 === $maxDepth ? false : $maxDepth,
'indent' === $mode,
$io,
);
}

/**
Expand Down
22 changes: 3 additions & 19 deletions tests/Console/Command/InfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
use Symfony\Component\Console\Output\OutputInterface;
use function extension_loaded;
use function implode;
use function preg_replace;
use function Safe\realpath;

/**
* @covers \KevinGH\Box\Console\Command\Info
Expand Down Expand Up @@ -602,32 +600,18 @@ public static function inputProvider(): iterable
}
}

public function test_it_cannot_provide_info_about_an_invalid_phar_without_extension(): void
public function test_it_cannot_provide_info_about_an_invalid_phar(): void
{
$file = self::FIXTURES.'/foo';

$this->expectException(InvalidPhar::class);

$this->commandTester->execute(
[
'command' => 'info',
'phar' => $file,
],
);

$expectedPath = realpath($file);

$expected = <<<OUTPUT
[ERROR] Could not read the file "{$expectedPath}".
OUTPUT;

$this->assertSameOutput(
$expected,
ExitCode::FAILURE,
static fn ($output) => preg_replace('/file[\ \n]+"/', 'file "', $output),
);
}

public function test_it_displays_the_error_in_debug_verbosity(): void
Expand Down

0 comments on commit f987f7f

Please sign in to comment.