Skip to content

Commit

Permalink
Fix verify command on PHAR files without the .phar extension (#244)
Browse files Browse the repository at this point in the history
- Fix some typos
- Ensures the `verify` command works on a PHAR which does not have the `.phar` extension
- Throws the throwable catch in debug mode instead of verbose for the `verify` command
  • Loading branch information
theofidry authored May 30, 2018
1 parent 107fa0a commit c4be6de
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 29 deletions.
20 changes: 10 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ help:
#---------------------------------------------------------------------------

.PHONY: clean
clean: ## Clean all created artifacts
clean: ## Cleans all created artifacts
clean:
git clean --exclude=.idea/ -ffdx

.PHONY: cs
PHPCSFIXER=vendor-bin/php-cs-fixer/vendor/bin/php-cs-fixer
cs: ## Fix CS
cs: ## Fixes CS
cs: $(PHPCSFIXER)
$(PHPNOGC) $(PHPCSFIXER) fix
$(PHPNOGC) $(PHPCSFIXER) fix --config .php_cs_53.dist

.PHONY: compile
compile: ## Compile the application into the PHAR
compile: ## Compiles the application into the PHAR
compile: box
cp -f box bin/box.phar

Expand All @@ -43,38 +43,38 @@ dump-requirement-checker: requirement-checker requirement-checker/vendor
#---------------------------------------------------------------------------

.PHONY: test
test: ## Run all the tests
test: ## Runs all the tests
test: tu e2e

.PHONY: tu
tu: ## Run the unit tests
tu: ## Runs the unit tests
tu: tu_requirement_checker tu_box

.PHONY: tu_box
tu_box: ## Run the unit tests
tu_box: ## Runs the unit tests
TU_BOX_DEPS = bin/phpunit fixtures/default_stub.php .requirement-checker fixtures/composer-dump/dir001/vendor
tu_box: $(TU_BOX_DEPS)
$(PHPNOGC) bin/phpunit

.PHONY: tu_box_phar_readonly
tu_box_phar_readonly: ## Runs the unit tests with the setting `phar.readonly` to `On`
tu_box_phar_readonly: ## Runs the unit tests with the setting `phar.readonly` to `On`
tu_box_phar_readonly: $(TU_BOX_DEPS)
php -d zend.enable_gc=0 -d phar.readonly=1 bin/phpunit

.PHONY: tu_requirement_checker
tu_requirement_checker: ## Run the unit tests
tu_requirement_checker: ## Runs the unit tests
tu_requirement_checker: requirement-checker/bin/phpunit requirement-checker/tests/DisplayNormalizer.php requirement-checker/actual_terminal_diff
cd requirement-checker && $(PHPNOGC) bin/phpunit

diff requirement-checker/expected_terminal_diff requirement-checker/actual_terminal_diff

.PHONY: tc
tc: ## Run the unit tests with code coverage
tc: ## Runs the unit tests with code coverage
tc: bin/phpunit
phpdbg -qrr -d zend.enable_gc=0 bin/phpunit --coverage-html=dist/coverage --coverage-text

.PHONY: tm
tm: ## Run Infection
tm: ## Runs Infection
tm: $(TU_BOX_DEPS)
$(PHPNOGC) bin/infection

Expand Down
Binary file added fixtures/verify/simple-phar
Binary file not shown.
8 changes: 6 additions & 2 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,12 @@ final class Configuration
* @param null|string $alias
* @param string $basePath Utility to private the base path used and be able to retrieve a
* path relative to it (the base path)
* @param null[]|string[] $composerJson
* @param null[]|string[] $composerLock
* @param array $composerJson The first element is the path to the `composer.json` file as a
* string and the second element its decoded contents as an
* associative array.
* @param array $composerLock The first element is the path to the `composer.lock` file as a
* string and the second element its decoded contents as an
* associative array.
* @param SplFileInfo[] $files List of files
* @param SplFileInfo[] $binaryFiles List of binary files
* @param bool $dumpAutoload Whether or not the Composer autoloader should be dumped
Expand Down
6 changes: 3 additions & 3 deletions src/Console/Command/Compile.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ private function registerStub(Configuration $config, Box $box, ?string $main, bo

$stub = $this->createStub($config, $main, $checkRequirements, $logger);

$box->getPhar()->setStub($stub->generate());
$box->getPhar()->setStub($stub);

return;
}
Expand Down Expand Up @@ -729,7 +729,7 @@ private function correctPermissions(string $path, Configuration $config, BuildLo
}
}

private function createStub(Configuration $config, ?string $main, bool $checkRequirements, BuildLogger $logger): StubGenerator
private function createStub(Configuration $config, ?string $main, bool $checkRequirements, BuildLogger $logger): string
{
$stub = StubGenerator::create()
->alias($config->getAlias())
Expand Down Expand Up @@ -783,7 +783,7 @@ private function createStub(Configuration $config, ?string $main, bool $checkReq
$stub->banner($banner);
}

return $stub;
return $stub->generate();
}

