/** * Lists the available steps definitions * * @param string $type * @param string $component * @param string $filter * @return string */ public static function stepsdefinitions($type, $component, $filter) { // We don't require the test environment to be enabled to list the steps definitions // so test writers can more easily set up the environment. behat_command::check_behat_setup(); // The loaded steps depends on the component specified. behat_config_manager::update_config_file($component, false); // The Moodle\BehatExtension\HelpPrinter\MoodleDefinitionsPrinter will parse this search format. if ($type) { $filter .= '&&' . $type; } if ($filter) { $filteroption = ' -d "' . $filter . '"'; } else { $filteroption = ' -di'; } // Get steps definitions from Behat. $options = ' --config="' . behat_config_manager::get_steps_list_config_filepath() . '" ' . $filteroption; list($steps, $code) = behat_command::run($options); if ($steps) { $stepshtml = implode('', $steps); } if (empty($stepshtml)) { $stepshtml = get_string('nostepsdefinitions', 'tool_behat'); } return $stepshtml; }
/** * Renders an error message adding the generic info about the tool purpose and setup. * * @param string $msg The error message * @return string HTML */ public function render_error($msg) { $html = $this->generic_info(); $a = new stdClass(); $a->errormsg = $msg; $a->behatcommand = behat_command::get_behat_command(); $a->behatinit = 'php admin' . DIRECTORY_SEPARATOR . 'tool' . DIRECTORY_SEPARATOR . 'behat' . DIRECTORY_SEPARATOR . 'cli' . DIRECTORY_SEPARATOR . 'init.php'; $msg = get_string('wrongbehatsetup', 'tool_behat', $a); // Error box including generic error string + specific error msg. $html .= $this->output->box_start('box errorbox'); $html .= html_writer::tag('div', $msg); $html .= $this->output->box_end(); $html .= $this->output->footer(); return $html; }
echo "Acceptance tests site installed for sites:" . PHP_EOL; // Display all sites which are installed/drop/diabled. for ($i = $options['fromrun']; $i <= $options['torun']; $i++) { if (empty($CFG->behat_parallel_run[$i - 1]['behat_wwwroot'])) { echo $CFG->behat_wwwroot . "/" . BEHAT_PARALLEL_SITE_NAME . $i . PHP_EOL; } else { echo $CFG->behat_parallel_run[$i - 1]['behat_wwwroot'] . PHP_EOL; } } } else { if ($options['drop']) { echo "Acceptance tests site dropped for " . $options['parallel'] . " parallel sites" . PHP_EOL; } else { if ($options['enable']) { echo "Acceptance tests environment enabled on {$CFG->behat_wwwroot}, to run the tests use:" . PHP_EOL; echo behat_command::get_behat_command(true, true); echo PHP_EOL; } else { if ($options['disable']) { echo "Acceptance tests environment disabled for " . $options['parallel'] . " parallel sites" . PHP_EOL; } else { echo $help; } } } } chdir($cwd); exit(0); /** * Create commands to be executed for parallel run. *
} else { $CFG->behat_dataroot = $behatdataroot . $i; } behat_config_manager::update_config_file('', true, $tags); } $CFG->behat_dataroot = $behatdataroot; $CFG->behat_wwwroot = $behatwwwroot; unset($CFG->behatrunprocess); } $cmds = array(); echo "Running " . ($options['torun'] - $options['fromrun'] + 1) . " parallel behat sites:" . PHP_EOL; for ($i = $options['fromrun']; $i <= $options['torun']; $i++) { $CFG->behatrunprocess = $i; // Options parameters to be added to each run. $myopts = !empty($options['replace']) ? str_replace($options['replace'], $i, $extraopts) : $extraopts; $behatcommand = behat_command::get_behat_command(false, false, true); $behatconfigpath = behat_config_manager::get_behat_cli_config_filepath($i); // Command to execute behat run. $cmds[BEHAT_PARALLEL_SITE_NAME . $i] = $behatcommand . ' --config ' . $behatconfigpath . " " . $myopts; echo "[" . BEHAT_PARALLEL_SITE_NAME . $i . "] " . $cmds[BEHAT_PARALLEL_SITE_NAME . $i] . PHP_EOL; } if (empty($cmds)) { echo "No commands to execute " . PHP_EOL; exit(1); } // Create site symlink if necessary. if (!behat_config_manager::create_parallel_site_links($options['fromrun'], $options['torun'])) { echo "Check permissions. If on windows, make sure you are running this command as admin" . PHP_EOL; exit(1); } // Execute all commands.
/** * Returns the path to the file which specifies if test environment is enabled * @return string */ protected static final function get_test_file_path() { return behat_command::get_behat_dir() . '/test_environment_enabled.txt'; }
/** * Returns the behat config file path used by the steps definition list * * @return string */ public static function get_steps_list_config_filepath() { global $USER; // We don't cygwin-it as it is called using exec() which uses cmd.exe. $userdir = behat_command::get_behat_dir() . '/users/' . $USER->id; make_writable_directory($userdir); return $userdir . '/behat.yml'; }
require_once $CFG->libdir . '/behat/classes/util.php'; require_once $CFG->libdir . '/behat/classes/behat_command.php'; // Run command (only one per time). if ($options['install']) { behat_util::install_site(); mtrace("Acceptance tests site installed"); } else { if ($options['drop']) { // Ensure no tests are running. test_lock::acquire('behat'); behat_util::drop_site(); mtrace("Acceptance tests site dropped"); } else { if ($options['enable']) { behat_util::start_test_mode(); $runtestscommand = behat_command::get_behat_command(true) . ' --config ' . behat_config_manager::get_behat_cli_config_filepath(); mtrace("Acceptance tests environment enabled, to run the tests use:\n " . $runtestscommand); } else { if ($options['disable']) { behat_util::stop_test_mode(); mtrace("Acceptance tests environment disabled"); } else { if ($options['diag']) { $code = behat_util::get_behat_status(); exit($code); } else { echo $help; } } } }
/** * Returns the path to the parallel run file which specifies if parallel test environment is enabled * and how many parallel runs to execute. * * @param int $runprocess run process for which behat dir is returned. * @return string */ public static final function get_parallel_test_file_path($runprocess = 0) { return behat_command::get_behat_dir($runprocess) . '/parallel_environment_enabled.txt'; }
} else { if ($options['diag']) { $code = behat_util::get_behat_status(); exit($code); } else { if ($options['updatesteps']) { if (defined('BEHAT_FEATURE_STEP_FILE') && BEHAT_FEATURE_STEP_FILE) { $behatstepfile = BEHAT_FEATURE_STEP_FILE; } else { echo "BEHAT_FEATURE_STEP_FILE is not set, please ensure you set this to writable file" . PHP_EOL; exit(1); } // Rewrite config file to ensure we have all the features covered. behat_config_manager::update_config_file(); // Run behat command to get steps in feature files. $featurestepscmd = behat_command::get_behat_command(true); $featurestepscmd .= ' --config ' . behat_config_manager::get_behat_cli_config_filepath(); $featurestepscmd .= ' --dry-run --format=moodle_step_count'; $processes = cli_execute_parallel(array($featurestepscmd), __DIR__ . "/../../../../"); $status = print_update_step_output(array_pop($processes), $behatstepfile); exit($status); } else { echo $help; exit(1); } } } } } } exit(0);
/** * Returns the behat config file path used by the steps definition list * * Note this can only be called from web-based scripts so it will return the * production dataroot not behat_dataroot. With this the steps definitions * list is accessible without having to install the behat test site. * * @return string */ public static function get_steps_list_config_filepath() { global $USER; $userdir = behat_command::get_behat_dir() . '/users/' . $USER->id; make_writable_directory($userdir); return $userdir . '/behat.yml'; }
require_once $CFG->libdir . '/behat/classes/util.php'; require_once $CFG->libdir . '/behat/classes/behat_command.php'; // Run command (only one per time). if ($options['install']) { behat_util::install_site(); mtrace("Acceptance tests site installed"); } else { if ($options['drop']) { // Ensure no tests are running. test_lock::acquire('behat'); behat_util::drop_site(); mtrace("Acceptance tests site dropped"); } else { if ($options['enable']) { behat_util::start_test_mode(); $runtestscommand = behat_command::get_behat_command() . ' --config ' . $CFG->behat_dataroot . DIRECTORY_SEPARATOR . 'behat' . DIRECTORY_SEPARATOR . 'behat.yml'; mtrace("Acceptance tests environment enabled, to run the tests use:\n " . $runtestscommand); } else { if ($options['disable']) { behat_util::stop_test_mode(); mtrace("Acceptance tests environment disabled"); } else { if ($options['diag']) { $code = behat_util::get_behat_status(); exit($code); } else { echo $help; } } } }
$CFG->behat_wwwroot = $behatwwwroot . "/" . BEHAT_PARALLEL_SITE_NAME . $i; } behat_config_manager::update_config_file('', true, $tags); } $CFG->behat_dataroot = $behatdataroot; $CFG->behat_wwwroot = $behatwwwroot; unset($CFG->behatrunprocess); } $cmds = array(); $extraopts = implode(' ', $extraopts); echo "Running " . ($options['torun'] - $options['fromrun'] + 1) . " parallel behat sites:" . PHP_EOL; for ($i = $options['fromrun']; $i <= $options['torun']; $i++) { $CFG->behatrunprocess = $i; // Options parameters to be added to each run. $myopts = !empty($options['replace']) ? str_replace($options['replace'], $i, $extraopts) : $extraopts; $behatcommand = behat_command::get_behat_command(); $behatconfigpath = behat_config_manager::get_behat_cli_config_filepath($i); // Command to execute behat run. $cmds[BEHAT_PARALLEL_SITE_NAME . $i] = $behatcommand . ' --config ' . $behatconfigpath . " " . $myopts; echo "[" . BEHAT_PARALLEL_SITE_NAME . $i . "] " . $cmds[BEHAT_PARALLEL_SITE_NAME . $i] . PHP_EOL; } if (empty($cmds)) { echo "No commands to execute " . PHP_EOL; exit(1); } // Execute all commands. $processes = cli_execute_parallel($cmds); $stoponfail = empty($options['stop-on-failure']) ? false : true; // Print header. print_process_start_info($processes); // Print combined run o/p from processes.