Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to ignore sub dependencies checking #63

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ You can configure the plugin via the [`COMPOSER_HOME/config.json`](https://getco
{
"config": {
"sllh-composer-versions-check": {
"root-packages-only": false,
"show-links": false
}
}
}
```

* `ignore-sub-dependencies`: Shows only outdated root packages.
* `show-links`: Shows outdated package links. Set to `true` to get a larger output, like the demo.
9 changes: 8 additions & 1 deletion src/VersionsCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,22 @@ final class VersionsCheck
*/
private $outdatedPackages = array();

public function checkPackages(ArrayRepository $distRepository, WritableRepositoryInterface $localRepository, RootPackageInterface $rootPackage)
public function checkPackages(ArrayRepository $distRepository, WritableRepositoryInterface $localRepository, RootPackageInterface $rootPackage, $rootPackagesOnly)
{
$packages = $localRepository->getPackages();
$rootRequires = array_keys($rootPackage->getRequires() + $rootPackage->getDevRequires());

foreach ($packages as $package) {
// Do not compare aliases. Aliased packages are also provided.
if ($package instanceof AliasPackage) {
continue;
}

// No root package are ignored and it is one. Skip.
if ($rootPackagesOnly && !\in_array($package->getName(), $rootRequires, true)) {
continue;
}

// Old composer versions BC
$versionConstraint = class_exists('Composer\Semver\Constraint\Constraint')
? new Constraint('>', $package->getVersion())
Expand Down
5 changes: 4 additions & 1 deletion src/VersionsCheckPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,15 @@ private function resolveOptions()
;

$options = array(
'root-packages-only' => false,
'show-links' => false,
);

if (null === $pluginConfig) {
return $options;
}

$options['root-packages-only'] = isset($pluginConfig['root-packages-only']) ? (bool) $pluginConfig['root-packages-only'] : $options['root-packages-only'];
$options['show-links'] = isset($pluginConfig['show-links']) ? (bool) $pluginConfig['show-links'] : $options['show-links'];

return $options;
Expand All @@ -129,7 +131,8 @@ private function checkVersions(RepositoryManager $repositoryManager, RootPackage
$this->versionsCheck->checkPackages(
$repository,
$repositoryManager->getLocalRepository(),
$rootPackage
$rootPackage,
$this->options['root-packages-only']
);
}

Expand Down
33 changes: 33 additions & 0 deletions tests/VersionsCheckPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,14 @@ public function getTestOptionsData()
'No option' => array(
null,
array(
'root-packages-only' => false,
'show-links' => false,
),
),
'Empty array options' => array(
array(),
array(
'root-packages-only' => false,
'show-links' => false,
),
),
Expand All @@ -110,6 +112,7 @@ public function getTestOptionsData()
),
),
array(
'root-packages-only' => false,
'show-links' => false,
),
),
Expand All @@ -120,6 +123,7 @@ public function getTestOptionsData()
),
),
array(
'root-packages-only' => false,
'show-links' => false,
),
),
Expand All @@ -130,6 +134,33 @@ public function getTestOptionsData()
),
),
array(
'root-packages-only' => false,
'show-links' => false,
),
),
'Activate root-packages-only' => array(
array(
'config' => array(
'sllh-composer-versions-check' => array(
'root-packages-only' => true,
),
),
),
array(
'root-packages-only' => true,
'show-links' => false,
),
),
'Disable root-packages-only' => array(
array(
'config' => array(
'sllh-composer-versions-check' => array(
'root-packages-only' => false,
),
),
),
array(
'root-packages-only' => false,
'show-links' => false,
),
),
Expand All @@ -142,6 +173,7 @@ public function getTestOptionsData()
),
),
array(
'root-packages-only' => false,
'show-links' => true,
),
),
Expand All @@ -154,6 +186,7 @@ public function getTestOptionsData()
),
),
array(
'root-packages-only' => false,
'show-links' => false,
),
),
Expand Down
101 changes: 83 additions & 18 deletions tests/VersionsCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,7 @@ public function testMultiplePackagesComparison(array $packagesData, $preferStabl
$this->rootPackage->setMinimumStability('dev');
$this->rootPackage->setPreferStable($preferStable);

$shouldBeUpdatedOutput = array();

foreach ($packagesData as $name => $packageData) {
list($actualVersion, $availableVersions, $expectedVersion) = $packageData;
$this->localRepository->addPackage(new Package($name, $actualVersion, $actualVersion));
foreach ($availableVersions as $availableVersion) {
$this->distRepository->addPackage(new Package($name, $availableVersion, $availableVersion));
}

if (false !== $expectedVersion) {
$shouldBeUpdatedOutput[] = sprintf(
' - <info>%s</info> (<comment>%s</comment>) latest is <comment>%s</comment>',
$name, $actualVersion, $expectedVersion
);
}
}
$shouldBeUpdatedOutput = $this->addPackagesAndGetShouldBeUpdatedOutput($packagesData);

$this->checkPackages();

Expand Down Expand Up @@ -202,11 +187,91 @@ public function testOutdatedWithLinks()
, $this->versionsCheck->getOutput(false));
}

/**
* @dataProvider getRootOnlyPackageTestsData
*/
public function testRootOnlyPackage(array $packagesData, array $packagesRequired, array $packagesDevRequired, $rootOnly, $outdatedPackagesCount)
{
$shouldBeUpdatedOutput = $this->addPackagesAndGetShouldBeUpdatedOutput($packagesData);
$this->rootPackage->setRequires($packagesRequired);
$this->rootPackage->setDevRequires($packagesDevRequired);

$this->checkPackages($rootOnly);

$this->assertSame(sprintf(<<<'EOF'
<warning>%d packages are not up to date:</warning>

%s


EOF
, $outdatedPackagesCount, implode("\n\n", $shouldBeUpdatedOutput)), $this->versionsCheck->getOutput());
}

/**
* @return array
*/
public function getRootOnlyPackageTestsData()
{
return array(
array(array(
'foo/bar' => array('1.0.0', array('1.0.0', '1.0.1', '1.0.2', '1.0.4', '2.0.0'), '2.0.0'),
'some/package' => array('1.0.4', array('1.0.0', '1.0.1', '1.0.2', '1.0.4', '2.0.0'), false),
'vendor/package-1' => array('2.0.0', array('1.0.0', '1.0.1', '1.0.2', '1.0.4', '2.0.0'), false),
'vendor/up-to-date' => array('9.9.0', array('8.0.0', '9.9.0', '1.0-dev'), false),
'vendor/dev-master' => array('9.9.0', array('8.0.0', '9.9.0', '10.0-dev'), false),
'vendor/package-2' => array('1.0.0', array('1.0.0', '1.0.1', '2.0.0', '2.0.0-alpha1'), '2.0.0'),
'vendor/package-3' => array('1.0.1', array('1.0.0', '1.0.1', '2.0.0-alpha1'), false),
'vendor/prefer-stable' => array('1.0.1', array('1.0.0', '1.0.1', '2.0.0-alpha1'), false),
), array(
'foo/bar' => new Link('foo/bar', 'foo/bar'),
), array(
'vendor/package-2' => new Link('vendor/package-2', 'vendor/package-2'),
), true, 2),
array(array(
'foo/bar' => array('1.0.0', array('1.0.0', '1.0.1', '1.0.2', '1.0.4', '2.0.0'), '2.0.0'),
'some/package' => array('1.0.4', array('1.0.0', '1.0.1', '1.0.2', '1.0.4', '2.0.0'), '2.0.0'),
'vendor/package-1' => array('2.0.0', array('1.0.0', '1.0.1', '1.0.2', '1.0.4', '2.0.0'), false),
'vendor/up-to-date' => array('9.9.0', array('8.0.0', '9.9.0', '1.0-dev'), false),
'vendor/dev-master' => array('9.9.0', array('8.0.0', '9.9.0', '10.0-dev'), '10.0-dev'),
'vendor/package-2' => array('1.0.0', array('1.0.0', '1.0.1', '2.0.0', '2.0.0-alpha1'), '2.0.0'),
'vendor/package-3' => array('1.0.1', array('1.0.0', '1.0.1', '2.0.0-alpha1'), '2.0.0-alpha1'),
'vendor/prefer-stable' => array('1.0.1', array('1.0.0', '1.0.1', '2.0.0-alpha1'), '2.0.0-alpha1'),
), array(
'foo/bar' => new Link('foo/bar', 'foo/bar'),
), array(
'vendor/package-2' => new Link('vendor/package-2', 'vendor/package-2'),
), false, 6),
);
}

private function addPackagesAndGetShouldBeUpdatedOutput(array $packagesData)
{
$shouldBeUpdatedOutput = array();

foreach ($packagesData as $name => $packageData) {
list($actualVersion, $availableVersions, $expectedVersion) = $packageData;
$this->localRepository->addPackage(new Package($name, $actualVersion, $actualVersion));
foreach ($availableVersions as $availableVersion) {
$this->distRepository->addPackage(new Package($name, $availableVersion, $availableVersion));
}

if (false !== $expectedVersion) {
$shouldBeUpdatedOutput[] = sprintf(
' - <info>%s</info> (<comment>%s</comment>) latest is <comment>%s</comment>',
$name, $actualVersion, $expectedVersion
);
}
}

return $shouldBeUpdatedOutput;
}

/**
* Calls VersionsCheck::checkPackages.
*/
private function checkPackages()
private function checkPackages($rootPackageOnly = false)
{
$this->versionsCheck->checkPackages($this->distRepository, $this->localRepository, $this->rootPackage);
$this->versionsCheck->checkPackages($this->distRepository, $this->localRepository, $this->rootPackage, $rootPackageOnly);
}
}