private function logMap(MapFile $fileMapper, BuildLogger $logger): void
Expand Down
35 changes: 35 additions & 0 deletions src/Console/Command/CreateTemporaryPharFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <[email protected]>
* Théo Fidry <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace KevinGH\Box\Console\Command;

use DateTimeImmutable;
use function KevinGH\Box\FileSystem\copy;

/**
* @private
*/
trait CreateTemporaryPharFile
{
final private function createTemporaryPhar(string $file): string
{
if ('' === pathinfo($file, PATHINFO_EXTENSION)) {
copy($file, $tmpFile = sys_get_temp_dir().'/'.(new DateTimeImmutable())->getTimestamp().$file.'.phar');

return $tmpFile;
}

return $file;
}
}
18 changes: 7 additions & 11 deletions src/Console/Command/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
namespace KevinGH\Box\Console\Command;

use Assert\Assertion;
use DateTimeImmutable;
use DirectoryIterator;
use Phar;
use PharData;
Expand All @@ -38,22 +37,22 @@
use function filesize;
use function is_array;
use function iterator_to_array;
use function KevinGH\Box\FileSystem\copy;
use function KevinGH\Box\FileSystem\remove;
use function KevinGH\Box\format_size;
use function key;
use function realpath;
use function sprintf;
use function str_repeat;
use function str_replace;
use function sys_get_temp_dir;
use function var_export;

/**
* @private
*/
final class Info extends Command
{
use CreateTemporaryPharFile;

private const PHAR_ARG = 'phar';
private const LIST_OPT = 'list';
private const METADATA_OPT = 'metadata';
Expand Down Expand Up @@ -162,18 +161,15 @@ public function execute(InputInterface $input, OutputInterface $output): int
return 1;
}

if ('' === pathinfo($file, PATHINFO_EXTENSION)) {
// It is likely to be a PHAR without extension
copy($file, $tmpFile = sys_get_temp_dir().'/'.(new DateTimeImmutable())->getTimestamp().$file.'.phar');
$tmpFile = $this->createTemporaryPhar($file);

try {
return $this->showInfo($tmpFile, $file, $input, $output, $io);
} finally {
try {
return $this->showInfo($tmpFile, $file, $input, $output, $io);
} finally {
if ($file !== $tmpFile) {
remove($tmpFile);
}
}

return $this->showInfo($file, $file, $input, $output, $io);
}

public function showInfo(string $file, string $originalFile, InputInterface $input, OutputInterface $output, SymfonyStyle $io): int
Expand Down
13 changes: 11 additions & 2 deletions src/Console/Command/Verify.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace KevinGH\Box\Console\Command;

use Assert\Assertion;
use function KevinGH\Box\FileSystem\remove;
use Phar;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -30,6 +31,8 @@
*/
final class Verify extends Command
{
use CreateTemporaryPharFile;

private const PHAR_ARG = 'phar';
private const VERBOSITY_LEVEL = OutputInterface::VERBOSITY_VERBOSE;

Expand Down Expand Up @@ -83,8 +86,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
self::VERBOSITY_LEVEL
);

$tmpPharPath = $this->createTemporaryPhar($pharPath);

try {
$phar = new Phar($pharPath);
$phar = new Phar($tmpPharPath);

$verified = true;
$signature = $phar->getSignature();
Expand All @@ -93,6 +98,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$verified = false;
$signature = null;
} finally {
if ($tmpPharPath !== $pharPath) {
remove($tmpPharPath);
}
}

if (false === $verified) {
Expand All @@ -108,7 +117,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
)
);

if (isset($throwable) && $output->isVerbose()) {
if (isset($throwable) && $output->isDebug()) {
throw $throwable;
}

Expand Down
18 changes: 17 additions & 1 deletion tests/Console/Command/VerifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ public function test_it_verifies_the_signature_of_the_given_file_using_the_phar_
$expected = <<<'OUTPUT'
The PHAR passed verification.

OUTPUT;

$this->assertSame($expected, $this->commandTester->getDisplay(true));
$this->assertSame(0, $this->commandTester->getStatusCode());
}

public function test_it_can_verify_a_PHAR_which_does_not_have_the_PHAR_extension(): void
{
$this->commandTester->execute([
'command' => 'verify',
'phar' => realpath(self::FIXTURES_DIR.'/simple-phar'),
]);

$expected = <<<'OUTPUT'
The PHAR passed verification.

OUTPUT;

$this->assertSame($expected, $this->commandTester->getDisplay(true));
Expand All @@ -70,7 +86,7 @@ public function test_it_verifies_the_signature_of_the_given_file_using_the_phar_
/**
* @dataProvider providePassingPharPaths
*/
public function test_it_verifies_the_signature_of_the_given_file_in_verbose_mode(string $pharPath): void
public function test_it_verifies_the_signature_of_the_given_file_in_debug_mode(string $pharPath): void
{
$signature = (new Phar($pharPath))->getSignature();

Expand Down

0 comments on commit c4be6de

Please sign in to comment.