1
0
Fork 0

Add support for warnings/infos keys to report more complex information to diverse Composer versions

pull/10309/head
Jordi Boggiano 2021-11-24 16:00:57 +01:00
parent ebf4cbdc69
commit 7e3626362e
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
2 changed files with 50 additions and 1 deletions

View File

@ -448,11 +448,12 @@ class HttpDownloader
* @internal * @internal
* *
* @param string $url * @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<array{versions: string, message: string}>, infos?: array<array{versions: string, message: string}>} $data
* @return void * @return void
*/ */
public static function outputWarnings(IOInterface $io, $url, $data) public static function outputWarnings(IOInterface $io, $url, $data)
{ {
// legacy warning/info keys
foreach (array('warning', 'info') as $type) { foreach (array('warning', 'info') as $type) {
if (empty($data[$type])) { if (empty($data[$type])) {
continue; continue;
@ -469,6 +470,25 @@ class HttpDownloader
$io->writeError('<'.$type.'>'.ucfirst($type).' from '.Url::sanitize($url).': '.$data[$type].'</'.$type.'>'); $io->writeError('<'.$type.'>'.ucfirst($type).' from '.Url::sanitize($url).': '.$data[$type].'</'.$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'].'</'.$type.'>');
}
}
} }
/** /**

View File

@ -12,6 +12,7 @@
namespace Composer\Test\Util; namespace Composer\Test\Util;
use Composer\IO\BufferIO;
use Composer\Util\HttpDownloader; use Composer\Util\HttpDownloader;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
@ -51,4 +52,32 @@ class HttpDownloaderTest extends TestCase
$this->assertNotEquals(200, $e->getCode()); $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 <info> tag are consumed by the OutputFormatter, but not <warning> as that is not a default output format
$this->assertSame('<warning>Warning from $URL: old warning msg</warning>
Info from $URL: old info msg
<warning>Warning from $URL: visible warning</warning>
Info from $URL: visible info
', $io->getOutput());
}
} }