Skip to content

Unpack replace and provides of packs #692

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

Closed
Closed
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
31 changes: 29 additions & 2 deletions src/Unpacker.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Composer\IO\IOInterface;
use Composer\Json\JsonFile;
use Composer\Json\JsonManipulator;
use Composer\Package\Link;
use Composer\Package\Locker;
use Composer\Package\Version\VersionSelector;
use Composer\Plugin\PluginInterface;
Expand Down Expand Up @@ -60,7 +61,7 @@ public function unpack(Operation $op, Result $result = null, &$links = []): Resu
null === $pkg ||
'symfony-pack' !== $pkg->getType() ||
!$op->shouldUnpack() ||
0 === \count($pkg->getRequires()) + \count($pkg->getDevRequires())
0 === \count($pkg->getRequires()) + \count($pkg->getDevRequires()) + \count($pkg->getReplaces()) + \count($pkg->getProvides())
) {
$result->addRequired($package['name'].($package['version'] ? ':'.$package['version'] : ''));

Expand All @@ -72,6 +73,16 @@ public function unpack(Operation $op, Result $result = null, &$links = []): Resu
}

$versionSelector = null;

if (!$package['dev']) {
foreach ($pkg->getReplaces() as $link) {
$this->addLinks($link, $links, 'replace');
}
foreach ($pkg->getProvides() as $link) {
$this->addLinks($link, $links, 'provide');
}
}

foreach ($pkg->getRequires() as $link) {
if ('php' === $link->getTarget()) {
continue;
Expand Down Expand Up @@ -106,7 +117,7 @@ public function unpack(Operation $op, Result $result = null, &$links = []): Resu

if (isset($links[$linkName])) {
$links[$linkName]['constraints'][] = $constraint;
if ('require' === $linkType) {
if ('require' === $linkType && !\in_array($links['linkName']['type'],['replace', 'provide'])) {
$links[$linkName]['type'] = 'require';
}
} else {
Expand Down Expand Up @@ -200,4 +211,20 @@ public function updateLock(Result $result, IOInterface $io): void
}
$this->composer->setLocker($locker);
}


private function addLinks(Link $link, array &$links, string $linkType): void
{
$constraint = $link->getPrettyConstraint();
$constraint = substr($this->resolver->parseVersion($link->getTarget(), $constraint, false), 1) ?: $constraint;

$linkName = $link->getTarget();
$constraint = $this->versionParser->parseConstraints($constraint);

$links[$linkName] = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong to me. You could have a conflict and a require-dev on the same package. Using the same $links indexed by package name won't work fine.

'type' => $linkType,
'name' => $linkName,
'constraints' => [$constraint],
];
}
}
154 changes: 154 additions & 0 deletions tests/UnpackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,158 @@ public function testDoNotDuplicateEntry(): void
putenv('COMPOSER='.$originalEnvComposer);
@unlink($composerJsonPath);
}

