Skip to content

Commit 4e2379e

Browse files
committed
Fixed isLegacyDrush() misdetecting Drush 12+ as legacy on PHP 8.4.
1 parent a240ad2 commit 4e2379e

2 files changed

Lines changed: 82 additions & 1 deletion

File tree

src/Drupal/Driver/DrushDriver.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,16 @@ public function bootstrap() {
135135
protected function isLegacyDrush() {
136136
try {
137137
// Try for a drush 9 version.
138-
$version = trim($this->drush('version', [], ['format' => 'string']));
138+
$output = trim($this->drush('version', [], ['format' => 'string']));
139+
// On PHP 8.4, deprecation warnings from Drush dependencies may be
140+
// written to stdout before the version string. Extract the actual
141+
// version number from the output to avoid misdetection.
142+
if (preg_match('/(\d+\.\d+\.\d+(\.\d+)?)\s*$/', $output, $matches)) {
143+
$version = $matches[1];
144+
}
145+
else {
146+
$version = $output;
147+
}
139148
return version_compare($version, '9', '<=');
140149
}
141150
catch (\RuntimeException $e) {

tests/Drupal/Tests/Driver/DrushDriverTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,76 @@ public function testWithNeither() {
4545
new DrushDriver('', '');
4646
}
4747

48+
/**
49+
* Tests `isLegacyDrush()` correctly detects version from noisy output.
50+
*
51+
* @dataProvider dataProviderIsLegacyDrush
52+
*/
53+
public function testIsLegacyDrush($drush_output, $expected) {
54+
$driver = new TestDrushDriver('alias');
55+
$driver->drushOutput = $drush_output;
56+
$result = $driver->callIsLegacyDrush();
57+
$this->assertSame($expected, $result);
58+
}
59+
60+
/**
61+
* Data provider for testIsLegacyDrush().
62+
*/
63+
public function dataProviderIsLegacyDrush() {
64+
return [
65+
'clean modern version' => [
66+
"12.5.2.0\n",
67+
FALSE,
68+
],
69+
'deprecation warnings before version' => [
70+
"Deprecated: Drush\\Drush::shell(): Implicitly marking parameter \$env as nullable\nDeprecated: Consolidation\\Config\\Config::__construct(): ...\n12.5.2.0\n",
71+
FALSE,
72+
],
73+
'drush 9 version' => [
74+
"9.7.2\n",
75+
FALSE,
76+
],
77+
'legacy drush version' => [
78+
"8.4.12\n",
79+
TRUE,
80+
],
81+
'legacy version with noise' => [
82+
"Some warning output\n8.1.0\n",
83+
TRUE,
84+
],
85+
'three-part modern version' => [
86+
"13.0.0\n",
87+
FALSE,
88+
],
89+
];
90+
}
91+
92+
}
93+
94+
/**
95+
* Testable subclass that stubs the `drush()` method.
96+
*/
97+
class TestDrushDriver extends DrushDriver {
98+
99+
/**
100+
* The output to return from `drush()`.
101+
*
102+
* @var string
103+
*/
104+
public $drushOutput = '';
105+
106+
/**
107+
* {@inheritdoc}
108+
*/
109+
public function drush($command, array $arguments = [], array $options = []) {
110+
return $this->drushOutput;
111+
}
112+
113+
/**
114+
* Exposes `isLegacyDrush()` for testing.
115+
*/
116+
public function callIsLegacyDrush() {
117+
return $this->isLegacyDrush();
118+
}
119+
48120
}

0 commit comments

Comments
 (0)