Skip to content
Snippets Groups Projects
Commit 64ddf83e authored by Adam G-H's avatar Adam G-H
Browse files

Issue #3317996 by phenaproxima: FixtureUtilityTrait's package manipulation...

Issue #3317996 by phenaproxima: FixtureUtilityTrait's package manipulation methods need to take installation path into account
parent ca95d870
No related branches found
No related tags found
No related merge requests found
...@@ -3,4 +3,4 @@ updater's ...@@ -3,4 +3,4 @@ updater's
stager's stager's
syncer syncer
syncers syncers
unrequested kirk
...@@ -30,11 +30,14 @@ class FixtureUtilityTraitTest extends PackageManagerKernelTestBase { ...@@ -30,11 +30,14 @@ class FixtureUtilityTraitTest extends PackageManagerKernelTestBase {
$this->dir = $this->container->get('package_manager.path_locator') $this->dir = $this->container->get('package_manager.path_locator')
->getProjectRoot(); ->getProjectRoot();
$this->addPackage($this->dir, ['name' => 'my/package']); $this->addPackage($this->dir, [
'name' => 'my/package',
]);
$this->addPackage($this->dir, [ $this->addPackage($this->dir, [
'name' => 'my/dev-package', 'name' => 'my/dev-package',
'version' => '2.1.0', 'version' => '2.1.0',
'dev_requirement' => TRUE, 'dev_requirement' => TRUE,
'install_path' => '../relative/path',
]); ]);
} }
...@@ -60,6 +63,18 @@ class FixtureUtilityTraitTest extends PackageManagerKernelTestBase { ...@@ -60,6 +63,18 @@ class FixtureUtilityTraitTest extends PackageManagerKernelTestBase {
$this->assertStringContainsString("Expected package 'my/package' to not be installed, but it was.", $e->getMessage()); $this->assertStringContainsString("Expected package 'my/package' to not be installed, but it was.", $e->getMessage());
} }
// We should not be able to add a package with an absolute installation
// path.
try {
$this->addPackage($this->dir, [
'name' => 'absolute/path',
'install_path' => '/absolute/path',
]);
}
catch (AssertionFailedError $e) {
$this->assertSame('Failed asserting that \'/absolute/path\' starts with "../".', $e->getMessage());
}
$expected_packages = [ $expected_packages = [
'my/package' => [ 'my/package' => [
'name' => 'my/package', 'name' => 'my/package',
...@@ -68,6 +83,7 @@ class FixtureUtilityTraitTest extends PackageManagerKernelTestBase { ...@@ -68,6 +83,7 @@ class FixtureUtilityTraitTest extends PackageManagerKernelTestBase {
'name' => 'my/dev-package', 'name' => 'my/dev-package',
'version' => '2.1.0', 'version' => '2.1.0',
'dev_requirement' => TRUE, 'dev_requirement' => TRUE,
'install_path' => '../relative/path',
], ],
]; ];
[$installed_json, $installed_php] = $this->getData(); [$installed_json, $installed_php] = $this->getData();
...@@ -75,6 +91,10 @@ class FixtureUtilityTraitTest extends PackageManagerKernelTestBase { ...@@ -75,6 +91,10 @@ class FixtureUtilityTraitTest extends PackageManagerKernelTestBase {
$this->assertSame($expected_packages, $installed_json['packages']); $this->assertSame($expected_packages, $installed_json['packages']);
$this->assertContains('my/dev-package', $installed_json['dev-package-names']); $this->assertContains('my/dev-package', $installed_json['dev-package-names']);
$this->assertNotContains('my/package', $installed_json['dev-package-names']); $this->assertNotContains('my/package', $installed_json['dev-package-names']);
// In installed.php, the relative installation path of my/dev-package should
// have been prefixed with the __DIR__ constant, which should be interpreted
// when installed.php is loaded by the PHP runtime.
$expected_packages['my/dev-package']['install_path'] = "$this->dir/vendor/composer/../relative/path";
$this->assertSame($expected_packages, $installed_php); $this->assertSame($expected_packages, $installed_php);
} }
...@@ -111,6 +131,7 @@ class FixtureUtilityTraitTest extends PackageManagerKernelTestBase { ...@@ -111,6 +131,7 @@ class FixtureUtilityTraitTest extends PackageManagerKernelTestBase {
'name' => 'my/dev-package', 'name' => 'my/dev-package',
'version' => '3.2.1', 'version' => '3.2.1',
'dev_requirement' => TRUE, 'dev_requirement' => TRUE,
'install_path' => '../relative/path',
], ],
'my/other-package' => [ 'my/other-package' => [
'name' => 'my/other-package', 'name' => 'my/other-package',
...@@ -125,6 +146,8 @@ class FixtureUtilityTraitTest extends PackageManagerKernelTestBase { ...@@ -125,6 +146,8 @@ class FixtureUtilityTraitTest extends PackageManagerKernelTestBase {
$this->assertContains('my/dev-package', $installed_json['dev-package-names']); $this->assertContains('my/dev-package', $installed_json['dev-package-names']);
$this->assertContains('my/other-package', $installed_json['dev-package-names']); $this->assertContains('my/other-package', $installed_json['dev-package-names']);
$this->assertNotContains('my/package', $installed_json['dev-package-names']); $this->assertNotContains('my/package', $installed_json['dev-package-names']);
// @see ::testAddPackage()
$expected_packages['my/dev-package']['install_path'] = "$this->dir/vendor/composer/../relative/path";
$this->assertSame($expected_packages, $installed_php); $this->assertSame($expected_packages, $installed_php);
} }
......
...@@ -80,6 +80,13 @@ trait FixtureUtilityTrait { ...@@ -80,6 +80,13 @@ trait FixtureUtilityTrait {
/** /**
* Adds a package. * Adds a package.
* *
* If $package contains an `install_path` key, it should be relative to the
* location of `installed.json` and `installed.php`, which are in
* `vendor/composer`. For example, if the package would be installed at
* `vendor/kirk/enterprise`, the install path should be `../kirk/enterprise`.
* If the package would be installed outside of vendor (for example, a Drupal
* module in the `modules` directory), it would be `../../modules/my_module`.
*
* @param string $dir * @param string $dir
* The root Composer-managed directory (e.g., the project root or staging * The root Composer-managed directory (e.g., the project root or staging
* area). * area).
...@@ -95,6 +102,9 @@ trait FixtureUtilityTrait { ...@@ -95,6 +102,9 @@ trait FixtureUtilityTrait {
/** /**
* Modifies a package's installed info. * Modifies a package's installed info.
* *
* See ::addPackage() for information on how the `install_path` key is
* handled, if $package has it.
*
* @param string $dir * @param string $dir
* The root Composer-managed directory (e.g., the project root or staging * The root Composer-managed directory (e.g., the project root or staging
* area). * area).
...@@ -140,7 +150,9 @@ trait FixtureUtilityTrait { ...@@ -140,7 +150,9 @@ trait FixtureUtilityTrait {
* Whether or not the package is expected to already be installed. * Whether or not the package is expected to already be installed.
*/ */
private function setPackage(string $dir, string $name, ?array $package, bool $should_exist): void { private function setPackage(string $dir, string $name, ?array $package, bool $should_exist): void {
$file = $dir . '/vendor/composer/installed.json'; $dir .= '/vendor/composer';
$file = $dir . '/installed.json';
$this->assertFileIsWritable($file); $this->assertFileIsWritable($file);
$data = file_get_contents($file); $data = file_get_contents($file);
...@@ -176,6 +188,10 @@ trait FixtureUtilityTrait { ...@@ -176,6 +188,10 @@ trait FixtureUtilityTrait {
} }
// Add the package back to the list, if we have data for it. // Add the package back to the list, if we have data for it.
if ($package) { if ($package) {
// If an install path was provided, ensure it's relative.
if (array_key_exists('install_path', $package)) {
$this->assertStringStartsWith('../', $package['install_path']);
}
$package['name'] = $name; $package['name'] = $name;
$data['packages'][] = $package; $data['packages'][] = $package;
...@@ -185,17 +201,23 @@ trait FixtureUtilityTrait { ...@@ -185,17 +201,23 @@ trait FixtureUtilityTrait {
} }
file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
$file = $dir . '/vendor/composer/installed.php'; $file = $dir . '/installed.php';
$this->assertFileIsWritable($file); $this->assertFileIsWritable($file);
$data = require $file; $data = require $file;
unset($data['versions'][$name]);
// The installation paths in $data will have been interpreted by the PHP
// runtime, so make them all relative again by stripping $dir out.
array_walk($data['versions'], function (array &$package) use ($dir): void {
if (array_key_exists('install_path', $package)) {
$package['install_path'] = str_replace("$dir/", '', $package['install_path']);
}
});
if ($package) { if ($package) {
$data['versions'][$name] = $package; $data['versions'][$name] = $package;
} }
else {
unset($data['versions'][$name]);
}
$data = var_export($data, TRUE); $data = var_export($data, TRUE);
$data = str_replace("'install_path' => '../", "'install_path' => __DIR__ . '/../", $data);
file_put_contents($file, "<?php\nreturn $data;"); file_put_contents($file, "<?php\nreturn $data;");
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment