Skip to content

Commit

Permalink
Run project-specific Drall during tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jigarius committed Dec 25, 2024
1 parent 7e8aac7 commit 1624b82
Show file tree
Hide file tree
Showing 33 changed files with 356 additions and 493 deletions.
2 changes: 2 additions & 0 deletions .docker/main/.profile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PATH=$(echo "$PATH" | sed -e "s/:\/opt\/drupal\/vendor\/bin//")
export PATH="/opt/drall/.docker/main/bin:$PATH"
8 changes: 5 additions & 3 deletions .docker/main/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ RUN cp "$PHP_INI_DIR/php.ini-development" "$PHP_INI_PATH" \
RUN docker-php-ext-configure pcntl --enable-pcntl \
&& docker-php-ext-install pcntl

# Provision Drall.
COPY . /opt/drall

# Provision Drupal.
COPY Makefile /opt/drupal/Makefile
COPY Makefile /opt/no-drupal/Makefile
COPY Makefile /opt/empty-drupal/Makefile

RUN echo ". /opt/drall/.docker/main/.profile" >> /root/.profile
RUN echo ". /opt/drall/.docker/main/.profile" >> /root/.bashrc
17 changes: 17 additions & 0 deletions .docker/main/bin/drall
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

set -e

dir=$(pwd)
while [ "$dir" != "/" ]; do
if [ -x "$dir/vendor/bin/drall" ]; then
"$dir/vendor/bin/drall" "$@"
break
fi
dir=$(dirname "$dir")
done

