967fde3335830e2961e22c8c8ea52106cfd759f5
[moodle.git] / admin / tool / behat / cli / util_single_run.php
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/>.
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  */
29 if (isset($_SERVER['REMOTE_ADDR'])) {
30     die(); // No access from web!.
31 }
33 // Basic functions.
34 require_once(__DIR__ . '/../../../../lib/clilib.php');
35 require_once(__DIR__ . '/../../../../lib/behat/lib.php');
37 // CLI options.
38 list($options, $unrecognized) = cli_get_params(
39     array(
40         'help'        => false,
41         'install'     => false,
42         'parallel'    => 0,
43         'run'         => 0,
44         'drop'        => false,
45         'enable'      => false,
46         'disable'     => false,
47         'diag'        => false,
48         'tags'        => '',
49         'updatesteps' => false,
50         'optimize-runs' => '',
51         'add-core-features-to-theme' => false,
52     ),
53     array(
54         'h' => 'help',
55         'o' => 'optimize-runs',
56         'a' => 'add-core-features-to-theme',
57     )
58 );
60 if ($options['install'] or $options['drop']) {
61     define('CACHE_DISABLE_ALL', true);
62 }
64 // Checking util_single_run.php CLI script usage.
65 $help = "
66 Behat utilities to manage the test environment
68 Usage:
69   php util_single_run.php [--install|--drop|--enable|--disable|--diag|--updatesteps|--help]
71 Options:
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
88 ";
90 if (!empty($options['help'])) {
91     echo $help;
92     exit(0);
93 }
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');
129 if ($unrecognized) {
130     $unrecognized = implode(PHP_EOL . "  ", $unrecognized);
131     cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
134 // Behat utilities.
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.
140 $run = 0;
141 $parallel = 0;
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');
147     } else {
148         $parallel = $options['parallel'];
149     }
151     if (empty($parallel) || $run > $parallel) {
152         echo "Parallel runs can't be more then ".$parallel.PHP_EOL;
153         exit(1);
154     }
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.
163     if (empty($run)) {
164         mtrace("Acceptance tests site installed");
165     }
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.
172     if (empty($run)) {
173         mtrace("Acceptance tests site dropped");
174     }
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);
180     }
182     // Enable test mode.
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.
186     if (empty($run)) {
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");
190         }
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 .
196             $runtestscommand);
197     }
199 } else if ($options['disable']) {
200     behat_util::stop_test_mode($run);
201     // This is only displayed once for parallel install.
202     if (empty($run)) {
203         mtrace("Acceptance tests environment disabled");
204     }
206 } else if ($options['diag']) {
207     $code = behat_util::get_behat_status();
208     exit($code);
210 } else if ($options['updatesteps']) {
211     if (defined('BEHAT_FEATURE_STEP_FILE') && BEHAT_FEATURE_STEP_FILE) {
212         $behatstepfile = BEHAT_FEATURE_STEP_FILE;
213     } else {
214         echo "BEHAT_FEATURE_STEP_FILE is not set, please ensure you set this to writable file" . PHP_EOL;
215         exit(1);
216     }
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);
225     exit($status);
226 } else {
227     echo $help;
228     exit(1);
231 exit(0);
233 /**
234  * Print update progress as dots for updating feature file step list.
235  *
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.
239  */
240 function print_update_step_output($process, $featurestepfile) {
241     $printedlength = 0;
243     echo "Updating steps feature file for parallel behat runs" . PHP_EOL;
245     // Show progress while running command.
246     while ($process->isRunning()) {
247         usleep(10000);
248         $op = $process->getIncrementalOutput();
249         if (trim($op)) {
250             echo ".";
251             $printedlength++;
252             if ($printedlength > 70) {
253                 $printedlength = 0;
254                 echo PHP_EOL;
255             }
256         }
257     }
259     // If any error then exit.
260     $exitcode = $process->getExitCode();
261     // Output err.
262     if ($exitcode != 0) {
263         echo $process->getErrorOutput();
264         exit($exitcode);
265     }
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];
277         }
278     }
280     if ($existing = @json_decode(file_get_contents($featurestepfile), true)) {
281         $steps = array_merge($existing, $steps);
282     }
283     arsort($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');
287         $exitcode = -1;
288     }
290     echo PHP_EOL. "Updated step count in " . $featurestepfile . PHP_EOL;
292     return $exitcode;