Skip to content

Commit 46b0639

Browse files
rhoflandjdforrester
authored andcommitted
Re-write relative paths for fields when recursing
This affects the fields: - "required", - "include", and - "path repository" Added test cases for each.
1 parent 47be3c0 commit 46b0639

File tree

14 files changed

+308
-2
lines changed

14 files changed

+308
-2
lines changed

src/ExtraPackage.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ public function __construct($path, Composer $composer, Logger $logger)
8989
*/
9090
public function getIncludes()
9191
{
92-
return $this->json['extra']['merge-plugin']['include'] ?? [];
92+
return isset($this->json['extra']['merge-plugin']['include']) ?
93+
$this->fixRelativePaths($this->json['extra']['merge-plugin']['include']) : [];
9394
}
9495

9596
/**
@@ -99,7 +100,8 @@ public function getIncludes()
99100
*/
100101
public function getRequires()
101102
{
102-
return $this->json['extra']['merge-plugin']['require'] ?? [];
103+
return isset($this->json['extra']['merge-plugin']['require']) ?
104+
$this->fixRelativePaths($this->json['extra']['merge-plugin']['require']) : [];
103105
}
104106

105107
/**
@@ -218,6 +220,9 @@ protected function prependRepositories(RootPackageInterface $root)
218220
if (!isset($repoJson['type'])) {
219221
continue;
220222
}
223+
if ($repoJson['type'] == 'path' && isset($repoJson['url'])) {
224+
$repoJson['url'] = $this->fixRelativePaths(array($repoJson['url']))[0];
225+
}
221226
$this->logger->info("Prepending {$repoJson['type']} repository");
222227
$repo = $repoManager->createRepository(
223228
$repoJson['type'],

tests/phpunit/MergePluginTest.php

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,41 @@ function ($args) use (&$packages) {
286286
$this->assertArrayHasKey('monolog/monolog', $packages);
287287
}
288288

289+
/**
290+
* Given a root package with no requires
291+
* and a subdirectory/composer.local.json with one require, which includes a composer.local.2.json
292+
* and a subdirectory/composer.local.2.json with one additional require
293+
* When the plugin is run
294+
* Then the root package should inherit both requires
295+
* and no modifications should be made by the pre-dependency hook.
296+
*/
297+
public function testRecursiveIncludesWithSubDirectory()
298+
{
299+
$dir = $this->fixtureDir(__FUNCTION__);
300+
301+
$root = $this->rootFromJson("{$dir}/composer.json");
302+
303+
$packages = array();
304+
$root->setRequires(Argument::type('array'))->will(
305+
function ($args) use (&$packages) {
306+
$packages = array_merge($packages, $args[0]);
307+
}
308+
);
309+
310+
$root->getRepositories()->shouldNotBeCalled();
311+
$root->getConflicts()->shouldNotBeCalled();
312+
$root->getReplaces()->shouldNotBeCalled();
313+
$root->getProvides()->shouldNotBeCalled();
314+
$root->getSuggests()->shouldNotBeCalled();
315+
316+
$extraInstalls = $this->triggerPlugin($root->reveal(), $dir);
317+
318+
$this->assertArrayHasKey('foo', $packages);
319+
$this->assertArrayHasKey('monolog/monolog', $packages);
320+
321+
$this->assertEquals(null, $extraInstalls);
322+
}
323+
289324
/**
290325
* Given a root package with no requires that disables recursion
291326
* and a composer.local.json with one require, which includes a composer.local.2.json
@@ -319,6 +354,162 @@ function ($args) use (&$packages) {
319354
$this->assertArrayNotHasKey('monolog/monolog', $packages);
320355
}
321356

357+
/**
358+
* Given a root package with no requires
359+
* and a subdirectory/composer.local.json with one require, which includes a composer.local.2.json
360+
* and a subdirectory/composer.local.2.json with one additional require and a repository of type 'path'
361+
* and url with a value of 'localdirectory'
362+
* When the plugin is run
363+
* Then the root package should inherit both requires
364+
* and should have the repository path url fixed to 'subdirectory/localdirectory'
365+
* and no modifications should be made by the pre-dependency hook.
366+
*/
367+
public function testRecursivePathRepositoriesWithSubDirectory()
368+
{
369+
$that = $this;
370+
$io = $this->io;
371+
$dir = $this->fixtureDir(__FUNCTION__);
372+
373+
$repoManager = $this->prophesize(
374+
'Composer\Repository\RepositoryManager'
375+
);
376+
$repoManager->createRepository(
377+
Argument::type('string'),
378+
Argument::type('array')
379+
)->will(
380+
function ($args) use ($that, $io) {
381+
$that->assertEquals('path', $args[0]);
382+
$that->assertEquals(
383+
'subdirectory/local/directory',
384+
$args[1]['url']
385+
);
386+
387+
return new \Composer\Repository\PathRepository(
388+
$args[1],
389+
$io->reveal(),
390+
new \Composer\Config()
391+
);
392+
}
393+
);
394+
$repoManager->prependRepository(Argument::any())->will(
395+
function ($args) use ($that) {
396+
$that->assertInstanceOf(
397+
'Composer\Repository\PathRepository',
398+
$args[0]
399+
);
400+
}
401+
);
402+
$this->composer->getRepositoryManager()->will(
403+
function () use ($repoManager) {
404+
return $repoManager->reveal();
405+
}
406+
);
407+
408+
$root = $this->rootFromJson("{$dir}/composer.json");
409+
410+
$packages = array();
411+
$root->setRequires(Argument::type('array'))->will(
412+
function ($args) use (&$packages) {
413+
$packages = array_merge($packages, $args[0]);
414+
}
415+
);
416+
417+
$root->setDevRequires()->shouldNotBeCalled();
418+
419+
$root->setRepositories(Argument::type('array'))->will(
420+
function ($args) use ($that) {
421+
$repos = $args[0];
422+
$that->assertEquals(1, count($repos));
423+
}
424+
);
425+
426+
$root->getConflicts()->shouldNotBeCalled();
427+
$root->getReplaces()->shouldNotBeCalled();
428+
$root->getProvides()->shouldNotBeCalled();
429+
$root->getSuggests()->shouldNotBeCalled();
430+
431+
$extraInstalls = $this->triggerPlugin($root->reveal(), $dir);
432+
433+
$this->assertArrayHasKey('foo', $packages);
434+
$this->assertArrayHasKey('wikimedia/composer-merge-plugin', $packages);
435+
436+
$this->assertEquals(null, $extraInstalls);
437+
}
438+
439+
440+
/**
441+
* Given a root package with no requires
442+
* and a composer.local.json with one require, which requires a composer.local.2.json
443+
* and a composer.local.2.json with one additional require
444+
* When the plugin is run
445+
* Then the root package should inherit both requires
446+
* and no modifications should be made by the pre-dependency hook.
447+
*/
448+
public function testRecursiveRequires()
449+
{
450+
$dir = $this->fixtureDir(__FUNCTION__);
451+
452+
$root = $this->rootFromJson("{$dir}/composer.json");
453+
454+
$packages = array();
455+
456+
$root->setRequires(Argument::type('array'))->will(
457+
function ($args) use (&$packages) {
458+
$packages = array_merge($packages, $args[0]);
459+
}
460+
);
461+
462+
$root->getRepositories()->shouldNotBeCalled();
463+
$root->getConflicts()->shouldNotBeCalled();
464+
$root->getReplaces()->shouldNotBeCalled();
465+
$root->getProvides()->shouldNotBeCalled();
466+
$root->getSuggests()->shouldNotBeCalled();
467+
468+
$extraInstalls = $this->triggerPlugin($root->reveal(), $dir);
469+
470+
471+
472+
$this->assertArrayHasKey('foo', $packages);
473+
$this->assertArrayHasKey('monolog/monolog', $packages);
474+
475+
$this->assertEquals(null, $extraInstalls);
476+
}
477+
478+
/**
479+
* Given a root package with no requires
480+
* and a subdirectory/composer.local.json with one require, which requires a composer.local.2.json
481+
* and a subdirectory/composer.local.2.json with one additional require
482+
* When the plugin is run
483+
* Then the root package should inherit both requires
484+
* and no modifications should be made by the pre-dependency hook.
485+
*/
486+
public function testRecursiveRequiresWithSubDirectory()
487+
{
488+
$dir = $this->fixtureDir(__FUNCTION__);
489+
490+
$root = $this->rootFromJson("{$dir}/composer.json");
491+
492+
$packages = array();
493+
$root->setRequires(Argument::type('array'))->will(
494+
function ($args) use (&$packages) {
495+
$packages = array_merge($packages, $args[0]);
496+
}
497+
);
498+
499+
$root->getRepositories()->shouldNotBeCalled();
500+
$root->getConflicts()->shouldNotBeCalled();
501+
$root->getReplaces()->shouldNotBeCalled();
502+
$root->getProvides()->shouldNotBeCalled();
503+
$root->getSuggests()->shouldNotBeCalled();
504+
505+
$extraInstalls = $this->triggerPlugin($root->reveal(), $dir);
506+
507+
$this->assertArrayHasKey('foo', $packages);
508+
$this->assertArrayHasKey('monolog/monolog', $packages);
509+
510+
$this->assertEquals(null, $extraInstalls);
511+
}
512+
322513
/**
323514
* Given a root package with requires
324515
* and a composer.local.json with requires
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extra": {
3+
"merge-plugin": {
4+
"include": [
5+
"subdirectory/composer.local.json"
6+
]
7+
}
8+
}
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"monolog/monolog": "~1.0"
4+
}
5+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"require": {
3+
"foo": "1.0.0"
4+
},
5+
"extra": {
6+
"merge-plugin": {
7+
"include": [
8+
"composer.local.2.json"
9+
]
10+
}
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extra": {
3+
"merge-plugin": {
4+
"include": [
5+
"subdirectory/composer.local.json"
6+
]
7+
}
8+
}
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"require": {
3+
"wikimedia/composer-merge-plugin": "dev-master#deadbeef"
4+
},
5+
"repositories": [
6+
{
7+
"type": "path",
8+
"url": "local/directory"
9+
}
10+
]
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"require": {
3+
"foo": "1.0.0"
4+
},
5+
"extra": {
6+
"merge-plugin": {
7+
"include": [
8+
"composer.local.2.json"
9+
]
10+
}
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extra": {
3+
"merge-plugin": {
4+
"require": [
5+
"composer.local.json"
6+
]
7+
}
8+
}
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"monolog/monolog": "~1.0"
4+
}
5+
}

0 commit comments

Comments
 (0)