Skip to content

Commit

Permalink
Add extension .phar to installed tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeliot-Tm committed Oct 27, 2024
1 parent 9dee308 commit 1a20241
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/commands/help/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/commands/install/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -80,6 +88,6 @@ private function getDestination(string $pharName, RequestedPhar $requestedPhar,
return $requestedPhar->getLocation();
}

return $destination->file($pharName);
return $destination->file($this->normaliseName($pharName));
}
}
4 changes: 4 additions & 0 deletions src/commands/install/InstallCommandConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
1 change: 1 addition & 0 deletions src/commands/install/InstallContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ protected function getKnownOptions(): array {
return [
'target' => 't',
'copy' => 'c',
'extension' => 'e',
'global' => 'g',
'temporary' => false,
'trust-gpg-keys' => false,
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/commands/install/InstallCommandConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
92 changes: 92 additions & 0 deletions tests/unit/commands/install/InstallCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php declare(strict_types=1);
/*
* This file is part of Phive.
*
* Copyright (c) Arne Blankerts <[email protected]>, Sebastian Heuer <[email protected]> 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();
}
}
52 changes: 52 additions & 0 deletions tests/unit/commands/install/InstallContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types=1);
/*
* This file is part of Phive.
*
* Copyright (c) Arne Blankerts <[email protected]>, Sebastian Heuer <[email protected]> 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'],
];
}
}

0 comments on commit 1a20241

Please sign in to comment.