/**
* Replaces are added from the packs into the main composer.json.
*
* Context:
*
* - There are two packs: "pack_foo" and "pack_bar"
* - Both points to a package named "real"
* - "pack_foo" is present in the "require" section
* - "pack_bar" is present in the "replace" section
*
* Expected result:
*
* - "real" package MUST be present ONLY in "replace" section
*/
public function testReplacesAreAdded(): void
{
// Setup project

$composerJsonPath = FLEX_TEST_DIR.'/composer.json';

@mkdir(FLEX_TEST_DIR);
@unlink($composerJsonPath);
file_put_contents($composerJsonPath, '{}');

$originalEnvComposer = getenv('COMPOSER');
putenv('COMPOSER='.$composerJsonPath);

// Setup packages

$realPkg = new Package('real', '1.0.0', '1.0.0');
$realPkgLink = new Link('lorem', 'real', class_exists(MatchAllConstraint::class) ? new MatchAllConstraint() : null, 'wraps', '1.0.0');

$virtualPkgFoo = new Package('pack_foo', '1.0.0', '1.0.0');
$virtualPkgFoo->setType('symfony-pack');
$virtualPkgFoo->setRequires([$realPkgLink]);

$virtualPkgBar = new Package('pack_bar', '1.0.0', '1.0.0');
$virtualPkgBar->setType('symfony-pack');
$virtualPkgBar->setReplaces([$realPkgLink]);

$packages = [$realPkg, $virtualPkgFoo, $virtualPkgBar];

// Setup Composer

$repManager = $this->getMockBuilder(RepositoryManager::class)->disableOriginalConstructor()->getMock();
$repManager->expects($this->any())->method('getLocalRepository')->willReturn(new InstalledArrayRepository($packages));

$composer = new Composer();
$composer->setRepositoryManager($repManager);

// Unpack

$resolver = $this->getMockBuilder(PackageResolver::class)->disableOriginalConstructor()->getMock();

$unpacker = new Unpacker($composer, $resolver, false);

$operation = new Operation(true, false);
$operation->addPackage('pack_foo', '*', false);
$operation->addPackage('pack_bar', '*', false);

$unpacker->unpack($operation);

// Check

$composerJson = json_decode(file_get_contents($composerJsonPath), true);

$this->assertArrayHasKey('replace', $composerJson);
$this->assertArrayHasKey('real', $composerJson['replace']);
$this->assertArrayNotHasKey('require-dev', $composerJson);
$this->assertArrayNotHasKey('require', $composerJson);

// Restore

putenv('COMPOSER='.$originalEnvComposer);
@unlink($composerJsonPath);
}

/**
* Replaces from dev dependencies are not added.
*
* Context:
*
* - There are two packs: "pack_foo" and "pack_bar"
* - Both points to a package named "real"
* - "pack_foo" is present in the "require" section
* - "pack_bar" is present in the "replace" section
*
* Expected result:
*
* - "real" package MUST be present ONLY in "require" section
*/
public function testReplacesAreNotAddedForDevDependencies(): void
{
// Setup project

$composerJsonPath = FLEX_TEST_DIR.'/composer.json';

@mkdir(FLEX_TEST_DIR);
@unlink($composerJsonPath);
file_put_contents($composerJsonPath, '{}');

$originalEnvComposer = getenv('COMPOSER');
putenv('COMPOSER='.$composerJsonPath);

// Setup packages

$realPkg = new Package('real', '1.0.0', '1.0.0');
$realPkgLink = new Link('lorem', 'real', class_exists(MatchAllConstraint::class) ? new MatchAllConstraint() : null, 'wraps', '1.0.0');

$virtualPkgFoo = new Package('pack_foo', '1.0.0', '1.0.0');
$virtualPkgFoo->setType('symfony-pack');
$virtualPkgFoo->setRequires([$realPkgLink]);

$virtualPkgBar = new Package('pack_bar', '1.0.0', '1.0.0');
$virtualPkgBar->setType('symfony-pack');
$virtualPkgBar->setReplaces([$realPkgLink]);

$packages = [$realPkg, $virtualPkgFoo, $virtualPkgBar];

// Setup Composer

$repManager = $this->getMockBuilder(RepositoryManager::class)->disableOriginalConstructor()->getMock();
$repManager->expects($this->any())->method('getLocalRepository')->willReturn(new InstalledArrayRepository($packages));

$composer = new Composer();
$composer->setRepositoryManager($repManager);

// Unpack

$resolver = $this->getMockBuilder(PackageResolver::class)->disableOriginalConstructor()->getMock();

$unpacker = new Unpacker($composer, $resolver, false);

$operation = new Operation(true, false);
$operation->addPackage('pack_foo', '*', false);
$operation->addPackage('pack_bar', '*', true);

$unpacker->unpack($operation);

// Check

$composerJson = json_decode(file_get_contents($composerJsonPath), true);

$this->assertArrayHasKey('require', $composerJson);
$this->assertArrayHasKey('real', $composerJson['require']);
$this->assertArrayNotHasKey('require-dev', $composerJson);
$this->assertArrayNotHasKey('replace', $composerJson);

// Restore

putenv('COMPOSER='.$originalEnvComposer);
@unlink($composerJsonPath);
}
}