2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * CLI tool with utilities to manage Behat integration in Moodle
20 * All CLI utilities uses $CFG->behat_dataroot and $CFG->prefix_dataroot as
21 * $CFG->dataroot and $CFG->prefix
24 * @copyright 2012 David Monllaó
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 if (isset($_SERVER['REMOTE_ADDR'])) {
30 die(); // No access from web!.
34 require_once(__DIR__ . '/../../../../lib/clilib.php');
35 require_once(__DIR__ . '/../../../../lib/behat/lib.php');
38 list($options, $unrecognized) = cli_get_params(
49 'updatesteps' => false,
50 'optimize-runs' => '',
51 'add-core-features-to-theme' => false,
55 'o' => 'optimize-runs',
56 'a' => 'add-core-features-to-theme',
60 if ($options['install'] or $options['drop']) {
61 define('CACHE_DISABLE_ALL', true);
64 // Checking util_single_run.php CLI script usage.
66 Behat utilities to manage the test environment
69 php util_single_run.php [--install|--drop|--enable|--disable|--diag|--updatesteps|--help]
72 --install Installs the test environment for acceptance tests
73 --drop Drops the database tables and the dataroot contents
74 --enable Enables test environment and updates tests list
75 --disable Disables test environment
76 --diag Get behat test environment status code
77 --updatesteps Update feature step file.
79 -o, --optimize-runs Split features with specified tags in all parallel runs.
80 -a, --add-core-features-to-theme Add all core features to specified theme's
82 -h, --help Print out this help
84 Example from Moodle root directory:
85 \$ php admin/tool/behat/cli/util_single_run.php --enable
87 More info in http://docs.moodle.org/dev/Acceptance_testing#Running_tests
90 if (!empty($options['help'])) {
95 // Describe this script.
96 define('BEHAT_UTIL', true);
97 define('CLI_SCRIPT', true);
98 define('NO_OUTPUT_BUFFERING', true);
99 define('IGNORE_COMPONENT_CACHE', true);
101 // Set run value, to be used by setup for configuring proper CFG variables.
102 if ($options['run']) {
103 define('BEHAT_CURRENT_RUN', $options['run']);
106 // Only load CFG from config.php, stop ASAP in lib/setup.php.
107 define('ABORT_AFTER_CONFIG', true);
108 require_once(__DIR__ . '/../../../../config.php');
110 // Remove error handling overrides done in config.php.
111 $CFG->debug = (E_ALL | E_STRICT);
112 $CFG->debugdisplay = 1;
113 error_reporting($CFG->debug);
114 ini_set('display_errors', '1');
115 ini_set('log_errors', '1');
117 // Finish moodle init.
118 define('ABORT_AFTER_CONFIG_CANCEL', true);
119 require("$CFG->dirroot/lib/setup.php");
121 raise_memory_limit(MEMORY_HUGE);
123 require_once($CFG->libdir.'/adminlib.php');
124 require_once($CFG->libdir.'/upgradelib.php');
125 require_once($CFG->libdir.'/clilib.php');
126 require_once($CFG->libdir.'/installlib.php');
127 require_once($CFG->libdir.'/testing/classes/test_lock.php');
130 $unrecognized = implode(PHP_EOL . " ", $unrecognized);
131 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
135 require_once($CFG->libdir . '/behat/classes/util.php');
136 require_once($CFG->libdir . '/behat/classes/behat_command.php');
137 require_once($CFG->libdir . '/behat/classes/behat_config_manager.php');
139 // Ensure run option is <= parallel run installed.
142 if ($options['run']) {
143 $run = $options['run'];
144 // If parallel option is not passed, then try get it form config.
145 if (!$options['parallel']) {
146 $parallel = behat_config_manager::get_behat_run_config_value('parallel');
148 $parallel = $options['parallel'];
151 if (empty($parallel) || $run > $parallel) {
152 echo "Parallel runs can't be more then ".$parallel.PHP_EOL;
155 $CFG->behatrunprocess = $run;
158 // Run command (only one per time).
159 if ($options['install']) {
160 behat_util::install_site();
162 // This is only displayed once for parallel install.
164 mtrace("Acceptance tests site installed");
167 } else if ($options['drop']) {
168 // Ensure no tests are running.
169 test_lock::acquire('behat');
170 behat_util::drop_site();
171 // This is only displayed once for parallel install.
173 mtrace("Acceptance tests site dropped");
176 } else if ($options['enable']) {
177 if (!empty($parallel)) {
178 // Save parallel site info for enable and install options.
179 behat_config_manager::set_behat_run_config_value('behatsiteenabled', 1);
183 behat_util::start_test_mode($options['add-core-features-to-theme'], $options['optimize-runs'], $parallel, $run);
185 // This is only displayed once for parallel install.
187 // Notify user that 2.5 profile has been converted to 3.5.
188 if (behat_config_manager::$autoprofileconversion) {
189 mtrace("2.5 behat profile detected, automatically converted to current 3.x format");
192 $runtestscommand = behat_command::get_behat_command(true, !empty($run));
194 $runtestscommand .= ' --config ' . behat_config_manager::get_behat_cli_config_filepath();
195 mtrace("Acceptance tests environment enabled on $CFG->behat_wwwroot, to run the tests use: " . PHP_EOL .
199 } else if ($options['disable']) {
200 behat_util::stop_test_mode($run);
201 // This is only displayed once for parallel install.
203 mtrace("Acceptance tests environment disabled");
206 } else if ($options['diag']) {
207 $code = behat_util::get_behat_status();
210 } else if ($options['updatesteps']) {
211 if (defined('BEHAT_FEATURE_STEP_FILE') && BEHAT_FEATURE_STEP_FILE) {
212 $behatstepfile = BEHAT_FEATURE_STEP_FILE;
214 echo "BEHAT_FEATURE_STEP_FILE is not set, please ensure you set this to writable file" . PHP_EOL;
218 // Run behat command to get steps in feature files.
219 $featurestepscmd = behat_command::get_behat_command(true);
220 $featurestepscmd .= ' --config ' . behat_config_manager::get_behat_cli_config_filepath();
221 $featurestepscmd .= ' --dry-run --format=moodle_stepcount';
222 $processes = cli_execute_parallel(array($featurestepscmd), __DIR__ . "/../../../../");
223 $status = print_update_step_output(array_pop($processes), $behatstepfile);
234 * Print update progress as dots for updating feature file step list.
236 * @param Process $process process executing update step command.
237 * @param string $featurestepfile feature step file in which steps will be saved.
238 * @return int exitcode.
240 function print_update_step_output($process, $featurestepfile) {
243 echo "Updating steps feature file for parallel behat runs" . PHP_EOL;
245 // Show progress while running command.
246 while ($process->isRunning()) {
248 $op = $process->getIncrementalOutput();
252 if ($printedlength > 70) {
259 // If any error then exit.
260 $exitcode = $process->getExitCode();
262 if ($exitcode != 0) {
263 echo $process->getErrorOutput();
267 // Extract features with step info and save it in file.
268 $featuresteps = $process->getOutput();
269 $featuresteps = explode(PHP_EOL, $featuresteps);
271 $realroot = realpath(__DIR__.'/../../../../').'/';
272 foreach ($featuresteps as $featurestep) {
273 if (trim($featurestep)) {
274 $step = explode("::", $featurestep);
275 $step[0] = str_replace($realroot, '', $step[0]);
276 $steps[$step[0]] = $step[1];
280 if ($existing = @json_decode(file_get_contents($featurestepfile), true)) {
281 $steps = array_merge($existing, $steps);
285 if (!@file_put_contents($featurestepfile, json_encode($steps, JSON_PRETTY_PRINT))) {
286 behat_error(BEHAT_EXITCODE_PERMISSIONS, 'File ' . $featurestepfile . ' can not be created');
290 echo PHP_EOL. "Updated step count in " . $featurestepfile . PHP_EOL;