[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/behat/cli/ -> util_single_run.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * CLI tool with utilities to manage Behat integration in Moodle
  19   *
  20   * All CLI utilities uses $CFG->behat_dataroot and $CFG->prefix_dataroot as
  21   * $CFG->dataroot and $CFG->prefix
  22   *
  23   * @package    tool_behat
  24   * @copyright  2012 David Monllaó
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  
  29  if (isset($_SERVER['REMOTE_ADDR'])) {
  30      die(); // No access from web!.
  31  }
  32  
  33  // Basic functions.
  34  require_once (__DIR__ . '/../../../../lib/clilib.php');
  35  require_once (__DIR__ . '/../../../../lib/behat/lib.php');
  36  
  37  // CLI options.
  38  list($options, $unrecognized) = cli_get_params(
  39      array(
  40          'help'        => false,
  41          'install'     => false,
  42          'parallel'    => 0,
  43          'run'         => '',
  44          'drop'        => false,
  45          'enable'      => false,
  46          'disable'     => false,
  47          'diag'        => false,
  48          'tags'        => '',
  49          'updatesteps' => false,
  50      ),
  51      array(
  52          'h' => 'help'
  53      )
  54  );
  55  
  56  if ($options['install'] or $options['drop']) {
  57      define('CACHE_DISABLE_ALL', true);
  58  }
  59  
  60  // Checking util_single_run.php CLI script usage.
  61  $help = "
  62  Behat utilities to manage the test environment
  63  
  64  Usage:
  65    php util_single_run.php [--install|--drop|--enable|--disable|--diag|--updatesteps|--help]
  66  
  67  Options:
  68  --install     Installs the test environment for acceptance tests
  69  --drop        Drops the database tables and the dataroot contents
  70  --enable      Enables test environment and updates tests list
  71  --disable     Disables test environment
  72  --diag        Get behat test environment status code
  73  --updatesteps Update feature step file.
  74  
  75  -h, --help Print out this help
  76  
  77  Example from Moodle root directory:
  78  \$ php admin/tool/behat/cli/util_single_run.php --enable
  79  
  80  More info in http://docs.moodle.org/dev/Acceptance_testing#Running_tests
  81  ";
  82  
  83  if (!empty($options['help'])) {
  84      echo $help;
  85      exit(0);
  86  }
  87  
  88  // Describe this script.
  89  define('BEHAT_UTIL', true);
  90  define('CLI_SCRIPT', true);
  91  define('NO_OUTPUT_BUFFERING', true);
  92  define('IGNORE_COMPONENT_CACHE', true);
  93  
  94  // Set run value, to be used by setup for configuring proper CFG variables.
  95  if ($options['run']) {
  96      define('BEHAT_CURRENT_RUN', $options['run']);
  97  }
  98  
  99  // Only load CFG from config.php, stop ASAP in lib/setup.php.
 100  define('ABORT_AFTER_CONFIG', true);
 101  require_once(__DIR__ . '/../../../../config.php');
 102  
 103  // Remove error handling overrides done in config.php.
 104  $CFG->debug = (E_ALL | E_STRICT);
 105  $CFG->debugdisplay = 1;
 106  error_reporting($CFG->debug);
 107  ini_set('display_errors', '1');
 108  ini_set('log_errors', '1');
 109  
 110  // Finish moodle init.
 111  define('ABORT_AFTER_CONFIG_CANCEL', true);
 112  require("$CFG->dirroot/lib/setup.php");
 113  
 114  raise_memory_limit(MEMORY_HUGE);
 115  
 116  require_once($CFG->libdir.'/adminlib.php');
 117  require_once($CFG->libdir.'/upgradelib.php');
 118  require_once($CFG->libdir.'/clilib.php');
 119  require_once($CFG->libdir.'/installlib.php');
 120  require_once($CFG->libdir.'/testing/classes/test_lock.php');
 121  
 122  if ($unrecognized) {
 123      $unrecognized = implode(PHP_EOL . "  ", $unrecognized);
 124      cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
 125  }
 126  
 127  // Behat utilities.
 128  require_once($CFG->libdir . '/behat/classes/util.php');
 129  require_once($CFG->libdir . '/behat/classes/behat_command.php');
 130  require_once($CFG->libdir . '/behat/classes/behat_config_manager.php');
 131  
 132  // Ensure run option is <= parallel run installed.
 133  if ($options['run']) {
 134      if (!$options['parallel']) {
 135          $options['parallel'] = behat_config_manager::get_parallel_test_runs();
 136      }
 137      if (empty($options['parallel']) || $options['run'] > $options['parallel']) {
 138          echo "Parallel runs can't be more then ".$options['parallel'].PHP_EOL;
 139          exit(1);
 140      }
 141      $CFG->behatrunprocess = $options['run'];
 142  }
 143  
 144  // Run command (only one per time).
 145  if ($options['install']) {
 146      behat_util::install_site();
 147  
 148      // This is only displayed once for parallel install.
 149      if (empty($options['run'])) {
 150          mtrace("Acceptance tests site installed");
 151      }
 152  
 153  } else if ($options['drop']) {
 154      // Ensure no tests are running.
 155      test_lock::acquire('behat');
 156      behat_util::drop_site();
 157      // This is only displayed once for parallel install.
 158      if (empty($options['run'])) {
 159          mtrace("Acceptance tests site dropped");
 160      }
 161  
 162  } else if ($options['enable']) {
 163      if (!empty($options['parallel'])) {
 164          // Save parallel site info for enable and install options.
 165          $filepath = behat_config_manager::get_parallel_test_file_path();
 166          if (!file_put_contents($filepath, $options['parallel'])) {
 167              behat_error(BEHAT_EXITCODE_PERMISSIONS, 'File ' . $filepath . ' can not be created');
 168          }
 169      }
 170  
 171      // Enable test mode.
 172      behat_util::start_test_mode();
 173  
 174      // This is only displayed once for parallel install.
 175      if (empty($options['run'])) {
 176          // Notify user that 2.5 profile has been converted to 3.5.
 177          if (behat_config_manager::$autoprofileconversion) {
 178              mtrace("2.5 behat profile detected, automatically converted to current 3.x format");
 179          }
 180  
 181          $runtestscommand = behat_command::get_behat_command(true, !empty($options['run']));
 182  
 183          $runtestscommand .= ' --config ' . behat_config_manager::get_behat_cli_config_filepath();
 184          mtrace("Acceptance tests environment enabled on $CFG->behat_wwwroot, to run the tests use: " . PHP_EOL .
 185              $runtestscommand);
 186      }
 187  
 188  } else if ($options['disable']) {
 189      behat_util::stop_test_mode();
 190      // This is only displayed once for parallel install.
 191      if (empty($options['run'])) {
 192          mtrace("Acceptance tests environment disabled");
 193      }
 194  
 195  } else if ($options['diag']) {
 196      $code = behat_util::get_behat_status();
 197      exit($code);
 198  
 199  } else if ($options['updatesteps']) {
 200      if (defined('BEHAT_FEATURE_STEP_FILE') && BEHAT_FEATURE_STEP_FILE) {
 201          $behatstepfile = BEHAT_FEATURE_STEP_FILE;
 202      } else {
 203          echo "BEHAT_FEATURE_STEP_FILE is not set, please ensure you set this to writable file" . PHP_EOL;
 204          exit(1);
 205      }
 206  
 207      // Run behat command to get steps in feature files.
 208      $featurestepscmd = behat_command::get_behat_command(true);
 209      $featurestepscmd .= ' --config ' . behat_config_manager::get_behat_cli_config_filepath();
 210      $featurestepscmd .= ' --dry-run --format=moodle_step_count';
 211      $processes = cli_execute_parallel(array($featurestepscmd), __DIR__ . "/../../../../");
 212      $status = print_update_step_output(array_pop($processes), $behatstepfile);
 213  
 214      exit($status);
 215  } else {
 216      echo $help;
 217      exit(1);
 218  }
 219  
 220  exit(0);
 221  
 222  /**
 223   * Print update progress as dots for updating feature file step list.
 224   *
 225   * @param Process $process process executing update step command.
 226   * @param string $featurestepfile feature step file in which steps will be saved.
 227   * @return int exitcode.
 228   */
 229  function print_update_step_output($process, $featurestepfile) {
 230      $printedlength = 0;
 231  
 232      echo "Updating steps feature file for parallel behat runs" . PHP_EOL;
 233  
 234      // Show progress while running command.
 235      while ($process->isRunning()) {
 236          usleep(10000);
 237          $op = $process->getIncrementalOutput();
 238          if (trim($op)) {
 239              echo ".";
 240              $printedlength++;
 241              if ($printedlength > 70) {
 242                  $printedlength = 0;
 243                  echo PHP_EOL;
 244              }
 245          }
 246      }
 247  
 248      // If any error then exit.
 249      $exitcode = $process->getExitCode();
 250      // Output err.
 251      if ($exitcode != 0) {
 252          echo $process->getErrorOutput();
 253          exit($exitcode);
 254      }
 255  
 256      // Extract features with step info and save it in file.
 257      $featuresteps = $process->getOutput();
 258      $featuresteps = explode(PHP_EOL, $featuresteps);
 259  
 260      $realroot = realpath(__DIR__.'/../../../../').'/';
 261      foreach ($featuresteps as $featurestep) {
 262          if (trim($featurestep)) {
 263              $step = explode("::", $featurestep);
 264              $step[0] = str_replace($realroot, '', $step[0]);
 265              $steps[$step[0]] = $step[1];
 266          }
 267      }
 268  
 269      if ($existing = @json_decode(file_get_contents($featurestepfile), true)) {
 270          $steps = array_merge($existing, $steps);
 271      }
 272      arsort($steps);
 273  
 274      if (!@file_put_contents($featurestepfile, json_encode($steps, JSON_PRETTY_PRINT))) {
 275          behat_error(BEHAT_EXITCODE_PERMISSIONS, 'File ' . $featurestepfile . ' can not be created');
 276          $exitcode = -1;
 277      }
 278  
 279      echo PHP_EOL. "Updated step count in " . $featurestepfile . PHP_EOL;
 280  
 281      return $exitcode;
 282  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1