trait ExecTrait

Same name in this branch
  1. 11.x core/tests/Drupal/Tests/Composer/Plugin/Scaffold/ExecTrait.php \Drupal\Tests\Composer\Plugin\Scaffold\ExecTrait
Same name in other branches
  1. 9 core/tests/Drupal/Tests/Composer/Plugin/Scaffold/ExecTrait.php \Drupal\Tests\Composer\Plugin\Scaffold\ExecTrait
  2. 8.9.x core/tests/Drupal/Tests/Composer/Plugin/Scaffold/ExecTrait.php \Drupal\Tests\Composer\Plugin\Scaffold\ExecTrait
  3. 10 core/tests/Drupal/Tests/Composer/Plugin/Scaffold/ExecTrait.php \Drupal\Tests\Composer\Plugin\Scaffold\ExecTrait

Convenience class for creating fixtures.

Hierarchy

  • trait \Drupal\Tests\Composer\Plugin\ExecTrait
4 files declare their use of ExecTrait
ComposerHookTest.php in core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Functional/ComposerHookTest.php
ManageGitIgnoreTest.php in core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Functional/ManageGitIgnoreTest.php
ScaffoldUpgradeTest.php in core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Functional/ScaffoldUpgradeTest.php
UnpackRecipeTest.php in core/tests/Drupal/Tests/Composer/Plugin/Unpack/Functional/UnpackRecipeTest.php

File

core/tests/Drupal/Tests/Composer/Plugin/ExecTrait.php, line 12

Namespace

Drupal\Tests\Composer\Plugin
View source
trait ExecTrait {
    
    /**
     * Runs an arbitrary command.
     *
     * @param string $cmd
     *   The command to execute (escaped as required)
     * @param string $cwd
     *   The current working directory to run the command from.
     * @param array $env
     *   Environment variables to define for the subprocess.
     * @param string $error_output
     *   (optional) Passed by reference to allow error output to be tested.
     *
     * @return string
     *   Standard output from the command
     */
    protected function mustExec($cmd, $cwd, array $env = [], string &$error_output = '') : string {
        $process = Process::fromShellCommandline($cmd, $cwd, $env + [
            'PATH' => getenv('PATH'),
            'HOME' => getenv('HOME'),
        ]);
        $process->setTimeout(300)
            ->setIdleTimeout(300)
            ->run();
        $exitCode = $process->getExitCode();
        if (0 != $exitCode) {
            throw new \RuntimeException("Exit code: {$exitCode}\n\n" . $process->getErrorOutput() . "\n\n" . $process->getOutput());
        }
        $error_output = $process->getErrorOutput();
        return $process->getOutput();
    }

}

Members

Title Sort descending Modifiers Object type Summary
ExecTrait::mustExec protected function Runs an arbitrary command.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.