1
0
Fork 0

Merge branch '2.2' into main

pull/10542/head
Jordi Boggiano 2022-02-16 13:35:30 +01:00
commit 63b5f2737b
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
8 changed files with 105 additions and 23 deletions

View File

@ -349,7 +349,7 @@ EOT
} }
$author = $io->askAndValidate( $author = $io->askAndValidate(
'Author [<comment>'.$author.'</comment>, n to skip]: ', 'Author ['.(is_string($author) ? '<comment>'.$author.'</comment>, ' : '') . 'n to skip]: ',
function ($value) use ($author) { function ($value) use ($author) {
if ($value === 'n' || $value === 'no') { if ($value === 'n' || $value === 'no') {
return; return;
@ -357,6 +357,10 @@ EOT
$value = $value ?: $author; $value = $value ?: $author;
$author = $this->parseAuthorString($value); $author = $this->parseAuthorString($value);
if ($author['email'] === null) {
return $author['name'];
}
return sprintf('%s <%s>', $author['name'], $author['email']); return sprintf('%s <%s>', $author['name'], $author['email']);
}, },
null, null,
@ -470,33 +474,41 @@ EOT
/** /**
* @private * @private
* @param string $author * @param string $author
* @return array{name: string, email: string} * @return array{name: string, email: string|null}
*/ */
public function parseAuthorString($author) public function parseAuthorString($author)
{ {
if (Preg::isMatch('/^(?P<name>[- .,\p{L}\p{N}\p{Mn}\'"()]+) <(?P<email>.+?)>$/u', $author, $match)) { if (Preg::isMatch('/^(?P<name>[- .,\p{L}\p{N}\p{Mn}\'"()]+)(?:\s+<(?P<email>.+?)>)?$/u', $author, $match)) {
if ($this->isValidEmail($match['email'])) { $hasEmail = isset($match['email']) && '' !== $match['email'];
if ($hasEmail && !$this->isValidEmail($match['email'])) {
throw new \InvalidArgumentException('Invalid email "'.$match['email'].'"');
}
return array( return array(
'name' => trim($match['name']), 'name' => trim($match['name']),
'email' => $match['email'], 'email' => $hasEmail ? $match['email'] : null,
); );
} }
}
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Invalid author string. Must be in the format: '. 'Invalid author string. Must be in the formats: '.
'John Smith <john@example.com>' 'Jane Doe or John Smith <john@example.com>'
); );
} }
/** /**
* @param string $author * @param string $author
* *
* @return array<int, array{name: string, email: string}> * @return array<int, array{name: string, email?: string}>
*/ */
protected function formatAuthors($author) protected function formatAuthors($author)
{ {
return array($this->parseAuthorString($author)); $author = $this->parseAuthorString($author);
if (null === $author['email']) {
unset($author['email']);
}
return array($author);
} }
/** /**

View File

@ -86,7 +86,7 @@ EOT
$tableStyle = $table->getStyle(); $tableStyle = $table->getStyle();
$tableStyle->setVerticalBorderChars(''); $tableStyle->setVerticalBorderChars('');
$tableStyle->setCellRowContentFormat('%s '); $tableStyle->setCellRowContentFormat('%s ');
$table->setHeaders(array('Name', 'Version', 'License')); $table->setHeaders(array('Name', 'Version', 'Licenses'));
foreach ($packages as $package) { foreach ($packages as $package) {
$link = PackageInfo::getViewSourceOrHomepageUrl($package); $link = PackageInfo::getViewSourceOrHomepageUrl($package);
if ($link !== null) { if ($link !== null) {
@ -124,13 +124,17 @@ EOT
case 'summary': case 'summary':
$usedLicenses = array(); $usedLicenses = array();
foreach ($packages as $package) { foreach ($packages as $package) {
$license = $package instanceof CompletePackageInterface ? $package->getLicense() : array(); $licenses = $package instanceof CompletePackageInterface ? $package->getLicense() : array();
$licenseName = $license[0]; if (count($licenses) === 0) {
$licenses[] = 'none';
}
foreach ($licenses as $licenseName) {
if (!isset($usedLicenses[$licenseName])) { if (!isset($usedLicenses[$licenseName])) {
$usedLicenses[$licenseName] = 0; $usedLicenses[$licenseName] = 0;
} }
$usedLicenses[$licenseName]++; $usedLicenses[$licenseName]++;
} }
}
// Sort licenses so that the most used license will appear first // Sort licenses so that the most used license will appear first
arsort($usedLicenses, SORT_NUMERIC); arsort($usedLicenses, SORT_NUMERIC);

View File

@ -61,7 +61,10 @@ class GitBitbucketDriver extends VcsDriver
*/ */
public function initialize() public function initialize()
{ {
Preg::match('#^https?://bitbucket\.org/([^/]+)/([^/]+?)(\.git|/?)?$#i', $this->url, $match); if (!Preg::isMatch('#^https?://bitbucket\.org/([^/]+)/([^/]+?)(\.git|/?)?$#i', $this->url, $match)) {
throw new \InvalidArgumentException(sprintf('The Bitbucket repository URL %s is invalid. It must be the HTTPS URL of a Bitbucket repository.', $this->url));
}
$this->owner = $match[1]; $this->owner = $match[1];
$this->repository = $match[2]; $this->repository = $match[2];
$this->originUrl = 'bitbucket.org'; $this->originUrl = 'bitbucket.org';

View File

@ -59,7 +59,10 @@ class GitHubDriver extends VcsDriver
*/ */
public function initialize() public function initialize()
{ {
Preg::match('#^(?:(?:https?|git)://([^/]+)/|git@([^:]+):/?)([^/]+)/(.+?)(?:\.git|/)?$#', $this->url, $match); if (!Preg::isMatch('#^(?:(?:https?|git)://([^/]+)/|git@([^:]+):/?)([^/]+)/(.+?)(?:\.git|/)?$#', $this->url, $match)) {
throw new \InvalidArgumentException(sprintf('The GitHub repository URL %s is invalid.', $this->url));
}
$this->owner = $match[3]; $this->owner = $match[3];
$this->repository = $match[4]; $this->repository = $match[4];
$this->originUrl = strtolower(!empty($match[1]) ? $match[1] : $match[2]); $this->originUrl = strtolower(!empty($match[1]) ? $match[1] : $match[2]);

View File

@ -94,7 +94,7 @@ class GitLabDriver extends VcsDriver
public function initialize() public function initialize()
{ {
if (!Preg::isMatch(self::URL_REGEX, $this->url, $match)) { if (!Preg::isMatch(self::URL_REGEX, $this->url, $match)) {
throw new \InvalidArgumentException('The URL provided is invalid. It must be the HTTP URL of a GitLab project.'); throw new \InvalidArgumentException(sprintf('The GitLab repository URL %s is invalid. It must be the HTTP URL of a GitLab project.', $this->url));
} }
$guessedDomain = !empty($match['domain']) ? $match['domain'] : $match['domain2']; $guessedDomain = !empty($match['domain']) ? $match['domain'] : $match['domain2'];

View File

@ -25,6 +25,14 @@ class InitCommandTest extends TestCase
$this->assertEquals('john@example.com', $author['email']); $this->assertEquals('john@example.com', $author['email']);
} }
public function testParseValidAuthorStringWithoutEmail()
{
$command = new InitCommand;
$author = $command->parseAuthorString('John Smith');
$this->assertEquals('John Smith', $author['name']);
$this->assertNull($author['email']);
}
public function testParseValidUtf8AuthorString() public function testParseValidUtf8AuthorString()
{ {
$command = new InitCommand; $command = new InitCommand;

View File

@ -217,6 +217,14 @@ class GitBitbucketDriverTest extends TestCase
); );
} }
public function testInitializeInvalidRepositoryUrl()
{
$this->expectException('\InvalidArgumentException');
$driver = $this->getDriver(array('url' => 'https://bitbucket.org/acme'));
$driver->initialize();
}
public function testSupports() public function testSupports()
{ {
$this->assertTrue( $this->assertTrue(

View File

@ -14,11 +14,11 @@ namespace Composer\Test\Repository\Vcs;
use Composer\Downloader\TransportException; use Composer\Downloader\TransportException;
use Composer\Repository\Vcs\GitHubDriver; use Composer\Repository\Vcs\GitHubDriver;
use Composer\Test\Mock\ProcessExecutorMock;
use Composer\Test\TestCase; use Composer\Test\TestCase;
use Composer\Util\Filesystem; use Composer\Util\Filesystem;
use Composer\Util\Http\Response;
use Composer\Test\Mock\ProcessExecutorMock;
use Composer\Config; use Composer\Config;
use Composer\Util\Http\Response;
use Composer\Util\ProcessExecutor; use Composer\Util\ProcessExecutor;
class GitHubDriverTest extends TestCase class GitHubDriverTest extends TestCase
@ -306,6 +306,50 @@ class GitHubDriverTest extends TestCase
$this->assertEquals($sha, $source['reference']); $this->assertEquals($sha, $source['reference']);
} }
/**
* @return void
*/
public function initializeInvalidReoUrl()
{
$this->expectException('\InvalidArgumentException');
$repoConfig = array(
'url' => 'https://github.com/acme',
);
$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$httpDownloader = $this->getMockBuilder('Composer\Util\HttpDownloader')
->setConstructorArgs(array($io, $this->config))
->getMock();
$gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, $httpDownloader, new ProcessExecutorMock);
$gitHubDriver->initialize();
}
/**
* @dataProvider supportsProvider
* @param bool $expected
* @param string $repoUrl
*/
public function testSupports($expected, $repoUrl)
{
$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$this->assertSame($expected, GitHubDriver::supports($io, $this->config, $repoUrl));
}
/**
* @return list<array{bool, string}>
*/
public function supportsProvider()
{
return array(
array(false, 'https://github.com/acme'),
array(true, 'https://github.com/acme/repository'),
array(true, 'git@github.com:acme/repository.git'),
);
}
/** /**
* @param string|object $object * @param string|object $object
* @param string $attribute * @param string $attribute