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

Fallback to unknown version if not running under Composer API 2 #1771

Merged
merged 1 commit into from Dec 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 25 additions & 14 deletions src/Console/Application.php
Expand Up @@ -36,6 +36,7 @@
namespace Infection\Console;

use function array_merge;
use function class_exists;
use Composer\InstalledVersions;
use Infection\Command\ConfigureCommand;
use Infection\Command\DescribeCommand;
Expand Down Expand Up @@ -74,20 +75,7 @@ final class Application extends BaseApplication

public function __construct(Container $container)
{
try {
$version = (string) InstalledVersions::getPrettyVersion(self::PACKAGE_NAME);
// @codeCoverageIgnoreStart
} catch (OutOfBoundsException $e) {
if (preg_match('#package .*' . preg_quote(self::PACKAGE_NAME, '#') . '.* not installed#i', $e->getMessage()) === 0) {
throw $e;
}

// We have a bogus exception: how can Infection be not installed if we're here?
$version = 'not-installed';
}
// @codeCoverageIgnoreEnd

parent::__construct(self::NAME, $version);
parent::__construct(self::NAME, self::getPrettyVersion());

$this->container = $container;
$this->setDefaultCommand('run');
Expand Down Expand Up @@ -136,4 +124,27 @@ protected function configureIO(InputInterface $input, OutputInterface $output):

OutputFormatterStyleConfigurator::configure($output);
}

private static function getPrettyVersion(): string
{
// Pre 2.0 Composer runtime didn't have this class.
// @codeCoverageIgnoreStart
if (!class_exists(InstalledVersions::class)) {
return 'unknown';
}
// @codeCoverageIgnoreEnd

try {
return (string) InstalledVersions::getPrettyVersion(self::PACKAGE_NAME);
// @codeCoverageIgnoreStart
} catch (OutOfBoundsException $e) {
if (preg_match('#package .*' . preg_quote(self::PACKAGE_NAME, '#') . '.* not installed#i', $e->getMessage()) === 0) {
throw $e;
}

// We have a bogus exception: how can Infection be not installed if we're here?
return 'not-installed';
}
// @codeCoverageIgnoreEnd
}
}