From 260ac0011ce9eec50dbde346f090dcffbab57895 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 14 Oct 2021 15:10:48 +0200 Subject: [PATCH] Add constants to describe error codes from Installer::run --- .../SolverProblemsException.php | 4 +++- src/Composer/Installer.php | 18 +++++++++++++----- .../installer/partial-update-without-lock.test | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/Composer/DependencyResolver/SolverProblemsException.php b/src/Composer/DependencyResolver/SolverProblemsException.php index 93eab3e63..846648752 100644 --- a/src/Composer/DependencyResolver/SolverProblemsException.php +++ b/src/Composer/DependencyResolver/SolverProblemsException.php @@ -21,6 +21,8 @@ use Composer\Package\PackageInterface; */ class SolverProblemsException extends \RuntimeException { + const ERROR_DEPENDENCY_RESOLUTION_FAILED = 2; + /** @var Problem[] */ protected $problems; /** @var array */ @@ -35,7 +37,7 @@ class SolverProblemsException extends \RuntimeException $this->problems = $problems; $this->learnedPool = $learnedPool; - parent::__construct('Failed resolving dependencies with '.count($problems).' problems, call getPrettyString to get formatted details', 2); + parent::__construct('Failed resolving dependencies with '.count($problems).' problems, call getPrettyString to get formatted details', self::ERROR_DEPENDENCY_RESOLUTION_FAILED); } public function getPrettyString(RepositorySet $repositorySet, Request $request, Pool $pool, $isVerbose, $isDevExtraction = false) diff --git a/src/Composer/Installer.php b/src/Composer/Installer.php index fab317822..421427afd 100644 --- a/src/Composer/Installer.php +++ b/src/Composer/Installer.php @@ -66,6 +66,13 @@ use Composer\Util\Platform; */ class Installer { + const ERROR_NONE = 0; // no error/success state + const ERROR_GENERIC_FAILURE = 1; + const ERROR_NO_LOCK_FILE_FOR_PARTIAL_UPDATE = 3; + const ERROR_LOCK_FILE_INVALID = 4; + // used/declared in SolverProblemsException, carried over here for completeness + const ERROR_DEPENDENCY_RESOLUTION_FAILED = 2; + /** * @var IOInterface */ @@ -209,6 +216,7 @@ class Installer * * @throws \Exception * @return int 0 on success or a positive error code on failure + * @phpstan-return self::ERROR_* */ public function run() { @@ -387,7 +395,7 @@ class Installer if (($this->updateAllowList || $this->updateMirrors) && !$lockedRepository) { $this->io->writeError('Cannot update ' . ($this->updateMirrors ? 'lock file information' : 'only a partial set of packages') . ' without a lock file present. Run `composer update` to generate a lock file.', true, IOInterface::QUIET); - return 1; + return self::ERROR_NO_LOCK_FILE_FOR_PARTIAL_UPDATE; } $this->io->writeError('Loading composer repositories with package information'); @@ -434,7 +442,7 @@ class Installer $ghe = new GithubActionError($this->io); $ghe->emit($err."\n".$prettyProblem); - return max(1, $e->getCode()); + return max(self::ERROR_GENERIC_FAILURE, $e->getCode()); } $this->io->writeError("Analyzed ".count($pool)." packages to resolve dependencies", true, IOInterface::VERBOSE); @@ -603,7 +611,7 @@ class Installer $ghe = new GithubActionError($this->io); $ghe->emit($err."\n".$prettyProblem); - return max(1, $e->getCode()); + return max(self::ERROR_GENERIC_FAILURE, $e->getCode()); } $lockTransaction->setNonDevPackages($nonDevLockTransaction); @@ -661,7 +669,7 @@ class Installer if (0 !== count($lockTransaction->getOperations())) { $this->io->writeError('Your lock file cannot be installed on this system without changes. Please run composer update.', true, IOInterface::QUIET); - return 1; + return self::ERROR_LOCK_FILE_INVALID; } } catch (SolverProblemsException $e) { $err = 'Your lock file does not contain a compatible set of packages. Please run composer update.'; @@ -673,7 +681,7 @@ class Installer $ghe = new GithubActionError($this->io); $ghe->emit($err."\n".$prettyProblem); - return max(1, $e->getCode()); + return max(self::ERROR_GENERIC_FAILURE, $e->getCode()); } } diff --git a/tests/Composer/Test/Fixtures/installer/partial-update-without-lock.test b/tests/Composer/Test/Fixtures/installer/partial-update-without-lock.test index 1738b2a84..22798086a 100644 --- a/tests/Composer/Test/Fixtures/installer/partial-update-without-lock.test +++ b/tests/Composer/Test/Fixtures/installer/partial-update-without-lock.test @@ -33,5 +33,5 @@ update b/unstable --EXPECT-OUTPUT-- Cannot update only a partial set of packages without a lock file present. Run `composer update` to generate a lock file. --EXPECT-EXIT-CODE-- -1 +3 --EXPECT--