Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Simplify the error handling of Extract #965

Merged
merged 1 commit into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 3 additions & 20 deletions src/Console/Command/Extract.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use KevinGH\Box\Box;
use KevinGH\Box\Pharaoh\Pharaoh;
use RecursiveIteratorIterator;
use RuntimeException;
use Symfony\Component\Console\Exception\RuntimeException as ConsoleRuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Throwable;
Expand Down Expand Up @@ -70,7 +69,7 @@ public function execute(IO $io): int
return ExitCode::FAILURE;
}

[$box, $cleanUpTmpPhar] = $this->getBox($filePath, $io);
[$box, $cleanUpTmpPhar] = $this->getBox($filePath);

if (null === $box) {
return ExitCode::FAILURE;
Expand All @@ -83,13 +82,7 @@ public function execute(IO $io): int
$restoreLimit();
};

try {
self::dumpPhar($outputDir, $box, $cleanUp);
} catch (RuntimeException $exception) {
$io->error($exception->getMessage());

return ExitCode::FAILURE;
}
self::dumpPhar($outputDir, $box, $cleanUp);

return ExitCode::SUCCESS;
}
Expand All @@ -115,7 +108,7 @@ private static function getPharFilePath(IO $io): ?string
/**
* @return array{Box, callable(): void}|array{null, null}
*/
private function getBox(string $filePath, IO $io): ?array
private function getBox(string $filePath): ?array
{
$cleanUp = static fn () => null;

Expand All @@ -133,10 +126,6 @@ private function getBox(string $filePath, IO $io): ?array
$cleanUp,
];
} catch (Throwable $throwable) {
// Continue
}

if ($io->isDebug()) {
$cleanUp();

throw new ConsoleRuntimeException(
Expand All @@ -145,12 +134,6 @@ private function getBox(string $filePath, IO $io): ?array
$throwable,
);
}

$io->error('The given file is not a valid PHAR.');

$cleanUp();

return [null, null];
}

/**
Expand Down
44 changes: 30 additions & 14 deletions tests/Console/Command/ExtractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,38 +114,54 @@ private static function pharProvider(): iterable
*/
public function test_it_cannot_extract_an_invalid_phar(
string $pharPath,
string $exceptionClassName,
string $expectedExceptionMessage,
): void {
$this->commandTester->execute(
[
'command' => 'extract',
'phar' => $pharPath,
'output' => $this->tmp,
],
);

$expectedOutput = <<<'OUTPUT'

[ERROR] The given file is not a valid PHAR.
try {
$this->commandTester->execute(
[
'command' => 'extract',
'phar' => $pharPath,
'output' => $this->tmp,
],
);

self::fail('Expected exception to be thrown.');
} catch (RuntimeException $exception) {
// Continue
$innerException = $exception->getPrevious();
}

OUTPUT;
self::assertSame(
$exceptionClassName,
$innerException::class,
);
self::assertMatchesRegularExpression(
$expectedExceptionMessage,
$innerException->getMessage(),
);

$this->assertSameOutput($expectedOutput, ExitCode::FAILURE);
self::assertSame([], $this->collectExtractedFiles());
}

public static function invalidPharPath(): iterable
{
yield 'not a valid PHAR with the PHAR extension' => [
self::FIXTURES.'/invalid.phar',
InvalidPhar::class,
'/^Could not create a Phar or PharData instance for the file/',
];

yield 'not a valid PHAR without the PHAR extension' => [
self::FIXTURES.'/invalid.phar',
self::FIXTURES.'/invalid',
InvalidPhar::class,
'/^Could not create a Phar or PharData instance for the file .+$/',
];

yield 'corrupted PHAR (was valid; got tempered with' => [
self::FIXTURES.'/corrupted.phar',
InvalidPhar::class,
'/^Could not create a Phar or PharData instance for the file .+$/',
];
}

Expand Down