Skip to content

Commit

Permalink
Improve support for detecting various versions and archive types.
Browse files Browse the repository at this point in the history
  • Loading branch information
codebymikey committed Mar 1, 2022
1 parent b58b669 commit c81782e
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 2 deletions.
1 change: 1 addition & 0 deletions .lando.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ services:
PHP_IDE_CONFIG: "serverName=appserver"
XDEBUG_CONFIG: ""
XDEBUG_MODE: debug
COMPOSER_ALLOW_XDEBUG: 1
config:
php: .php.ini
app2:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.x.x / xxxx-xx-xx
========================
* Improve support for detecting various versions and archive types.

1.4.1 / 2021-02-22
========================
* Add better Composer 2 support.
Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,10 @@
"mikey179/vfsstream": "^1.6",
"phpunit/phpunit": "^8.5",
"ext-json": "*"
},
"config": {
"allow-plugins": {
"composer/installers": true
}
}
}
4 changes: 2 additions & 2 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ protected function waitOnPromises(array $promises) {
$progress = NULL;
if ($this->io instanceof ConsoleIO && !$this->io->isDebug() && count($promises) > 1 && !getenv('COMPOSER_DISABLE_PROGRESS_BAR')) {
// Disable progress bar by setting COMPOSER_DISABLE_PROGRESS_BAR=1 as we
// are unable to read composer's "--no-input" option easily from here
// are unable to read composer's "--no-progress" option easily from here
// without introducing extra complexity with the PluginEvents::COMMAND
// event.
$progress = $this->io->getProgressBar();
Expand Down Expand Up @@ -571,7 +571,7 @@ protected function guessDefaultsFromUrl($url) {
$distribution_type = 'zip';
// Attempt to guess the version number and type from the URL.
$match = [];
if (preg_match('/(v?[\d.]{2,})\.(zip|rar|tgz|tar(?:\.(gz|bz2))?)$/', $url, $match)) {
if (preg_match('/(v?[\d.]{2,}).+(zip|rar|tgz|tar(?:\.(gz|bz2))?)([?#\/].*)?$/', $url, $match)) {
$version = $match[1];
$distribution_type = explode('.', $match[2])[0];
if ($distribution_type === 'tgz') {
Expand Down
66 changes: 66 additions & 0 deletions tests/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
use Composer\Package\Link;
use Composer\Package\Package;
use Composer\Package\RootPackage;
use Composer\Plugin\PluginInterface;
use Composer\Repository\RepositoryManager;
use Composer\Repository\WritableRepositoryInterface;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
use Composer\Util\Filesystem;
use Composer\Util\Loop;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -116,6 +118,8 @@ class PluginTest extends TestCase {
protected function setUp(): void {
parent::setUp();

$composer_2 = version_compare(PluginInterface::PLUGIN_API_VERSION, '2.0', '>=');

$this->downloadedFiles = [];
$this->removedFiles = [];

Expand Down Expand Up @@ -145,6 +149,7 @@ protected function setUp(): void {
'getInstallationManager',
'getDownloadManager',
'getRepositoryManager',
'getLoop',
]
);
$this->composer
Expand All @@ -168,6 +173,15 @@ protected function setUp(): void {
->method('getRepositoryManager')
->willReturn($repository_manager);

if ($composer_2) {
// Add mock for the Composer loop.
$loop = $this->createMock(Loop::class);
$loop->method('wait');
$this->composer
->method('getLoop')
->willReturn($loop);
}

$this->io = $this->createMock(IOInterface::class);

$this->fixture = $this->getPluginMockInstance();
Expand Down Expand Up @@ -319,6 +333,58 @@ public function testDrupalLibrariesDownload(
);
}

/**
* Fixtures data provider.
*/
public function defaultUrlParserProvider() {
return [
'url-version-detection' => [
'url-version-detection',
[
'package1' => ['1.0.0', 'zip'],
'package2' => ['4.9.2', 'rar'],
'package3' => ['0.11', 'tar'],
'package4' => ['4.10.0', 'tar'],
'package5' => ['1.17.2', 'tar'],
'package6' => ['1.0.0', 'zip'],
'package7' => ['1.0.0', 'zip'],
'package8' => ['1.2.0', 'tar'],
'package9' => ['1.2.0', 'tar'],
],
],
];
}

/**
* Test default URL parser version detection.
*
* @param string $fixture_name
* The fixture directory name.
* @param array $expected_metadata
* The list of expected parsed metadata.
*
* @dataProvider defaultUrlParserProvider
*
* @throws \Exception
*/
public function testDefaultUrlParser(
string $fixture_name,
array $expected_metadata
) {
$this->fixtureDirectory = $this->fixtureDirectory($fixture_name);
$root_project = $this->rootFromJson("{$this->fixtureDirectory}/composer.json");

$this->triggerPlugin($root_project, $this->fixtureDirectory);

$installed_libraries = json_decode(file_get_contents($this->installedLibrariesJsonFile), TRUE);
$output = [];
foreach ($installed_libraries['installed'] as $package => $package_metadata) {
$output[$package] = [$package_metadata['version'], $package_metadata['type']];
}
// ksort($expected_metadata); ksort($output);
$this->assertEquals($expected_metadata, $output, 'Extracted version and type');
}

/**
* Fixtures data provider.
*/
Expand Down
15 changes: 15 additions & 0 deletions tests/fixtures/url-version-detection/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extra": {
"drupal-libraries": {
"package1": "https://example.com/package.zip",
"package2": "https://example.com/package/package2_4.9.2.rar?_token=123",
"package3": "https://example.com/package/package3/contents_0.11.tgz#token_hash",
"package4": "https://download.ckeditor.com/notification/releases/notification_4.10.0.tar",
"package5": "https://download.ckeditor.com/wordcount/releases/wordcount_1.17.2.tar.bz2",
"package6": "https://example.com/dynamic-path",
"package7": "https://example.com/dynamic-path.tar.gz/random-auth-token",
"package8": "https://example.com/dynamic-path?version=1.2.0.tar.gz",
"package9": "https://example.com/dynamic-path?version=1.2.0&type=tgz"
}
}
}

0 comments on commit c81782e

Please sign in to comment.