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

Issue #3252097 by phenaproxima, tedbow: Remove PathLocator::getStageDirectory()

parent 09bd176e
No related branches found
No related tags found
1 merge request!130Issue #3252097: Remove PathLocator::getStageDirectory()
......@@ -77,7 +77,6 @@ services:
package_manager.path_locator:
class: Drupal\package_manager\PathLocator
arguments:
- '@config.factory'
- '%app.root%'
# Validators.
......
......@@ -3,21 +3,12 @@
namespace Drupal\package_manager;
use Composer\Autoload\ClassLoader;
use Drupal\Component\FileSystem\FileSystem;
use Drupal\Core\Config\ConfigFactoryInterface;
/**
* Computes file system paths that are needed to stage code changes.
*/
class PathLocator {
/**
* The config factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The absolute path of the running Drupal code base.
*
......@@ -28,13 +19,10 @@ class PathLocator {
/**
* Constructs a PathLocator object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
* @param string $app_root
* The absolute path of the running Drupal code base.
*/
public function __construct(ConfigFactoryInterface $config_factory, string $app_root) {
$this->configFactory = $config_factory;
public function __construct(string $app_root) {
$this->appRoot = $app_root;
}
......@@ -48,23 +36,6 @@ class PathLocator {
return $this->getProjectRoot();
}
/**
* Returns the path of the directory where changes should be staged.
*
* This directory may be made world-writeable for clean-up, so it should be
* somewhere that doesn't put the Drupal installation at risk. Each staging
* area will use a sub-directory with a random name.
*
* @return string
* The absolute path of the directory where changes should be staged.
*/
public function getStageDirectory(): string {
// Append the site ID to the directory in order to support parallel test
// runs, or multiple sites hosted on the same server.
$site_id = $this->configFactory->get('system.site')->get('uuid');
return FileSystem::getOsTemporaryDirectory() . DIRECTORY_SEPARATOR . '.package_manager_' . $site_id;
}
/**
* Returns the absolute path of the project root.
*
......
......@@ -2,6 +2,7 @@
namespace Drupal\package_manager;
use Drupal\Component\FileSystem\FileSystem;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\TempStore\SharedTempStoreFactory;
......@@ -281,7 +282,7 @@ class Stage {
$this->dispatch(new PreDestroyEvent($this));
// Delete all directories in parent staging directory.
$parent_stage_dir = $this->pathLocator->getStageDirectory();
$parent_stage_dir = static::getStagingRoot();
if (is_dir($parent_stage_dir)) {
$this->fileSystem->deleteRecursive($parent_stage_dir, function (string $path): void {
$this->fileSystem->chmod($path, 0777);
......@@ -438,7 +439,18 @@ class Stage {
if (!$this->lock) {
throw new \LogicException(__METHOD__ . '() cannot be called because the stage has not been created or claimed.');
}
return $this->pathLocator->getStageDirectory() . DIRECTORY_SEPARATOR . $this->lock[0];
return static::getStagingRoot() . DIRECTORY_SEPARATOR . $this->lock[0];
}
/**
* Returns the directory where staging areas will be created.
*
* @return string
* The absolute path of the directory containing the staging areas managed
* by this class.
*/
protected static function getStagingRoot(): string {
return FileSystem::getOsTemporaryDirectory() . DIRECTORY_SEPARATOR . '.package_manager';
}
}
......@@ -59,11 +59,9 @@ class ExcludedPathsTest extends BrowserTestBase {
*/
public function testExcludedPaths(): void {
$active_dir = __DIR__ . '/../../fixtures/fake_site';
$parent_stage_dir = $this->siteDirectory . '/stage';
$path_locator = $this->prophesize(PathLocator::class);
$path_locator->getActiveDirectory()->willReturn($active_dir);
$path_locator->getStageDirectory()->willReturn($parent_stage_dir);
$site_path = 'sites/example.com';
......@@ -92,7 +90,7 @@ class ExcludedPathsTest extends BrowserTestBase {
$property->setAccessible(TRUE);
$property->setValue($subscriber, $database->reveal());
$stage = new Stage(
$stage = new class(
$path_locator->reveal(),
$this->container->get('package_manager.beginner'),
$this->container->get('package_manager.stager'),
......@@ -100,8 +98,25 @@ class ExcludedPathsTest extends BrowserTestBase {
$this->container->get('file_system'),
$this->container->get('event_dispatcher'),
$this->container->get('tempstore.shared'),
);
$stage_dir = $parent_stage_dir . DIRECTORY_SEPARATOR . $stage->create();
) extends Stage {
/**
* The directory where staging areas will be created.
*
* @var string
*/
public static $stagingRoot;
/**
* {@inheritdoc}
*/
protected static function getStagingRoot(): string {
return static::$stagingRoot;
}
};
$stage::$stagingRoot = $this->siteDirectory . '/stage';
$stage_dir = $stage::$stagingRoot . DIRECTORY_SEPARATOR . $stage->create();
$this->assertDirectoryExists($stage_dir);
$this->assertDirectoryNotExists("$stage_dir/sites/simpletest");
......
......@@ -167,7 +167,6 @@ class DiskSpaceValidatorTest extends PackageManagerKernelTestBase {
$path_locator = $this->prophesize('\Drupal\package_manager\PathLocator');
$path_locator->getProjectRoot()->willReturn('root');
$path_locator->getActiveDirectory()->willReturn('root');
$path_locator->getStageDirectory()->willReturn('/fake/stage/dir');
$path_locator->getVendorDirectory()->willReturn('vendor');
$this->container->set('package_manager.path_locator', $path_locator->reveal());
......
......@@ -26,13 +26,10 @@ class LockFileValidatorTest extends PackageManagerKernelTestBase {
$vendor = vfsStream::newDirectory('vendor');
$this->vfsRoot->addChild($vendor);
$stage = vfsStream::newDirectory('stage');
$this->vfsRoot->addChild($stage);
$path_locator = $this->prophesize(PathLocator::class);
$path_locator->getActiveDirectory()->willReturn($this->vfsRoot->url());
$path_locator->getProjectRoot()->willReturn($this->vfsRoot->url());
$path_locator->getStageDirectory()->willReturn($stage->url());
$path_locator->getWebRoot()->willReturn('');
$path_locator->getVendorDirectory()->willReturn($vendor->url());
$this->container->set('package_manager.path_locator', $path_locator->reveal());
......
......@@ -111,7 +111,6 @@ class WritableFileSystemValidatorTest extends PackageManagerKernelTestBase {
$path_locator = $this->prophesize(PathLocator::class);
$path_locator->getActiveDirectory()->willReturn($root->url());
$path_locator->getStageDirectory()->willReturn('/fake/stage/dir');
$path_locator->getWebRoot()->willReturn('');
$path_locator->getVendorDirectory()->willReturn($vendor->url());
$this->container->set('package_manager.path_locator', $path_locator->reveal());
......
......@@ -26,7 +26,7 @@ class FileSystemOperationsTest extends AutomaticUpdatesFunctionalTestBase {
/**
* The updater service under test.
*
* @var \Drupal\automatic_updates\Updater
* @var \Drupal\Tests\automatic_updates\Functional\TestUpdater
*/
private $updater;
......@@ -55,11 +55,10 @@ class FileSystemOperationsTest extends AutomaticUpdatesFunctionalTestBase {
$this->siteDirectory,
'stage',
]);
$locator->getStageDirectory()->willReturn($this->stageDir);
$locator->getProjectRoot()->willReturn($drupal_root);
$locator->getWebRoot()->willReturn('');
$this->updater = new Updater(
$this->updater = new TestUpdater(
$locator->reveal(),
$this->container->get('package_manager.beginner'),
$this->container->get('package_manager.stager'),
......@@ -68,6 +67,7 @@ class FileSystemOperationsTest extends AutomaticUpdatesFunctionalTestBase {
$this->container->get('event_dispatcher'),
$this->container->get('tempstore.shared')
);
$this->updater::$stagingRoot = $this->stageDir;
// Use the public and private files directories in the fake site fixture.
$settings = Settings::getAll();
......@@ -125,3 +125,24 @@ class FileSystemOperationsTest extends AutomaticUpdatesFunctionalTestBase {
}
}
/**
* A test-only version of the updater.
*/
class TestUpdater extends Updater {
/**
* The directory where staging areas will be created.
*
* @var string
*/
public static $stagingRoot;
/**
* {@inheritdoc}
*/
protected static function getStagingRoot(): string {
return static::$stagingRoot ?: parent::getStagingRoot();
}
}
......@@ -2,6 +2,7 @@
namespace Drupal\Tests\automatic_updates\Kernel\ReadinessValidation;
use Drupal\automatic_updates\Updater;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\package_manager\Event\PreApplyEvent;
use Drupal\package_manager\Exception\StageValidationException;
......@@ -26,6 +27,16 @@ class StagedProjectsValidatorTest extends AutomaticUpdatesKernelTestBase {
'package_manager_bypass',
];
/**
* {@inheritdoc}
*/
public function register(ContainerBuilder $container) {
parent::register($container);
$container->getDefinition('automatic_updates.updater')
->setClass(TestUpdater::class);
}
/**
* {@inheritdoc}
*/
......@@ -62,12 +73,12 @@ class StagedProjectsValidatorTest extends AutomaticUpdatesKernelTestBase {
// subdirectory using the stage ID after it is created below.
$vendor = vfsStream::newDirectory('au_stage');
$this->vfsRoot->addChild($vendor);
$locator->getStageDirectory()->willReturn($this->vfsRoot->url() . DIRECTORY_SEPARATOR . 'au_stage');
TestUpdater::$stagingRoot = $vendor->url();
}
else {
// If we are testing non-existent staging directory we can use the path
// directly.
$locator->getStageDirectory()->willReturn($stage_dir);
TestUpdater::$stagingRoot = $stage_dir;
}
$this->container->set('package_manager.path_locator', $locator->reveal());
......@@ -213,3 +224,24 @@ class StagedProjectsValidatorTest extends AutomaticUpdatesKernelTestBase {
}
}
/**
* A test-only version of the updater.
*/
class TestUpdater extends Updater {
/**
* The directory where staging areas will be created.
*
* @var string
*/
public static $stagingRoot;
/**
* {@inheritdoc}
*/
protected static function getStagingRoot(): string {
return static::$stagingRoot ?: parent::getStagingRoot();
}
}
......@@ -52,7 +52,6 @@ class UpdaterTest extends AutomaticUpdatesKernelTestBase {
$locator->getActiveDirectory()->willReturn($fixture_dir);
$locator->getProjectRoot()->willReturn($fixture_dir);
$locator->getVendorDirectory()->willReturn($fixture_dir);
$locator->getStageDirectory()->willReturn('/tmp');
$this->container->set('package_manager.path_locator', $locator->reveal());
$id = $this->container->get('automatic_updates.updater')->begin([
......
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