if [ "$dir" == "/" ]; then
echo "Drall executable not found."
exit 1
fi
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
{
"name": "jigarius/drall-demo",
"description": "A Drupal demo site for developing and testing Drall.",
"description": "A Drupal multi-site installation for developing and testing Drall.",
"type": "project",
"license": "GPL-2.0-or-later",
"homepage": "https://www.drupal.org/project/drupal",
"support": {
"docs": "https://www.drupal.org/docs/user_guide/en/index.html",
"chat": "https://www.drupal.org/node/314178"
},
"repositories": [
{
"type": "composer",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
76 changes: 76 additions & 0 deletions .docker/main/empty-drupal/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"description": "A Drupal installation with no sites.",
"type": "project",
"license": "GPL-2.0-or-later",
"repositories": [
{
"type": "composer",
"url": "https://packages.drupal.org/8"
},
{
"type": "path",
"url": "/opt/drall",
"options": {
"symlink": false
}
}
],
"require": {
"composer/installers": "^2",
"drupal/core-composer-scaffold": "*",
"drupal/core-recommended": "^11",
"drupal/core-vendor-hardening": "*",
"jigarius/drall": "*"
},
"conflict": {
"drupal/drupal": "*"
},
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"sort-packages": true,
"allow-plugins": {
"composer/installers": true,
"drupal/core-composer-scaffold": true,
"drupal/core-project-message": true,
"drupal/core-vendor-hardening": true,
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"extra": {
"drupal-scaffold": {
"locations": {
"web-root": "web/"
}
},
"installer-paths": {
"web/core": [
"type:drupal-core"
],
"web/libraries/{$name}": [
"type:drupal-library"
],
"web/modules/contrib/{$name}": [
"type:drupal-module"
],
"web/profiles/contrib/{$name}": [
"type:drupal-profile"
],
"web/themes/contrib/{$name}": [
"type:drupal-theme"
],
"drush/Commands/contrib/{$name}": [
"type:drupal-drush"
],
"web/modules/custom/{$name}": [
"type:drupal-custom-module"
],
"web/profiles/custom/{$name}": [
"type:drupal-custom-profile"
],
"web/themes/custom/{$name}": [
"type:drupal-custom-theme"
]
}
}
}
3 changes: 3 additions & 0 deletions .docker/main/empty-drupal/web/sites/sites.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

$sites = [];
29 changes: 29 additions & 0 deletions .docker/main/no-drupal/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"description": "Project that contains Drall without Drupal.",
"type": "project",
"license": "GPL-2.0-or-later",
"repositories": [
{
"type": "composer",
"url": "https://packages.drupal.org/8"
},
{
"type": "path",
"url": "/opt/drall",
"options": {
"symlink": false
}
}
],
"require": {
"jigarius/drall": "*"
},
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"sort-packages": true,
"allow-plugins": {
"composer/installers": true
}
}
}
48 changes: 26 additions & 22 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ ssh:


.PHONY: provision
provision: provision/drall provision/drupal
provision: provision/drall provision/no-drupal provision/empty-drupal provision/drupal


.PHONY: provision/drupal
provision/drupal:
mkdir -p /opt/drupal
cp /opt/drall/.docker/main/composer.json /opt/drupal/ || echo "Skipping drupal/composer.json"

cp /opt/drall/.docker/main/drupal/composer.json /opt/drupal/ || echo "Skipping: drupal/composer.json"
rm -f /opt/drupal/composer.lock
composer --working-dir=/opt/drupal install --no-progress

cp -r /opt/drall/.docker/main/drush /opt/drupal/ || echo "Skipping drush directory."
cp -r /opt/drall/.docker/main/sites /opt/drupal/web/ || echo "Skipping sites directory."
cp -r /opt/drall/.docker/main/drupal/drush /opt/drupal/ || echo "Skipping: drupal/drush"
cp -r /opt/drall/.docker/main/drupal/web/sites /opt/drupal/web/ || echo "Skipping: drupal/web/sites"

mkdir -p /opt/drupal/web/sites/default
mkdir -p /opt/drupal/web/sites/donnie
Expand All @@ -29,9 +29,27 @@ provision/drupal:
cp /opt/drupal/web/sites/default/default.settings.php /opt/drupal/web/sites/mikey/settings.php
cp /opt/drupal/web/sites/default/default.settings.php /opt/drupal/web/sites/ralph/settings.php

@echo ''
@echo 'Drupal databases can be provisioned with: make provision/drupal/database'


.PHONY: provision/no-drupal
provision/no-drupal:
mkdir -p /opt/no-drupal
cp /opt/drall/.docker/main/no-drupal/composer.json /opt/no-drupal/ || echo "Skipping: no-drupal/composer.json"
rm -f /opt/no-drupal/composer.lock
composer --working-dir=/opt/no-drupal install --no-progress


.PHONY: provision/empty-drupal
provision/empty-drupal:
mkdir -p /opt/empty-drupal
cp /opt/drall/.docker/main/empty-drupal/composer.json /opt/empty-drupal/ || echo "Skipping: empty-drupal/composer.json"
rm -f /opt/empty-drupal/composer.lock
composer --working-dir=/opt/empty-drupal install --no-progress
cp /opt/drall/.docker/main/empty-drupal/web/sites/sites.php /opt/empty-drupal/web/sites/sites.php


.PHONY: provision/drupal/database
provision/drupal/database:
rm -f web/sites/*/settings.php
Expand All @@ -56,17 +74,14 @@ provision/drupal/database:
provision/drall:
composer install --working-dir=/opt/drall --no-progress

# The GitHub Action shivammathur/setup-php@v2 gives higher priority to
# the executables present in /opt/drall/vendor/bin. Thus, we remove
# Drush from this directory to force /opt/drupal/vendor/bin/drush.
rm -f /opt/drall/vendor/bin/drush


# Due to the way Composer works, jigarius/drall cannot be symlinked into
# the Drupal setup used for development. Thus, after every change made to
# Drall, it must be re-installed inside the Drupal installation.
.PHONY: refresh
refresh:
rsync -Ervu --inplace --delete --exclude=.coverage --exclude=.phpunit.cache --exclude=.idea --exclude=.git --exclude=vendor /opt/drall/ /opt/no-drupal/vendor/jigarius/drall/
rsync -Ervu --inplace --delete --exclude=.coverage --exclude=.phpunit.cache --exclude=.idea --exclude=.git --exclude=vendor /opt/drall/ /opt/empty-drupal/vendor/jigarius/drall/
rsync -Ervu --inplace --delete --exclude=.coverage --exclude=.phpunit.cache --exclude=.idea --exclude=.git --exclude=vendor /opt/drall/ /opt/drupal/vendor/jigarius/drall/


Expand All @@ -92,17 +107,6 @@ test:

.PHONY: info
info:
@cd $(DRUPAL_PATH)
@echo "PWD: $(PWD)"
@echo "Drupal path: $(DRUPAL_PATH)"

which php
@php --version

which composer
@composer --version

which drush
@drush --version

which drall
@drall --version
8 changes: 3 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ services:
- "8080:80"
volumes:
- .:/opt/drall
- .docker/main/sites/sites.php:/opt/drupal/web/sites/sites.php
- .docker/main/sites/sites.reddish.php:/opt/drupal/web/sites/sites.reddish.php
- .docker/main/sites/sites.bluish.php:/opt/drupal/web/sites/sites.bluish.php
- .docker/main/composer.json:/opt/drupal/composer.json
- .docker/main/drush:/opt/drupal/drush
- .docker/main/drupal/composer.json:/opt/drupal/composer.json
- ./Makefile:/opt/no-drupal/Makefile
- ./Makefile:/opt/empty-drupal/Makefile
- ./Makefile:/opt/drupal/Makefile
environment:
- DRALL_ENVIRONMENT=development
Expand Down
2 changes: 1 addition & 1 deletion src/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class IntegrationTestCase extends TestCase {
* Error message.
*/
protected function assertOutputEquals(string $expected, mixed $actual, string $message = ''): void {
$actual = preg_replace('@(\s+)\n@', "\n", $actual);
$actual = preg_replace('@(\s+)\n@', "\n", $actual ?? '');
$this->assertEquals($expected, $actual, $message);
}

Expand Down
12 changes: 5 additions & 7 deletions src/Service/SiteDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public function getSiteAliases(
?string $group = NULL,
?string $filter = NULL,
): array {
// Use Drupal Finder to ensure that the Drupal is installed. This ensures
// consistency in errors raised by methods that depend on sites.*.php.
$this->drupalFinder()->getDrupalRoot();

$result = array_values($this->siteAliasManager()->getMultiple());

if ($group) {
Expand Down Expand Up @@ -158,13 +162,7 @@ public function getSiteAliasNames(
* Path/to/drush.
*/
public function getDrushPath(): string {
if (!$vendorDir = $this->drupalFinder->getVendorDir()) {
// This should only happen when drall is installed globally and not in a
// specific Drupal project.
return 'drush';
}

return "$vendorDir/bin/drush";
return $this->drupalFinder->getVendorDir() . "/bin/drush";
}

private function getSitesFile($group = NULL): ?SitesFile {
Expand Down
21 changes: 9 additions & 12 deletions src/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
*/
abstract class TestCase extends TestCaseBase {

const PATH_DRUPAL = '/opt/drupal';

const PATH_NO_DRUPAL = '/opt/no-drupal';

const PATH_EMPTY_DRUPAL = '/opt/empty-drupal';

/**
* Original current working directory.
*
Expand All @@ -19,23 +25,14 @@ abstract class TestCase extends TestCaseBase {

protected function setUp(): void {
$this->cwd = getcwd();
chdir($this->drupalDir());
// By default, all tests run in the Drupal installation.
chdir(static::PATH_DRUPAL);
}

protected function tearDown(): void {
chdir($this->cwd);
}

/**
* Get the path to the Drupal project root.
*
* @return string
* /path/to/drupal.
*/
protected function drupalDir(): string {
return getenv('DRUPAL_PATH');
}

/**
* Get the path to the project's root directory.
*
Expand Down Expand Up @@ -85,7 +82,7 @@ protected function createTempFile(string $data): string {
}

protected function createDrupalFinderStub(?string $root = NULL): DrupalFinderComposerRuntime {
$root ??= $this->drupalDir();
$root ??= static::PATH_DRUPAL;
$drupalFinder = $this->createStub(DrupalFinderComposerRuntime::class);
$drupalFinder->method('getComposerRoot')->willReturn($root);
$drupalFinder->method('getDrupalRoot')->willReturn("$root/web");
Expand Down
Loading

0 comments on commit 1624b82

Please sign in to comment.