Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 1 addition & 43 deletions src/Drupal/Driver/DrushDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ class DrushDriver implements DrushDriverInterface, CreationAliasCapabilityInterf
*/
protected string $arguments = '';

/**
* Tracks legacy drush.
*/
protected static bool $isLegacyDrush;

/**
* Set drush alias or root path.
*
Expand Down Expand Up @@ -119,10 +114,6 @@ public function getRandom(): Random {
* {@inheritdoc}
*/
public function bootstrap(): void {
if (!isset(self::$isLegacyDrush)) {
self::$isLegacyDrush = $this->isLegacyDrush();
}

$this->bootstrapped = TRUE;
}

Expand All @@ -145,11 +136,6 @@ public function processBatch(): void {
* {@inheritdoc}
*/
public function cacheClear(?string $type = 'all'): void {
if (self::$isLegacyDrush) {
$this->drush('cache-clear', [$type], []);
return;
}

// Drush-only cache clear does not need a full rebuild.
if ($type === 'drush') {
$this->drush('cache-clear', ['drush'], []);
Expand Down Expand Up @@ -356,12 +342,7 @@ public function getArguments(): string {
public function drushResult(string $command, array $arguments = [], array $options = []): DrushResult {
$argument_string = implode(' ', $arguments);

if (isset(static::$isLegacyDrush) && static::$isLegacyDrush) {
$options['nocolor'] = TRUE;
}
else {
$options['no-ansi'] = NULL;
}
$options['no-ansi'] = NULL;

$option_string = static::parseArguments($options);
$alias = isset($this->alias) ? '@' . $this->alias : '--root=' . $this->root;
Expand Down Expand Up @@ -463,29 +444,6 @@ protected function resolveProjectDrush(string $fallback): string {
return $fallback;
}

/**
* Determine if drush is a legacy version.
*
* @return bool
* Returns TRUE if drush is older than drush 9.
*/
protected function isLegacyDrush(): bool {
try {
// Try for a drush 9 version.
$output = trim($this->drush('version', [], ['format' => 'string']));
// On PHP 8.4, deprecation warnings from Drush dependencies may be
// written to stdout before the version string. Extract the actual
// version number from the output to avoid misdetection.
$version = preg_match('/(\d+\.\d+\.\d+(\.\d+)?)\s*$/', $output, $matches) ? $matches[1] : $output;
return version_compare($version, '9', '<=');
}
catch (\RuntimeException) {
// The version of drush is old enough that only `--version` was available,
// so this is a legacy version.
return TRUE;
}
}

/**
* Parse user id from drush user-information output.
*
Expand Down
72 changes: 7 additions & 65 deletions tests/Drupal/Tests/Driver/Unit/DrushDriverMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Drupal\Driver\Entity\EntityStub;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -26,26 +25,11 @@
#[Group('drush')]
class DrushDriverMethodsTest extends TestCase {

/**
* Sets 'DrushDriver::$isLegacyDrush' directly, bypassing bootstrap caching.
*/
protected function forceLegacyDrush(bool $legacy): void {
$reflection = new \ReflectionClass(DrushDriver::class);
$prop = $reflection->getProperty('isLegacyDrush');
$prop->setValue(NULL, $legacy);
}

/**
* Tests that 'bootstrap()' flips the bootstrapped flag.
*
* Runs in its own process so the static 'isLegacyDrush' cache starts
* uninitialised; without isolation the bootstrap's caching guard skips
* the version-detection assignment.
*/
#[RunInSeparateProcess]
public function testBootstrapMarksAsBootstrapped(): void {
$driver = $this->createDriver();
$driver->drushResponse = "12.5.2.0\n";

$this->assertFalse($driver->isBootstrapped());
$driver->bootstrap();
Expand Down Expand Up @@ -82,11 +66,10 @@ public function testProcessBatchIsNoop(): void {
}

/**
* Tests 'cacheClear()' on a modern Drush (cache:rebuild path).
* Tests 'cacheClear()' rebuilds the cache.
*/
public function testCacheClearOnModernDrushRebuilds(): void {
public function testCacheClearRebuilds(): void {
$driver = $this->createDriver();
$this->forceLegacyDrush(FALSE);

$driver->cacheClear();

Expand All @@ -95,19 +78,6 @@ public function testCacheClearOnModernDrushRebuilds(): void {
$this->assertContains('cache:rebuild', $commands);
}

/**
* Tests 'cacheClear()' on a legacy Drush (cache-clear path).
*/
public function testCacheClearOnLegacyDrushUsesCacheClear(): void {
$driver = $this->createDriver();
$this->forceLegacyDrush(TRUE);

$driver->cacheClear('all');

$this->assertSame('cache-clear', $driver->invocations[0]['command']);
$this->assertSame(['all'], $driver->invocations[0]['arguments']);
}

/**
* Tests that 'cacheClearStatic()' is a no-op.
*/
Expand Down Expand Up @@ -154,11 +124,10 @@ public function testUserCreateWithRolesInvokesRoleAssignment(): void {
}

/**
* Tests 'cacheClear()' on legacy Drush with a drush-only bin.
* Tests 'cacheClear()' with a drush-only bin skips the rebuild.
*/
public function testCacheClearDrushOnlyOnModernDrushSkipsRebuild(): void {
public function testCacheClearDrushOnlySkipsRebuild(): void {
$driver = $this->createDriver();
$this->forceLegacyDrush(FALSE);

$driver->cacheClear('drush');

Expand All @@ -167,16 +136,6 @@ public function testCacheClearDrushOnlyOnModernDrushSkipsRebuild(): void {
$this->assertSame(['drush'], $driver->invocations[0]['arguments']);
}

/**
* Tests 'isLegacyDrush()' treats 'version' failure as legacy.
*/
public function testIsLegacyDrushTreatsExceptionAsLegacy(): void {
$driver = $this->createDriver();
$driver->drushThrows = TRUE;

$this->assertTrue($driver->callIsLegacyDrushWithThrowing());
}

/**
* Tests that 'drush()' actually spawns the configured binary.
*
Expand Down Expand Up @@ -219,20 +178,19 @@ public function testDrushFallsBackToErrorOutputWhenStdoutEmpty(): void {
}

/**
* Tests that 'drush()' emits the legacy '--nocolor' flag when set.
* Tests that 'drush()' always emits the '--no-ansi' flag.
*/
public function testDrushEmitsLegacyFlagWhenMarkedLegacy(): void {
public function testDrushAlwaysEmitsNoAnsiFlag(): void {
$echo = $this->resolveSystemBinary('echo');
if ($echo === NULL) {
$this->markTestSkipped('echo binary is not available on this system.');
}

$this->forceLegacyDrush(TRUE);
$driver = new DrushDriver('alias', binary: $echo);

$result = $driver->drush('version');

$this->assertStringContainsString('--nocolor=1', $result);
$this->assertStringContainsString('--no-ansi', $result);
}

/**
Expand Down Expand Up @@ -414,11 +372,6 @@ class RecordingDrushDriver extends DrushDriver {
*/
public string $drushResponse = '';

/**
* When TRUE, 'drush()' throws a RuntimeException.
*/
public bool $drushThrows = FALSE;

/**
* {@inheritdoc}
*/
Expand All @@ -429,20 +382,9 @@ public function drush(string $command, array $arguments = [], array $options = [
'options' => $options,
];

if ($this->drushThrows) {
throw new \RuntimeException('drush stubbed failure');
}

return $this->drushResponse;
}

/**
* Exposes 'isLegacyDrush()' for testing the exception-path coverage.
*/
public function callIsLegacyDrushWithThrowing(): bool {
return $this->isLegacyDrush();
}

}

/**
Expand Down
50 changes: 0 additions & 50 deletions tests/Drupal/Tests/Driver/Unit/DrushDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,49 +64,6 @@ public function testWithNeither(): void {
new DrushDriver('', '');
}

/**
* Tests 'isLegacyDrush()' correctly detects version from noisy output.
*
* @dataProvider dataProviderIsLegacyDrush
*/
#[DataProvider('dataProviderIsLegacyDrush')]
public function testIsLegacyDrush(string $drush_output, bool $expected): void {
$driver = new TestDrushDriver('alias');
$driver->drushOutput = $drush_output;
$result = $driver->callIsLegacyDrush();
$this->assertSame($expected, $result);
}

/**
* Data provider for testIsLegacyDrush().
*/
public static function dataProviderIsLegacyDrush(): \Iterator {
yield 'clean modern version' => [
"12.5.2.0\n",
FALSE,
];
yield 'deprecation warnings before version' => [
"Deprecated: Drush\\Drush::shell(): Implicitly marking parameter \$env as nullable\nDeprecated: Consolidation\\Config\\Config::__construct(): ...\n12.5.2.0\n",
FALSE,
];
yield 'drush 9 version' => [
"9.7.2\n",
FALSE,
];
yield 'legacy drush version' => [
"8.4.12\n",
TRUE,
];
yield 'legacy version with noise' => [
"Some warning output\n8.1.0\n",
TRUE,
];
yield 'three-part modern version' => [
"13.0.0\n",
FALSE,
];
}

/**
* Tests 'parseUserId()' correctly extracts UID from drush output.
*
Expand Down Expand Up @@ -160,13 +117,6 @@ public function drush($command, array $arguments = [], array $options = []): str
return $this->drushOutput;
}

/**
* Exposes 'isLegacyDrush()' for testing.
*/
public function callIsLegacyDrush(): bool {
return $this->isLegacyDrush();
}

/**
* Exposes 'parseUserId()' for testing.
*/
Expand Down
Loading