From 5329056ac945aa3e1add631af2eb21ae1094f42f Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 6 Feb 2022 08:39:51 +0100 Subject: [PATCH] TestCase::executeCliCommand(): retry Composer commands on a particular exception ... as otherwise a complete build may fail, while the failure is not related to an actual test failure, I've seen this particular issue a couple of times in test runs (most typically with the problematic Composer v1 / PHP 5.5 / Windows combination). Adding a one-time only retry of a command run and only for that particular issue should prevent having to re-run a complete build for this. --- tests/TestCase.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/TestCase.php b/tests/TestCase.php index 57b00c04..506fd4c8 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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. @@ -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.'); @@ -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; }