From 1a20241f225f2ccf3e9e27d3336440352fa23371 Mon Sep 17 00:00:00 2001 From: Anatoliy Melnikov <5785276@gmail.com> Date: Sun, 27 Oct 2024 19:19:30 +0300 Subject: [PATCH] Add extension .phar to installed tools --- src/commands/help/help.md | 2 + src/commands/install/InstallCommand.php | 10 +- src/commands/install/InstallCommandConfig.php | 4 + src/commands/install/InstallContext.php | 1 + .../install/InstallCommandConfigTest.php | 22 +++++ .../commands/install/InstallCommandTest.php | 92 +++++++++++++++++++ .../commands/install/InstallContextTest.php | 52 +++++++++++ 7 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 tests/unit/commands/install/InstallCommandTest.php create mode 100644 tests/unit/commands/install/InstallContextTest.php diff --git a/src/commands/help/help.md b/src/commands/help/help.md index 0748fc43..554b8019 100644 --- a/src/commands/help/help.md +++ b/src/commands/help/help.md @@ -25,6 +25,8 @@ _-t, --target_ Set custom target directory for the PHAR _-c, --copy_ Copy PHAR file instead of using symlink + _-e, --extension_ Add extension ".phar" to installed file. It can be usefull for autocompletion by IDE + in configuration files of installed tools. _-g, --global_ Install a copy of the PHAR globally (likely requires root privileges) _--temporary_ Do not add entries in phive.xml for installed PHARs _--trust-gpg-keys_ Silently import these keys when required (40-digit fingerprint diff --git a/src/commands/install/InstallCommand.php b/src/commands/install/InstallCommand.php index 379b7303..2dcc60ac 100644 --- a/src/commands/install/InstallCommand.php +++ b/src/commands/install/InstallCommand.php @@ -63,6 +63,14 @@ protected function getConfig(): InstallCommandConfig { return $this->config; } + private function normaliseName(string $pharName): string { + if ($this->config->withExtension() && '.phar' !== substr($pharName, -5)) { + $pharName .= '.phar'; + } + + return $pharName; + } + private function resolveToRelease(RequestedPhar $requestedPhar): SupportedRelease { $repository = $this->pharResolver->resolve($requestedPhar); $releases = $repository->getReleasesByRequestedPhar($requestedPhar); @@ -80,6 +88,6 @@ private function getDestination(string $pharName, RequestedPhar $requestedPhar, return $requestedPhar->getLocation(); } - return $destination->file($pharName); + return $destination->file($this->normaliseName($pharName)); } } diff --git a/src/commands/install/InstallCommandConfig.php b/src/commands/install/InstallCommandConfig.php index 2f6af058..ea32b1a7 100644 --- a/src/commands/install/InstallCommandConfig.php +++ b/src/commands/install/InstallCommandConfig.php @@ -82,6 +82,10 @@ public function forceAcceptUnsignedPhars(): bool { return $this->cliOptions->hasOption('force-accept-unsigned'); } + public function withExtension(): bool { + return $this->cliOptions->hasOption('extension'); + } + /** * @throws ConfiguredPharException * diff --git a/src/commands/install/InstallContext.php b/src/commands/install/InstallContext.php index 359d77bf..d151a326 100644 --- a/src/commands/install/InstallContext.php +++ b/src/commands/install/InstallContext.php @@ -22,6 +22,7 @@ protected function getKnownOptions(): array { return [ 'target' => 't', 'copy' => 'c', + 'extension' => 'e', 'global' => 'g', 'temporary' => false, 'trust-gpg-keys' => false, diff --git a/tests/unit/commands/install/InstallCommandConfigTest.php b/tests/unit/commands/install/InstallCommandConfigTest.php index 96910884..75f08bd5 100644 --- a/tests/unit/commands/install/InstallCommandConfigTest.php +++ b/tests/unit/commands/install/InstallCommandConfigTest.php @@ -148,6 +148,28 @@ public function testDoNotAddToPhiveXml($switch): void { $this->assertSame($switch, $config->doNotAddToPhiveXml()); } + /** + * @dataProvider boolProvider + * + * @param bool $switch + */ + public function testWithExtension($switch): void { + $options = $this->getOptionsMock(); + $options->expects($this->once()) + ->method('hasOption') + ->with('extension') + ->willReturn($switch); + + $config = new InstallCommandConfig( + $options, + $this->getPhiveXmlConfigMock(), + $this->getEnvironmentMock(), + $this->getTargetDirectoryLocatorMock() + ); + + $this->assertSame($switch, $config->withExtension()); + } + public function testConvertsRequestedPharAliasToLowercase(): void { $options = $this->getOptionsMock(); $options->expects($this->any()) diff --git a/tests/unit/commands/install/InstallCommandTest.php b/tests/unit/commands/install/InstallCommandTest.php new file mode 100644 index 00000000..7992b136 --- /dev/null +++ b/tests/unit/commands/install/InstallCommandTest.php @@ -0,0 +1,92 @@ +, Sebastian Heuer and contributors + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace PharIo\Phive; + +use PharIo\FileSystem\Directory; +use PharIo\Version\AnyVersionConstraint; +use PHPUnit\Framework\TestCase; + +/** + * @covers \PharIo\Phive\InstallCommandConfig + */ +class InstallCommandTest extends TestCase +{ + use ScalarTestDataProvider; + + /** + * @dataProvider boolProvider + * + * @param bool $switch + */ + public function testAddingOfExtension($switch): void + { + $pharName = $expectedPharName = 'some-path'; + if ($switch) { + $expectedPharName .= '.phar'; + } + + $directory = $this->createMock(Directory::class); + $directory + ->expects($this->once()) + ->method('file') + ->with($expectedPharName); + + $requestedPhars = [ + new RequestedPhar( + new PharAlias($pharName), + new AnyVersionConstraint(), + new AnyVersionConstraint(), + null, + true + ), + ]; + + $config = $this->createMock(InstallCommandConfig::class); + $config + ->method('getTargetDirectory') + ->willReturn($directory); + + $config + ->method('getRequestedPhars') + ->willReturn($requestedPhars); + + $config + ->method('withExtension') + ->willReturn($switch); + + $installService = $this->createMock(InstallService::class); + $sourceRepository = $this->createMock(SourceRepository::class); + + $pharResolver = $this->createMock(RequestedPharResolverService::class); + $pharResolver + ->method('resolve') + ->willReturn($sourceRepository); + + $url = $this->createMock(PharUrl::class); + $url + ->method('getPharName') + ->willReturn($pharName); + + $release = $this->createMock(SupportedRelease::class); + $release + ->method('getUrl') + ->willReturn($url); + + $selector = $this->createMock(ReleaseSelector::class); + $selector + ->method('select') + ->willReturn($release); + + $command = new InstallCommand($config, $installService, $pharResolver, $selector); + $command->execute(); + } +} diff --git a/tests/unit/commands/install/InstallContextTest.php b/tests/unit/commands/install/InstallContextTest.php new file mode 100644 index 00000000..c1cfebd5 --- /dev/null +++ b/tests/unit/commands/install/InstallContextTest.php @@ -0,0 +1,52 @@ +, Sebastian Heuer and contributors + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ +namespace PharIo\Phive; + +use PHPUnit\Framework\TestCase; + +/** + * @covers \PharIo\Phive\InstallContext + */ +class InstallContextTest extends TestCase { + /** + * @dataProvider knownOptionCharProvider + * + * @param string $optionChar + */ + public function testHasOptionForChar($optionChar): void + { + $context = new InstallContext(); + self::assertTrue($context->hasOptionForChar($optionChar)); + } + + /** + * @dataProvider knowsOptionProvider + * + * @param string $option + */ + public function testKnowsOption($option): void + { + $context = new InstallContext(); + self::assertTrue($context->knowsOption($option)); + } + + public function knowsOptionProvider(): array { + return [ + ['extension'], + ]; + } + + public function knownOptionCharProvider(): array { + return [ + ['e'], + ]; + } +} \ No newline at end of file