Skip to content

Commit 6f7025c

Browse files
authored
Merge pull request guncha25#18 from Rade333/feature/drush-timeout-and-return-value
Drush timeout option and return value
2 parents d3f7921 + 6e2eded commit 6f7025c

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ Provides drush (`runDrush`) command.
4747

4848
### Configuration
4949
- working_directory: Working directory where drush should be executed. Defaults to codeception root.
50+
- timeout: Timeout in seconds for drush command. Set to 0 for no timeout. Default to 60 seconds.
5051
- drush: Drush executable. Defaults to `drush`.
5152
```
5253
modules:
5354
- DrupalDrush:
5455
working_directory: './web'
56+
timeout: 120
5557
drush: './vendor/bin/drush'
5658
options:
5759
uri: http://mydomain.com
@@ -60,9 +62,17 @@ modules:
6062

6163
### Usage
6264

63-
Run drush config import and store output.
65+
Run drush status and store output.
6466

65-
`$output = $i->runDrush('cim -y');`
67+
`$output = $i->runDrush('status');`
68+
69+
Run drush config import and store output from STDERR.
70+
71+
`$output = $i->runDrush('cim -y', [], TRUE)->getErrorOutput();`
72+
73+
Run drush cache rebuild and store the exit code.
74+
75+
`$exit_code = $i->runDrush('cr', [], TRUE)->getExitCode();`
6676

6777
Get one-time login url.
6878

src/Codeception/Module/DrupalDrush.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* modules:
1414
* - DrupalDrush:
1515
* working_directory: './web'
16+
* timeout: 120
1617
* drush: './vendor/bin/drush'
1718
* alias: '@mysite.com'
1819
* options:
@@ -42,11 +43,14 @@ class DrupalDrush extends Module {
4243
* e.g. "en devel -y".
4344
* @param array $options
4445
* Associative array of options.
46+
* @param bool $return_process
47+
* If TRUE, the Process object will be returned. If false, the output of
48+
* Process::getOutput() will be returned. Defaults to FALSE.
4549
*
46-
* @return string
47-
* The process output.
50+
* @return string|\Symfony\Component\Process\Process
51+
* The process output, or the process itself.
4852
*/
49-
public function runDrush($command, array $options = []) {
53+
public function runDrush($command, array $options = [], $return_process = FALSE) {
5054
if ($alias = $this->_getConfig('alias')) {
5155
$command = $alias . ' ' . $command;
5256
}
@@ -56,7 +60,7 @@ public function runDrush($command, array $options = []) {
5660
elseif ($this->_getConfig('options')) {
5761
$command = $this->normalizeOptions($this->_getConfig('options')) . $command;
5862
}
59-
return Drush::runDrush($command, $this->_getConfig('drush'), $this->_getConfig('working_directory'));
63+
return Drush::runDrush($command, $this->_getConfig('drush'), $this->_getConfig('working_directory'), $this->_getConfig('timeout'), $return_process);
6064
}
6165

6266
/**

src/Codeception/Module/DrupalUser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function createUserWithRoles(array $roles = [], $password = FALSE) {
143143
*/
144144
public function logInAs($username) {
145145
$alias = $this->_getConfig('alias') ? $this->_getConfig('alias') . ' ' : '';
146-
$output = Drush::runDrush($alias. 'uli --name=' . $username, $this->_getConfig('drush'), $this->_getConfig('working_directory'));
146+
$output = Drush::runDrush($alias. 'uli --name=' . $username, $this->_getConfig('drush'), $this->_getConfig('working_directory'), $this->_getConfig('timeout'));
147147
$gen_url = str_replace(PHP_EOL, '', $output);
148148
$url = substr($gen_url, strpos($gen_url, '/user/reset'));
149149
$this->driver->amOnPage($url);

src/Codeception/Util/Drush.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ class Drush {
2121
* The drush command to use.
2222
* @param string $pwd
2323
* Working directory.
24+
* @param int|float $timeout.
25+
* Drush execution timeout.
26+
* @param bool $return_process
27+
* If TRUE, the Process object will be returned. If false, the output of
28+
* Process::getOutput() will be returned. Defaults to FALSE.
2429
*
25-
* @return string
26-
* The process output.
30+
* @return string|\Symfony\Component\Process\Process
31+
* The process output, or the process object itself.
2732
*/
28-
public static function runDrush($command, $drush = 'drush', $pwd = NULL) {
33+
public static function runDrush($command, $drush = 'drush', $pwd = NULL, $timeout = NULL, $return_process = FALSE) {
2934
$command_args = array_merge([$drush], explode(' ', $command));
3035
$process = new Process($command_args);
3136

@@ -34,7 +39,14 @@ public static function runDrush($command, $drush = 'drush', $pwd = NULL) {
3439
$process->setWorkingDirectory($pwd);
3540
}
3641

37-
return $process->mustRun()->getOutput();
42+
// Set timeout if configured.
43+
if (isset($timeout)) {
44+
$process->setTimeout($timeout);
45+
}
46+
47+
$process->mustRun();
48+
49+
return $return_process ? $process : $process->getOutput();
3850
}
3951

4052
}

0 commit comments

Comments
 (0)