Skip to content

Commit

Permalink
Merge pull request #164 from PHPCSStandards/feature/tests-retry-compo…
Browse files Browse the repository at this point in the history
…ser-install-on-failure

TestCase::executeCliCommand(): retry Composer commands on a particular exception
  • Loading branch information
Potherca committed Mar 5, 2022
2 parents 4d7e0db + 5329056 commit 31b67dc
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion tests/TestCase.php
Expand Up @@ -403,6 +403,9 @@ protected static function getPhpcsCommand($workingDir = null)
* Defaults to `null` = the working directory of the current PHP process.
* Note: if the command itself already contains a "working directory" argument,
* this parameter will normally not need to be passed.
* @param bool $autoRetry Internal. Whether the command should be retried if it fails on a particular
* Composer exception. This parameter should only be set by the method itself
* when recursing on itself.
*
* @return array Format:
* 'exitcode' int The exit code from the command.
Expand All @@ -412,7 +415,7 @@ protected static function getPhpcsCommand($workingDir = null)
* @throws RuntimeException When the passed arguments do not comply.
* @throws RuntimeException When no resource could be obtained to execute the command.
*/
public static function executeCliCommand($command, $workingDir = null)
public static function executeCliCommand($command, $workingDir = null, $autoRetry = true)
{
if (is_string($command) === false || $command === '') {
throw new RuntimeException('Command must be a non-empty string.');
Expand Down Expand Up @@ -454,6 +457,22 @@ public static function executeCliCommand($command, $workingDir = null)
. '---------------------------------------' . \PHP_EOL
);

/*
* Prevent the complete CI run failing on a particular error which Composer sometimes
* runs into. Retry the command instead. In most cases, that should fix it.
* If the command still fails, just return the results.
*/
if (
$autoRetry === true
&& $result['exitcode'] === 1
&& strpos($command, '"' . \COMPOSER_PHAR . '"') !== false
&& strpos($result['stderr'], '[Composer\\Downloader\\TransportException]') !== false
&& strpos($result['stderr'], 'Peer fingerprint did not match') !== false
) {
// Retry and return the results of the retry.
return self::executeCliCommand($command, $workingDir, false);
}

return $result;
}

Expand Down

0 comments on commit 31b67dc

Please sign in to comment.