|
| 1 | +<?php |
| 2 | +namespace Drush\TestTraits; |
| 3 | + |
| 4 | +use Symfony\Component\Process\Process; |
| 5 | +use Symfony\Component\Process\Exception\ProcessTimedOutException; |
| 6 | + |
| 7 | +/** |
| 8 | + * CliTestTrait provides an `execute()` method that is useful |
| 9 | + * for launching executable programs in functional tests. |
| 10 | + */ |
| 11 | +trait CliTestTrait |
| 12 | +{ |
| 13 | + use OutputUtilsTrait; |
| 14 | + |
| 15 | + /** |
| 16 | + * Default timeout for commands. |
| 17 | + * |
| 18 | + * @var int |
| 19 | + */ |
| 20 | + private $defaultTimeout = 60; |
| 21 | + |
| 22 | + /** |
| 23 | + * Timeout for command. Set to zero for no timeouts. |
| 24 | + * |
| 25 | + * Reset to $defaultTimeout after executing a command. |
| 26 | + * |
| 27 | + * @var int |
| 28 | + */ |
| 29 | + protected $timeout = 60; |
| 30 | + |
| 31 | + /** |
| 32 | + * Default idle timeout for commands. |
| 33 | + * |
| 34 | + * @var int |
| 35 | + */ |
| 36 | + private $defaultIdleTimeout = 15; |
| 37 | + |
| 38 | + /** |
| 39 | + * Idle timeouts for commands. |
| 40 | + * |
| 41 | + * Reset to $defaultIdleTimeout after executing a command. |
| 42 | + * |
| 43 | + * @var int |
| 44 | + */ |
| 45 | + protected $idleTimeout = 15; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var Process |
| 49 | + */ |
| 50 | + protected $process = null; |
| 51 | + |
| 52 | + /** |
| 53 | + * Accessor for the last output, non-trimmed. |
| 54 | + * |
| 55 | + * @return string |
| 56 | + * Raw output as text. |
| 57 | + * |
| 58 | + * @access public |
| 59 | + */ |
| 60 | + public function getOutputRaw() |
| 61 | + { |
| 62 | + return $this->process ? $this->process->getOutput() : ''; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Accessor for the last stderr output, non-trimmed. |
| 67 | + * |
| 68 | + * @return string |
| 69 | + * Raw stderr as text. |
| 70 | + * |
| 71 | + * @access public |
| 72 | + */ |
| 73 | + public function getErrorOutputRaw() |
| 74 | + { |
| 75 | + return $this->process ? $this->process->getErrorOutput() : ''; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Actually runs the command. |
| 80 | + * |
| 81 | + * @param string $command |
| 82 | + * The actual command line to run. |
| 83 | + * @param integer $expected_return |
| 84 | + * The return code to expect |
| 85 | + * @param sting cd |
| 86 | + * The directory to run the command in. |
| 87 | + * @param array $env |
| 88 | + * Extra environment variables. |
| 89 | + * @param string $input |
| 90 | + * A string representing the STDIN that is piped to the command. |
| 91 | + */ |
| 92 | + public function execute($command, $expected_return = 0, $cd = null, $env = null, $input = null) |
| 93 | + { |
| 94 | + try { |
| 95 | + // Process uses a default timeout of 60 seconds, set it to 0 (none). |
| 96 | + $this->process = new Process($command, $cd, $env, $input, 0); |
| 97 | + $this->process->inheritEnvironmentVariables(true); |
| 98 | + if ($this->timeout) { |
| 99 | + $this->process->setTimeout($this->timeout) |
| 100 | + ->setIdleTimeout($this->idleTimeout); |
| 101 | + } |
| 102 | + $return = $this->process->run(); |
| 103 | + if ($expected_return !== $return) { |
| 104 | + $message = 'Unexpected exit code ' . $return . ' (expected ' . $expected_return . ") for command:\n" . $command; |
| 105 | + throw new \Exception($message . $this->buildProcessMessage()); |
| 106 | + } |
| 107 | + // Reset timeouts to default. |
| 108 | + $this->timeout = $this->defaultTimeout; |
| 109 | + $this->idleTimeout = $this->defaultIdleTimeout; |
| 110 | + } catch (ProcessTimedOutException $e) { |
| 111 | + if ($e->isGeneralTimeout()) { |
| 112 | + $message = 'Command runtime exceeded ' . $this->timeout . " seconds:\n" . $command; |
| 113 | + } else { |
| 114 | + $message = 'Command had no output for ' . $this->idleTimeout . " seconds:\n" . $command; |
| 115 | + } |
| 116 | + throw new \Exception($message . $this->buildProcessMessage()); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public static function escapeshellarg($arg) |
| 121 | + { |
| 122 | + // Short-circuit escaping for simple params (keep stuff readable) |
| 123 | + if (preg_match('|^[a-zA-Z0-9.:/_-]*$|', $arg)) { |
| 124 | + return $arg; |
| 125 | + } elseif (self::isWindows()) { |
| 126 | + return self::_escapeshellargWindows($arg); |
| 127 | + } else { |
| 128 | + return escapeshellarg($arg); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + public static function isWindows() |
| 133 | + { |
| 134 | + return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; |
| 135 | + } |
| 136 | + |
| 137 | + public static function _escapeshellargWindows($arg) |
| 138 | + { |
| 139 | + // Double up existing backslashes |
| 140 | + $arg = preg_replace('/\\\/', '\\\\\\\\', $arg); |
| 141 | + |
| 142 | + // Double up double quotes |
| 143 | + $arg = preg_replace('/"/', '""', $arg); |
| 144 | + |
| 145 | + // Double up percents. |
| 146 | + $arg = preg_replace('/%/', '%%', $arg); |
| 147 | + |
| 148 | + // Add surrounding quotes. |
| 149 | + $arg = '"' . $arg . '"'; |
| 150 | + |
| 151 | + return $arg; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Borrowed from \Symfony\Component\Process\Exception\ProcessTimedOutException |
| 156 | + * |
| 157 | + * @return string |
| 158 | + */ |
| 159 | + public function buildProcessMessage() |
| 160 | + { |
| 161 | + $error = sprintf( |
| 162 | + "%s\n\nExit Code: %s(%s)\n\nWorking directory: %s", |
| 163 | + $this->process->getCommandLine(), |
| 164 | + $this->process->getExitCode(), |
| 165 | + $this->process->getExitCodeText(), |
| 166 | + $this->process->getWorkingDirectory() |
| 167 | + ); |
| 168 | + |
| 169 | + if (!$this->process->isOutputDisabled()) { |
| 170 | + $error .= sprintf( |
| 171 | + "\n\nOutput:\n================\n%s\n\nError Output:\n================\n%s", |
| 172 | + $this->process->getOutput(), |
| 173 | + $this->process->getErrorOutput() |
| 174 | + ); |
| 175 | + } |
| 176 | + |
| 177 | + return $error; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Checks that the output matches the expected output. |
| 182 | + * |
| 183 | + * This matches against a simplified version of the actual output that has |
| 184 | + * absolute paths and duplicate whitespace removed, to avoid false negatives |
| 185 | + * on minor differences. |
| 186 | + * |
| 187 | + * @param string $expected |
| 188 | + * The expected output. |
| 189 | + * @param string $filter |
| 190 | + * Optional regular expression that should be ignored in the error output. |
| 191 | + */ |
| 192 | + |
| 193 | + protected function assertOutputEquals($expected, $filter = '') |
| 194 | + { |
| 195 | + $output = $this->getSimplifiedOutput(); |
| 196 | + if (!empty($filter)) { |
| 197 | + $output = preg_replace($filter, '', $output); |
| 198 | + } |
| 199 | + $this->assertEquals($expected, $output); |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Checks that the error output matches the expected output. |
| 204 | + * |
| 205 | + * This matches against a simplified version of the actual output that has |
| 206 | + * absolute paths and duplicate whitespace removed, to avoid false negatives |
| 207 | + * on minor differences. |
| 208 | + * |
| 209 | + * @param string $expected |
| 210 | + * The expected output. |
| 211 | + * @param string $filter |
| 212 | + * Optional regular expression that should be ignored in the error output. |
| 213 | + */ |
| 214 | + protected function assertErrorOutputEquals($expected, $filter = '') |
| 215 | + { |
| 216 | + $output = $this->getSimplifiedErrorOutput(); |
| 217 | + if (!empty($filter)) { |
| 218 | + $output = preg_replace($filter, '', $output); |
| 219 | + } |
| 220 | + $this->assertEquals($expected, $output); |
| 221 | + } |
| 222 | +} |
0 commit comments