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

feat: Prompt a question before removing the output directory when extracting a PHAR #968

Merged
merged 1 commit into from
Mar 31, 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
24 changes: 24 additions & 0 deletions src/Console/Command/Extract.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
use Phar;
use PharData;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Throwable;
use UnexpectedValueException;
use function file_exists;
use function KevinGH\Box\FileSystem\copy;
use function KevinGH\Box\FileSystem\mkdir;
use function KevinGH\Box\FileSystem\remove;
use function realpath;
use function sprintf;
Expand Down Expand Up @@ -69,6 +72,27 @@ public function execute(IO $io): int
return ExitCode::FAILURE;
}

if (file_exists($outputDir)) {
$canDelete = $io->askQuestion(
new ConfirmationQuestion(
'The output directory already exists. Do you want to delete its current content?',
// If is interactive, we want the prompt to default to false since it can be an error made by the user.
// Otherwise, this is likely launched by a script or Pharaoh in which case we do not care.
!$io->isInteractive(),
),
);

if ($canDelete) {
remove($outputDir);
// Continue
} else {
// Do nothing
return ExitCode::FAILURE;
}
}

mkdir($outputDir);

self::dumpPhar($pharPath, $outputDir);

return ExitCode::SUCCESS;
Expand Down
81 changes: 81 additions & 0 deletions tests/Console/Command/ExtractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use KevinGH\Box\Test\RequiresPharReadonlyOff;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use function count;
use function KevinGH\Box\FileSystem\make_path_relative;

/**
Expand Down Expand Up @@ -56,12 +57,14 @@ public function test_it_can_extract_a_phar(
string $pharPath,
array $expectedFiles,
): void {
$this->commandTester->setInputs(['yes']);
$this->commandTester->execute(
[
'command' => 'extract',
'phar' => $pharPath,
'output' => $this->tmp,
],
['interactive' => false],
);

$actualFiles = $this->collectExtractedFiles();
Expand Down Expand Up @@ -117,6 +120,83 @@ private static function pharProvider(): iterable
];
}

/**
* @dataProvider confirmationQuestionProvider
*/
public function test_it_asks_confirmation_before_deleting_the_output_dir(
bool $outputDirExists,
bool $interactive,
bool $input,
bool $expectedToSucceed,
): void {
$outputDir = $outputDirExists ? $this->tmp : $this->tmp.'/subdir';

$this->commandTester->setInputs([$input ? 'yes' : 'no']);
$this->commandTester->execute(
[
'command' => 'extract',
'phar' => self::FIXTURES.'/simple-phar.phar',
'output' => $outputDir,
],
['interactive' => $interactive],
);

$actualFiles = $this->collectExtractedFiles();

if ($expectedToSucceed) {
self::assertSame(ExitCode::SUCCESS, $this->commandTester->getStatusCode());
self::assertGreaterThan(0, count($actualFiles));
} else {
self::assertSame(ExitCode::FAILURE, $this->commandTester->getStatusCode());
self::assertSame(0, count($actualFiles));
}
}

public static function confirmationQuestionProvider(): iterable
{
yield 'exists; accept' => [
true,
true,
true,
true,
];

yield 'exists; refuse' => [
true,
true,
false,
false,
];

yield 'does not exist: the question is not asked' => [
false,
true,
false,
true,
];

yield 'exists; not interactive; accept' => [
true,
false,
true,
true,
];

yield 'exists; not interactive; refuse' => [
true,
false,
false,
true,
];

yield 'not interactive; does not exist: the question is not asked' => [
false,
false,
false,
true,
];
}

/**
* @dataProvider invalidPharPath
*/
Expand All @@ -132,6 +212,7 @@ public function test_it_cannot_extract_an_invalid_phar(
'phar' => $pharPath,
'output' => $this->tmp,
],
['interactive' => false],
);

self::fail('Expected exception to be thrown.');
Expand Down