From 7e3626362eb67b102e95c17fb5a34ba219d46bc6 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 24 Nov 2021 16:00:57 +0100 Subject: [PATCH] Add support for warnings/infos keys to report more complex information to diverse Composer versions --- src/Composer/Util/HttpDownloader.php | 22 +++++++++++++- .../Composer/Test/Util/HttpDownloaderTest.php | 29 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/Composer/Util/HttpDownloader.php b/src/Composer/Util/HttpDownloader.php index 59715ea29..bb1f1f52d 100644 --- a/src/Composer/Util/HttpDownloader.php +++ b/src/Composer/Util/HttpDownloader.php @@ -448,11 +448,12 @@ class HttpDownloader * @internal * * @param string $url - * @param array{warning?: string, info?: string, warning-versions?: string, info-versions?: string} $data + * @param array{warning?: string, info?: string, warning-versions?: string, info-versions?: string, warnings?: array, infos?: array} $data * @return void */ public static function outputWarnings(IOInterface $io, $url, $data) { + // legacy warning/info keys foreach (array('warning', 'info') as $type) { if (empty($data[$type])) { continue; @@ -469,6 +470,25 @@ class HttpDownloader $io->writeError('<'.$type.'>'.ucfirst($type).' from '.Url::sanitize($url).': '.$data[$type].''); } + + // modern Composer 2.2+ format with support for multiple warning/info messages + foreach (array('warnings', 'infos') as $key) { + if (empty($data[$key])) { + continue; + } + + $versionParser = new VersionParser(); + foreach ($data[$key] as $spec) { + $type = substr($key, 0, -1); + $constraint = $versionParser->parseConstraints($spec['versions']); + $composer = new Constraint('==', $versionParser->normalize(Composer::getVersion())); + if (!$constraint->matches($composer)) { + continue; + } + + $io->writeError('<'.$type.'>'.ucfirst($type).' from '.Url::sanitize($url).': '.$spec['message'].''); + } + } } /** diff --git a/tests/Composer/Test/Util/HttpDownloaderTest.php b/tests/Composer/Test/Util/HttpDownloaderTest.php index 62b601bef..cfed8d6a3 100644 --- a/tests/Composer/Test/Util/HttpDownloaderTest.php +++ b/tests/Composer/Test/Util/HttpDownloaderTest.php @@ -12,6 +12,7 @@ namespace Composer\Test\Util; +use Composer\IO\BufferIO; use Composer\Util\HttpDownloader; use PHPUnit\Framework\TestCase; @@ -51,4 +52,32 @@ class HttpDownloaderTest extends TestCase $this->assertNotEquals(200, $e->getCode()); } } + + public function testOutputWarnings() + { + $io = new BufferIO(); + HttpDownloader::outputWarnings($io, '$URL', array()); + $this->assertSame('', $io->getOutput()); + HttpDownloader::outputWarnings($io, '$URL', array( + 'warning' => 'old warning msg', + 'warning-versions' => '>=2.0', + 'info' => 'old info msg', + 'info-versions' => '>=2.0', + 'warnings' => array( + array('message' => 'should not appear', 'versions' => '<2.2'), + array('message' => 'visible warning', 'versions' => '>=2.2-dev'), + ), + 'infos' => array( + array('message' => 'should not appear', 'versions' => '<2.2'), + array('message' => 'visible info', 'versions' => '>=2.2-dev'), + ), + )); + + // the tag are consumed by the OutputFormatter, but not as that is not a default output format + $this->assertSame('Warning from $URL: old warning msg +Info from $URL: old info msg +Warning from $URL: visible warning +Info from $URL: visible info +', $io->getOutput()); + } }