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

TestCase::executeCliCommand(): retry Composer commands on a particular exception #164

Merged
merged 1 commit into from Mar 5, 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
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